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 1123849 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: 1123849
  • 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.

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

Fast explanation:

  1. Make a javascript file or use <script> tag to develop main app.
  2. Now you can work with WhitestormJS framework and make a 3D scene.
  3. Make a WHS.World object. It will process all other objects added to world (it's children).
  4. World renders in a <canvas> tag and we need add it to DOM. To do this - simply specify container property in WHS.World. It's default: document.body.
  5. Another object is WHS.Sphere. It is a basic component for THREE's THREE.SphereGeometry. It is based on WHS.Shape class that wraps API for working with meshes. You can specify position (pos), rotation (rot), scale, material and all mesh-related stuff right in this class.


const world = new WHS.World({
    stats: "fps", // fps, ms, mb or false if not need.
    autoresize: "window", // Call autoresize when app window is resized.

    gravity: { // Physic gravity.
        x: 0,
        y: -100,
        z: 0
    },

    camera: {
      z: 50 // Move camera.
    },

    container: document.body
});

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

  mass: 10, // Mass of physics object.

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

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

sphere.addTo(world); // Add Sphere to world.

world.start(); // Start animations and physics simulation.

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="Browser">Manual</h3>

<ol>
 <li>Make a copy of the <a href="https://cdn.jsdelivr.net/whitestormjs/latest/whitestorm.min.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="Development">Developing a Whitestorm app</h2>

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

<h3>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>Follow the {{anch("Manual")}} insrtructions above to apply the Whitestorm library to your HTML.</p>

<p><strong>Fast explanation:</strong></p>

<ol>
 <li>Make a javascript file or use&nbsp;<code>&lt;script&gt;</code>&nbsp;tag to develop main app.</li>
 <li>Now you can work with WhitestormJS framework and make a 3D scene.</li>
 <li>Make a&nbsp;<code>WHS.World</code>&nbsp;object. It will process all other objects added to world (it's children).</li>
 <li>World renders in a&nbsp;<code>&lt;canvas&gt;</code>&nbsp;tag and we need add it to DOM. To do this - simply specify&nbsp;<code>container</code>&nbsp;property in WHS.World. It's default:&nbsp;<code>document.body</code>.</li>
 <li>Another object is&nbsp;<code>WHS.Sphere</code>. It is a basic component for THREE's&nbsp;<code>THREE.SphereGeometry</code>. It is based on&nbsp;<code>WHS.Shape</code>&nbsp;class that wraps API for working with meshes. You can specify position (pos), rotation (rot), scale, material and all mesh-related stuff right in this class.</li>
</ol>

<pre class="brush: js">




const world = new WHS.World({
    stats: "fps", // fps, ms, mb or false if not need.
    autoresize: "window", // Call autoresize when app window is resized.

    gravity: { // Physic gravity.
        x: 0,
        y: -100,
        z: 0
    },

    camera: {
      z: 50 // Move camera.
    },

    container: document.body
});

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

  mass: 10, // Mass of physics object.

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

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

sphere.addTo(world); // Add Sphere to world.

world.start(); // Start animations and physics simulation.
</pre>

<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