<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2enclosuresfull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"><channel><title>Learning Three.js</title><link>http://learningthreejs.com/</link><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/LearningThreejs" /><language>en</language><managingEditor>noemail@noemail.org (Jerome Etienne)</managingEditor><lastBuildDate>Mon, 27 Feb 2012 06:16:22 PST</lastBuildDate><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="learningthreejs" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><description></description><itunes:owner><itunes:email>noemail@noemail.org</itunes:email></itunes:owner><itunes:explicit>no</itunes:explicit><itunes:subtitle></itunes:subtitle><item><title>Linkify, a tQuery Extension</title><link>http://learningthreejs.com/blog/2012/02/27/linkify-tquery-extension/</link><pubDate>Sun, 26 Feb 2012 23:55:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2012/02/27/linkify-tquery-extension</guid><description>&lt;p&gt;This post is an update on
&lt;a href="https://github.com/jeromeetienne/tquery"&gt;tquery&lt;/a&gt; progress.
It is about &lt;code&gt;linkify&lt;/code&gt;, a first experimental extension.
Thanks to it, any 3D object may become a link.
So  3D objects object act as a &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag, i.e. the object becomes
clickable and clicking on it open a new url.
It is built on top of domEvents.
It is used to incoporate
&lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html"&gt;dom kindof events&lt;/a&gt;
in 3D world.
We saw them a few week back in
&lt;a href="http://learningthreejs.com/blog/2012/01/17/dom-events-in-3d-space/"&gt;'dom events in 3D space' post&lt;/a&gt;.
Previous posts on tquery may be found &lt;a href="http://learningthreejs.com/blog/categories/tquery/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/MlW7PeuXGDM" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;




&lt;!-- more --&gt;


&lt;p&gt;Building extensions on top of others is kinda the point of an extension system.
My dream scenario is :
(1) People do plugins for three.js, like they do in jQuery).
(2) They share their work.
(3) They build on top of each other.
(4) Loop to 1.
All in opensource spirit. All good in my book. Ok enougth talk, let's code.&lt;/p&gt;

&lt;h2&gt;Dom Events in 3D Space&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;domEvents&lt;/em&gt; have been ported to tQuery.
It is an important part because jQuery developpers use this a lot, thru
&lt;a href="http://api.jquery.com/on/"&gt;.on()&lt;/a&gt;/&lt;a href="http://api.jquery.com/off/"&gt;.off()&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery('cube').on('mouseover', function(event){
    console.log("somebody put the mouse over a cube");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;The supported events are click obviously,
&lt;a href="http://www.quirksmode.org/dom/events/click.html"&gt;dblclick, mouseup, mousedown&lt;/a&gt;
, &lt;a href="http://www.quirksmode.org/dom/events/mouseover.html"&gt;mouseover and mouseout&lt;/a&gt;.
It has been improved to better match
&lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html"&gt;actual dom events&lt;/a&gt;.
The callback now receives a event object. It contains &lt;code&gt;.type&lt;/code&gt; and &lt;code&gt;.target&lt;/code&gt;
as described in &lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event"&gt;dom specification&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery('.myClass').on('click', function(event){
    console.log("An event of type", event.type, "has been trigger on ", event.target);
    // If you wish to stop propagation, just do 
    event.stopPropagation();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-bubbling"&gt;Event bubbling&lt;/a&gt;
is now supported. So events are dispatched to the
&lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget"&gt;target&lt;/a&gt;
and follow its parent chain upward. It is possible to cancel propagation with an usual
&lt;code&gt;.stopPropagation()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Linkify or How Any 3D Object May Become a Link&lt;/h2&gt;

&lt;p&gt;Linkify is an experimental plugins built on top of &lt;code&gt;tquery.domevent&lt;/code&gt;. It is
shown in the screencast.
It makes any 3D objects object act as a &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag, i.e. the object becomes
clickable and clicking on it open a new url. When the mouse is over it, an underline
is added to make it even more webpage like.
Code is rather short.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery('text').linkify('http://example.com')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;It has been quite time consuming to set up the basis for the project:
tests, bechmarks, docs, or plugins interdependancy resolution.
For each of those field, i had to review the various alternatives,
pick one and setting it up as properly as possible.&lt;/p&gt;

&lt;p&gt;Here are the current choises, they may change in the future.
&lt;a href="http://requirejs.org/"&gt;require.js&lt;/a&gt; will be used for the dependancies between plugins.
Tests are done with &lt;a href="http://visionmedia.github.com/mocha/"&gt;mocha&lt;/a&gt;,
a javascript test framework for javascript which run in node.js and browser.
Later, we may improve that by testing the rendering output using
&lt;a href="http://www.pixastic.com/"&gt;pixastic&lt;/a&gt; to do
&lt;a href="http://en.wikipedia.org/wiki/Mean_squared_error"&gt;statistical&lt;/a&gt;
&lt;a href="http://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio"&gt;images&lt;/a&gt;
&lt;a href="http://en.wikipedia.org/wiki/Root_mean_square_deviation"&gt;comparison&lt;/a&gt;.
Benchmarks are done with
&lt;a href="http://benchmarkjs.com/"&gt;benchmarks.js&lt;/a&gt;, a robust benchmarking library for javascript,
using
&lt;a href="https://github.com/jeromeetienne/benchrunner"&gt;benchrunner&lt;/a&gt;
as runner.
It is the engine behind the wellknown
&lt;a href="http://jsperf.com/browse"&gt;jsperf&lt;/a&gt;
site.
Inline documentation is written in &lt;a href="http://code.google.com/p/jsdoc-toolkit/"&gt;jsdoc&lt;/a&gt;
format using
&lt;a href="http://www.thebrightlines.com/2010/05/06/new-template-for-jsdoctoolkit-codeview/"&gt;codeview&lt;/a&gt;
template.&lt;/p&gt;

&lt;p&gt;That's all folks. Have fun :)&lt;/p&gt;
</description></item><item><title>a Valentine card in tQuery</title><link>http://learningthreejs.com/blog/2012/02/15/valentine-card-in-tquery/</link><pubDate>Wed, 15 Feb 2012 01:21:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2012/02/15/valentine-card-in-tquery</guid><description>&lt;p&gt;The &lt;a href="https://github.com/jeromeetienne/tquery"&gt;tQuery&lt;/a&gt; experimentation is going on.
It is a lot of fun to code :)
This post is just a short presentation of two plugins currently in incubation:
tquery.text and tquery.shape.
Yesterday was
&lt;a href="http://en.wikipedia.org/wiki/Valentine's_Day"&gt;valentine day&lt;/a&gt;,
so i thought it would be cool to stay in topic.
The
&lt;a href="http://www.youtube.com/watch?v=8EHqrAXcKrY"&gt;screencast&lt;/a&gt;
is a live coding of a
&lt;a href="http://learningthreejs.com/data/2012-02-15-valentine-card-in-tquery/"&gt;valentine card&lt;/a&gt; in tQuery :)&lt;/p&gt;

&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/8EHqrAXcKrY" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;




&lt;!-- more --&gt;


&lt;h2&gt;Let's Write Text in 3D&lt;/h2&gt;

&lt;p&gt;tQuery.text plugin writes text in 3D.
It is based on
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/TextGeometry.js"&gt;TextGeometry&lt;/a&gt;.
Here is a simple example.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery.createText("tQuery is Fun!").addTo(world);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Let's Easily Create Shape&lt;/h2&gt;

&lt;p&gt;&lt;img class='right  ' src='http://learningthreejs.com/data/2012-02-15-valentine-card-in-tquery/images/shape.triangle-small.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;tQuery shape is made to easily build and
&lt;a href="http://en.wikipedia.org/wiki/Extrusion"&gt;extrude&lt;/a&gt;
shapes in javascript.
It is based on
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/core/Shape.js"&gt;THREE.Shape&lt;/a&gt;.
tquery.shape is cool because it uses a API very similar to
&lt;a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#complex-shapes-(paths)"&gt;canvas 2D&lt;/a&gt;
&lt;a href="https://developer.mozilla.org/en/Canvas_tutorial/Drawing_shapes"&gt;path&lt;/a&gt;.
This line will build a triangle.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery.createShape().moveTo(0,0).lineTo(1,1).lineTo(-1,1).lineTo(0,0);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;img class='left  ' src='http://learningthreejs.com/data/2012-02-15-valentine-card-in-tquery/images/shape.fish-small.png' width='' height='' alt='' title=''&gt;
&lt;img class='right ' src='http://learningthreejs.com/data/2012-02-15-valentine-card-in-tquery/images/shape.smiley-small.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;Some shapes are already available, like the triangle above, or a fish, smiley
and a heart. Here is a smiley.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var shape = tQuery.createSmileyShape();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;The valentince card&lt;/h2&gt;

&lt;p&gt;Last but not least, the valentine card!
&lt;a href="http://learningthreejs.com/data/2012-02-15-valentine-card-in-tquery/"&gt;Try it out&lt;/a&gt;!
It uses the plugins above to build an animated valentine card.
It is done with less than 20lines...
tQuery seems to produce short code.&lt;/p&gt;

&lt;iframe src="http://learningthreejs.com/data/2012-02-15-valentine-card-in-tquery/"
    webkitallowfullscreen mozallowfullscreen allowfullscreen 
    width="100%" height="349" frameborder="0"&gt;
&lt;/iframe&gt;


&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Today we saw 2 plugins still in progress.
tQuery experiment seems to go well for now.
The code is moving at fast pace.
I am currently experimenting with &lt;a href="http://requirejs.org/"&gt;require.js&lt;/a&gt;
to automatically resolve dependancies between plugins.
What's next ? likely a series with a step by step on how to build a tunnel game.&lt;/p&gt;

&lt;p&gt;That's all folks! have fun :)&lt;/p&gt;
</description></item><item><title>An Extension System for three.js</title><link>http://learningthreejs.com/blog/2012/02/08/extension-system-for-three-js/</link><pubDate>Wed, 08 Feb 2012 06:24:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2012/02/08/extension-system-for-three-js</guid><description>&lt;p&gt;  This post an overview of a work-in-progress.
It is about an extension system on top of three.js which mimics jQuery API.
I came up with the idea while thinking about how to
trim three.js and make it easier to maintain. I have always been impressed
with jQuery plugin ecosystem. It is lively, varied,
and contains impressive specimens. I would love to have such
a rich ecosystem for three.js's plugins.&lt;/p&gt;

&lt;p&gt;  Let's call this experiment &lt;strong&gt;tQuery&lt;/strong&gt; as in "three.js + jQuery".
It makes it easier to understand if you already known this library.
This is a v0 in the
&lt;a href="http://catb.org/~esr/writings/homesteading/cathedral-bazaar/ar01s04.html"&gt;"publish early, publish often"&lt;/a&gt;
vibe.
The goal of this little project is to see if we can mix
to mix three.js power with jquery API usability...
How far this concept can fly ? We will see.&lt;/p&gt;

&lt;p&gt;So what do we want ? The code must make &lt;strong&gt;three.js easy to extend&lt;/strong&gt;
and should &lt;strong&gt;mimics jQuery whenever possible&lt;/strong&gt;.
In order to see if the system hold under load, i wrote several extensions already.
It is very early. The architecture of it all is far from stable.
Code is advancing at fast pace tho :)
The screencast below is short live coding session. Just to give an idea of the current
status.&lt;/p&gt;

&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/Aa7sHUE224A" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;




&lt;!-- more --&gt;


&lt;h2&gt;DOM&lt;/h2&gt;

&lt;p&gt;With jQuery and the
&lt;a href="http://en.wikipedia.org/wiki/Document_Object_Model"&gt;DOM&lt;/a&gt;, you
get a tree of
&lt;a href="https://developer.mozilla.org/en/DOM/element"&gt;elements&lt;/a&gt;
from the page.
In fact, three.js got this tree as well. Surprising hey ?
We just name it a
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/scenes/Scene.js"&gt;scene&lt;/a&gt;
instead of a tree.
And our element are called
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/core/Object3D.js"&gt;Object3D&lt;/a&gt;.
But all that is just a matter of vocabulary.&lt;/p&gt;

&lt;p&gt;One one hand, jquery one, you got the dom and its tree of element.
on the other hand, three.js one, you got the scene and its tree of object3D.
Same Thing!&lt;/p&gt;

&lt;h2&gt;Chained API&lt;/h2&gt;

&lt;p&gt;jQuery got a chained API, so tQuery got a chained API. When we said
&lt;em&gt;copy jQuery whenever possible&lt;/em&gt;, we were not kidding :)&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery('.fooKlass').scale(2).translate(1,0,0);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;IDs and classes&lt;/h2&gt;

&lt;p&gt;They got
&lt;a href="http://api.jquery.com/id-selector/"&gt;Ids&lt;/a&gt;
and
&lt;a href="http://api.jquery.com/class-selector/"&gt;classes&lt;/a&gt;
, or more recently
&lt;a href="http://api.jquery.com/data/"&gt;data&lt;/a&gt;.
So we did all the same with tQuery.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var cube    = tQuery().createCube();
cube.id("myId");    // set the id of this element
cube.addClass('fooKlass');  // add 'fooKlass' class to this cube
cube.data('goom', 'baa');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Selector&lt;/h2&gt;

&lt;p&gt;jQuery got selectors so tQuery got selectors too.
Here are the selector based on geometry, they are similar to the
[element selector] in jQuery, e.g. &lt;code&gt;jQuery('span')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery('sphere');   // select all objects with a sphere gemotry
tQuery('cube'); // smae with a cube gemotry
// and so on
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Here are the one for classes and id.
Note how the syntax is similar to css selector.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery('#myId') // to get the object with the 'myId' id
tQuery('.fooklass') // to get objects with the class 'fooklass'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;obvious we got compose them like with jQuery&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery('.bar.foo')  // objets with the class 'bar' and 'foo'
tQuery('.bar cube') // objets with the class 'bar' with a cube as descandant
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Events&lt;/h2&gt;

&lt;p&gt;Obviously jQuery got events, so we got events in tQuery.
we use &lt;em&gt;domEvents&lt;/em&gt; we saw a few week back in
&lt;a href="http://learningthreejs.com/blog/2012/01/17/dom-events-in-3d-space/"&gt;'dom events in 3D space' post&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tQuery('cube').on('mouseover', function(){
    console.log("somebody put the mouse over a cube");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;A Basic Page&lt;/h2&gt;

&lt;iframe src="http://jeromeetienne.github.com/tquery/examples/minimal"
    webkitallowfullscreen mozallowfullscreen allowfullscreen 
    width="260" height="280" frameborder="0" style="float: right; margin-left: 1em;"&gt;
&lt;/iframe&gt;


&lt;p&gt;This is a minimal page. the code is below, the preview on the right.
Quite short.&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;!doctype html&amp;gt;&amp;lt;title&amp;gt;tQuery Basic Page&amp;lt;/title&amp;gt;
&amp;lt;script src="tquery-bundle.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;script&amp;gt;

    var world = tQuery.createWorld().fullpage().start();
    var object = tQuery.createTorus().addTo(world);

    &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This was early presentation of tQuery. I like the idea, we will see how it goes.
The purpose of this experiement is to help those who know jQuery to use three.js.
So they reuse jquery syntax but inside a webgl context.
This is a very early project.
How far is it possible to push this concept of &lt;em&gt;"three.js power + jQuery API usuability"&lt;/em&gt;.
It seems all very cute at first sight but only time will tell.&lt;/p&gt;

&lt;p&gt;That's all folks. More on tQuery soon. Have fun :)&lt;/p&gt;
</description></item><item><title>Fun With Live Video in WebGL</title><link>http://learningthreejs.com/blog/2012/02/07/live-video-in-webgl/</link><pubDate>Tue, 07 Feb 2012 04:41:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2012/02/07/live-video-in-webgl</guid><description>&lt;p&gt;This post is about live video in webgl.
It is possible in today browsers to read the webcam using a new feature from html5, called WebRTC.
This standard is
about real-time communications such video conferences.
It is an open source project supported by Google, Mozilla and Opera.
Quite neat hey!&lt;/p&gt;

&lt;p&gt;I think live video has a lot of potential usages in 3D.
It is so cool for interactivity.
The player sees himself on the screen. It becomes part of the actions.
Quite immersive effect.
We can imagine something like &lt;a href="http://secondlife.com/"&gt;second life&lt;/a&gt;, where
people wander around and interact live with each other in a virtual world.
Our demo is about TV... Another obvious use may be
&lt;a href="http://en.wikipedia.org/wiki/Reflection_(physics)"&gt;reflections&lt;/a&gt;
like
&lt;a href="http://en.wikipedia.org/wiki/Mirror"&gt;mirror&lt;/a&gt;
or
&lt;a href="http://en.wikipedia.org/wiki/Specular_reflection"&gt;water&lt;/a&gt;.
What about your face from the webcam reflecting in water with animated waves ?
Would be nice!&lt;/p&gt;

&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/vnNihxl3taE" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;




&lt;!-- more --&gt;


&lt;p&gt;It is surely nice but WebRTC is still quite on the edge.
To enable webrtc on your computer, see how to
&lt;a href="http://www.webrtc.org/running-the-demos"&gt;run webrtc demos&lt;/a&gt;.
It is currently available only in
&lt;a href="http://tools.google.com/dlpage/chromesxs"&gt;Canari&lt;/a&gt;.
Mozilla people are working hard to make it happen as soon as possible.
So it may be too early to use it for 'serious' things.
But way enougth to do cool experiments like the one we gonna do today :)&lt;/p&gt;

&lt;p&gt;&lt;a href="http://learningthreejs.com/data/live-video-in-webgl"&gt;Try it out&lt;/a&gt;!!
The first step will be to create a video element.
We gonna start to make it play either a normal video file
then to play video from the webcam using
&lt;a href="https://dvcs.w3.org/hg/audio/raw-file/tip/streams/StreamProcessing.html"&gt;mediastream API&lt;/a&gt;.
After that, we gonna map this video to a normal texture.
And we will be done !
It is that simple, now let's get started.&lt;/p&gt;

&lt;h2&gt;Let's create the video element&lt;/h2&gt;

&lt;p&gt;The
&lt;a href="http://en.wikipedia.org/wiki/HTML5_video"&gt;video element&lt;/a&gt;
is the DOM way to handle video in webpage.
Let's create the video element.
Later we will use it as texture and display it in WebGL.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;video       = document.createElement('video');
video.width = 320;
video.height    = 240;
video.autoplay  = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;It you wish to create a video from a file webm, mp4 or ogv, just set
its &lt;code&gt;.src&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;video.src = "http://example.com/supercatvideo.webm";
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;It wasn't too hard, hey :) So now we know how to get a video from a file.
Let's see if we can use the webcam and get this nice immersive effect for our
users.&lt;/p&gt;

&lt;h2&gt;Let's Use the Webcam if Available&lt;/h2&gt;

&lt;p&gt;Our first step is to detect if the media stream API is available.
The following line will do the job nicely.
&lt;code&gt;hasUserMedia&lt;/code&gt; will be true if it is available, false otherwise.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var hasUserMedia = navigator.webkitGetUserMedia ? true : false;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;If it isn't, you may point the users to
&lt;a href="http://www.webrtc.org/running-the-demos"&gt;this doc&lt;/a&gt;
on how to get it
and/or using a normal video file.
Now we need to check if we can read the webcam.
For that, we use the following lines.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;navigator.webkitGetUserMedia('video', function(stream){
    video.src   = webkitURL.createObjectURL(stream);
}, function(error){
    console.log("Failed to get a stream due to", error);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;A pseudo URL will be created by &lt;code&gt;.createObjectURL&lt;/code&gt;.
It would allows the video element to automagically read the webcam.
It looks a bit like that.&lt;/p&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;blob:http%3A//learningthreejs.com/e33eb278-08a8-4052-9dca-3c7663c88bc0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Handle the textures&lt;/h2&gt;

&lt;p&gt;Now we got the &lt;a href="https://developer.mozilla.org/En/HTML/Element/Video"&gt;video element&lt;/a&gt; ready.
Let's create a texture using it as source.
The last step before seeing the video moving on screen :)
Use this simple line. It is enougth.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var videoTexture = new THREE.Texture( video );
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;This texture is a normal texture and can be used as usual in materials.
For example, in a &lt;a href="http://en.wikipedia.org/wiki/Lambertian_reflectance"&gt;lambert&lt;/a&gt; material.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var material    = new THREE.MeshLambertMaterial({
    map : videoTexture
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;But this texture is special, it is a video. So it need to be constantly updated.
In your render loop, add those lines. They monitor the state of your video.
Every time the video got enougth data to be display, the texture is updated
and sent to the GPU.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if( video.readyState === video.HAVE_ENOUGH_DATA ){
    videoTexture.needsUpdate = true;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Now you can display your webcam inside your webgl !!
This is simple and cool.
Browser support will increase with time.
Live video is a very powerfull tool.
The image from the webcam is a normal one.
On it, you can perform
&lt;a href="https://github.com/mrdoob/three.js/tree/master/examples/js/postprocessing"&gt;post processing&lt;/a&gt;,
&lt;a href="http://en.wikipedia.org/wiki/Edge_detection"&gt;edge detection&lt;/a&gt;,
and many other crazy things. Up to you to experiment.
Let's all for today folks, have fun :)&lt;/p&gt;
</description></item><item><title>Casting Shadows</title><link>http://learningthreejs.com/blog/2012/01/20/casting-shadows/</link><pubDate>Fri, 20 Jan 2012 05:44:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2012/01/20/casting-shadows</guid><description>&lt;p&gt;This post is about
&lt;a href="http://en.wikipedia.org/wiki/Shadow_mapping"&gt;shadow casting&lt;/a&gt;,
a technique which approximates the effect you see in real life everyday.
They may be tricky to tune but they looks so good, it worths it.
Shadows are an efficient tool when you to make your scene more realistic.
We will see how they can be used inside
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
and see more about lights while we are at it.&lt;/p&gt;

&lt;p&gt;As usual, there is a &lt;a href="http://learningthreejs.com/data/casting-shadows/"&gt;demo&lt;/a&gt;.
It is kept it real simple thus you can read the code more easily.
The scene is a simple object in the middle, a spotlight moving around and a plane
to receive the object shadow. The light frustum is left visible in orange.&lt;/p&gt;

&lt;!-- more --&gt;




&lt;iframe src="http://learningthreejs.com/data/casting-shadows"
    webkitallowfullscreen mozallowfullscreen allowfullscreen 
    width="100%" height="420" frameborder="0"&gt;
&lt;/iframe&gt;


&lt;h2&gt;Let's Code Shadows&lt;/h2&gt;

&lt;p&gt;Casting shadows in
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
involves 3 parts: the
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLRenderer.js"&gt;renderer&lt;/a&gt;
which does the computation, the
&lt;a href="https://github.com/mrdoob/three.js/tree/master/src/lights"&gt;lights&lt;/a&gt;
which cast shadows, and
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/core/Object3D.js"&gt;objects&lt;/a&gt;
which receives lights and shadows.&lt;/p&gt;

&lt;h2&gt;Set up the Renderer&lt;/h2&gt;

&lt;p&gt;The renderer is the one which will compute the shadows positions for your 3D scene.
Shadow casting is quite expensive. It is only supported by
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLRenderer.js"&gt;WebGLRenderer&lt;/a&gt;.
It uses
&lt;a href="http://en.wikipedia.org/wiki/Shadow_mapping"&gt;Shadow mapping&lt;/a&gt;, a technique specific
to WebGL, performed directly on the &lt;a href="http://en.wikipedia.org/wiki/Graphics_processing_unit"&gt;GPU&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;renderer.shadowMapEnabled = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;img class='right ' src='http://learningthreejs.com/data/casting-shadows/images/screenshot-withsoftshadow-small.png' width='' height='' alt='' title=''&gt;
&lt;img class='left ' src='http://learningthreejs.com/data/casting-shadows/images/screenshot-nosoftshadow-small.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;You can smooth produced shadows with &lt;code&gt;shadowMapSoft&lt;/code&gt;. It default to false.
On the left, the shadow is crisp, on the right it is soft.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// to antialias the shadow
renderer.shadowMapSoft = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Configure your objects&lt;/h2&gt;

&lt;p&gt;For
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/core/Object3D.js"&gt;Object3D&lt;/a&gt;,
two parameters controls how they interact with lights and shadows.
Set &lt;code&gt;.castShadow&lt;/code&gt; to true if the object occludes light, so to cast a shadow.
Set &lt;code&gt;.receiveShadow&lt;/code&gt; to true if the object is supposed to receive shadows.
Both default to false&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;object3d.castShadow = true;
object3d.receiveShadow  = false;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;This is the configuration for the central object in the demo. It will occlude lights but
won't be able to receive shadow. So you wont see any
&lt;a href="http://en.wikipedia.org/wiki/Self-shadowing"&gt;self shadow&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Tune your Lights&lt;/h2&gt;

&lt;p&gt;&lt;img class='left  ' src='http://learningthreejs.com/data/casting-shadows/images/light-directionallight-small.jpg' width='' height='' alt='' title=''&gt;
&lt;img class='right ' src='http://learningthreejs.com/data/casting-shadows/images/light-spotlight-small.jpg' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/lights/DirectionalLight.js"&gt;THREE.DirectionalLight&lt;/a&gt;
or
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/lights/SpotLight.js"&gt;THREE.SpotLight&lt;/a&gt;
are able to cast shadows.
Let's details them.
A directional light is when light rays are parallel.
A bit like when you look at the sun rays on the left.
It mostly behaves like a light source very far from us.
A spot light is when light rays seems to originate from a single point, and
spreads outward in a coned direction, like in the dance club on the right
(Images are from wikipedia).
To enable the shadow casting on a light,
just use this line.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;light.castShadow = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;You can tune the &lt;code&gt;shadowDarkness&lt;/code&gt;.
It is the opacity of the shadow. 0 means no shadow, 1 means pure back shadow.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;light.shadowDarkness = 0.5;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;img class='right ' src='http://learningthreejs.com/data/casting-shadows/images/screenshot-shadowCameraVisible-small.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;In the same vibe, it possible to show the shadow camera on the screen
with &lt;code&gt;shadowCameraVisible&lt;/code&gt;.
A very usefull feature during tuning or debugging.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;light.shadowCameraVisible = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;More of Directional Lights&lt;/h2&gt;

&lt;p&gt;&lt;img class='right ' src='http://learningthreejs.com/data/casting-shadows/images/screenshot-fustrum-orthographic-small.png' width='' height='' alt='' title=''&gt;
&lt;img class='left  ' src='http://learningthreejs.com/data/casting-shadows/images/screenshot-fustrum-perspective-small.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;Additionally, when casting shadow with a
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/lights/DirectionalLight.js"&gt;THREE.DirectionalLight&lt;/a&gt;,
you need to setup an orthographic camera.
What is that ? it is a different form of
&lt;a href="http://en.wikipedia.org/wiki/3D_projection"&gt;3D projection&lt;/a&gt;.
&lt;a href="http://en.wikipedia.org/wiki/Perspective_(graphical)"&gt;Perspective&lt;/a&gt;
is the way we see things in real life.
So it seems more natural to us than
&lt;a href="http://en.wikipedia.org/wiki/Orthographic_projection"&gt;orthographic projection&lt;/a&gt;.
On the left, an illustration shows a perspective projection.
You can see what is inside the orange shape.
On the right, the same for a orthographic one.&lt;/p&gt;

&lt;p&gt;Recent
&lt;a href="https://github.com/mrdoob/three.js/commit/32b581f24fddeaf9e91b7825aa93ec0ad3a45c83"&gt;three.js r47 release&lt;/a&gt;
includes a
&lt;a href="http://mrdoob.github.com/three.js/examples/webgl_camera.html"&gt;very didactic example&lt;/a&gt;
from
&lt;a href="http://alteredqualia.com/"&gt;alteredq&lt;/a&gt;.
Play with it, it may understand the difference between
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/cameras/OrthographicCamera.js"&gt;orthographic&lt;/a&gt;
and
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/cameras/PerspectiveCamera.js"&gt;perspective&lt;/a&gt;
cameras.
Here is a possible configuration of the frustum for the orthographic camera of our light.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;light.shadowCameraRight     =  5;
light.shadowCameraLeft      = -5;
light.shadowCameraTop       =  5;
light.shadowCameraBottom    = -5;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This is it, you can code shadow casting in three.js now :)
Go ahead and play with them. They are hard to master but very efficient visually.
Keep in mind that those shadows aren't real.
They only uses
&lt;a href="http://en.wikipedia.org/wiki/Shadow_mapping"&gt;'some tricks'&lt;/a&gt;
to make them appears as real. And they do so in real time !!
To achieve this result, they take significant shortcuts which
produce artifacts. To avoid those require tuning and experience.&lt;/p&gt;

&lt;p&gt;That's all for today folks. Have fun.&lt;/p&gt;
</description></item><item><title>Boilerplate Builder for Three.js</title><link>http://learningthreejs.com/blog/2012/01/19/boilerplate-builder-for-three-js/</link><pubDate>Thu, 19 Jan 2012 01:59:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2012/01/19/boilerplate-builder-for-three-js</guid><description>&lt;p&gt;We recently introduced a
&lt;a href="https://github.com/jeromeetienne/threejsboilerplate"&gt;boilerplate for three.js&lt;/a&gt;
in a
&lt;a href="http://learningthreejs.com/blog/2011/12/20/boilerplate-for-three-js/"&gt;previous post&lt;/a&gt;.
It aims to ease your first contact with
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;.
It contains a template for a simple project,
a sample on which we already applied good practices.
You download it and run it in a matter of minutes.
Thus you can immediatly focus on your own things.
In short, it aims for &lt;em&gt;Fast bootstrap + Good Practices&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It seems nice, hey ?
Well there is a glitch.
The
&lt;a href="https://github.com/jeromeetienne/threejsboilerplate"&gt;boilerplate for three.js&lt;/a&gt;
needs more flexibily.
In a way, it acts as a ground on top of which you start your own thing.
We do our best to provide good defaults, but they are only defaults.
What if you got specific requirements ?
How to start your project then ?
The
&lt;a href="http://jeromeetienne.github.com/threejsboilerplatebuilder/"&gt;boilerplate builder&lt;/a&gt;
has been written for you :)&lt;/p&gt;

&lt;p&gt;&lt;a href="http://jeromeetienne.github.com/threejsboilerplatebuilder/"&gt;Try it out&lt;/a&gt;.
Go ahead, play with the options,
discover what they do and customize your own boilerplate.
The
&lt;a href="http://www.youtube.com/watch?v=ANnPWZGRsGk"&gt;screencast&lt;/a&gt;
below is short introduction where i describe the
&lt;a href="http://jeromeetienne.github.com/threejsboilerplatebuilder/"&gt;boilerplate builder&lt;/a&gt;.&lt;/p&gt;

&lt;!-- more --&gt;




&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/ANnPWZGRsGk?hl=en&amp;fs=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;h2&gt;Making-Of the Builder&lt;/h2&gt;

&lt;p&gt;The
&lt;a href="http://jeromeetienne.github.com/threejsboilerplatebuilder/"&gt;builder&lt;/a&gt;
itself was interesting to build.
It uses various nice features from the current web.
It is a pure-browser webapp with a file download,
a preview of a webgl project
and a nice looking visual appearance.&lt;/p&gt;

&lt;h3&gt;Pure Browser Download&lt;/h3&gt;

&lt;p&gt;I like to write pure-browser application.
They dont require a server to run, only static files.
It makes it much easier to host your application.
So
&lt;a href="http://jeromeetienne.github.com/threejsboilerplatebuilder/"&gt;boilerplate builder&lt;/a&gt;
has been written as a pure-browser application :)
It uses
&lt;a href="http://jszip.stuartk.co.uk/"&gt;jszip&lt;/a&gt;
, a library which create .zip files with Javascript.
Its creates the boilerplater.zip that you download.
Additionally, it uses
&lt;a href="https://github.com/dcneiner/Downloadify"&gt;downloadify&lt;/a&gt;
, a small library to create and download files without server.&lt;/p&gt;

&lt;p&gt;Together,
&lt;a href="http://jszip.stuartk.co.uk/"&gt;jszip&lt;/a&gt;
and
&lt;a href="https://github.com/dcneiner/Downloadify"&gt;downloadify&lt;/a&gt;
makes it easy to pack several files together,
and allow the user to download it.
All that in pure-browser, neat no?
I love what the web is becoming!&lt;/p&gt;

&lt;h3&gt;Boilerplate Preview&lt;/h3&gt;

&lt;p&gt;The preview is a bit different.
We start to load the &lt;em&gt;index.html&lt;/em&gt; template for the boilerplate.
We apply all the options you configured and produce the final version.
To actually preview this file, we encode it in
&lt;a href="http://en.wikipedia.org/wiki/Base64"&gt;base64&lt;/a&gt;
to build a
&lt;a href="http://en.wikipedia.org/wiki/Data_URI_scheme"&gt;data url&lt;/a&gt;
with it.
Only then, we create an iframe with this data uri
and you can see the webgl preview :)&lt;/p&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Data_URI_scheme"&gt;Data url&lt;/a&gt;
is &lt;a href="http://tools.ietf.org/html/rfc2397"&gt;an old thing from 1998&lt;/a&gt;
which is back in spotlight due to HTML5.
It allows to encode data directly in the URL.
It may be used to include image directly in css for examples.
Very usefull but not for human consumption.
To give you an idea, here is
&lt;a href="http://pastebin.com/yF3XDSFW"&gt;index.html&lt;/a&gt;
as a data url in
&lt;a href="http://en.wikipedia.org/wiki/Base64"&gt;base64&lt;/a&gt;.
You could encode it as text if you
&lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI"&gt;escape&lt;/a&gt;
it
&lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent"&gt;properly&lt;/a&gt;.
It looks like long ugly random string.&lt;/p&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;data:text/html;base64,PCFkb2N0eXBlIGh0....
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h3&gt;Visual Appearance&lt;/h3&gt;

&lt;p&gt;It includes
&lt;a href="http://twitter.github.com/bootstrap/"&gt;twitter bootstrap&lt;/a&gt; for css.
I am quite grateful for this framework.
Without it, the
&lt;a href="http://jeromeetienne.github.com/threejsboilerplatebuilder/"&gt;builder page&lt;/a&gt;
will be so ugly, that people run away without even trying the application itself.
&lt;a href="http://twitter.github.com/bootstrap/"&gt;twitter bootstrap&lt;/a&gt;
makes it so easy to write a webapp which looks good on the screen.
I think all the css-impaired of the world should thanks twitter for this framework :)&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;It has been quite fun to write it.
I learned some web skills,
discovered new part of three.js,
and on top of that, it looks good on screen.
I could not ask for more.
The goal of
&lt;a href="http://jeromeetienne.github.com/threejsboilerplatebuilder/"&gt;boilerplate builder for three.js&lt;/a&gt;
is to add more &lt;em&gt;flexibility&lt;/em&gt; to the &lt;em&gt;fast bootstrap + good practices&lt;/em&gt; from the
&lt;a href="https://github.com/jeromeetienne/threejsboilerplate"&gt;boilerplate&lt;/a&gt;.
I hope it will help people starting lots of new
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
projects :)&lt;/p&gt;

&lt;p&gt;That's all folks.
Have fun.&lt;/p&gt;
</description></item><item><title>Dom Events in 3D Space</title><link>http://learningthreejs.com/blog/2012/01/17/dom-events-in-3d-space/</link><pubDate>Tue, 17 Jan 2012 02:56:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2012/01/17/dom-events-in-3d-space</guid><description>&lt;p&gt;Ever dreamed of a &lt;strong&gt;object3d.on('click', function(){ ... });&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;I have :) This post presents a little experiment.
What about implementing the concept of
&lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html"&gt;dom events&lt;/a&gt;
in 3D Space.
In a web page, a
&lt;a href="http://www.quirksmode.org/dom/events/click.html"&gt;click&lt;/a&gt;
event is trigger when a user click on a
&lt;a href="http://en.wikipedia.org/wiki/HTML_element"&gt;element&lt;/a&gt;.
This is common knowledge in web development.
What about having that but in a three.js scene ?
Maybe people will start do 3D user interface with that, who knows.
How great would that be ?!?
So let's do that.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://jeromeetienne.github.com/threex/examples/threex.domevent/"&gt;Try it out&lt;/a&gt;.
The demo contains 3 teapots.
Each bind a different type of events.
When the events are triggered, teapots are animated.
Animations are made by &lt;a href="https://github.com/sole/tween.js/"&gt;tween.js&lt;/a&gt;, a nice js tweening engine
seen in a &lt;a href="http://learningthreejs.com/blog/2011/08/17/tweenjs-for-smooth-animation/"&gt;previous post&lt;/a&gt;.
Play with it to get a feel of it, maybe think about the type of UI you could do in 3D.&lt;/p&gt;

&lt;!-- more --&gt;




&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/c2KLj8sie9Q?hl=en&amp;fs=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;h2&gt;Let's Get Started&lt;/h2&gt;

&lt;p&gt;First let's include the source.
You download &lt;a href="https://github.com/jeromeetienne/threex/blob/master/threex.domevent.js"&gt;threex.domevent.js&lt;/a&gt;.
and copy this line in your page.&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script src='threex.domevent.js'&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Let's Use It&lt;/h2&gt;

&lt;iframe src="http://jeromeetienne.github.com/threex/examples/threex.domevent"
    webkitallowfullscreen mozallowfullscreen allowfullscreen 
    width="420" height="315" frameborder="0" style="float: right; margin-left: 1em;"&gt;
&lt;/iframe&gt;


&lt;p&gt;Let's say we want do to an action when the user is clicking on a object.
We just do the following.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mesh.on('click', function(){
    mesh.scale.x *= 2;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;This short line means "&lt;em&gt;if the user click on this mesh, make it twice wider&lt;/em&gt;".
Eloquent meaning, short syntax ... pretty sweet in my book.
If you wish to stop listening just do as usual.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mesh.off('click', callback)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;In fact, there is 2 naming for those functions:
one is
&lt;a href="https://developer.mozilla.org/en/DOM/element.addEventListener"&gt;addEventListener&lt;/a&gt;
/
&lt;a href="https://developer.mozilla.org/en/DOM/element.removeEventListener"&gt;removeEventListener&lt;/a&gt;
from
&lt;a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html"&gt;HTMLElement&lt;/a&gt;
The other is copied on
&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; api:
&lt;a href="http://api.jquery.com/on/"&gt;on&lt;/a&gt;
/
&lt;a href="http://api.jquery.com/off/"&gt;off&lt;/a&gt;
Pick the one you like. They are doing the same thing.&lt;/p&gt;

&lt;p&gt;Always in a effort to stay close to usual pratices, the events name are the same as in DOM.
The semantic is the same too.
Currently, the available events are
&lt;a href="http://www.quirksmode.org/dom/events/click.html"&gt;click, dblclick, mouseup, mousedown&lt;/a&gt;,
&lt;a href="http://www.quirksmode.org/dom/events/mouseover.html"&gt;mouseover and mouse out&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Some Internals&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
already has the ability to interact with the mouse.
You can see it in action
&lt;a href="http://mrdoob.github.com/three.js/examples/webgl_interactive_cubes.html"&gt;here&lt;/a&gt;
and
&lt;a href="http://mrdoob.github.com/three.js/examples/webgl_interactive_voxelpainter.html"&gt;here&lt;/a&gt;.
Internally they use 2 three.js classes:
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/core/Projector.js"&gt;THREE.Projector&lt;/a&gt;
and
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/core/Ray.js"&gt;THREE.Ray&lt;/a&gt;.
&lt;a href="https://github.com/jeromeetienne/threex/blob/master/threex.domevent.js"&gt;threex.domevent.js&lt;/a&gt;
is an higher level api on top of those functions,
an interface which mimic dom events,
something closer to the usual web developper.&lt;/p&gt;

&lt;p&gt;It is a nice api. clean, short, object oriented and feels familiar to web developpers.
A little hichup tho, it modifies THREE.Object3D class.
It is a global class, so it may be legitimatly considered unclean by some people.
If this bother you, simply do &lt;code&gt;THREEx.DomEvent.noConflict()&lt;/code&gt; and use the
standalone API. It is documented in the
&lt;a href="http://jeromeetienne.github.com/threex/docs/threex.domevent.html"&gt;annoted source&lt;/a&gt;.
In fact, the object oriented API is just a thin wrapper
on top of the standalone API.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We all know the click event when the user click on a webpage.
This experiment wishes to provide to web developpers the same experience in 3D.
I hope people will do crazy innovations by using 3D in user interfaces.
This is a first version. Maybe we will implement
&lt;a href="http://www.quirksmode.org/js/events_order.html"&gt;bubbling&lt;/a&gt;
in the future, even events like
&lt;a href="http://www.quirksmode.org/dom/events/change.html"&gt;'change'&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As usual, &lt;a href="https://github.com/jeromeetienne/threex/blob/master/threex.domevent.js"&gt;threex.domevent.js&lt;/a&gt;
source is available on
&lt;a href="https://github.com/jeromeetienne/threex/blob/master/threex.domevent.js"&gt;github&lt;/a&gt;.
There is an
&lt;a href="http://jeromeetienne.github.com/threex/docs/threex.domevent.html"&gt;annoted source&lt;/a&gt;
for implementation details.
That's all folks. Have fun.&lt;/p&gt;
</description></item><item><title>Tunnel Effect for your Demo</title><link>http://learningthreejs.com/blog/2012/01/11/tunnel-effect/</link><pubDate>Wed, 11 Jan 2012 04:00:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2012/01/11/tunnel-effect</guid><description>&lt;p&gt;This post presents a tunnel effect.
This is a classic in 3D demo.
They are visually efficient and easy to code.
In fact, tunnels are so trendy that
even
&lt;a href="http://en.wikipedia.org/wiki/Doctor_Who"&gt;doctor who&lt;/a&gt;
and
&lt;a href="http://en.wikipedia.org/wiki/Stargate"&gt;stargate&lt;/a&gt;
have
&lt;a href="http://www.youtube.com/watch?v=IKo9f5npLNM"&gt;fun&lt;/a&gt;
&lt;a href="http://www.youtube.com/watch?v=KDIdJtW0vN4"&gt;in them&lt;/a&gt;
:)&lt;/p&gt;

&lt;p&gt;&lt;a href="http://jeromeetienne.github.com/tunnelgl/"&gt;Try the demo&lt;/a&gt;.
It has been built using the
&lt;a href="https://github.com/jeromeetienne/threejsboilerplate"&gt;boilerplate for three.js&lt;/a&gt;
seen in a
&lt;a href="http://learningthreejs.com/blog/2011/12/20/boilerplate-for-three-js/"&gt;previous post&lt;/a&gt;.
The code is simple and small.
Less than 20lines has been added on top of the boilerplate.
We will create a
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/core/Geometry.js"&gt;THREE.Geometry&lt;/a&gt;
to get the shape of tunnel.
Then we will use a
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/textures/Texture.js"&gt;texture&lt;/a&gt;
trick to create the
&lt;a href="http://en.wikipedia.org/wiki/Optical_illusion"&gt;visual illusion&lt;/a&gt;
of moving.&lt;/p&gt;

&lt;!-- more --&gt;




&lt;iframe src="http://jeromeetienne.github.com/tunnelgl/"
    webkitallowfullscreen mozallowfullscreen allowfullscreen 
    width="100%" height="420px" frameborder="0"&gt;
&lt;/iframe&gt;


&lt;h2&gt;Let's build the walls&lt;/h2&gt;

&lt;p&gt;The first step is to build the &lt;em&gt;walls&lt;/em&gt; of the tunnel.
It is easier that one would expect.
A tunnel may be seen as a cylinder with the camera inside.
Once you realized that, most of the work is done.
Luckily for us,
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
got an easy way to build cylinders, called
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/CylinderGeometry.js"&gt;CylinderGeometry&lt;/a&gt;.
Nevertheless our tunnel / cylinder got 2 special points.&lt;/p&gt;

&lt;p&gt;First, it is open-ended. So we must not build its top and bottom.
We need this to see thru its extremities.
This is handled by a parameter in
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/CylinderGeometry.js"&gt;CylinderGeometry&lt;/a&gt;.
We just set &lt;code&gt;openended&lt;/code&gt; parameter to true and the rest is done for us :)&lt;/p&gt;

&lt;p&gt;Second, the camera is usually located outside of objects.
But our tunnel/cylinder has the camera inside it.
To make our object visible from the inside, we need to turn it inside out.
For that, just use &lt;code&gt;mesh.flipSided = true&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;Let's go forward&lt;/h2&gt;

&lt;p&gt;Now we need to go forward in this tunnel
For that we will use an old trick.
We won't move the tunnel walls themselves, only their appearance.
It gives the visual illusion that we are moving.
Remember what they say in matrix ?
&lt;a href="http://www.youtube.com/watch?v=dzm8kTIj_0M"&gt;"there is no spoon"&lt;/a&gt;.
It is all the same, we are moving while staying still :)&lt;/p&gt;

&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/dzm8kTIj_0M?hl=en&amp;fs=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;h2&gt;A Texture isn't a spoon&lt;/h2&gt;

&lt;p&gt;First we want to move the appearance of the cylinder, thus the player got the illusion to go forward.
We will use &lt;a href="https://github.com/mrdoob/three.js/blob/master/src/textures/Texture.js"&gt;THREE.Texture&lt;/a&gt; for that.
We wont move the actual pixels of the textures, only its coordinates.
Additionnaly we want the texture to repeat on the cylinder, thus it appears as continuous.
WebGL texture is a powerfull and flexible tool.
You can tweak so many features.&lt;/p&gt;

&lt;p&gt;First let's make this texture move.
Suppose we want the texture to loop once every 10 seconds.
So the coordinate &lt;code&gt;.offset.y&lt;/code&gt; needs to go from 0 to 1 in 10 seconds.
This line is enougth to make the tunnel move forward.
You can change your speed within the tunnel by changing this number.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;texture.offset.y    += 0.1 * seconds;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Now the texture repetition.
For that we will use a texture parameter called &lt;em&gt;wrap&lt;/em&gt;.
It indicates how the gpu repeat the texture on a face.
Here is a good
&lt;a href="http://lucera-project.blogspot.com/2010/06/opengl-wrap.html"&gt;tutorial on opengl wrap&lt;/a&gt;.
By default, wrapS / wrapT are set to &lt;code&gt;THREE.ClampToEdgeWrapping&lt;/code&gt;.
So the texture is scaled to match exactly the size of the face.
In our case, we want to repeat the texture and not scale it.
So we use &lt;code&gt;THREE.RepeatWrapping&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;texture.wrapT   = THREE.RepeatWrapping;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We have seen how to do a tunnel with
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;.
This is very simple to code and awesome on the screen.
It is less than 20 lines added to the &lt;a href="https://github.com/jeromeetienne/threejsboilerplate"&gt;boilerplate&lt;/a&gt;.
The cylinder geometry was already provided.
We used
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/textures/Texture.js"&gt;THREE.Texture&lt;/a&gt;
offsets to provide the optical illusion of moving.&lt;/p&gt;

&lt;p&gt;Later, we may add a &lt;a href="http://en.wikipedia.org/wiki/TARDIS"&gt;blue phonebooth&lt;/a&gt;
and play &lt;em&gt;doctor who&lt;/em&gt; :)
The code is available on
&lt;a href="https://github.com/jeromeetienne/tunnelgl"&gt;github&lt;/a&gt;
under MIT license.
Feel free to fork and modify.
That's all folks, have fun.&lt;/p&gt;
</description></item><item><title>Let’s Make a 3D Game: Supporting Mobile</title><link>http://learningthreejs.com/blog/2011/12/28/let-s-make-a-3d-game-supporting-mobile/</link><pubDate>Wed, 28 Dec 2011 04:46:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/12/28/let-s-make-a-3d-game-supporting-mobile</guid><description>&lt;p&gt;This post is about supporting mobile.
We will try to port &lt;a href="http://marblesoccer.com"&gt;marblesoccer&lt;/a&gt; on mobile.
When doing a
&lt;a href="http://127.0.0.1:8000/blog/2011/12/20/boilerplate-for-three-js/"&gt;boilerplate for three.js&lt;/a&gt;,
mobile had to be supported for compatibility.
So it gave me the idea of this post.
What about porting our game on mobile ?
Porting a 3D web game to mobile ? crazy :)&lt;/p&gt;

&lt;p&gt;The desktop version looks kindof ok.
What would be the result of this experiment ?
Is that even possible ?
What about usable ?
This is the purpose of this experiment to find out.&lt;/p&gt;

&lt;!-- more --&gt;




&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/Ow_ceac1aEE?hl=en&amp;fs=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;h2&gt;Mobile isn't desktop&lt;/h2&gt;

&lt;p&gt;Indeed... desktop and mobile are quite different plateforms.
Which differences are relevant to us ?
First, mobile network is bad, especially latency.
So avoid download of long files, such as texture or sound.
Here is a good talk on
&lt;a href="http://www.youtube.com/watch?v=L2YqfVNHQO4"&gt;Mobile Web Performance&lt;/a&gt;.
Another thing, mobile got no keyboard, no mouse, but a touch screen
We need to get a game controller for this environement.
We use &lt;a href="https://github.com/jeromeetienne/virtualjoystick.js"&gt;virtualjoystick.js&lt;/a&gt;.
See details in our &lt;a href="http://learningthreejs.com/blog/2011/12/26/let-s-make-a-3d-game-virtual-joystick/"&gt;previous post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One big thing is that currently, on mobile, WebGL hasnt reached mainstream to say the least.
No major vendor is shipping phone with webgl, so nobody or close get webgl on phone.
So for our little experiment, we will display in
&lt;a href="http://www.w3.org/TR/2010/WD-2dcontext-20100304/"&gt;Canvas 2D&lt;/a&gt;
with
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/renderers/CanvasRenderer.js"&gt;THREE.CanvasRenderer&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Porting to canvas 2D&lt;/h2&gt;

&lt;p&gt;So what need to be done ?
First step is to use the proper renderer when suitable.
Second is fixing material and geometry to fit canvas2D renderer capability.
Last step is to look for room of optimisations.
Ok now let's intanciate the renderer. If webgl is available, use
&lt;code&gt;THREE.WebGLRenderer&lt;/code&gt;
else use
&lt;code&gt;THREE.CanvasRenderer&lt;/code&gt;.
Not too hard hey ?
We already did that in the
&lt;a href="http://learningthreejs.com/blog/2011/12/20/boilerplate-for-three-js/"&gt;boilerplate for three.js&lt;/a&gt;&lt;/p&gt;

&lt;iframe src="http://marblesoccer.com?render=canvas&amp;bypasslanding=1"
    webkitallowfullscreen mozallowfullscreen allowfullscreen 
    width="420" height="315" frameborder="0" style="float: right; margin-left: 1em;"&gt;
&lt;/iframe&gt;


&lt;p&gt;We simplify geometry to reduce the number of polygon.
For marble geometry, the sphere got 512 faces on webgl, and only 9 on canvas2d.
Drastic :)
What about material ? For webgl, we used
&lt;a href="http://en.wikipedia.org/wiki/Phong_shading"&gt;phong&lt;/a&gt;
for fancy lightings,
We used
&lt;a href="http://en.wikipedia.org/wiki/Texture_mapping"&gt;textures&lt;/a&gt;
for realistic effects.
But with canvas2D, those technics cant be used.
They are way too slow.&lt;/p&gt;

&lt;p&gt;This is enougth to get it working.
It display something reasonable on the screen at least.
We sacrifice a lot tho, no more texture not fancy lighting.
And now the bad new, it results in 3fps on my ipad2 ios4... ouch.&lt;/p&gt;

&lt;h2&gt;More measures&lt;/h2&gt;

&lt;p&gt;How come performances are so bad ? So i did more measures.
I disabled the display of map and marbles to see how they impact performance.
If we display the map and the marbles, we got 3fps on my ipad2.
If we display only marbles, no more maps, we got 23fps, much better.
but still not great... Considere that we are only displaying marbles and they are real simple.&lt;/p&gt;

&lt;p&gt;If we display no marble, and no map. we got only 30fps. So all the rest, all the non display
part is already using a big part of time ? what are we doing ? not much...
Still we run
&lt;a href="http://learningthreejs.com/blog/2011/10/17/lets-make-a-3d-game-microphysics-js/"&gt;realistic 3D physics&lt;/a&gt;
and ipad2 cpu isnt as fast as usual desktop ones.&lt;/p&gt;

&lt;h2&gt;Time to optimize&lt;/h2&gt;

&lt;p&gt;Ok it is slow but this is a first try.
I admit the code isnt not too optimized.
&lt;a href="http://en.wikipedia.org/wiki/Central_processing_unit"&gt;cpu&lt;/a&gt;
/
&lt;a href="http://en.wikipedia.org/wiki/Graphics_processing_unit"&gt;gpu&lt;/a&gt;
performance are so good on desktop, i may have been sloppy here and there :)
There are areas of optimisations.
We need to draw less polygons.&lt;/p&gt;

&lt;p&gt;First we need to &lt;em&gt;reduce the geometry&lt;/em&gt; of the map.
We can do that by clustering voxels:
if 2 voxels got the same color and touch each other, display one large box, instead of 2 small boxes.
We did it to optimize our physics.
See details in &lt;a href="http://learningthreejs.com/blog/2011/10/17/lets-make-a-3d-game-microphysics-js/"&gt;microphysics post&lt;/a&gt;.
Additionnaly we could use marblesoccer's
&lt;a href="http://127.0.0.1:8000/blog/2011/09/14/lets-make-a-3D-game-map-editor/"&gt;map editor&lt;/a&gt;
to redesign a map with a simpler geometry.&lt;/p&gt;

&lt;p&gt;Another low-hanging fruit is to remove faces which are never seen, like in
&lt;a href="http://mrdoob.github.com/three.js/examples/webgl_geometry_minecraft_ao.html"&gt;minecraft example&lt;/a&gt;
from
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;.
We could try to 2D sprites instead of 3D spheres for marble.
We wont use
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/objects/Sprite.js"&gt;THREE.Sprite&lt;/a&gt;.
It isnt supported
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/renderers/CanvasRenderer.js"&gt;THREE.CanvasRenderer&lt;/a&gt;.
But dont worry, it is possible with the particle system.
See how &lt;code&gt;THREE.Particle&lt;/code&gt; is used in
&lt;a href="http://mrdoob.github.com/three.js/examples/canvas_particles_sprites.html"&gt;canvas_particles_sprites.html&lt;/a&gt;
example.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;And after all that, what can you expect ?
Will that run at 60fps ? 30fps ? Not likely or it will require a lot of effort.
&lt;img class='left ' src='http://learningthreejs.com/data/lets-make-a-3d-game-supporting-mobile/images/screenshot-webgl-small.png' width='' height='' alt='' title=''&gt;
&lt;img class='right ' src='http://learningthreejs.com/data/lets-make-a-3d-game-supporting-mobile/images/screenshot-canvas-small.png' width='' height='' alt='' title=''&gt;
So animations arent smooth, what about the look?
Watch what you got on the screen...
On the right, a canvas version. &lt;a href="http://marblesoccer.com/?render=canvas"&gt;live here&lt;/a&gt;.
On the left you can see a webgl version. &lt;a href="http://marblesoccer.com"&gt;live here&lt;/a&gt;.
Lets face it... &lt;em&gt;canvas version is ugly&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;After a significant work, you get poor performance and crappy look.
Not many players would accept that...
currently canvas performance doesnt seems suitable to display 3D on mobile.
It is a good way to monitor performances and see how they evolve with time.&lt;/p&gt;
</description></item><item><title>Let’s Make a 3D Game: Virtual Joystick</title><link>http://learningthreejs.com/blog/2011/12/26/let-s-make-a-3d-game-virtual-joystick/</link><pubDate>Mon, 26 Dec 2011 03:21:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/12/26/let-s-make-a-3d-game-virtual-joystick</guid><description>&lt;p&gt;Here is another article of the "Let's Make a 3D Game"
&lt;a href="http://learningthreejs.com/blog/categories/tutorial3dgame/"&gt;series&lt;/a&gt;.
We have already seen how to handle other inputs like
&lt;a href="http://learningthreejs.com/blog/2011/09/12/lets-Make-a-3D-game-keyboard/"&gt;keyboard&lt;/a&gt;
and
&lt;a href="http://learningthreejs.com/blog/2011/09/20/lets-make-a-3D-game-device-orientation/"&gt;device orientation&lt;/a&gt;.
This post is about &lt;a href="https://github.com/jeromeetienne/virtualjoystick.js"&gt;virtualjoystick.js&lt;/a&gt;
It &lt;strong&gt;virtual joystick&lt;/strong&gt;, another input you can use for your games.
A virtual joystick emulates a joystick behaviour on a touch screen.
&lt;a href="https://github.com/jeromeetienne/virtualjoystick.js"&gt;virtualjoystick.js&lt;/a&gt;
has been coded in a effort to port
&lt;a href="http://marblesoccer.com"&gt;marblesoccer&lt;/a&gt;
to mobile device.
Show, dont tell,
&lt;a href="http://jeromeetienne.github.com/virtualjoystick.js/"&gt;Try it out&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This
&lt;a href="http://jeromeetienne.github.com/virtualjoystick.js/"&gt;demo&lt;/a&gt;
works with mouse events too thus, it is easier to test/debug.
&lt;a href="https://github.com/jeromeetienne/virtualjoystick.js"&gt;virtualjoystick.js&lt;/a&gt;
has been widely inpired by
&lt;a href="http://sebleedelisle.com/2011/04/multi-touch-game-controller-in-javascripthtml5-for-ipad/"&gt;this post&lt;/a&gt;
by
&lt;a href="http://sebleedelisle.com/"&gt;Seb Lee-Delisle&lt;/a&gt;.
The screencast below is short introduction about
&lt;a href="https://github.com/jeromeetienne/virtualjoystick.js"&gt;virtualjoystick.js&lt;/a&gt;&lt;/p&gt;

&lt;!-- more --&gt;




&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/viyr_W0z1U8?hl=en&amp;fs=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;h2&gt;Let's Get Started&lt;/h2&gt;

&lt;p&gt;First step, you download
&lt;a href="https://raw.github.com/jeromeetienne/virtualjoystick.js/master/virtualjoystick.js"&gt;virtualjoystick.js&lt;/a&gt;
from its
&lt;a href="https://github.com/jeromeetienne/virtualjoystick.js"&gt;github&lt;/a&gt;
Then include it in your own code.&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script src="virtualjoystick.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;The joystick is composed of 2 parts: the &lt;em&gt;base&lt;/em&gt; and the &lt;em&gt;stick&lt;/em&gt;.
First the plare touch the screen, it gives the position of the &lt;em&gt;base&lt;/em&gt;.
Then it drags its fingers to gives the position of the &lt;em&gt;stick&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Let's Use it&lt;/h2&gt;

&lt;iframe src="http://jeromeetienne.github.com/virtualjoystick.js/"
    webkitallowfullscreen mozallowfullscreen allowfullscreen 
    width="420" height="315" frameborder="0" style="float: right; margin-left: 1em;"&gt;
&lt;/iframe&gt;


&lt;p&gt;First step is to create the object from &lt;code&gt;VirtualJoystick&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var joystick = new VirtualJoystick()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;The constructor has some options.
They have sensible default.
You can change them to fit your specific needs.
See
&lt;a href="https://github.com/jeromeetienne/virtualjoystick.js#readme"&gt;github README&lt;/a&gt;
for a full API description.
You may look at the &lt;a href="https://github.com/jeromeetienne/virtualjoystick.js/blob/master/index.html"&gt;index.html&lt;/a&gt;.
It is an example which uses the library.&lt;/p&gt;

&lt;p&gt;It is possible to read
&lt;a href="http://en.wikipedia.org/wiki/Analog_stick"&gt;analogic&lt;/a&gt;
coordinates.
&lt;code&gt;joystick.deltaX()&lt;/code&gt; gives the &lt;em&gt;delta x&lt;/em&gt; between the base and the stick in pixel.
&lt;code&gt;joystick.deltaY()&lt;/code&gt; gives the &lt;em&gt;delta y&lt;/em&gt;.
Those analogic coordinates may be interpreted as a
&lt;a href="http://www.slagcoin.com/joystick/restrictors.html"&gt;joystick with 4 switches&lt;/a&gt;.
Similar to
&lt;a href="http://en.wikipedia.org/wiki/Arrow_keys"&gt;arrow keys&lt;/a&gt;
in a way.
&lt;code&gt;joystick.up()&lt;/code&gt; tells you if your joystick is up or not.
You guessed the meaning of &lt;code&gt;.down()&lt;/code&gt;, &lt;code&gt;.right()&lt;/code&gt; and &lt;code&gt;.left()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;The source is available on
&lt;a href="https://github.com/jeromeetienne/virtualjoystick.js"&gt;github&lt;/a&gt;
under MIT license.
Later, a button may be implemented as well.
It is alway usefull to able to fire in video games :)
That's all folks, have fun.&lt;/p&gt;
</description></item><item><title>Boilerplate for Three.js</title><link>http://learningthreejs.com/blog/2011/12/20/boilerplate-for-three-js/</link><pubDate>Tue, 20 Dec 2011 10:09:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/12/20/boilerplate-for-three-js</guid><description>&lt;p&gt;This post presents
&lt;a href="https://github.com/jeromeetienne/threejsboilerplate"&gt;boilerplate for three.js&lt;/a&gt;.
I looked at
&lt;a href="http://html5boilerplate.com/"&gt;html5 boilerplate&lt;/a&gt;
and found
&lt;a href="http://www.youtube.com/watch?v=NMEB78VX2P0"&gt;it&lt;/a&gt;
&lt;a href="http://www.youtube.com/watch?v=oDlsOyPKUTM"&gt;awesome&lt;/a&gt;.
&lt;a href="http://html5boilerplate.com/"&gt;html5 boilerplate&lt;/a&gt;
is a fast way start a clean project.
It avoids repetitive tasks, following &lt;a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself"&gt;DRY&lt;/a&gt; principles.
It includes all those good practices, which are so easy to forget.
It seems all good to me :)
&lt;a href="https://github.com/jeromeetienne/threejsboilerplate"&gt;boilerplate for three.js&lt;/a&gt; tries to apply similar principles.
I frequently write simple three.js demo for tutorials, and repeat the first steps way too often for my tastes :)
It has been done in effort to make 3D on the web even easier.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://jeromeetienne.github.com/threejsboilerplate/"&gt;Try it&lt;/a&gt; out.
"Boilerplate for three.js is a web template for a fast, robust and future-proof site.
Boilerplate is not a framework, nor does it prescribe any philosophy of
development, it's just got some tricks to get your project off the ground
quickly and right-footed."
- freely adapted from &lt;a href="http://html5boilerplate.com/"&gt;html5 boilerplate site&lt;/a&gt;.&lt;/p&gt;

&lt;!-- more --&gt;


&lt;h2&gt;Walkthrough&lt;/h2&gt;

&lt;p&gt;This project is at an early stage.
Dont hesitate to suggest improvements or
bug fixes in
&lt;a href="https://github.com/jeromeetienne/threejsboilerplate/issues"&gt;github issues&lt;/a&gt;.
It has been done to run everywhere, and to promote good practices.
The
&lt;a href="http://www.youtube.com/watch?v=kOReCN5t2Eo"&gt;screencast&lt;/a&gt;
below will walk you through the source of the project.
22min... a long one :)&lt;/p&gt;

&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/kOReCN5t2Eo?hl=en&amp;fs=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;h2&gt;Compatibility is key&lt;/h2&gt;

&lt;p&gt;It has to run everywhere.
We believe that compatibility is crucial on the web.
The boilerplate seamlessly works on desktop and mobile.
It is working on webgl/canvas2d and supports mouse/touch events.&lt;/p&gt;

&lt;p&gt;It renders on
&lt;a href="http://en.wikipedia.org/wiki/WebGL"&gt;webgl&lt;/a&gt;
if available, else it fallbacks on
&lt;a href="http://html5doctor.com/an-introduction-to-the-canvas-2d-api/"&gt;canvas2D&lt;/a&gt;.
It is often forgotten, but &lt;a href=""&gt;three.js&lt;/a&gt; is able render on various backends,
they are called &lt;a href="https://github.com/mrdoob/three.js/tree/master/src/renderers"&gt;renderers&lt;/a&gt;.
It isnt always possible to fallback tho.
Materials are especially sensible to the type of renderer you use.
For examples, many materials are webgl specific as they contains shaders and canvas2D got no shaders.
Up to you to find the balance that fit your needs.&lt;/p&gt;

&lt;p&gt;The camera controls is rather basic.
If you need a different controls for your camera, pick one in
&lt;a href="https://github.com/mrdoob/three.js/tree/master/src/extras/controls"&gt;this list&lt;/a&gt;.
This one is simple to use and understand, a nice feature for a boilerplate.
It supports
&lt;a href="http://www.quirksmode.org/js/events_mouse.html"&gt;mouse events&lt;/a&gt;
and
&lt;a href="https://developer.mozilla.org/en/DOM/Touch_events"&gt;touch events&lt;/a&gt;.
So you can run with a touch screen with a glitch.&lt;/p&gt;

&lt;h2&gt;Good Practices&lt;/h2&gt;

&lt;p&gt;Some features have been added: fullscreen, screenshot and window resize.
I consider them &lt;a href="http://en.wikipedia.org/wiki/Best_practice"&gt;good pratices&lt;/a&gt; which are often forgotten.
Some numbers based on &lt;a href="https://github.com/mrdoob/three.js/tree/master/examples"&gt;three.js examples&lt;/a&gt;:
128 examples total,
26 of them handle touch events,
11 handle window resize,
2 handle screenshot,
0 handle fullscreen.&lt;/p&gt;

&lt;iframe src="http://jeromeetienne.github.com/threejsboilerplate"
    webkitallowfullscreen mozallowfullscreen allowfullscreen 
    width="420" height="315" frameborder="0" style="float: right; margin-left: 1em;"&gt;
&lt;/iframe&gt;


&lt;p&gt;&lt;strong&gt;Fullscreen&lt;/strong&gt; is supported to enjoy your 3D on a large display
without visual distraction.
If you press &lt;em&gt;f&lt;/em&gt;, your demo will go fullscreen.
For technical details, see "&lt;a href="http://learningthreejs.com/blog/2011/11/17/lets-make-a-3d-game-make-it-fullscreen/"&gt;Make It Fullscreen&lt;/a&gt;" post.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Screenshot&lt;/strong&gt; makes it easy to share the image
as what you see on the screen at a given moment.
If you press &lt;em&gt;p&lt;/em&gt;, a new tab will open with a screenshot of the rendered content.
For technical details, see "&lt;a href="http://learningthreejs.com/blog/2011/09/03/screenshot-in-javascript/"&gt;screenhot in javascript&lt;/a&gt;" post.
It it usefull to share nice images and to help debug on various plateforms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Window resize&lt;/strong&gt; is supported because it is required. Without it, your 3D scene will loose scaleing and center
when you resize the window or go fullscreen.
For technical details, see "&lt;a href="http://learningthreejs.com/blog/2011/08/30/window-resize-for-your-demos/"&gt;window resize your demos&lt;/a&gt;" post.&lt;/p&gt;

&lt;h2&gt;How to use it ?&lt;/h2&gt;

&lt;p&gt;It is quite simple, first you download the code as
a &lt;a href="https://github.com/jeromeetienne/threejsboilerplate/zipball/master"&gt;.zip file&lt;/a&gt;
or with the &lt;a href="http://git-scm.com/"&gt;git&lt;/a&gt; command line below.&lt;/p&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone git://github.com/jeromeetienne/threejsboilerplate.git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;then start updating &lt;a href="https://github.com/jeromeetienne/threejsboilerplate/blob/master/index.html"&gt;index.html&lt;/a&gt; to
fit your needs.
Below is a screencast where i use the
&lt;a href="https://github.com/jeromeetienne/threejsboilerplate"&gt;boilerplate&lt;/a&gt;
to display a
&lt;a href="http://en.wikipedia.org/wiki/Utah_teapot"&gt;teapot&lt;/a&gt;
in only 3min!
Why a teapot ? Because it is the "hello world" of 3D :)
Be warned, the video is a bit speedy, im just starting at doing screencast.
Next ones will hopefully be slower.&lt;/p&gt;

&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/0XPOCi6FJX0?hl=en&amp;fs=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Later, it may be cool to have a builder.
I think it may be possible to make it client only.
&lt;a href="http://jszip.stuartk.co.uk/"&gt;jszip&lt;/a&gt; library would create the zip containing all the files.
&lt;a href="https://github.com/jeromeetienne/shorttag.js"&gt;shorttag.js&lt;/a&gt; library would compile templates according to user needs.
A builder would produce a cleaner and smaller result for you to play with.&lt;/p&gt;

&lt;p&gt;I gave a short talk at &lt;a href="http://parisjs.org"&gt;parisjs&lt;/a&gt; about it, here are the &lt;a href="http://jeromeetienne.github.com/slides-3jsbp-parisjs14"&gt;slides&lt;/a&gt;.
I hope
&lt;a href="https://github.com/jeromeetienne/threejsboilerplate"&gt;boilerplate for three.js&lt;/a&gt;.
will help make
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
even easier to use.
This project is quite new and will likely improve soon.
That's all folks, have fun :)&lt;/p&gt;
</description></item><item><title>Particles: Online Editor for Sparks.js</title><link>http://learningthreejs.com/blog/2011/12/19/particles-online-editor-for-sparks-js/</link><pubDate>Mon, 19 Dec 2011 03:11:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/12/19/particles-online-editor-for-sparks-js</guid><description>&lt;p&gt;This post is the second of our &lt;a href="http://learningthreejs.com/blog/categories/particles"&gt;serie on particles&lt;/a&gt;.
It presents
&lt;a href="https://github.com/jeromeetienne/sparkseditor"&gt;sparkseditor&lt;/a&gt;
, an online editor for
&lt;a href="https://github.com/zz85/sparks.js"&gt;sparks.js&lt;/a&gt;
with
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;.
In a few word, it is a webpage which provide an text editor
with a
&lt;a href="https://github.com/zz85/sparks.js"&gt;sparks.js&lt;/a&gt;
effect.
You got the code in the editor
and
you see &lt;em&gt;live&lt;/em&gt; the resulting particles effect.
I like this &lt;em&gt;live&lt;/em&gt; aspect a lot :)
I think it makes your design more direct, lower latency, less overhead.
More creative in a way.
&lt;a href="http://jeromeetienne.github.com/sparkseditor/"&gt;Try it out&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This editor has been made to lower the barrier of entry on
&lt;a href="https://github.com/zz85/sparks.js"&gt;sparks.js&lt;/a&gt;
with
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
particles.
The UI is rather simple and obvious. You can find a small presentation in the
screencast below.&lt;/p&gt;

&lt;!-- more --&gt;




&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/nu00FhIW5bc?hl=en&amp;fs=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;h2&gt;Live editor rocks&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://jeromeetienne.github.com/sparkseditor/"&gt;Sparkseditor&lt;/a&gt;
is widely inpired by
&lt;a href="http://glsl.heroku.com/e"&gt;glsl editor&lt;/a&gt;
from
&lt;a href="http://mrdoob.com/"&gt;mrdoob&lt;/a&gt;
and
&lt;a href="http://www.iquilezles.org/apps/shadertoy/"&gt;shadertoy&lt;/a&gt;
from
&lt;a href="http://www.iquilezles.org/"&gt;Inigo Quilez&lt;/a&gt;.
On the same vibe,
&lt;a href="http://lea.verou.me/"&gt;lea verou&lt;/a&gt;
recently
released
&lt;a href="http://lea.verou.me/2011/12/introducing-dabblet-an-interactive-css-playground/"&gt;dablet&lt;/a&gt;,
an online editor for
&lt;a href="http://en.wikipedia.org/wiki/Cascading_Style_Sheets"&gt;css&lt;/a&gt;.
There is a clear trend here...
What is it about those live editors ?
A live editor produces a result immediatly.
This helps design your effect faster.
Be light on your foot kindof style.
Very agile way to design.&lt;/p&gt;

&lt;p&gt;Additionally, it is easy to share with others because we have &lt;em&gt;bookmarkability&lt;/em&gt;.
We do that by storing state in url.
On the down side, it makes super long+ugly urls...
&lt;a href="http://en.wikipedia.org/wiki/URL_shortening"&gt;url shortening&lt;/a&gt;
helps us reduces this issue.
In our case, we use
&lt;a href="https://bitly.com/"&gt;bitly&lt;/a&gt;
service.&lt;/p&gt;

&lt;p&gt;This editor is purely static files.
No specific server to run, no need to admin and no risk to go offline.
Oh and by the way i dont not even have to pay for hosting this application.
I think it shows html5 in all its power.
The web is becoming something real nice :) html5 i love you!&lt;/p&gt;

&lt;h2&gt;conclusion&lt;/h2&gt;

&lt;p&gt;Under the hood,
&lt;a href="https://github.com/jeromeetienne/sparkseditor"&gt;sparkseditor&lt;/a&gt;
uses
&lt;a href="https://github.com/jeromeetienne/threex/blob/master/threex.sparks.js"&gt;threex.sparks.js&lt;/a&gt;, a
&lt;a href="https://github.com/jeromeetienne/threex"&gt;threex&lt;/a&gt;
helper, to make
&lt;a href="https://github.com/zz85/sparks.js/"&gt;sparks.js&lt;/a&gt;
even easier to use.
This helper will be the subject of a future post of our
&lt;a href="http://learningthreejs.com/blog/categories/particles"&gt;particles series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The source is open-source under
&lt;a href="https://github.com/jeromeetienne/sparkseditor/blob/master/MIT-LICENSE.txt"&gt;MIT&lt;/a&gt;.
You can get it in its &lt;a href="https://github.com/jeromeetienne/sparkseditor"&gt;git repository&lt;/a&gt;.
If you hit bugs, fill issues on github.
Feel free to fork and modify it!
That's all folks, have fun :)&lt;/p&gt;
</description></item><item><title>Particles: Introduction to Sparks.js</title><link>http://learningthreejs.com/blog/2011/12/14/particles-introduction-to-sparks-js/</link><pubDate>Wed, 14 Dec 2011 03:08:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/12/14/particles-introduction-to-sparks-js</guid><description>&lt;p&gt;This post is the first of a &lt;a href="http://learningthreejs.com/blog/categories/particles"&gt;serie on particles&lt;/a&gt;.
It specifically is about
&lt;a href="https://github.com/zz85/sparks.js"&gt;sparks.js&lt;/a&gt;.
&lt;em&gt;sparks.js&lt;/em&gt; is lightweight 3d
&lt;a href="http://en.wikipedia.org/wiki/Particle_system"&gt;Particle system&lt;/a&gt;
in javascript, for use with
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
and
&lt;a href="https://github.com/sole/tween.js"&gt;tween.js&lt;/a&gt;.
It is from
&lt;a href="http://www.lab4games.net/zz85/blog/"&gt;zz85&lt;/a&gt;
who already did
&lt;a href="http://mrdoob.github.com/three.js/examples/webgl_geometry_text.html"&gt;3D text&lt;/a&gt;
and
&lt;a href="http://mrdoob.github.com/three.js/examples/webgl_geometry_subdivison.html"&gt;Catmull Clark subdivision&lt;/a&gt;.
a productive guy :)&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://mrdoob.github.com/three.js/examples/webgl_particles_shapes.html"&gt;demo&lt;/a&gt;
below is one of the many &lt;a href="https://github.com/mrdoob/three.js/tree/master/examples"&gt;three.js examples&lt;/a&gt;.
While im on it, the &lt;a href="https://github.com/mrdoob/three.js/tree/master/examples"&gt;example directory&lt;/a&gt; is a gold mine.
Go dig in it to understand how to code three.js :)
Back to the point, this
&lt;a href="http://mrdoob.github.com/three.js/examples/webgl_particles_shapes.html"&gt;demo&lt;/a&gt;
is rather cool no ?
You want to do the same ?
During this short post, let me walk you walk thru the code for particles in this example.&lt;/p&gt;

&lt;!-- more --&gt;




&lt;iframe src="http://mrdoob.github.com/three.js/examples/webgl_particles_shapes.html" width="100%" height="420" frameborder="0"&gt;&lt;/iframe&gt;


&lt;h2&gt;Lets Get Started&lt;/h2&gt;

&lt;p&gt;So lets see how to use it. First step, you download
&lt;a href="https://raw.github.com/zz85/sparks.js/master/Sparks.js"&gt;sparks.js&lt;/a&gt;
from
&lt;a href="https://github.com/zz85/sparks.js"&gt;its github&lt;/a&gt;.
Then include it in your own code.&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script src="Sparks.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;First a few words on what is a &lt;a href="http://en.wikipedia.org/wiki/Particle_system"&gt;particle system&lt;/a&gt;.
It is usually composed of 2 big parts: the &lt;em&gt;emitter&lt;/em&gt; and the &lt;em&gt;particle&lt;/em&gt; itself.
Emitter creates particles.
Particles are usually smallish objects on the screen.
As you got many particles at the same time, they appears a single mass.&lt;/p&gt;

&lt;!-- more --&gt;


&lt;h2&gt;Let's create an emitter&lt;/h2&gt;

&lt;p&gt;First we create the emitter like this.
&lt;code&gt;emitter&lt;/code&gt; is the main object we will play with.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var counter = new SPARKS.SteadyCounter( 500 );
var emitter = new SPARKS.Emitter( counter );
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;code&gt;counter&lt;/code&gt; controls how frequently particles are created. Here it will
emit 500 particles per seconds.
Now let's start emit particles.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;emitter.start();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Sparks.js has a very flexible core.
It mainly uses two stacks of functions.
&lt;em&gt;Initializers&lt;/em&gt; is the stack run at the creation of a particle.
&lt;em&gt;Actions&lt;/em&gt; is another stack which is run at every step of a particle life.
Those functions are run in order.
Up to you to configure them to fit your needs.
You can easily code your own &lt;em&gt;initializer&lt;/em&gt; or &lt;em&gt;action&lt;/em&gt;.
Dont worry it got a bunch of predefined ones.&lt;/p&gt;

&lt;h2&gt;Let's initialize&lt;/h2&gt;

&lt;p&gt;Lets me walk you thru the ones used in our example. The whole stack is below.
&lt;code&gt;emitter.addInitializer()&lt;/code&gt; to push a new initializer, and &lt;code&gt;emitter.removeInitializer()&lt;/code&gt;
to remove it, not too hard :)&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;emitterpos = new THREE.Vector3( 0, 0, 0 );
emitter.addInitializer( new SPARKS.Position( new SPARKS.PointZone( emitterpos ) ) );
emitter.addInitializer( new SPARKS.Lifetime( 1, 15 ));
var vector = new THREE.Vector3( 0, -5, 1 );
emitter.addInitializer( new SPARKS.Velocity( new SPARKS.PointZone( vector ) ) );
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SPARKS.Position(zone)&lt;/code&gt; initializer set the original position of the particle.
A &lt;code&gt;zone&lt;/code&gt; provide a location in space.
&lt;code&gt;new SPARKS.PointZone( emitterpos )&lt;/code&gt; means the particles will always start from this specific point in space.
It is possible to have other zones.
For example, &lt;code&gt;SPARKS.LineZone( startVector3, endVector3 )&lt;/code&gt; represents a line between 2 points, so
your particle would start anywhere on this line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SPARKS.Lifetime(mintime, maxtime)&lt;/code&gt; initializer set particle's lifetime.
You can specify a range and a random value will be assigned.
Don't forget to add &lt;code&gt;SPARKS.Age&lt;/code&gt; action to handle its lifetime.
And the last one.
&lt;code&gt;SPARKS.Velocity(zone)&lt;/code&gt; set particle's velocity based on a &lt;code&gt;zone&lt;/code&gt; location.
The initializer stack is setup the particle at the begining of its life.
Let's see what happen during this life.&lt;/p&gt;

&lt;h2&gt;Let's do some actions&lt;/h2&gt;

&lt;p&gt;Actions are performed at every step of a particle life.
Same as with initializers, &lt;code&gt;emitter.addAction()&lt;/code&gt; to push a new action,
&lt;code&gt;emitter.removeAction()&lt;/code&gt; to remove it.
For our examples the whole action stack is this.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;emitter.addAction( new SPARKS.Age() );
emitter.addAction( new SPARKS.Accelerate( 0, 0, -50 ) );
emitter.addAction( new SPARKS.Move() );
emitter.addAction( new SPARKS.RandomDrift( 90, 100, 2000 ) );
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Now lets details it.
We have already seen &lt;code&gt;SPARKS.Age()&lt;/code&gt;. It is handle the lifetime of each particle.
&lt;code&gt;SPARKS.Accelerate(x,y,z)&lt;/code&gt; changes the velocity by adding a fixed amount at every step.
This one produces a gravity effect with a negative &lt;code&gt;y&lt;/code&gt;.
&lt;code&gt;SPARKS.Move()&lt;/code&gt; makes the particles move in our 3D space.
&lt;code&gt;SPARKS.RandomDrift(x,y,z)&lt;/code&gt; changes the velocity by adding a random amount at every step.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I hope this short introduction got you excited about
&lt;a href="https://github.com/zz85/sparks.js/"&gt;sparks.js&lt;/a&gt;.
Next posts on the
&lt;a href="http://learningthreejs.com/blog/categories/particles"&gt;particle series&lt;/a&gt;
may be a UI editor, stay tuned!
I find sparks.js clean and flexible.
Flexibility is very important for particles.
It helps provide a wide variety of effect in your games/demos.
For more informations and authoritative answer, see the
&lt;a href="https://github.com/zz85/sparks.js/"&gt;github repository&lt;/a&gt;.
That's all for today folks, have fun.&lt;/p&gt;
</description></item><item><title>Constructive Solid Geometry With csg.js</title><link>http://learningthreejs.com/blog/2011/12/10/constructive-solid-geometry-with-csg-js/</link><pubDate>Sat, 10 Dec 2011 07:30:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/12/10/constructive-solid-geometry-with-csg-js</guid><description>&lt;p&gt;This post is about
&lt;a href="http://en.wikipedia.org/wiki/Constructive_solid_geometry"&gt;constructive solid geometry&lt;/a&gt;
, an impressive word :)
In fact, it is just a way to build complex objects from simpler ones.
Those simple objects are assembled using
&lt;a href="http://en.wikipedia.org/wiki/Algebra_of_sets"&gt;operations&lt;/a&gt;
such as union, difference and intersection.&lt;/p&gt;

&lt;p&gt;Recently
&lt;a href="http://madebyevan.com/"&gt;Evan Wallas&lt;/a&gt;
released
&lt;a href="http://evanw.github.com/csg.js/"&gt;csg.js&lt;/a&gt;,
a clean self-contained library to do
&lt;a href="http://en.wikipedia.org/wiki/Constructive_solid_geometry"&gt;constructive solid geometry&lt;/a&gt;.
So &lt;a href="http://chandler.prallfamily.com/"&gt;Chandler Prall&lt;/a&gt;
wrote a
&lt;a href="http://chandler.prallfamily.com/2011/12/constructive-solid-geometry-with-three-js/"&gt;bridge&lt;/a&gt;
to make it easy to use with
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With all this nice code, i wrote the little
&lt;a href="http://learningthreejs.com/data/constructive-solid-geometry-with-csg-js/"&gt;demo of a dice&lt;/a&gt;
you can see on the right. Thus you can click to change the operations, play
with it and have a feel of the various operations.&lt;/p&gt;

&lt;!-- more --&gt;




&lt;iframe src="http://learningthreejs.com/data/constructive-solid-geometry-with-csg-js/"
    width="100%" height="420" frameborder="0" style="float: right; margin-left: 1em;"&gt;
&lt;/iframe&gt;


&lt;h2&gt;Let's start&lt;/h2&gt;

&lt;p&gt;So lets see how to use it. First step, you download
csg.js from
&lt;a href="http://evanw.github.com/csg.js/"&gt;here&lt;/a&gt;
, ThreeCSG.js bridge from
&lt;a href="http://chandler.prallfamily.com/labs/three/csg/ThreeCSG.js"&gt;here&lt;/a&gt;
. Then include those line in your own code.&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script src="csg.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="ThreeCSG.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Let's convert&lt;/h2&gt;

&lt;p&gt;Ok now that we got the source, let's use it.
&lt;a href="http://chandler.prallfamily.com/2011/12/constructive-solid-geometry-with-three-js/"&gt;ThreeCSG.js&lt;/a&gt;
is a bridge between
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
and
&lt;a href="http://evanw.github.com/csg.js/"&gt;csg.js&lt;/a&gt;.
Both libraries use a different format for geometry.
&lt;a href="http://chandler.prallfamily.com/2011/12/constructive-solid-geometry-with-three-js/"&gt;ThreeCSG.js&lt;/a&gt;
does the conversion back and forth.
To convert your three.js geometry to a csg geometry, use this line.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var geometryCsg = THREE.CSG.toCSG(geometryThree);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Then you likely apply
&lt;a href="http://en.wikipedia.org/wiki/Algebra_of_sets"&gt;boolean operations&lt;/a&gt;
on
&lt;a href="http://evanw.github.com/csg.js/"&gt;csg.js&lt;/a&gt; geometry.
To convert it back to three.js, just do&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var geometryThree   = THREE.CSG.fromCSG(geometryCsg);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;You end up with a normal &lt;a href="https://github.com/mrdoob/three.js/blob/master/src/core/Geometry.js"&gt;three.js geometry&lt;/a&gt;
than you can use everywhere, like
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/GeometryUtils.js"&gt;GeometryUtils&lt;/a&gt;
or
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/objects/Mesh.js"&gt;Mesh&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Let's assemble&lt;/h2&gt;

&lt;p&gt;Now the fun part, lets assemble objects with those misterious
&lt;a href="http://en.wikipedia.org/wiki/Algebra_of_sets"&gt;boolean operations&lt;/a&gt;
on ours
&lt;a href="http://evanw.github.com/csg.js/"&gt;csg.js&lt;/a&gt;
geometries.
There are &lt;a href="http://en.wikipedia.org/wiki/Constructive_solid_geometry"&gt;3 of them&lt;/a&gt;:
&lt;a href="http://en.wikipedia.org/wiki/Complement_(set_theory)"&gt;difference&lt;/a&gt;,
&lt;a href="http://en.wikipedia.org/wiki/Union_(set_theory)"&gt;union&lt;/a&gt;
and
&lt;a href="http://en.wikipedia.org/wiki/Intersection_(set_theory)"&gt;intersect&lt;/a&gt;.
To build our dice, first we build a
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/CubeGeometry.js"&gt;cube&lt;/a&gt;
then we subtract a bunch of
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/SphereGeometry.js"&gt;spheres&lt;/a&gt;
to makes the holes.&lt;/p&gt;

&lt;p&gt;&lt;img class='right ' src='http://learningthreejs.com/data/constructive-solid-geometry-with-csg-js/images/image-substract-320x240.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;Once you converted your objects, you apply operations on them.
For the dice, we use &lt;code&gt;.subtract()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cube.substract(spheres)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;div style="clear:both;"&gt;&lt;/div&gt;


&lt;p&gt;&lt;img class='right ' src='http://learningthreejs.com/data/constructive-solid-geometry-with-csg-js/images/image-union-320x240.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;But it is possible to use &lt;code&gt;.union()&lt;/code&gt; to add them.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cube.union(spheres)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Or to keep only the common part of both objects with &lt;code&gt;.intersect()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img class='right ' src='http://learningthreejs.com/data/constructive-solid-geometry-with-csg-js/images/image-intersect-320x240.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;You can chain those operations to your own taste.
Up to you to be creative :)&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cube.intersect(spheres)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Constructive_solid_geometry"&gt;Constructive solid Geometry&lt;/a&gt;
is simple and quite powerfull.
&lt;a href="http://evanw.github.com/csg.js/"&gt;csg.js&lt;/a&gt;
,
&lt;a href="http://chandler.prallfamily.com/2011/12/constructive-solid-geometry-with-three-js/"&gt;ThreeCSG.js&lt;/a&gt;
and
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
makes it real easy to play with.
You may look at the source of our dice
&lt;a href="http://learningthreejs.com/data/constructive-solid-geometry-with-csg-js/"&gt;demo&lt;/a&gt;
to see a working version of this code.
That's all for today, have fun :)&lt;/p&gt;
</description><enclosure url="http://chandler.prallfamily.com/labs/three/csg/ThreeCSG.js" length="5270" type="application/octet-stream" /><media:content url="http://chandler.prallfamily.com/labs/three/csg/ThreeCSG.js" fileSize="5270" type="application/octet-stream" /><itunes:explicit>no</itunes:explicit><itunes:subtitle> This post is about constructive solid geometry , an impressive word :) In fact, it is just a way to build complex objects from simpler ones. Those simple objects are assembled using operations such as union, difference and intersection. Recently Evan Wal</itunes:subtitle><itunes:summary> This post is about constructive solid geometry , an impressive word :) In fact, it is just a way to build complex objects from simpler ones. Those simple objects are assembled using operations such as union, difference and intersection. Recently Evan Wallas released csg.js, a clean self-contained library to do constructive solid geometry. So Chandler Prall wrote a bridge to make it easy to use with three.js. With all this nice code, i wrote the little demo of a dice you can see on the right. Thus you can click to change the operations, play with it and have a feel of the various operations. Let's start So lets see how to use it. First step, you download csg.js from here , ThreeCSG.js bridge from here . Then include those line in your own code. ```html &amp;lt;script src="csg.js"&amp;gt;&amp;lt;/script&amp;gt; &amp;lt;script src="ThreeCSG.js"&amp;gt;&amp;lt;/script&amp;gt; ``` Let's convert Ok now that we got the source, let's use it. ThreeCSG.js is a bridge between three.js and csg.js. Both libraries use a different format for geometry. ThreeCSG.js does the conversion back and forth. To convert your three.js geometry to a csg geometry, use this line. ```javascript var geometryCsg = THREE.CSG.toCSG(geometryThree); ``` Then you likely apply boolean operations on csg.js geometry. To convert it back to three.js, just do ```javascript var geometryThree = THREE.CSG.fromCSG(geometryCsg); ``` You end up with a normal three.js geometry than you can use everywhere, like GeometryUtils or Mesh. Let's assemble Now the fun part, lets assemble objects with those misterious boolean operations on ours csg.js geometries. There are 3 of them: difference, union and intersect. To build our dice, first we build a cube then we subtract a bunch of spheres to makes the holes. Once you converted your objects, you apply operations on them. For the dice, we use .subtract(). ```javascript cube.substract(spheres) ``` But it is possible to use .union() to add them. ```javascript cube.union(spheres) ``` Or to keep only the common part of both objects with .intersect(). You can chain those operations to your own taste. Up to you to be creative :) ```javascript cube.intersect(spheres) ``` Conclusion Constructive solid Geometry is simple and quite powerfull. csg.js , ThreeCSG.js and three.js makes it real easy to play with. You may look at the source of our dice demo to see a working version of this code. That's all for today, have fun :) </itunes:summary></item><item><title>Lets Make a 3D Game: Make It Embedded</title><link>http://learningthreejs.com/blog/2011/11/21/lets-make-a-3d-game-make-it-embedded/</link><pubDate>Mon, 21 Nov 2011 07:32:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/11/21/lets-make-a-3d-game-make-it-embedded</guid><description>&lt;iframe src="http://marblesoccer.com"
    allowfullscreen webkitallowfullscreen mozallowfullscreen
    width="420" height="315" frameborder="0" style="float: right; margin-left: 1em;"&gt;
&lt;/iframe&gt;


&lt;p&gt;This post is part of the &lt;a href="http://learningthreejs.com/blog/categories/tutorial3dgame/"&gt;"Let's make a 3D game"&lt;/a&gt; series.
The previous post was on
&lt;a href="http://learningthreejs.com/blog/2011/11/17/lets-make-a-3d-game-make-it-fullscreen/"&gt;fullscreen API&lt;/a&gt;.
Here is another one on resizing the display area.
This post is about embedding your game in another page.
It is usefull to include it in a blog, in facebook, iGoogle or other game plateforms.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://marblesoccer.com"&gt;MarbleSoccer&lt;/a&gt;
now contains all the tricks explained in this post.
&lt;em&gt;Show dont tell&lt;/em&gt;, you can see it embedded on the left.
Embedding your game implies various things.
As your game is hosted in another page, it likely got a smaller display area.
HTML5
&lt;a href="http://www.w3.org/TR/css3-mediaqueries/"&gt;CSS media query&lt;/a&gt;
makes it easy to fit various sizes.
Another part are the
&lt;a href="http://en.wikipedia.org/wiki/DOM_events"&gt;DOM events&lt;/a&gt;
from the iframe.
They will be propagated to the host page and may produce undesirable effects.
We see how to shield them.
But first let's see about
&lt;a href="http://en.wikipedia.org/wiki/HTML_element#Frames"&gt;iframe&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Let's go play in an iframe&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/HTML_element#Frames"&gt;iframe&lt;/a&gt;
is an easy and secure way to embed a page in another.
Let's declare it.&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;iframe src="http://marblesoccer.com"
    allowfullscreen webkitallowfullscreen mozallowfullscreen
    width="480" height="320" frameborder="0"&amp;gt;
&amp;lt;/iframe&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;!-- more --&gt;


&lt;p&gt;The attributes are pretty classics: &lt;code&gt;frameborder&lt;/code&gt; to remove an ugly default border,
&lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; for size and &lt;code&gt;src&lt;/code&gt; for your game page.
The ones ending with &lt;code&gt;allowfullscreen&lt;/code&gt; tell the browser that this iframe is
allowed to go fullscreen. More about fullscreen in this
&lt;a href="http://learningthreejs.com/blog/2011/11/17/lets-make-a-3d-game-make-it-fullscreen/"&gt;previous post&lt;/a&gt;
or in the &lt;a href="http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html"&gt;spec&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You may need to determined if your game is embedded or not.
Use this line will tell if it is in a iframe or not.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var isInIframe  = (window != window.top);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Fit in a smaller display area&lt;/h2&gt;

&lt;p&gt;When your game is embedded, it is likely to have a smaller display area.
How to deal with this ?
First, we have 2 types of rendering in our game:
a 3D display where
&lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt;
displays the
&lt;a href="http://en.wikipedia.org/wiki/WebGL"&gt;WebGL&lt;/a&gt;, and
a DOM display for
&lt;a href="http://en.wikipedia.org/wiki/On-screen_display"&gt;OSD&lt;/a&gt;
such as score, timers and other popups.&lt;/p&gt;

&lt;p&gt;For &lt;em&gt;3D rendering&lt;/em&gt;, we have already seen window resizing in
&lt;a href="http://learningthreejs.com/blog/2011/08/30/window-resize-for-your-demos/"&gt;this post&lt;/a&gt;.
Just download
&lt;a href="http://learningthreejs.com/data/THREEx/THREEx.WindowResize.js"&gt;THREEx.WindowResize&lt;/a&gt;
and add this line and you are done. Not too hard, hey.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;THREEx.WindowResize(renderer, camera);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Now &lt;em&gt;the DOM display&lt;/em&gt;. It may simply be done via CSS
and
&lt;a href="http://www.w3.org/TR/css3-mediaqueries/"&gt;media queries&lt;/a&gt;.
Typically, you may reduce the size of your font or icons.
I won't try to teach css, other do that much
&lt;a href="https://developer.mozilla.org/en/CSS/Media_queries"&gt;better&lt;/a&gt;
&lt;a href="http://www.html5rocks.com/en/mobile/mobifying.html#toc-mediaqueries"&gt;than&lt;/a&gt;
&lt;a href="http://thinkvitamin.com/code/media-queries-width-and-height-video-tutorial/"&gt;me&lt;/a&gt;.
Just a pick of what i did, not sure at all it is the best way.
I reduce the OSD display if your game page is 640px or less.&lt;/p&gt;

&lt;p&gt;```css&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@media all and (max-width: 640px) {
    /* here put your style specific for embedded case */
    body { font-size : 60%; }
    img { width : 48px; }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Shield Events&lt;/h2&gt;

&lt;p&gt;Strange section title, hey.
It means &lt;em&gt;prevents DOM events from the iframe to interfere with the host page&lt;/em&gt;.
Not much clearer...
Maybe with an example ? Let's see the arrows+scroll case.
Show dont tell.
Below are 2 iframes: on the left, no shielding happens, on the right shielding happens.
Try to click on them and use arrows up/down.&lt;/p&gt;

&lt;iframe src="http://learningthreejs.com/data/THREEx/examples/threex.embedded/noshield-iframe.html" width='50%' height='120px'&gt;&lt;/iframe&gt;


&lt;iframe src="http://learningthreejs.com/data/THREEx/examples/threex.embedded/withshield-iframe.html" width='49%' height='120px'&gt;&lt;/iframe&gt;


&lt;p&gt;On the left, the host page scrolls, but not on the right.
Why does this happen ? good question :)
If our game iframe got the focus and users press up or down, the iframe will received
&lt;a href="http://www.quirksmode.org/dom/events/keys.html"&gt;keydown/keyup events&lt;/a&gt;.
Up to now, all is ok...
Troubles appear when those events are bubbling to the host page, they may trigger a scrolling.&lt;/p&gt;

&lt;p&gt;Imagine the page going up and down while you play, the game becomes unplayable very fast :)
So here is the code which prevents this behavior. It listens to arrows
&lt;a href="http://www.quirksmode.org/dom/events/keys.html"&gt;keydown events&lt;/a&gt;.
and prevent their default.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;document.addEventListener('keydown', function(event){
    // if it is keydown on a arrow, prevent default
    if( event.keyCode &amp;gt;= 37 &amp;amp;&amp;amp; event.keyCode &amp;lt;= 40 ){
        event.preventDefault();
    }
}, true);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I gathered the code in
&lt;a href="http://learningthreejs.com/data/THREEx/threex.embedded.js"&gt;threex.embedded&lt;/a&gt;,
see its
&lt;a href="http://learningthreejs.com/data/THREEx/docs/threex.embedded.html"&gt;annoted source&lt;/a&gt;.
Iframe is a easy and secure way to make your game embeddable.
We have seen how to handle smaller display area
with
&lt;a href="http://learningthreejs.com/data/THREEx/THREEx.WindowResize.js"&gt;THREEx.WindowResize&lt;/a&gt;
and
&lt;a href="http://www.w3.org/TR/css3-mediaqueries/"&gt;media queries&lt;/a&gt;.
Additionnaly we even shield DOM events, so we can use arrow keys for player control.
You are all set! Go embed your game now :)&lt;/p&gt;
</description><enclosure url="http://learningthreejs.com/data/THREEx/THREEx.WindowResize.js" length="1159" type="application/x-javascript" /><media:content url="http://learningthreejs.com/data/THREEx/THREEx.WindowResize.js" fileSize="1159" type="application/x-javascript" /><itunes:explicit>no</itunes:explicit><itunes:subtitle> This post is part of the "Let's make a 3D game" series. The previous post was on fullscreen API. Here is another one on resizing the display area. This post is about embedding your game in another page. It is usefull to include it in a blog, in facebook,</itunes:subtitle><itunes:summary> This post is part of the "Let's make a 3D game" series. The previous post was on fullscreen API. Here is another one on resizing the display area. This post is about embedding your game in another page. It is usefull to include it in a blog, in facebook, iGoogle or other game plateforms. MarbleSoccer now contains all the tricks explained in this post. Show dont tell, you can see it embedded on the left. Embedding your game implies various things. As your game is hosted in another page, it likely got a smaller display area. HTML5 CSS media query makes it easy to fit various sizes. Another part are the DOM events from the iframe. They will be propagated to the host page and may produce undesirable effects. We see how to shield them. But first let's see about iframe Let's go play in an iframe iframe is an easy and secure way to embed a page in another. Let's declare it. ```html &amp;lt;iframe src="http://marblesoccer.com" allowfullscreen webkitallowfullscreen mozallowfullscreen width="480" height="320" frameborder="0"&amp;gt; &amp;lt;/iframe&amp;gt; ``` The attributes are pretty classics: frameborder to remove an ugly default border, width and height for size and src for your game page. The ones ending with allowfullscreen tell the browser that this iframe is allowed to go fullscreen. More about fullscreen in this previous post or in the spec. You may need to determined if your game is embedded or not. Use this line will tell if it is in a iframe or not. ```javascript var isInIframe = (window != window.top); ``` Fit in a smaller display area When your game is embedded, it is likely to have a smaller display area. How to deal with this ? First, we have 2 types of rendering in our game: a 3D display where three.js displays the WebGL, and a DOM display for OSD such as score, timers and other popups. For 3D rendering, we have already seen window resizing in this post. Just download THREEx.WindowResize and add this line and you are done. Not too hard, hey. ```javascript THREEx.WindowResize(renderer, camera); ``` Now the DOM display. It may simply be done via CSS and media queries. Typically, you may reduce the size of your font or icons. I won't try to teach css, other do that much better than me. Just a pick of what i did, not sure at all it is the best way. I reduce the OSD display if your game page is 640px or less. ```css @media all and (max-width: 640px) { /* here put your style specific for embedded case */ body { font-size : 60%; } img { width : 48px; } } ``` Shield Events Strange section title, hey. It means prevents DOM events from the iframe to interfere with the host page. Not much clearer... Maybe with an example ? Let's see the arrows+scroll case. Show dont tell. Below are 2 iframes: on the left, no shielding happens, on the right shielding happens. Try to click on them and use arrows up/down. On the left, the host page scrolls, but not on the right. Why does this happen ? good question :) If our game iframe got the focus and users press up or down, the iframe will received keydown/keyup events. Up to now, all is ok... Troubles appear when those events are bubbling to the host page, they may trigger a scrolling. Imagine the page going up and down while you play, the game becomes unplayable very fast :) So here is the code which prevents this behavior. It listens to arrows keydown events. and prevent their default. ```javascript document.addEventListener('keydown', function(event){ // if it is keydown on a arrow, prevent default if( event.keyCode &amp;gt;= 37 &amp;amp;&amp;amp; event.keyCode &amp;lt;= 40 ){ event.preventDefault(); } }, true); ``` Conclusion I gathered the code in threex.embedded, see its annoted source. Iframe is a easy and secure way to make your game embeddable. We have seen how to handle smaller display area with THREEx.WindowResize and media queries. Additionnaly we even shield DOM events, so we can use arrow keys for player control. You are all set! Go embed your game now :) </itunes:summary></item><item><title>Lets Make a 3D Game: Make It Fullscreen</title><link>http://learningthreejs.com/blog/2011/11/17/lets-make-a-3d-game-make-it-fullscreen/</link><pubDate>Wed, 16 Nov 2011 22:59:00 PST</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/11/17/lets-make-a-3d-game-make-it-fullscreen</guid><description>&lt;p&gt;This post is part of the &lt;a href="http://learningthreejs.com/blog/categories/tutorial3dgame/"&gt;"Lets make a 3D game"&lt;/a&gt; series.
It is about the &lt;a href="http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html"&gt;fullscreen API&lt;/a&gt;.
This API allows to make DOM elements fullscreen.
Fullscreen is quite important for games.
It provides a larger display so a more immersive experience for your players.
All that from javascript, so no more needed to ask "please f11" to your
players, isnt that sweet ? :)&lt;/p&gt;

&lt;p&gt;&lt;img class='right ' src='http://learningthreejs.com/data/lets-make-a-3d-game-make-it-fullscreen/images/fullscreen-icon.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html"&gt;fullscreen API&lt;/a&gt;
is still in discussion, but the basics are settled. At the time of this writing,
it is available in
&lt;a href="http://blog.pearce.org.nz/2011/11/firefoxs-html-full-screen-api-enabled.html"&gt;firefox nightly&lt;/a&gt;,
&lt;a href="http://peter.sh/2011/01/javascript-full-screen-api-navigation-timing-and-repeating-css-gradients/"&gt;webkit nightly&lt;/a&gt;and
&lt;a href="http://updates.html5rocks.com/2011/10/Let-Your-Content-Do-the-Talking-Fullscreen-API"&gt;chrome stable&lt;/a&gt;.
It has been already added in &lt;a href="http://marblesoccer.com"&gt;marbleSoccer&lt;/a&gt;.
The icon is from &lt;a href="http://thenounproject.com/"&gt;The Noun Project&lt;/a&gt;, a source of nice and clean icons.
Try it out! Click on it to toggle fullscreen state. If you dont see the icon, your browser
doesn't yet have the fullscreen API.&lt;/p&gt;

&lt;center&gt;
    &lt;iframe webkitallowfullscreen mozallowfullscreen allowfullscreen width="100%" height="320" src="http://marblesoccer.com" frameborder="0"&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;p&gt;Ok now is time for code :)&lt;/p&gt;

&lt;h2&gt;Let's get started&lt;/h2&gt;

&lt;p&gt;As usual, i provide a little helper to make it easier for you to include it in
your games. It is called &lt;a href="http://learningthreejs.com/data/THREEx/THREEx.FullScreen.js"&gt;THREEx.FullScreen.js&lt;/a&gt;.
It hides the prefix of each vendor and the little discrepencies between their API
implementation.
You download this file from &lt;a href="http://learningthreejs.com/data/THREEx/THREEx.FullScreen.js"&gt;here&lt;/a&gt; and include
it in your page like this&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script src='THREEx.FullScreen.js'&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;!-- more --&gt;


&lt;h2&gt;How to use it ?&lt;/h2&gt;

&lt;p&gt;The API is simple, only 4 calls. Lets see them one by one.
To test if it is possible to have fullscreen on your system, do&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;THREEx.FullScreen.available();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;To test if fullscreen is currently activated on your page&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;THREEx.FullScreen.activated();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;To Request fullscreen on a given element, just do&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;THREEx.FullScreen.request(element);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;If element isnt provided, it defaults to &lt;code&gt;document.body&lt;/code&gt;.
To cancel fullscreen on your page, use this line.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;THREEx.FullScreen.cancel();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Quite straight forward, no ? :) As an example, let's make a toggle, the same used
in &lt;a href="http://marblesoccer.com"&gt;marbleSoccer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if( THREEx.FullScreen.activated() ){
    THREEx.FullScreen.cancel();
}else{
    THREEx.FullScreen.request();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;What about the standard ?&lt;/h2&gt;

&lt;p&gt;There is a &lt;a href="http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html"&gt;w3c proposal&lt;/a&gt; in dicussion.
John dyer has written an in-depth &lt;a href="http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/"&gt;summary&lt;/a&gt;.
Mozilla provides details on &lt;a href="https://wiki.mozilla.org/Gecko:FullScreenAPI"&gt;their API&lt;/a&gt;.
At the time of this writing
It is available in
&lt;a href="http://blog.pearce.org.nz/2011/11/firefoxs-html-full-screen-api-enabled.html"&gt;firefox nightly&lt;/a&gt;,
&lt;a href="http://peter.sh/2011/01/javascript-full-screen-api-navigation-timing-and-repeating-css-gradients/"&gt;webkit nightly&lt;/a&gt;
and
&lt;a href="http://updates.html5rocks.com/2011/10/Let-Your-Content-Do-the-Talking-Fullscreen-API"&gt;chrome stable&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;For more details on &lt;a href="http://learningthreejs.com/data/THREEx/THREEx.FullScreen.js"&gt;THREEx.FullScreen&lt;/a&gt;,
see its &lt;a href="http://learningthreejs.com/data/THREEx/docs/THREEx.FullScreen.html"&gt;annoted source&lt;/a&gt;.
It is a simple to add in your game.
It provides a more immersive experience to your players.
On a related subject, we will soon likely do a post about embedding your game in another page.
It is usefull when you want to include it in a blog, in facebook or other game plateforms.&lt;/p&gt;
</description><enclosure url="http://learningthreejs.com/data/THREEx/THREEx.FullScreen.js" length="2197" type="application/x-javascript" /><media:content url="http://learningthreejs.com/data/THREEx/THREEx.FullScreen.js" fileSize="2197" type="application/x-javascript" /><itunes:explicit>no</itunes:explicit><itunes:subtitle> This post is part of the "Lets make a 3D game" series. It is about the fullscreen API. This API allows to make DOM elements fullscreen. Fullscreen is quite important for games. It provides a larger display so a more immersive experience for your players.</itunes:subtitle><itunes:summary> This post is part of the "Lets make a 3D game" series. It is about the fullscreen API. This API allows to make DOM elements fullscreen. Fullscreen is quite important for games. It provides a larger display so a more immersive experience for your players. All that from javascript, so no more needed to ask "please f11" to your players, isnt that sweet ? :) The fullscreen API is still in discussion, but the basics are settled. At the time of this writing, it is available in firefox nightly, webkit nightlyand chrome stable. It has been already added in marbleSoccer. The icon is from The Noun Project, a source of nice and clean icons. Try it out! Click on it to toggle fullscreen state. If you dont see the icon, your browser doesn't yet have the fullscreen API. Ok now is time for code :) Let's get started As usual, i provide a little helper to make it easier for you to include it in your games. It is called THREEx.FullScreen.js. It hides the prefix of each vendor and the little discrepencies between their API implementation. You download this file from here and include it in your page like this ```html &amp;lt;script src='THREEx.FullScreen.js'&amp;gt;&amp;lt;/script&amp;gt; ``` How to use it ? The API is simple, only 4 calls. Lets see them one by one. To test if it is possible to have fullscreen on your system, do ```javascript THREEx.FullScreen.available(); ``` To test if fullscreen is currently activated on your page ```javascript THREEx.FullScreen.activated(); ``` To Request fullscreen on a given element, just do ```javascript THREEx.FullScreen.request(element); ``` If element isnt provided, it defaults to document.body. To cancel fullscreen on your page, use this line. ```javascript THREEx.FullScreen.cancel(); ``` Quite straight forward, no ? :) As an example, let's make a toggle, the same used in marbleSoccer. ```javascript if( THREEx.FullScreen.activated() ){ THREEx.FullScreen.cancel(); }else{ THREEx.FullScreen.request(); } ``` What about the standard ? There is a w3c proposal in dicussion. John dyer has written an in-depth summary. Mozilla provides details on their API. At the time of this writing It is available in firefox nightly, webkit nightly and chrome stable. Conclusion For more details on THREEx.FullScreen, see its annoted source. It is a simple to add in your game. It provides a more immersive experience to your players. On a related subject, we will soon likely do a post about embedding your game in another page. It is usefull when you want to include it in a blog, in facebook or other game plateforms. </itunes:summary></item><item><title>Lets Make a 3D Game: microphysics.js, even easier</title><link>http://learningthreejs.com/blog/2011/11/02/lets-make-a-3d-game-helper-for-microphysics-js/</link><pubDate>Wed, 02 Nov 2011 10:16:00 PDT</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/11/02/lets-make-a-3d-game-helper-for-microphysics-js</guid><description>&lt;p&gt;This post is part of the &lt;a href="http://learningthreejs.com/blog/categories/tutorial3dgame/"&gt;"Lets make a 3D game"&lt;/a&gt; series.
It is a follow up from the previous article on &lt;a href="http://learningthreejs.com/blog/2011/10/17/lets-make-a-3d-game-microphysics-js/"&gt;microphysics.js&lt;/a&gt;.
It will describe how to easily include &lt;strong&gt;microphysics.js&lt;/strong&gt; in your three.js games.
&lt;a href="https://raw.github.com/jeromeetienne/microphysics.js/master/THREEx.microphysics.js"&gt;THREEx.microphysics.js&lt;/a&gt; is a THREEx wrapper for microphysics.js.
It helps binding &lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt; objects to &lt;a href="http://learningthreejs.com/blog/2011/10/17/lets-make-a-3d-game-microphysics-js/"&gt;microphysics.js&lt;/a&gt;.
The API is chained for convenience.&lt;/p&gt;

&lt;h2&gt;Let's get started&lt;/h2&gt;

&lt;p&gt;So lets see how to use it.
First step, you download it
&lt;a href="https://raw.github.com/jeromeetienne/microphysics.js/master/THREEx.microphysics.js"&gt;here&lt;/a&gt;.
Then include it in your own code with this line.&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script src="THREEx.microphysics.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;!-- more --&gt;


&lt;h2&gt;Initialisation&lt;/h2&gt;

&lt;p&gt;You instanciate the physics engine, like that.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var microphysics = new THREEx.Microphysics(opts);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;code&gt;opts&lt;/code&gt; is optional.
&lt;code&gt;opts.timeStep&lt;/code&gt; controls the frequency of the world update.
The smaller it is the more accurate is the physics but the longer it is to compute.
It defaults to &lt;code&gt;1/60&lt;/code&gt;. Once instanciated, you start it.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;microphysics.start();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Binding THREE.Mesh&lt;/h2&gt;

&lt;p&gt;Of course we need to add some mesh in the world. After this line, the &lt;code&gt;mesh&lt;/code&gt;
is bound to microphysics.js, so its position is driven by the physics.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;microphysics.bindMesh(mesh, opts);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mesh.position&lt;/code&gt; is honored.
If you need to unbind a &lt;code&gt;mesh&lt;/code&gt;, just do&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;microphysics.unbindMesh(mesh);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;At the time of this writing, microphysics.js support only moving sphere and static
boxes, so geometry may only be &lt;code&gt;THREE.SphereGeometry&lt;/code&gt; or &lt;code&gt;THREE.CubeGeometry&lt;/code&gt;.
If your mesh got another geometry, use &lt;code&gt;opts.geometry&lt;/code&gt; to say how you wish the mesh
to be handled.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;microphysics.bindMesh(mesh, {
     geometry   : new THREE.CubeGeometry(200,200,200);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;It is also possible to overwrite &lt;code&gt;Mesh.position&lt;/code&gt; with &lt;code&gt;opts.position&lt;/code&gt;, or
to send options directly to microphysics.js with &lt;code&gt;opts.physics&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;microphysics.bindMesh(mesh, {
    // to overwrite the Mesh.position
    position    : { x : 1, y : 1, z : 2 },
    // to pass options directly to microphysics.js
    physics     : { restitution : 0.98 }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Updating the physics&lt;/h2&gt;

&lt;p&gt;In your render loop, just add this line. It will first update the physics world and
then move accordingly any &lt;code&gt;THREE.Mesh&lt;/code&gt; you bound.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;microphysics.update();  
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;Needs a Direct Access ?&lt;/h2&gt;

&lt;p&gt;If you need to have direct access to microphysics.js, uses
&lt;code&gt;microphysics.body(mesh)&lt;/code&gt; to get the &lt;code&gt;vphy.Body&lt;/code&gt; bound to &lt;code&gt;mesh&lt;/code&gt;.
To access &lt;code&gt;vphy.World&lt;/code&gt;, just use &lt;code&gt;microphysics.word()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In the previous article on &lt;a href="http://learningthreejs.com/blog/2011/10/17/lets-make-a-3d-game-microphysics-js/"&gt;microphysics.js&lt;/a&gt;,
we learned how to use microphysics.js directly. This article makes it really easy to include
in your &lt;a href="https://github.com/mrdoob/three.js/"&gt;three.js&lt;/a&gt; demo/game.
It is so nice that it is what is used in the
&lt;a href="http://jeromeetienne.github.com/microphysics.js/playground/"&gt;playground&lt;/a&gt;.
That's all for today folks. Have fun :)&lt;/p&gt;
</description></item><item><title>Lets Make a 3D Game: microphysics.js</title><link>http://learningthreejs.com/blog/2011/10/17/lets-make-a-3d-game-microphysics-js/</link><pubDate>Mon, 17 Oct 2011 03:36:00 PDT</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/10/17/lets-make-a-3d-game-microphysics-js</guid><description>&lt;p&gt;This post is part of the &lt;a href="http://learningthreejs.com/blog/categories/tutorial3dgame/"&gt;"Lets make a 3D game"&lt;/a&gt; series.
3D and physics simulation always go well together
&lt;a href="http://www.youtube.com/watch?v=Rd7TyU9RdQk"&gt;even&lt;/a&gt;
&lt;a href="http://www.youtube.com/watch?v=o_xr8Htj9GI"&gt;more&lt;/a&gt;
&lt;a href="http://www.youtube.com/watch?v=Xfrzi-yVcsM"&gt;so&lt;/a&gt;
&lt;a href="http://www.youtube.com/watch?v=uvCbc8vFUMo"&gt;with&lt;/a&gt;
&lt;a href="http://www.youtube.com/watch?v=7lBUBBW_sF0"&gt;marble&lt;/a&gt;
&lt;a href="http://www.youtube.com/watch?v=c7npJ3E-ydA"&gt;games&lt;/a&gt;.
One is required for &lt;a href="http://marblesoccer.com"&gt;marblesoccer&lt;/a&gt; but i wasnt
convinced by current 3d physics engines. I explain why at the end.
Fortunatly, &lt;a href="http://twitter.com/#!/pyalot"&gt;@pyalot&lt;/a&gt; from &lt;a href="http://codeflow.org/"&gt;codeflow.org&lt;/a&gt;
has been kind enough to write one taylor-made for us: &lt;strong&gt;microphysics.js&lt;/strong&gt;!!&lt;/p&gt;

&lt;p&gt;It is bite-sized, elegant and efficient.
Less than 500 lines at the moment!!
It is small engouh to be understood, important feature for a tutorial blog.
It is a work in progress tho.
We aren't aware of any bugs.
New features will be added and the API is expected to move.
Currently it implements moving spheres and static boxes (or &lt;a href="http://en.wikipedia.org/wiki/Axis-aligned_bounding_box"&gt;AABB&lt;/a&gt; as we like to say).
This is all we need for &lt;a href="http://marblesoccer.com"&gt;marblesoccer&lt;/a&gt;, the good thing about tailor-made.
&lt;em&gt;We are in business!!!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Below is a screencast of me doing a short introduction of the
&lt;a href="http://jeromeetienne.github.com/microphysics.js/playground/"&gt;playground&lt;/a&gt;.
This just a page for you to experiment with microphysics.js.&lt;/p&gt;

&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/DI5PV2_sLoM" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;


&lt;h2&gt;Let's get started&lt;/h2&gt;

&lt;p&gt;So lets see how to use it.
First step, you download it &lt;a href="https://raw.github.com/jeromeetienne/microphysics.js/master/codeflow/physics.js"&gt;here&lt;/a&gt;.
Then include it in your own code with this line.&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script src="physics.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;!-- more --&gt;


&lt;h2&gt;Let's Create a World&lt;/h2&gt;

&lt;p&gt;&lt;img class='right ' src='http://learningthreejs.com/data/lets-make-a-3d-game-microphysics-js/images/galactus.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;Quite a title hey ?
Dont you feel like &lt;a href="http://en.wikipedia.org/wiki/Galactus"&gt;galactus&lt;/a&gt; when you say it ?
First you instanciate the physics &lt;code&gt;world&lt;/code&gt; like this.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var world = new vphy.World()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Now you start it. Dont forget to give it the date as you see it.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;world.start(Date.now()/1000);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;world&lt;/code&gt; is now fully initialized.
You just have to periodically update it in your game/render loop.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var timeStep    = 1/180;
world.step(timeStep, Date.now()/1000);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;timeStep&lt;/code&gt; parameter is the precision of the physics engine, expressed in seconds.
Quite a subtle tradeoff.
The smaller it is, the more accurate is the physics, but the slower it is to compute.
Up to you to find the balance that fit your needs.&lt;/p&gt;

&lt;h2&gt;Let's Add Bodies&lt;/h2&gt;

&lt;p&gt;&lt;img class='left ' src='http://learningthreejs.com/data/lets-make-a-3d-game-microphysics-js/images/The_shining_heres_johnny.jpg' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;Don't worry, this is not about killing people and dispose of their dead bodies :)
In physics, A &lt;a href="http://en.wikipedia.org/wiki/Rigid_body"&gt;body&lt;/a&gt; is a solid object that you put in your world.
microphysics bodies can be spheres or static boxes.
Lets start right away by creating a sphere.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var sphere  = new vphy.Sphere({
    x : 10,
    y : 10,
    z : 10,
    restitution : 0.6,
    radius : 5,
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;This will position it at &lt;code&gt;(10,10,10)&lt;/code&gt; in the world.
&lt;a href="http://en.wikipedia.org/wiki/Coefficient_of_restitution"&gt;restitution&lt;/a&gt; will determine how
bouncy is this during a collision.
A bouncing ball restitutes a lot.
A falling eggs restitutes less :)
This declaration seems quite verbose at first.
Don't worry those parameters got sensible defaults, no need to specify them all.&lt;/p&gt;

&lt;p&gt;Now lets add it to our world&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;world.add(sphere);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;If you need to remove it, just do &lt;code&gt;world.remove(sphere)&lt;/code&gt;. Not too hard hey ?
Now lets create a static box.
Boxes are called &lt;em&gt;AABB&lt;/em&gt;.
It stands for &lt;a href="http://en.wikipedia.org/wiki/Axis-aligned_bounding_box"&gt;Axis-aligned bounding box&lt;/a&gt;.
It is graphic jarguon for the smallest box containing your object.
&lt;code&gt;vphy.Sphere&lt;/code&gt;and &lt;code&gt;vphy.AABB&lt;/code&gt; both derived from &lt;code&gt;vphy.Body&lt;/code&gt;.
&lt;code&gt;x, y, z, resitution&lt;/code&gt; are &lt;code&gt;vphy.Body&lt;/code&gt; parameters, common to both.
So we wont review them again.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var body = new vphy.AABB({
    width : 1,
    height: 1,
    depth : 1
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;code&gt;width&lt;/code&gt;, &lt;code&gt;height&lt;/code&gt; and &lt;code&gt;depth&lt;/code&gt; gives the dimensions of the box.
After &lt;code&gt;world.step()&lt;/code&gt;, you can read the new position of each body. Quite usefull
to push back the resulting physics in your 3D scene :)&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var pos = body.getPosition();   // x = pos[0], y = pos[1], z = pos[2]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Ok, so we got a &lt;code&gt;world&lt;/code&gt; with solid objects in it, all bound to &lt;a href="http://en.wikipedia.org/wiki/Physical_law"&gt;physical law&lt;/a&gt;.
Now what about moving them ?&lt;/p&gt;

&lt;h2&gt;Let's move our Bodies&lt;/h2&gt;

&lt;p&gt;&lt;img class='right ' src='http://learningthreejs.com/data/lets-make-a-3d-game-microphysics-js/images/aerobic-small.jpg' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;Lets make our sphere moves.
The bodies you added to the world will move according to the &lt;a href="http://en.wikipedia.org/wiki/Force"&gt;forces&lt;/a&gt; applied on them.
All that according to
&lt;a href="http://en.wikipedia.org/wiki/Newton%27s_laws_of_motion"&gt;laws of motion&lt;/a&gt;
from &lt;a href="http://en.wikipedia.org/wiki/Isaac_Newton"&gt;Newton&lt;/a&gt;.
He discovered that by receiving an &lt;a href="http://en.wikipedia.org/wiki/Isaac_Newton#Apple_analogy"&gt;apple on the head&lt;/a&gt;,
creativity can take strange paths sometime :)&lt;/p&gt;

&lt;p&gt;Ok let's add &lt;a href="http://en.wikipedia.org/wiki/Gravity_of_Earth"&gt;gravity&lt;/a&gt;, the force which moved this falling apple.
This force is applied along a given direction to all our objects.
The library already contains an helper just for that. Simply do&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;world.add(new vphy.LinearAccelerator({
    x   :  0, 
    y   : -9.8,
    z   :  0
}));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Quite easy, no? Now lets see a custom accelerator, for example a player moving
according to the keyboard. The player will be a &lt;code&gt;vphy.Sphere&lt;/code&gt; and we will
reuse the &lt;a href="http://learningthreejs.com/data/THREEx/THREEx.KeyboardState.js"&gt;keyboard helper&lt;/a&gt; we
did in this &lt;a href="http://learningthreejs.com/blog/2011/09/12/lets-Make-a-3D-game-keyboard/"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var player  = new vphy.Sphere({ radius : 20 });
world.add({
    type: vphy.types.ACCELERATOR,   // let the lib know it is an accelerator
    perform: function(bodies){      // bodies is the array of all vphy.Body
        if( keyboard.pressed('right') ) player.accelerate(1,0,0);
        if( keyboard.pressed('left') )  player.accelerate(-1,0,0);
        if( keyboard.pressed('up') )    player.accelerate(0,0,1);
        if( keyboard.pressed('down') )  player.accelerate(0,0,-1);
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.perform()&lt;/code&gt; will be called at every world step.
It accesses &lt;code&gt;player&lt;/code&gt; via
&lt;a href="https://developer.mozilla.org/en/JavaScript/Guide/Closures"&gt;closure&lt;/a&gt;
, read current keyboard state and accelerate in the proper direction.&lt;/p&gt;

&lt;h2&gt;Motivation&lt;/h2&gt;

&lt;p&gt;The need for 3D physics is clear for &lt;a href="http://marblesoccer.com"&gt;marblesoccer&lt;/a&gt;.
Marble in physics are fun, generic and instinctive for the player.
Ok so how to get a 3D physics engine ?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Do it yourself ? &lt;/strong&gt;
Well no, it is hard, long and im lazy :)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use an existing one ? &lt;/strong&gt;
i tried some and left unimpressed. All those are new experimental stuff.
Documentation is inexistant.
They are issued from existing libraries in other languages and convert them to js, sometime multiple conversions in a row.
I experienced major bugs when i tried. Were those bugs ? Was it me misusing it ?
Quite possible as the doc is inexistant.
All in all, i didnt feel it would be a reliable dependancy for our game.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Used a 2D one, like Box2D ? &lt;/strong&gt;
Box2D is kind of special.
&lt;a href="http://blog.sethladd.com/"&gt;Seth Ladd&lt;/a&gt; recently did
&lt;a href="http://blog.sethladd.com/2011/09/box2d-collision-damage-for-javascript.html"&gt;a&lt;/a&gt;
&lt;a href="http://blog.sethladd.com/2011/09/box2d-impulse-and-javascript.html"&gt;lot&lt;/a&gt;
&lt;a href="http://blog.sethladd.com/2011/09/box2d-with-complex-and-concave-objects.html"&gt;of&lt;/a&gt;
&lt;a href="http://blog.sethladd.com/2011/09/box2d-and-polygons-for-javascript.html"&gt;good&lt;/a&gt;
&lt;a href="http://blog.sethladd.com/2011/09/box2d-web-workers-better-performance.html"&gt;things&lt;/a&gt;
to explain box2D. Ok, box2D is a converted one but it is of very good quality.
So why not using box2D ?
Well because it is 2D and we do 3D.
Quite an insight, hey :)
It would be such a tough limitation.
This webgl + box2D strategy can produce excelent results tho, like this
&lt;a href="http://game.2x.io/"&gt;game demo&lt;/a&gt; from &lt;a href="http://twitter.com/#!/einaros"&gt;@einaros&lt;/a&gt;.
Take a close look at the physics when object move, it is amazingly
realistic and it is all box2D.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ask somebody else to do it ? &lt;/strong&gt;
We got a &lt;em&gt;winner!&lt;/em&gt; &lt;a href="http://twitter.com/#!/pyalot"&gt;@pyalot&lt;/a&gt; from &lt;a href="http://codeflow.org/"&gt;codeflow.org&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Credits&lt;/h2&gt;

&lt;p&gt;All images are from &lt;a href="http://en.wikipedia.org"&gt;wikipedia&lt;/a&gt;. All hard work is from &lt;a href="http://twitter.com/#!/pyalot"&gt;@pyalot&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This is the first post about physics.
It presented microphysics.js API.
Thus you can start playing with it immediatly.
More posts will come shortly.
At least, one about performance and another one on how to easily bind microphysics to your three.js game.
That's all folks.
Have fun with microphysics.js :)&lt;/p&gt;
</description><enclosure url="http://learningthreejs.com/data/THREEx/THREEx.KeyboardState.js" length="3173" type="application/x-javascript" /><media:content url="http://learningthreejs.com/data/THREEx/THREEx.KeyboardState.js" fileSize="3173" type="application/x-javascript" /><itunes:explicit>no</itunes:explicit><itunes:subtitle> This post is part of the "Lets make a 3D game" series. 3D and physics simulation always go well together even more so with marble games. One is required for marblesoccer but i wasnt convinced by current 3d physics engines. I explain why at the end. Fortu</itunes:subtitle><itunes:summary> This post is part of the "Lets make a 3D game" series. 3D and physics simulation always go well together even more so with marble games. One is required for marblesoccer but i wasnt convinced by current 3d physics engines. I explain why at the end. Fortunatly, @pyalot from codeflow.org has been kind enough to write one taylor-made for us: microphysics.js!! It is bite-sized, elegant and efficient. Less than 500 lines at the moment!! It is small engouh to be understood, important feature for a tutorial blog. It is a work in progress tho. We aren't aware of any bugs. New features will be added and the API is expected to move. Currently it implements moving spheres and static boxes (or AABB as we like to say). This is all we need for marblesoccer, the good thing about tailor-made. We are in business!!! Below is a screencast of me doing a short introduction of the playground. This just a page for you to experiment with microphysics.js. Let's get started So lets see how to use it. First step, you download it here. Then include it in your own code with this line. ```html &amp;lt;script src="physics.js"&amp;gt;&amp;lt;/script&amp;gt; ``` Let's Create a World Quite a title hey ? Dont you feel like galactus when you say it ? First you instanciate the physics world like this. ```javascript var world = new vphy.World() ``` Now you start it. Dont forget to give it the date as you see it. ```javascript world.start(Date.now()/1000); ``` The world is now fully initialized. You just have to periodically update it in your game/render loop. ```javascript var timeStep = 1/180; world.step(timeStep, Date.now()/1000); ``` The timeStep parameter is the precision of the physics engine, expressed in seconds. Quite a subtle tradeoff. The smaller it is, the more accurate is the physics, but the slower it is to compute. Up to you to find the balance that fit your needs. Let's Add Bodies Don't worry, this is not about killing people and dispose of their dead bodies :) In physics, A body is a solid object that you put in your world. microphysics bodies can be spheres or static boxes. Lets start right away by creating a sphere. ```javascript var sphere = new vphy.Sphere({ x : 10, y : 10, z : 10, restitution : 0.6, radius : 5, }); ``` This will position it at (10,10,10) in the world. restitution will determine how bouncy is this during a collision. A bouncing ball restitutes a lot. A falling eggs restitutes less :) This declaration seems quite verbose at first. Don't worry those parameters got sensible defaults, no need to specify them all. Now lets add it to our world ```javascript world.add(sphere); ``` If you need to remove it, just do world.remove(sphere). Not too hard hey ? Now lets create a static box. Boxes are called AABB. It stands for Axis-aligned bounding box. It is graphic jarguon for the smallest box containing your object. vphy.Sphereand vphy.AABB both derived from vphy.Body. x, y, z, resitution are vphy.Body parameters, common to both. So we wont review them again. ```javascript var body = new vphy.AABB({ width : 1, height: 1, depth : 1 }); ``` width, height and depth gives the dimensions of the box. After world.step(), you can read the new position of each body. Quite usefull to push back the resulting physics in your 3D scene :) ```javascript var pos = body.getPosition(); // x = pos[0], y = pos[1], z = pos[2] ``` Ok, so we got a world with solid objects in it, all bound to physical law. Now what about moving them ? Let's move our Bodies Lets make our sphere moves. The bodies you added to the world will move according to the forces applied on them. All that according to laws of motion from Newton. He discovered that by receiving an apple on the head, creativity can take strange paths sometime :) Ok let's add gravity, the force which moved this falling apple. This force is applied along a given direction to all our objects. The library already contains an helper just for that. Simply do ```javascript world.add(new vphy.LinearAccelerator({ x : 0, y : -9.8, z :</itunes:summary></item><item><title>Performance: Merging Geometry</title><link>http://learningthreejs.com/blog/2011/10/05/performance-merging-geometry/</link><pubDate>Wed, 05 Oct 2011 02:29:00 PDT</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/10/05/performance-merging-geometry</guid><description>&lt;p&gt;This article is about merging geometry and how it can improve performance.
It is important for perfomance to reduce the number of &lt;a href="http://www.khronos.org/registry/webgl/specs/latest/"&gt;WebGL&lt;/a&gt; calls as much as possible.
The rules of thumbs is &lt;em&gt;the less data are exchanged between the cpu and the gpu, the better it is for performance&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://learningthreejs.com/data/performance-merging-geometry/"&gt;demo&lt;/a&gt; is rendering many cubes and put them randomly in the space.
&lt;img class='left ' src='http://learningthreejs.com/data/performance-merging-geometry/images/demo-screenshot-2000.png' width='240' height='120' alt='' title=''&gt;
&lt;img class='right ' src='http://learningthreejs.com/data/performance-merging-geometry/images/demo-screenshot-120000.png' width='240' height='120' alt='' title=''&gt;
It is a simplictic way to measure the performance, but it will do for us.
If the geometries arent merged, my computer is able to display &lt;em&gt;2000&lt;/em&gt; cubes at 30fps. See on the left.
But if the geometry are merged, it displays &lt;em&gt;120000&lt;/em&gt; cubes at 30fps. See on the right.
Screenshots make it pretty obvious :) This is &lt;strong&gt;60 times&lt;/strong&gt; more cubes!!!&lt;/p&gt;

&lt;!-- more --&gt;


&lt;h2&gt;So Let's Merge Geometries&lt;/h2&gt;

&lt;p&gt;A geometry is the shape of the 3D object. three.js got already
&lt;a href="https://github.com/mrdoob/three.js/tree/master/src/extras/geometries"&gt;several predefined&lt;/a&gt; for you.
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/PlaneGeometry.js"&gt;plane&lt;/a&gt;,
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/CubeGeometry.js"&gt;cube&lt;/a&gt; or
&lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/SphereGeometry.js"&gt;sphere&lt;/a&gt;
are the common ones.
This post is about merging them, so lets do that.
This line will merge &lt;code&gt;otherGeometry&lt;/code&gt; into &lt;code&gt;geometry&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;THREE.GeometryUtils.merge(geometry, otherGeometry);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;&lt;code&gt;THREE.GeometryUtils.merge()&lt;/code&gt; is in &lt;a href="https://github.com/mrdoob/three.js/blob/master/src/extras/GeometryUtils.js"&gt;THREE.GeometryUtils&lt;/a&gt; with other geometry related functions.
Btw it is more a concatenation than an actual merge them. No duplicate is removed in the process.&lt;/p&gt;

&lt;h2&gt;Combo: merging geometry with mesh&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;THREE.GeometryUtils.merge()&lt;/code&gt; supports to merge geometries with meshes too.
In this case, the function will honor the mesh position/orientation.
As a bonus, if your mesh uses &lt;code&gt;THREE.MeshFaceMaterial&lt;/code&gt;, materials will be copied along.
It is quite convenient when building a large geometry, or when optimising existing scenes with meshes.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var mesh = new THREE.Mesh(new THREE.CubeGeometry(10,10,10), new THREE.MeshNormalMaterial());
mesh.position.x = 30;
mesh.rotation.y = Math.PI/3;
THREE.GeometryUtils.merge(geometry, mesh);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;This will merge a &lt;code&gt;THREE.CubeGeometry&lt;/code&gt; with a translation of &lt;code&gt;30&lt;/code&gt; on &lt;code&gt;x&lt;/code&gt;
and a rotation of &lt;code&gt;Math.PI/3&lt;/code&gt; on &lt;code&gt;y&lt;/code&gt;.
Simple enougth hey ? what about Limitations ?&lt;/p&gt;

&lt;h2&gt;When to use it ?&lt;/h2&gt;

&lt;p&gt;The answer is &lt;em&gt;whenever possible&lt;/em&gt; due to the performance improvement.
The merged geometry will act as a single atomic shape.
So it is perfect with static geometry.
But it may not be suitable in all cases.
For example, it won't be possible to move the merged objects independantly from each other.
Or you can no more remove or add a object without recomputing the whole geometry.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;So merging geometry reduces the amount of webgl calls, so dramatically improve
performance (x60 time in our little demo). It is simple enougth to add to your
demos or games when they have large static geometries. So use it as much as possible.
That's all folks. I hope it has been usefull :)&lt;/p&gt;
</description></item><item><title>Let's Make a 3D Game: Device Orientation</title><link>http://learningthreejs.com/blog/2011/09/20/lets-make-a-3D-game-device-orientation/</link><pubDate>Tue, 20 Sep 2011 01:35:00 PDT</pubDate><guid isPermaLink="false">http://learningthreejs.com/blog/2011/09/20/lets-make-a-3D-game-device-orientation</guid><description>&lt;p&gt;Here is another article of the "Let's Make a 3D Game" &lt;a href="http://learningthreejs.com/blog/categories/tutorial3dgame/"&gt;series&lt;/a&gt;.
It is about &lt;strong&gt;device orientation&lt;/strong&gt;, another input you can use for your games.
We have already seen how to handle the &lt;a href="http://learningthreejs.com/blog/2011/09/12/lets-Make-a-3D-game-keyboard/"&gt;keyboard&lt;/a&gt; input.
Device orientation input is more instinctive tho. The user doesnt need to learn controls.
It is already enabled in our game &lt;a href="http://marblesoccer.com"&gt;marblesoccer&lt;/a&gt;.
Here is a &lt;a href="http://www.youtube.com/watch?v=kW4oHaHCilo"&gt;video&lt;/a&gt; of me playing with it.&lt;/p&gt;

&lt;center&gt;
    &lt;iframe width="425" height="349" src="http://www.youtube.com/embed/kW4oHaHCilo" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/center&gt;




&lt;!-- more --&gt;


&lt;p&gt;You need a computer which support WebGL &lt;em&gt;and&lt;/em&gt; device orientation to actually play
with it. Unfortunatly, macbook is the only computer i know able to do that.
On one hand, most smartphones already support &lt;em&gt;device orientation API&lt;/em&gt;.
On the other hand, smartphones support for WebGL is sparse at best.
So this input will become very usefull if we start supporting
&lt;a href="https://github.com/mrdoob/three.js/tree/master/src/renderers"&gt;three.js renderers&lt;/a&gt;
available on phones, such as canvas or DOM.&lt;/p&gt;

&lt;h2&gt;Let's get started&lt;/h2&gt;

&lt;p&gt;As usual, i did a little helper to make it easier for you to include it in
your games. It is called &lt;a href="http://learningthreejs.com/data/THREEx/THREEx.DeviceOrientationState.js"&gt;THREEx.DeviceOrientationState&lt;/a&gt;.
You download this API from &lt;a href="http://learningthreejs.com/data/THREEx/THREEx.DeviceOrientationState.js"&gt;here&lt;/a&gt; and include
it in your page like this&lt;/p&gt;

&lt;p&gt;```html&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script src='THREEx.DeviceOrientationState.js'&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;How to use it ?&lt;/h2&gt;

&lt;p&gt;Now that the script is included, create a &lt;code&gt;device&lt;/code&gt; variable like this.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var device = new THREEx.DeviceOrientationState();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;After that, if you want the angle the device with the x axis, just use
&lt;code&gt;.angleX()&lt;/code&gt; function. You can use &lt;code&gt;.angleY()&lt;/code&gt; and &lt;code&gt;.angleZ()&lt;/code&gt;, you
guessed which angle they provide.
Suppose you want a &lt;code&gt;THREE.Mesh&lt;/code&gt; to stay still on y axis while you are moving the device,
just copy this line&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mesh.rotation.y = device.angleY();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;If you ever want to stop listening to the device orientation, just use this line&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;device.destroy()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;h2&gt;What about the standard ?&lt;/h2&gt;

&lt;p&gt;Sometime it may be interesting to understand the root of things.
Let's do a rapid introduction of the device orientation API itself.
For authoritative details, the &lt;a href="http://www.w3.org/TR/orientation-event/"&gt;spec&lt;/a&gt;
is always a nice place.
If you want more info on it, &lt;a href="http://www.html5rocks.com/en/tutorials/device/orientation/"&gt;html5rocks&lt;/a&gt;
or &lt;a href="https://developer.mozilla.org/en/detecting_device_orientation"&gt;mdn&lt;/a&gt;
articles are a good start.
First let's bind the event.&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;window.addEventListener('deviceorientation',  callback, false);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Here is an example of callback&lt;/p&gt;

&lt;p&gt;```javascript&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function callback(event){
    console.log("orientation gamma:", event.gamma, "beta", event.beta, "alpha", event.alpha);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;Be carefull tho, gamma, beta, alpha may not be set depending on the device your game is
running on. For example on macbook, &lt;code&gt;alpha&lt;/code&gt; is always &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;For more details on &lt;a href="http://learningthreejs.com/data/THREEx/THREEx.DeviceOrientationState.js"&gt;THREEx.DeviceOrientationState&lt;/a&gt;,
see its &lt;a href="http://learningthreejs.com/data/THREEx/docs/THREEx.DeviceOrientationState.html"&gt;annoted source&lt;/a&gt;.
It is a simple and instinctive way to control the player.
It is closer to real-life and thus provides a more immersive experience.
In the future we will likely do be
a &lt;a href="http://www.youtube.com/watch?v=-sEJ4Lo0cm8"&gt;virtual joystick&lt;/a&gt; for touch enabled device.&lt;/p&gt;
</description><enclosure url="http://learningthreejs.com/data/THREEx/THREEx.DeviceOrientationState.js" length="1193" type="application/x-javascript" /><media:content url="http://learningthreejs.com/data/THREEx/THREEx.DeviceOrientationState.js" fileSize="1193" type="application/x-javascript" /><itunes:explicit>no</itunes:explicit><itunes:subtitle> Here is another article of the "Let's Make a 3D Game" series. It is about device orientation, another input you can use for your games. We have already seen how to handle the keyboard input. Device orientation input is more instinctive tho. The user does</itunes:subtitle><itunes:summary> Here is another article of the "Let's Make a 3D Game" series. It is about device orientation, another input you can use for your games. We have already seen how to handle the keyboard input. Device orientation input is more instinctive tho. The user doesnt need to learn controls. It is already enabled in our game marblesoccer. Here is a video of me playing with it. You need a computer which support WebGL and device orientation to actually play with it. Unfortunatly, macbook is the only computer i know able to do that. On one hand, most smartphones already support device orientation API. On the other hand, smartphones support for WebGL is sparse at best. So this input will become very usefull if we start supporting three.js renderers available on phones, such as canvas or DOM. Let's get started As usual, i did a little helper to make it easier for you to include it in your games. It is called THREEx.DeviceOrientationState. You download this API from here and include it in your page like this ```html &amp;lt;script src='THREEx.DeviceOrientationState.js'&amp;gt;&amp;lt;/script&amp;gt; ``` How to use it ? Now that the script is included, create a device variable like this. ```javascript var device = new THREEx.DeviceOrientationState(); ``` After that, if you want the angle the device with the x axis, just use .angleX() function. You can use .angleY() and .angleZ(), you guessed which angle they provide. Suppose you want a THREE.Mesh to stay still on y axis while you are moving the device, just copy this line ```javascript mesh.rotation.y = device.angleY(); ``` If you ever want to stop listening to the device orientation, just use this line ```javascript device.destroy() ``` What about the standard ? Sometime it may be interesting to understand the root of things. Let's do a rapid introduction of the device orientation API itself. For authoritative details, the spec is always a nice place. If you want more info on it, html5rocks or mdn articles are a good start. First let's bind the event. ```javascript window.addEventListener('deviceorientation', callback, false); ``` Here is an example of callback ```javascript function callback(event){ console.log("orientation gamma:", event.gamma, "beta", event.beta, "alpha", event.alpha); } ``` Be carefull tho, gamma, beta, alpha may not be set depending on the device your game is running on. For example on macbook, alpha is always null. Conclusion For more details on THREEx.DeviceOrientationState, see its annoted source. It is a simple and instinctive way to control the player. It is closer to real-life and thus provides a more immersive experience. In the future we will likely do be a virtual joystick for touch enabled device. </itunes:summary></item><media:rating>nonadult</media:rating></channel></rss>

