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 1123137 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: 1123137
  • Created:
  • Creator: sasha240100
  • Is current revision? No
  • Comment

Revision Content

Whitestorm.js is a framework built on the top of Three.js technology. It's main distinction is existance of built-in Physics engine. Basically, it is a modified Physi.js, API wrapper around Ammo.js (Bullet Physics ported to JavaScript) with usage of web-workers technology. Next main addition is a plugin system based Whitestorm.js modularity. Developers can share their plugins & components and include them in really big projects.

Environment setup

Node

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

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

// ...

Bower

  1. Run bower install whitestormjs
  2. Include whitetormjs to your browser:
<script src="bower_components/whitestormjs/build/whitestorm.js"></script>

Browser

Include a script tag linking the WhitestormJS library in your head or after your body:

<script src="{path_to_lib}/whitestorm.js"></script>

Development

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).

{{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.

Links

Tutorials

Revision Source

<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. It's main distinction is existance of built-in Physics engine. Basically, it is a modified&nbsp;<a href="https://chandlerprall.github.io/Physijs/">Physi.js</a>, API 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. Next main addition is a&nbsp;<a href="https://github.com/WhitestormJS/generator-whs-plugin">plugin system</a>&nbsp;based Whitestorm.js modularity. Developers can share their plugins &amp; components and include them in really big projects.</p>

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

<h2>Environment setup</h2>

<h3>Node</h3>

<ol>
 <li>Run&nbsp;<code>npm install whitestormjs</code></li>
 <li>Import&nbsp;<strong>whitetormjs</strong>&nbsp;module to your app and start using:</li>
</ol>

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

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

// ...</pre>

<h3>Bower</h3>

<ol>
 <li>Run&nbsp;<code>bower install whitestormjs</code></li>
 <li>Include&nbsp;<strong>whitetormjs</strong>&nbsp;to your browser:</li>
</ol>

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

<h3>Browser</h3>

<p>Include a script tag linking the&nbsp;<a href="https://cdn.jsdelivr.net/whitestormjs/latest/whitestorm.min.js">WhitestormJS</a>&nbsp;library in your&nbsp;<code>head</code>&nbsp;or after your&nbsp;<code>body</code>:</p>

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

<h2>Development</h2>

<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>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>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>{{JSFiddleEmbed("https://jsfiddle.net/4phf4oty/1/","","350")}}</p>

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

<h2>&nbsp;</h2>

<h2>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>Playground</h2>

<p><a href="https://camo.githubusercontent.com/9f4ad1d95ab33555db869e085a653761c291c8b3/68747470733a2f2f63646e2e706272642e636f2f696d616765732f356e6f57364d7a45532e706e67" target="_blank"><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>

<h3>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></li>
 <li><a href="https://whitestormjs.xyz/playground/">Playground</a></li>
</ul>

<h3>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>
Revert to this revision