Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1125223 of Building up a basic demo with Whitestorm.js

  • Revision slug: Games/Techniques/3D_on_the_web/Building_up_a_basic_demo_with_Whitestorm.js
  • Revision title: Building up a basic demo with Whitestorm.js
  • Revision id: 1125223
  • Created:
  • Creator: chrisdavidmills
  • Is current revision? No
  • Comment

Revision Content

{{draft}}

Whitestorm.js is a framework built on the top of Three.js technology, enhanced by features such as a built-in Physics engine — a modified Physi.js that acts as a wrapper around Ammo.js (Bullet Physics ported to JavaScript) with usage of web-workers technology — and a  plugin system providing useful modularity. In this article we show how you can get started with Whitestorm.js.

Environment setup

Whitestorm.js can be used in Node applications, and included in client-side projects either manually or as a bower component.  Let's look at how we can do this.

Node

  1. Run npm install whitestormjs.
  2. Import the whitetormjs module to your app and start using it, as demonstrated below:
import * as WHS from 'whitestormjs';

const world = new WHS.World({
  background: {
    color: 0xDDDDDD
  }
});

// ...

Bower

  1. Run bower install whitestormjs
  2. Include whitetormjs in your app as follows:
<script src="bower_components/whitestormjs/build/whitestorm.js"></script>

Manual

  1. Make a copy of the WhitestormJS library file in your app directory.
  2. Include a {{htmlelement("script")}} element in your HTML {{htmlelement("head")}} (or after your {{htmlelement("body")}}) to apply the Whitestorm JavaScript to your HTML:
<script src="{path_to_lib}/whitestorm.js"></script>

Developing a Whitestorm app

Let's look at how we can create a quick sample Whitestorm app.

Getting started

Create a new index.html file in a new directory. Use our simple template if you don't already have one handy.

Remove any content that you might already have inside your document body (like the {{htmlelement("p")}} element in our teplate above.)

Follow the {{anch("Manual")}} insrtructions above to apply the Whitestorm library to your HTML.

Create a new {{htmlelement("script")}} element just below the existing one, in which to write your app code.

Creating a world

Whitestorm has a global WHS object that stores all of its functionality. To start off with making a whitestorm app you need to create a WHS.World object, which represents the app's 3D scene. This allows you to set parameters of the scene like camera position and gravity, and it also processes any other objects added to the world (like spheres, for example — objects inside the scene can be considered children of the WHS.World object).

Add the following code inside your second <script> element:

const world = new WHS.World({
  stats: "fps",
  autoresize: "window", // Call autoresize when app window is resized.

  gravity: {
    x: 0,
    y: -5,
    z: 0
  },

  camera: {
    z: 50
  },

  container: document.body
});

The members set inside this object are as follows:

  1. stats: Declares whether you want to display stats while your app is running. Options are "fps" (frames per second), "ms" (milliseconds elapsed), "mb" (megabytes downloaded), or boolean false if not required (which is also the default).
  2. autoresize: Declares whether you want the Whitestorm app {{htmlelement("canvas")}} to resize when the browser window is resized ("window"), when its container is resized ("container"), or not at all (boolean false, which is the default).
  3. gravity: Sets what gravity you want to act on objects in the scene, in the x, y, and z directions. The default is 0 in all directions.
  4. camera: Sets the position and other properties of the camera that will be viewing the scene.
  5. container: Sets the HTML DOM object that the Whitestorm app's <canvas> will be appended inside on the page. The default is document.body.

Note: See the Whitestorm reference for more information on WHS.World features.

Creating a sphere

Whitestorm has available a number of primitives available for creating common shapes in the world, which mostly act as wrappers around Three.js primitives. WHS.Sphere for example is analogous to Three's THREE.SphereGeometry, and is based on WHS.Shape — a class that wraps the Three API for working with meshes.

Let's create one now. Add the following below your previous code addition:

const sphere = new WHS.Sphere({ // Create sphere object.
  geometry: { // Create a THREE.SphereGeometry(3)
    radius: 3
  },

  mass: 10, // Mass of physical object.

  material: { // Material
    color: 0xffffff,
    kind: 'basic' // THREE.MeshBasicMaterial
  },

  pos: { // Position vector.
    x: 0,
    y: 50,
    z: 0
  }
});

Here we are specifying:

  1. geometry: Sets geometry information about the object; in this case we only need to specify a radius for the sphere.
  2. mass: Sets the relative mass of the object, which will for example have an effect on gravity's pull on the object.
  3. material: Sets the type of material we want to cover the surface of the object with.
  4. pos: Sets the initial position of the object on the scene, as an x, y, z position vector.

Note: See the Whitestorm sphere documentation for more information.

Making things happen

Nothing will happen yet; you still need to add your sphere to the world, and start the app rendering. The following two lines will do this — add them to the scene now:

sphere.addTo(world); // Add Sphere to world.
world.start(); // Start animations and physics simulation.

And that should be it — try saving and refeshing your code now, and you should see something like the following (with the ball moving down the screne due to the graivty in the y direction):

[INSERT SCREENSHOT]

There is also explanation for usage with webpack

Creating a world

When you create a world in Whitestorm.js it automatically creates THREE.Scene, WebGL renderer and applies camera. But don't worry, all of them you can access simply as: world.sceneworld.rendererworld.camera. WHS.World is highly editable class. All parameters are non-required and have their own default values. Check or change them you can in WHS.World.defaultsSee all of them

const world = new WHS.World({
  background: {
    color: 0xffffff
  },

  camera: {
    z: 50
  },

  gravity: {
    y: -100
  }
});

Using physics in Whitestorm.js is not required. You may rather use whitestorm.light.js build that is without included Ammo.js and physics part.

Rendering a box

Same rule with defaults is also related to other classes. Each mesh consist of geometry & material. As we make a WHS.Box component - our geometry is THREE.BoxGeometry. If you know Three.js - you would probably ask: What if i want to use THREE.BoxBufferGeometry? - The answer is a buffer property.

const cube = new WHS.Box({
  geometry: {
    width: 10,
    height: 10,
    depth: 10
  },

  material: {
    kind: 'basic',
    color: 0x0095DD
  },

  rot: {
    x: 0.4,
    y: 0.2
  }
});

cube.addTo(world);

cube.addTo() method is used to apply this box to a specific world object that will render it and process collisions, "z-index" (Calculated by camera direction and other objects positions), it's color on rendered image (can depend on lights & shadows of other objects).

That's how combination of world & cube code looks in your browser:

You can try it right now:

{{JSFiddleEmbed("https://jsfiddle.net/4phf4oty/1/","","350")}}

This JSFiddle uses whitestorm.light.js (A version without built-in physics).
You may also check the one with physics: https://jsfiddle.net/g2s5bgrL/

Components & Plugins

Almost each whitestorm.js class is a component. Box, Sphere, Tetrahedron - all these are components, but basic (simple Three.js geometries based on WHS.Shape).

Adding custom components is also welcome. To develop your own plugin/component you have to install generator-whs-plugin and follow it's instruction:

  1. Make a new repository.
  2. Install Yeomannpm install -g yo
  3. Install Whitestorm.js plugin generatornpm install -g generator-whs-plugin
  4. Run yo whs-plugin.
  5. Edit files in src/ for your needs. You will see already defined plugin source there, just change it as you want.

Playground

Playground is where you can create a basic whitestormjs app without downloading a WhitestormJS or setting up a project. You can also share your app with others by clicking “share” button and then you can share it in twitter, facebook or just copy to clipboard.

Conclusion

Whitestorm.js framework wil be best suitable for complex 3D browser games that rely on calculating objects transforms by collisions between them and other physics features. But flexibility of Whitestorm.js allows you to use it's API without parts you don't need, so usage in games that don't rely on physics on even basic apps or just beatiful websites is also welcome cause you can use whitestorm.light.js that is more lightweight than whitestorm.js.

Tutorials

 

Revision Source

<p>{{draft}}</p>

<p class="summary"><a href="https://whitestormjs.xyz/">Whitestorm.js</a> is a framework built on the top of&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Games/Techniques/3D_on_the_web/Building_up_a_basic_demo_with_Three.js">Three.js</a>&nbsp;technology, enhanced by features such as a built-in Physics engine — a modified&nbsp;<a href="https://chandlerprall.github.io/Physijs/">Physi.js</a> that acts as a wrapper around&nbsp;<a href="https://github.com/kripken/ammo.js/">Ammo.js</a>&nbsp;(Bullet Physics ported to JavaScript) with usage of web-workers technology — and a&nbsp; <a href="https://github.com/WhitestormJS/generator-whs-plugin">plugin system</a> providing useful modularity. In this article we show how you can get started with Whitestorm.js.</p>

<p><a href="https://whitestormjs.xyz/"><img alt="" src="https://camo.githubusercontent.com/5e38fd133cffcd074aef767d3ea2ad2ab54eea7e/68747470733a2f2f7062732e7477696d672e636f6d2f6d656469612f4372696245412d58674141753777772e6a70673a6c61726765" style="height:931px; width:1891px" /></a></p>

<h2 id="Environment_setup">Environment setup</h2>

<p>Whitestorm.js can be used in Node applications, and included in client-side projects either manually or as a bower component.&nbsp; Let's look at how we can do this.</p>

<h3 id="Node">Node</h3>

<ol>
 <li>Run&nbsp;<code>npm install whitestormjs</code>.</li>
 <li>Import the <strong>whitetormjs</strong>&nbsp;module to your app and start using it, as demonstrated below:</li>
</ol>

<pre class="brush: js">
import * as WHS from 'whitestormjs';

const world = new WHS.World({
  background: {
    color: 0xDDDDDD
  }
});

// ...</pre>

<h3 id="Bower">Bower</h3>

<ol>
 <li>Run&nbsp;<code>bower install whitestormjs</code></li>
 <li>Include&nbsp;<strong>whitetormjs</strong> in your app as follows:</li>
</ol>

<pre class="brush: html">
&lt;script src="bower_components/whitestormjs/build/whitestorm.js"&gt;&lt;/script&gt;</pre>

<h3 id="Manual">Manual</h3>

<ol>
 <li>Make a copy of the <a href="https://cdn.rawgit.com/WhitestormJS/whitestorm.js/9ae51f4b18a80e4dc31a1f9a2e0f6f742bdf7d95/build/whitestorm.js">WhitestormJS</a>&nbsp;library file in your app directory.</li>
 <li>Include a {{htmlelement("script")}} element in your HTML {{htmlelement("head")}} (or after your {{htmlelement("body")}}) to apply the Whitestorm JavaScript to your HTML:</li>
</ol>

<pre class="brush: html">
&lt;script src="{path_to_lib}/whitestorm.js"&gt;&lt;/script&gt;</pre>

<h2 id="Developing_a_Whitestorm_app">Developing a Whitestorm app</h2>

<p>Let's look at how we can create a quick sample Whitestorm app.</p>

<h3 id="Getting_started">Getting started</h3>

<p>Create a new <code>index.html</code> file in a new directory. Use our <a href="https://github.com/mdn/learning-area/blob/master/html/introduction-to-html/getting-started/index.html">simple template</a> if you don't already have one handy.</p>

<p>Remove any content that you might already have inside your document body (like the {{htmlelement("p")}} element in our teplate above.)</p>

<p>Follow the {{anch("Manual")}} insrtructions above to apply the Whitestorm library to your HTML.</p>

<p>Create a new {{htmlelement("script")}} element just below the existing one, in which to write your app code.</p>

<h3>Creating a world</h3>

<p>Whitestorm has a global <code>WHS</code> object that stores all of its functionality. To start off with making a whitestorm app you need to create a <code>WHS.World</code> object, which represents the app's 3D scene. This allows you to set parameters of the scene like camera position and gravity, and it also processes any other objects added to the world (like spheres, for example — objects inside the scene can be considered children of the <code>WHS.World</code> object).</p>

<p>Add the following code inside your second <code>&lt;script&gt;</code> element:</p>

<pre class="brush: js">
const world = new WHS.World({
  stats: "fps",
  autoresize: "window", // Call autoresize when app window is resized.

  gravity: {
    x: 0,
    y: -5,
    z: 0
  },

  camera: {
    z: 50
  },

  container: document.body
});</pre>

<p>The members set inside this object are as follows:</p>

<ol>
 <li><code>stats</code>: Declares whether you want to display stats while your app is running. Options are <code>"fps"</code> (frames per second), <code>"ms"</code> (milliseconds elapsed), <code>"mb"</code> (megabytes downloaded), or boolean <code>false</code> if not required (which is also the default).</li>
 <li><code>autoresize</code>: Declares whether you want the Whitestorm app {{htmlelement("canvas")}} to resize when the browser window is resized (<code>"window"</code>), when its container is resized (<code>"container"</code>), or not at all (boolean <code>false</code>, which is the default).</li>
 <li><code>gravity</code>: Sets what gravity you want to act on objects in the scene, in the <code>x</code>, <code>y</code>, and <code>z</code> directions. The default is 0 in all directions.</li>
 <li><code>camera</code>: Sets the position and other properties of the camera that will be viewing the scene.</li>
 <li><code>container</code>: Sets the HTML DOM object that the Whitestorm app's <code>&lt;canvas&gt;</code> will be appended inside on the page. The default is <code>document.body</code>.</li>
</ol>

<div class="note">
<p><strong>Note</strong>: See the Whitestorm reference for <a href="https://whitestormjs.xyz/?javascript#world">more information on WHS.World</a> features.</p>
</div>

<h3>Creating a sphere</h3>

<p>Whitestorm has available a number of primitives available for creating common shapes in the world, which mostly act as wrappers around Three.js primitives. <code>WHS.Sphere</code> for example is analogous to Three's&nbsp;<code>THREE.SphereGeometry</code>, and is based on&nbsp;<code>WHS.Shape</code> — a class that wraps the Three API for working with meshes.</p>

<p>Let's create one now. Add the following below your previous code addition:</p>

<pre class="brush: js">
const sphere = new WHS.Sphere({ // Create sphere object.
  geometry: { // Create a THREE.SphereGeometry(3)
    radius: 3
  },

  mass: 10, // Mass of physical object.

  material: { // Material
    color: 0xffffff,
    kind: 'basic' // THREE.MeshBasicMaterial
  },

  pos: { // Position vector.
    x: 0,
    y: 50,
    z: 0
  }
});</pre>

<p>Here we are specifying:</p>

<ol>
 <li><code>geometry</code>: Sets geometry information about the object; in this case we only need to specify a radius for the sphere.</li>
 <li><code>mass</code>: Sets the relative mass of the object, which will for example have an effect on gravity's pull on the object.</li>
 <li><code>material</code>: Sets the type of material we want to cover the surface of the object with.</li>
 <li><code>pos</code>: Sets the initial position of the object on the scene, as an <code>x</code>, <code>y</code>, <code>z</code> position vector.</li>
</ol>

<div class="note">
<p><strong>Note</strong>: See the <a href="https://whitestormjs.xyz/?javascript#sphere">Whitestorm sphere documentation</a> for more information.</p>
</div>

<h3>Making things happen</h3>

<p>Nothing will happen yet; you still need to add your sphere to the world, and start the app rendering. The following two lines will do this — add them to the scene now:</p>

<pre class="brush: js">
sphere.addTo(world); // Add Sphere to world.
world.start(); // Start animations and physics simulation.
</pre>

<p>And that should be it — try saving and refeshing your code now, and you should see something like the following (with the ball moving down the screne due to the graivty in the <code>y</code> direction):</p>

<p>[INSERT SCREENSHOT]</p>

<div class="note">
<p>There is also explanation for&nbsp;<a href="https://github.com/WhitestormJS/test-whitestorm-webpack">usage with webpack</a></p>
</div>

<h2 id="Creating_a_world">Creating a world</h2>

<p>When you create a world in Whitestorm.js it automatically creates&nbsp;<code>THREE.Scene</code>, WebGL renderer and applies camera. But don't worry, all of them you can access simply as:&nbsp;<code>world.scene</code>,&nbsp;<code>world.renderer</code>,&nbsp;<code>world.camera</code>. WHS.World is highly editable class. All parameters are non-required and have their own default values. Check or change them you can in&nbsp;<code>WHS.World.defaults</code>.&nbsp;<a href="https://github.com/WhitestormJS/whitestorm.js/blob/dev/src/framework/core/World.js#L13">See all of them</a></p>

<pre class="brush: js">


const world = new WHS.World({
  background: {
    color: 0xffffff
  },

  camera: {
    z: 50
  },

  gravity: {
    y: -100
  }
});</pre>

<div class="note">
<p><strong>Using physics in Whitestorm.js is not required</strong>. You may rather use&nbsp;<code>whitestorm.light.js</code>&nbsp;build that is without included <a href="https://github.com/kripken/ammo.js/">Ammo.js</a> and physics part.</p>
</div>

<h2 id="Rendering_a_box">Rendering a box</h2>

<p>Same rule with defaults is also related to other classes. Each mesh consist of geometry &amp; material. As we make a WHS.Box component - our geometry is&nbsp;<code>THREE.BoxGeometry</code>. If you know Three.js - you would probably ask: What if i want to use&nbsp;<code>THREE.BoxBufferGeometry</code>? - The answer is a&nbsp;<a href="https://github.com/WhitestormJS/whitestorm.js/blob/dev/src/framework/meshes/Box.js#L44"><code>buffer</code></a>&nbsp;property.</p>

<pre class="brush: js">


const cube = new WHS.Box({
  geometry: {
    width: 10,
    height: 10,
    depth: 10
  },

  material: {
    kind: 'basic',
    color: 0x0095DD
  },

  rot: {
    x: 0.4,
    y: 0.2
  }
});

cube.addTo(world);</pre>

<p><code>cube.addTo()</code>&nbsp;method is used to apply this box to a specific world object that will render it and process collisions, "z-index" (Calculated by camera direction and other objects positions), it's color on rendered image (can depend on lights &amp; shadows of other objects).</p>

<p>That's how combination of world &amp; cube code looks in your browser:</p>

<p><img alt="" src="https://mdn.mozillademos.org/files/11849/cube.png" style="height:400px; width:600px" /></p>

<p>You can try it right now:</p>

<p>{{JSFiddleEmbed("https://jsfiddle.net/4phf4oty/1/","","350")}}</p>

<div class="note">
<dl>
 <dt><strong>This JSFiddle uses <code>whitestorm.light.js</code></strong> <strong>(A version without built-in physics).</strong></dt>
 <dt>You may also check the one with physics:&nbsp;<a href="https://jsfiddle.net/g2s5bgrL/">https://jsfiddle.net/g2s5bgrL/</a></dt>
</dl>
</div>

<h2 id="Components_Plugins">Components &amp; Plugins</h2>

<p>Almost each whitestorm.js class is a component. Box, Sphere, Tetrahedron - all these are components, but basic (simple Three.js geometries based on&nbsp;<a href="https://whitestormjs.xyz/#shape">WHS.Shape</a>).</p>

<p>Adding custom components is also welcome. To develop your own plugin/component you have to install&nbsp;<a href="https://github.com/WhitestormJS/generator-whs-plugin">generator-whs-plugin</a>&nbsp;and follow it's instruction:</p>

<ol>
 <li>Make a new repository.</li>
 <li>Install&nbsp;<strong>Yeoman</strong>:&nbsp;<code>npm install -g yo</code></li>
 <li>Install&nbsp;<strong>Whitestorm.js plugin generator</strong>:&nbsp;<code>npm install -g generator-whs-plugin</code></li>
 <li>Run&nbsp;<code>yo whs-plugin</code>.</li>
 <li>Edit files in&nbsp;<strong>src/</strong>&nbsp;for your needs. You will see already defined plugin source there, just change it as you want.</li>
</ol>

<h2 id="Playground">Playground</h2>

<p><a href="https://whitestormjs.xyz/playground/?example=saturn&amp;dir=demo"><img alt="" data-canonical-src="https://cdn.pbrd.co/images/5noW6MzES.png" src="https://camo.githubusercontent.com/9f4ad1d95ab33555db869e085a653761c291c8b3/68747470733a2f2f63646e2e706272642e636f2f696d616765732f356e6f57364d7a45532e706e67" /></a></p>

<p>Playground is where you can create a basic whitestormjs app without downloading a WhitestormJS or setting up a project. You can also share your app with others by clicking “share” button and then you can share it in twitter, facebook or just copy to clipboard.</p>

<h2 id="Conclusion">Conclusion</h2>

<p>Whitestorm.js framework wil be best suitable for complex 3D browser games that rely on calculating objects transforms by collisions between them and other physics features. But flexibility of Whitestorm.js allows you to use it's API without parts you don't need, so usage in games that don't rely on physics on even basic apps or just beatiful websites is also welcome cause you can use <code>whitestorm.light.js</code> that is more lightweight than <code>whitestorm.js.</code></p>

<h3 id="Links">Links</h3>

<ul>
 <li><a href="https://whitestormjs.xyz/">WhitestormJS website / API</a></li>
 <li><a href="https://github.com/WhitestormJS/whitestorm.js">Github</a>&nbsp;(Feel free to contribute)</li>
 <li><a href="https://whitestormjs.xyz/playground/">Playground</a></li>
</ul>

<h3 id="Tutorials">Tutorials</h3>

<ul>
 <li><a href="https://medium.com/whitestormjs-framework/developing-a-street-basketball-game-part-i-getting-workflow-ready-f4f6968e4d10#.iw8ionj09">Developing a street basketball game. Part I: Getting workflow ready</a></li>
 <li><a href="https://medium.com/whitestormjs-framework/developing-a-street-basketball-game-part-ll-throw-a-ball-into-a-basket-261f123d6e9c#.5266ogrvj">Developing a street basketball game. Part ll: Throw a ball into a basket.</a></li>
 <li><a href="https://medium.com/whitestormjs-framework/developing-a-street-basketball-game-part-lll-level-selecting-and-stats-b076747b13b3#.pincbpdwo">Developing a street basketball game. Part lll: Level selecting and stats</a></li>
</ul>

<p>&nbsp;</p>
Revert to this revision