Animations makes a scene more interactive and also makes it impressive giving realistic look to BabylonJS. Let us now understand animations of BabylonJS in detail. We will apply animation on shapes to move it from one position to another. To use animations, you need to create an object on animation with the required parameters of BabylonJS.
Let us now see the syntax for the same −
var animationBox = new BABYLON.Animation( "myAnimation", "scaling.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE );
Parameters
Consider the following parameters related to Animations with BabylonJS −
- Name of the animation.
- Property of the shape – for example, scaling, changing position, etc. Scaling is what is shown in the syntax; here, it will scale the box along the x-axis.
- Frames per second requested: highest FPS possible in this animation.
- Here you decide and enter what kind of value will be modified: is it a float (e.g. a translation), a vector (e.g. a direction), or a quaternion.
- Exact values are −
- BABYLON.Animation.ANIMATIONTYPE_FLOAT
- BABYLON.Animation.ANIMATIONTYPE_VECTOR2
- BABYLON.Animation.ANIMATIONTYPE_VECTOR3
- BABYLON.Animation.ANIMATIONTYPE_QUATERNION
- BABYLON.Animation.ANIMATIONTYPE_COLOR3
- Behaviour for animation – to stop or to start the animation again.
- Use previous values and increment it −
- BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE
- Restart from initial value −
- BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE
- Keep their final value
- BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT
Let us now create the animation object −
var animationBox = new BABYLON.Animation( "myAnimation", "scaling.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE );
Demo for Animation
<!doctype html> <html> <head> <meta charset = "utf-8"> <title>BabylonJs - Basic Element-Creating Scene</title> <script src = "babylon.js"></script> <style> canvas {width: 100%; height: 100%;} </style> </head> <body> <canvas id = "renderCanvas"></canvas> <script type = "text/javascript"> var canvas = document.getElementById("renderCanvas"); var engine = new BABYLON.Engine(canvas, true); var createScene = function() { var scene = new BABYLON.Scene(engine); scene.clearColor = new BABYLON.Color3(0, 1, 0); var camera = new BABYLON.ArcRotateCamera("Camera", 1, 0.8, 10, new BABYLON.Vector3(0, 0, 0), scene); camera.attachControl(canvas, true); var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene); light.intensity = 0.7; var pl = new BABYLON.PointLight("pl", BABYLON.Vector3.Zero(), scene); pl.diffuse = new BABYLON.Color3(1, 1, 1); pl.specular = new BABYLON.Color3(1, 1, 1); pl.intensity = 0.8; var box = BABYLON.Mesh.CreateBox("box", '3', scene); box.position = new BABYLON.Vector3(-10,0,0); var box1 = BABYLON.Mesh.CreateBox("box1", '3', scene); box1.position = new BABYLON.Vector3(0,0,0); var animationBox = new BABYLON.Animation("myAnimation", "scaling.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE); var animationBox1 = new BABYLON.Animation("myAnimation1", "scaling.z", 10, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE); // An array with all animation keys var keys = []; //At the animation key 0, the value of scaling is "1" keys.push({ frame: 0, value: 1 }); //At the animation key 20, the value of scaling is "0.2" keys.push({ frame: 20, value: 0.2 }); keys.push({ frame: 60, value: 0.4 }); //At the animation key 100, the value of scaling is "1" keys.push({ frame: 100, value: 1 }); animationBox.setKeys(keys); box.animations = []; box.animations.push(animationBox); scene.beginAnimation(box, 0, 100, true); // An array with all animation keys var keys = []; //At the animation key 0, the value of scaling is "1" keys.push({ frame: 0, value: 1 }); //At the animation key 20, the value of scaling is "0.2" keys.push({ frame: 60, value: 0.2 }); //At the animation key 100, the value of scaling is "1" keys.push({ frame: 100, value: 1 }); animationBox1.setKeys(keys); box1.animations = []; box1.animations.push(animationBox1); scene.beginAnimation(box1, 0, 100, true); return scene; }; var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
Output
// An array with all animation keys var keys = []; //At the animation key 0, the value of scaling is "1" keys.push({ frame: 0, value: 1 }); //At the animation key 20, the value of scaling is "0.2" keys.push({ frame: 20, value: 0.2 }); //At the animation key 100, the value of scaling is "1" keys.push({ frame: 100, value: 1 }); animationBox.setKeys(keys); box.animations = []; box.animations.push(animationBox); scene.beginAnimation(box, 0, 100, true); //defines the start and the end on the target shape box.
Following are the other functions available on animation object −
- pause()
- restart()
- stop()
- reset()
We can store the beginAnimation reference in a variable and use the reference to stop, pause or reset animation.
var newAnimation = scene.beginAnimation(box1, 0, 100, true);
For example,
newAnimation.pause();
There are functions available on animation object to control the keyframes.
BABYLON.Animation.prototype.floatInterpolateFunction = function (startValue, endValue, gradient) { return startValue + (endValue - startValue) * gradient; }; BABYLON.Animation.prototype.quaternionInterpolateFunction = function (startValue, endValue, gradient) { return BABYLON.Quaternion.Slerp(startValue, endValue, gradient); }; BABYLON.Animation.prototype.vector3InterpolateFunction = function (startValue, endValue, gradient) { return BABYLON.Vector3.Lerp(startValue, endValue, gradient); };
Here is the list of functions that you can change −
- floatInterpolateFunction
- quaternionInterpolateFunction
- quaternionInterpolateFunctionWithTangents
- vector3InterpolateFunction
- vector3InterpolateFunctionWithTangents
- vector2InterpolateFunction
- vector2InterpolateFunctionWithTangents
- sizeInterpolateFunction
- color3InterpolateFunction
- matrixInterpolateFunction
To create a quick animation, there is a function available which can be used directly.
For example,
Animation.CreateAndStartAnimation = function(name, mesh, tartgetProperty, framePerSecond, totalFrame, from, to, loopMode);
Here you can use only 2 keyframes – start and end.
Demo
<!doctype html> <html> <head> <meta charset = "utf-8"> <title>BabylonJs - Basic Element-Creating Scene</title> <script src = "babylon.js"></script> <style> canvas {width: 100%; height: 100%;} </style> </head> <body> <canvas id = "renderCanvas"></canvas> <script type = "text/javascript"> var canvas = document.getElementById("renderCanvas"); var engine = new BABYLON.Engine(canvas, true); var createScene = function() { var scene = new BABYLON.Scene(engine); scene.clearColor = new BABYLON.Color3(0, 1, 0); var camera = new BABYLON.ArcRotateCamera("Camera", 1, 0.8, 10, new BABYLON.Vector3(0, 0, 0), scene); camera.attachControl(canvas, true); var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene); light.intensity = 0.7; var pl = new BABYLON.PointLight("pl", BABYLON.Vector3.Zero(), scene); pl.diffuse = new BABYLON.Color3(1, 1, 1); pl.specular = new BABYLON.Color3(1, 1, 1); pl.intensity = 0.8; var box = BABYLON.Mesh.CreateBox("box", '3', scene); box.position = new BABYLON.Vector3(0,0,0); BABYLON.Animation.CreateAndStartAnimation('boxscale', box, 'scaling.x', 30, 120, 1.0, 1.5); return scene; }; var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
Output
Animation Blending
You can achieve animation blending with the help of enableBlending = true;
This blended animation will change from the current object state.
Easing Functions
To make the animation more impressive, there are some easing functions which we have already used with css earlier.
Following is a list of easing functions −
- BABYLON.CircleEase ()
- BABYLON.BackEase (amplitude)
- BABYLON.BounceEase (bounces, bounciness)
- BABYLON.CubicEase ()
- BABYLON.ElasticEase (oscillations, springiness)
- BABYLON.ExponentialEase (exponent)
- BABYLON.PowerEase (power)
- BABYLON.QuadraticEase ()
- BABYLON.QuarticEase ()
- BABYLON.QuinticEase ()
- BABYLON.SineEase ()
Demo
<!doctype html> <html> <head> <meta charset = "utf-8"> <title>BabylonJs - Basic Element-Creating Scene</title> <script src = "babylon.js"></script> <style> canvas {width: 100%; height: 100%;} </style> </head> <body> <canvas id = "renderCanvas"></canvas> <script type = "text/javascript"> var canvas = document.getElementById("renderCanvas"); var engine = new BABYLON.Engine(canvas, true); var createScene = function() { var scene = new BABYLON.Scene(engine); scene.clearColor = new BABYLON.Color3(0, 1, 0); var camera = new BABYLON.ArcRotateCamera("Camera", 1, 0.8, 10, new BABYLON.Vector3(0, 0, 0), scene); camera.attachControl(canvas, true); var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene); light.intensity = 0.7; var pl = new BABYLON.PointLight("pl", BABYLON.Vector3.Zero(), scene); pl.diffuse = new BABYLON.Color3(1, 1, 1); pl.specular = new BABYLON.Color3(1, 1, 1); pl.intensity = 0.8; var box1 = BABYLON.Mesh.CreateTorus("torus", 5, 1, 10, scene, false); box1.position = new BABYLON.Vector3(0,0,0); var animationBox1 = new BABYLON.Animation("myAnimation1", "scaling.z", 10, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE); // An array with all animation keys var keys = []; //At the animation key 0, the value of scaling is "1" keys.push({ frame: 0, value: 1 }); //At the animation key 20, the value of scaling is "0.2" keys.push({ frame: 60, value: 0.2 }); //At the animation key 100, the value of scaling is "1" keys.push({ frame: 100, value: 1 }); animationBox1.setKeys(keys); box1.animations = []; // box1.animations.push(animationBox1); var easingFunction = new BABYLON.QuarticEase(); easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEINOUT); animationBox1.setEasingFunction(easingFunction); box1.animations.push(animationBox1); scene.beginAnimation(box1, 0, 100, true); return scene; }; var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
Output
Animation Event
You can carry out anything necessary on animation event. If you want to change anything when the frame is changed or when the animation is complete, it can be achieved by adding events to the animation.
var event1 = new BABYLON.AnimationEvent(50, function() { console.log("Yeah!"); }, true); // You will get hte console.log when the frame is changed to 50 using animation. animation.addEvent(event1); //attaching event to the animation.
BabylonJS – Sprites
What does sprites refer to in computer graphics? It is basically a 2-dimensional bitmap that is integrated into a larger scene. When multiple smaller images are combined into a single bitmap to save memory, the resulting image is called a sprite sheet. Let us get started with sprites and how to use them.
The first step to start working with sprites is to create a sprite manager.
var spriteManagerTrees = new BABYLON.SpriteManager("treesManagr", "Assets/Palm-arecaceae.png", 2000, 800, scene);
Consider the following parameters to create sprite manager −
- Name − The name of this manager.
- URL − The image url to be used.
- Capacity of manager − The maximum number of instances in this manager.For example, the above insteance will create 2000 trees.
- Cell size − The size taken by the image.
- Scene − The scene to which the manager will be added.
var spriteManagerPlayer = new BABYLON.SpriteManager("playerManagr","Assets/Player.png", 2, 64, scene);
Take a look at the above object.We have given a player image and are now creating 2 instances of it. The size of the image is 64. Each image of a sprite must be contained in a64 pixel square, no more no less.
Let us now create instance of the same linked to the sprite manager.
var player = new BABYLON.Sprite("player", spriteManagerPlayer);
You can play around with this player object just like any other shapes or meshes. You can assign position, size, angle, etc.
player.size = 0.3; player.angle = Math.PI/4; player.invertU = -1; player.width = 0.3; player.height = 0.4;
Demo
<!doctype html> <html> <head> <meta charset = "utf-8"> <title>BabylonJs - Basic Element-Creating Scene</title> <script src = "babylon.js"></script> <style> canvas {width: 100%; height: 100%;} </style> </head> <body> <canvas id = "renderCanvas"></canvas> <script type = "text/javascript"> var canvas = document.getElementById("renderCanvas"); var engine = new BABYLON.Engine(canvas, true); var createScene = function() { var scene = new BABYLON.Scene(engine); //scene.clearColor = new BABYLON.Color3(0, 1, 0); // Create camera and light var light = new BABYLON.PointLight("Point", new BABYLON.Vector3(5, 10, 5), scene); var camera = new BABYLON.ArcRotateCamera("Camera", 1, 0.8, 8, new BABYLON.Vector3(0, 0, 0), scene); camera.attachControl(canvas, true); var spriteManagerTrees = new BABYLON.SpriteManager("trees", "images/tree.png", 1000, 400, scene); for (var i = 0; i < 1000; i++) { var tree = new BABYLON.Sprite("tree", spriteManagerTrees); tree.position.x = Math.random() * 100 - 50; tree.position.z = Math.random() * 100 - 50; tree.isPickable = true; //Some "dead" trees if (Math.round(Math.random() * 5) === 0) { tree.angle = Math.PI * 90 / 180; tree.position.y = -0.3; } } var spriteManagerTrees1 = new BABYLON.SpriteManager("trees1", "images/tree1.png", 1000,400, scene); for (var i = 0; i < 1000; i++) { var tree1 = new BABYLON.Sprite("tree1", spriteManagerTrees1); if (i %2 == 0) { tree1.position.x = Math.random() * 100 - 50; } else { tree1.position.z = Math.random() * 100 - 50; } tree1.isPickable = true; } spriteManagerTrees.isPickable = true; spriteManagerTrees1.isPickable = true; var spriteManagerPlayer = new BABYLON.SpriteManager("playerManager", "images/bird.png", 2, 200, scene); var player = new BABYLON.Sprite("player", spriteManagerPlayer); player.position.x = 2; player.position.y = 2; player.position.z = 0; var spriteManagerPlayer1 = new BABYLON.SpriteManager("playerManager1", "images/bird.png", 2, 200, scene); var player1 = new BABYLON.Sprite("player", spriteManagerPlayer1); player1.position.x = 1; player1.position.y = 2; player1.position.z = 0; var spriteManagerPlayer2 = new BABYLON.SpriteManager("playerManager2", "images/bird.png", 2, 200, scene); var player2 = new BABYLON.Sprite("player", spriteManagerPlayer2); player2.position.x = 0; player2.position.y = 1; player2.position.z = 0; scene.onPointerDown = function (evt) { var pickResult = scene.pickSprite(this.pointerX, this.pointerY); if (pickResult.hit) { pickResult.pickedSprite.angle += 1; } }; return scene; }; var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
Output
In this demo, we have used an image called tree.png, tree1.png to show trees, bird.png to show bird in the scene. These images are stored in images/ folder locally and are also pasted below for reference. You can download any image of your choice and use in the demo link.
The images used for Tree are shown below.
images/tree.png
images/tree1.png
images/bird.png
Let us now see one more demo with sprites-balloons.
Demo with sprites-balloons
<!doctype html> <html> <head> <meta charset = "utf-8"> <title>BabylonJs - Basic Element-Creating Scene</title> <script src = "babylon.js"></script> <style> canvas {width: 100%; height:100%;} </style> </head> <body> <canvas id = "renderCanvas"></canvas> <script type = "text/javascript"> var canvas = document.getElementById("renderCanvas"); var engine = new BABYLON.Engine(canvas, true); var createScene = function() { var scene = new BABYLON.Scene(engine); var light = new BABYLON.PointLight("Point", new BABYLON.Vector3(5, 10, 5), scene); var camera = new BABYLON.ArcRotateCamera("Camera", -3.4, 1.0, 82, new BABYLON.Vector3(0, -15, 0), scene); camera.setPosition(new BABYLON.Vector3(30, 0,100)); camera.attachControl(canvas, true); var spriteManagerTrees = new BABYLON.SpriteManager("trees", "images/balloon.png", 50, 450, scene); var treearray = []; for (var i = 0; i < 50; i++) { var tree = new BABYLON.Sprite("tree", spriteManagerTrees); tree.position.x = Math.random() * 100 - 10; tree.position.z = Math.random() * 100 - 10; tree.position.y = -35; tree.isPickable = true; treearray.push(tree); } spriteManagerTrees.isPickable = true; scene.onPointerDown = function (evt) { var pickResult = scene.pickSprite(this.pointerX, this.pointerY); if (pickResult.hit) { pickResult.pickedSprite.position.y = -3000; } }; k = -35; var animate = function() { if (k > 3) return; k += 0.05; for (var i = 0; i < treearray.length; i++) { treearray[i].position.y = k; } }; scene.registerBeforeRender(animate); return scene; }; var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
Output
In this demo, we have used image called ballon.png. The images are stored in images/ folder locally and are also pasted below for reference. You can download any image of your choice and use in the demo link.
images/balloon.png
Balloons will rise in the sky and once they stop, you can click on them and they will disappear. This is done using the pickSprite function which gives details when clicked on the created sprite.
The onPointerDown function is called when the mouse action takes place and the position of sprite is changed.
var animate = function() { if (k > 3) return; k += 0.05; for (var i = 0; i < treearray.length; i++) { treearray[i].position.y = k; } }; scene.registerBeforeRender(animate);
The function animate is called in registerBeforeRender, which takes care of moving the ballons from initial -35 to +3. It is moved slowly by incrementing it by .05.
BabylonJS – Particles
A particle system is a technique in computer graphics which makes use of a large number of very small sprites, 3D models, or other graphic objects to simulate certain kinds of “fuzzy” phenomena, which are otherwise very hard to reproduce with conventional rendering techniques.
To create particle system, you have to call the class as follows −
var particleSystem = new BABYLON.ParticleSystem("particles", 2000, scene);//2000 refers to the total number of particles to be produced.
The following properties need to be considered for the particle system −
particleSystem.particleTexture = new BABYLON.Texture("Flare.png", scene); particleSystem.textureMask = new BABYLON.Color4(0.1, 0.8, 0.8, 1.0); particleSystem.emitter = fountain particleSystem.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0); particleSystem.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0); particleSystem.colorDead = new BABYLON.Color4(0, 0, 0.2, 0.0);
The emitter property takes the mesh from which the particle has to be emitted. The color1 and color2 are the colors for the particles.
ColorDead is the color applied to the particle just before it disappears from the scene hence called colorDead.
particleSystem.minSize = 0.1; particleSystem.maxSize = 0.5; particleSystem.minLifeTime = 0.3; particleSystem.maxLifeTime = 1.5;
MinSize and maxSize is the size given to the particles. MinlifeTime and maxLifeTime is the lifetime given to the particles.
particleSystem.emitRate = 1500;
The emitRate is the rate at which particles will be emitted.
We have used torus in the demo shown below. We have used the particle system and its properties to get all particles around the torus.
Demo 1
<!doctype html> <html> <head> <meta charset = "utf-8"> <title>BabylonJs - Basic Element-Creating Scene</title> <script src = "babylon.js"></script> <style> canvas {width: 100%; height: 100%;} </style> </head> <body> <canvas id = "renderCanvas"></canvas> <script type = "text/javascript"> var canvas = document.getElementById("renderCanvas"); var engine = new BABYLON.Engine(canvas, true); var createScene = function() { var scene = new BABYLON.Scene(engine); // Setup environment var light0 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(0, 2, 8), scene); var camera = new BABYLON.ArcRotateCamera("ArcRotateCamera", 1, 0.8, 20, new BABYLON.Vector3(0, 0, 0), scene); camera.attachControl(canvas, true); var fountain = BABYLON.Mesh.CreateTorus("torus", 2, 1, 8, scene, false); var particleSystem = new BABYLON.ParticleSystem("particles", 2000, scene); particleSystem.particleTexture = new BABYLON.Texture("images/dot.jpg", scene); particleSystem.textureMask = new BABYLON.Color4(0.1, 0.8, 0.8, 1.0); particleSystem.emitter = fountain; particleSystem.minEmitBox = new BABYLON.Vector3(-1, 0, 0); // Starting all from particleSystem.maxEmitBox = new BABYLON.Vector3(1, 0, 0); // To... particleSystem.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0); particleSystem.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0); particleSystem.colorDead = new BABYLON.Color4(0, 0, 0.2, 0.0); particleSystem.minSize = 0.1; particleSystem.maxSize = 0.5; particleSystem.minLifeTime = 0.3; particleSystem.maxLifeTime = 1.5; particleSystem.emitRate = 1500; particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE; particleSystem.gravity = new BABYLON.Vector3(0, -9.81, 0); particleSystem.direction1 = new BABYLON.Vector3(-7, 8, 3); particleSystem.direction2 = new BABYLON.Vector3(7, 8, -3); particleSystem.minAngularSpeed = 0; particleSystem.maxAngularSpeed = Math.PI; particleSystem.minEmitPower = 1; particleSystem.maxEmitPower = 3; particleSystem.updateSpeed = 0.005; particleSystem.start(); var keys = []; var animation = new BABYLON.Animation("animation", "rotation.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE); // At the animation key 0, the value of scaling is "1" keys.push({ frame: 0, value: 0 }); // At the animation key 50, the value of scaling is "0.2" keys.push({ frame: 50, value: Math.PI }); // At the animation key 100, the value of scaling is "1" keys.push({ frame: 100, value: 0 }); // Launch animation animation.setKeys(keys); fountain.animations.push(animation); scene.beginAnimation(fountain, 0, 100, true); return scene; } var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
Output
The above line of code generates the following output −
In this demo, we have used image called dot.jpg. The images are stored in images/ folder locally and are also pasted below for reference. You can download any image of your choice and use in the demo link.
Following is the imageused for particle texture: images/dot.jpg
Demo 2
<!doctype html> <html> <head> <meta charset = "utf-8"> <title>BabylonJs - Ball/Ground Demo</title> <script src = "babylon.js"></script> <style> canvas {width: 100%; height: 100%;} </style> </head> <body> <canvas id = "renderCanvas"></canvas> <script type = "text/javascript"> var canvas = document.getElementById("renderCanvas"); var engine = new BABYLON.Engine(canvas, true); var createScene = function() { var scene = new BABYLON.Scene(engine); scene.clearColor = new BABYLON.Color3( .5, .5, .5); var camera = new BABYLON.ArcRotateCamera("camera1", 0, 0, 0, new BABYLON.Vector3(0, 0, -0), scene); camera.setPosition(new BABYLON.Vector3(-100, 0,-100)); camera.attachControl(canvas, true); var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 0.5, 0), scene); var pl = new BABYLON.PointLight("pl", new BABYLON.Vector3(0, 0, 0), scene); var gmat = new BABYLON.StandardMaterial("mat1", scene); gmat.alpha = 1.0; var ground = BABYLON.Mesh.CreateGround("ground", 100, 100, 20, scene); ground.material = gmat; gmat.wireframe = true; var particleSystem = new BABYLON.ParticleSystem("particles", 2000, scene); particleSystem.particleTexture = new BABYLON.Texture("images/dot.jpg", scene); particleSystem.textureMask = new BABYLON.Color4(0.1, 0.8, 0.8, 1.0); particleSystem.emitter = ground; particleSystem.minEmitBox = new BABYLON.Vector3(-1, 0, 0); // Starting all from particleSystem.maxEmitBox = new BABYLON.Vector3(1, 0, 0); // To... particleSystem.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0); particleSystem.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0); particleSystem.colorDead = new BABYLON.Color4(0, 0, 0.2, 0.0); particleSystem.minSize = 0.1; particleSystem.maxSize = 0.5; particleSystem.minLifeTime = 0.3; particleSystem.maxLifeTime = 1.5; particleSystem.emitRate = 1500; particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE; particleSystem.gravity = new BABYLON.Vector3(0, -9.81, 0); particleSystem.direction1 = new BABYLON.Vector3(-7, 8, 3); particleSystem.direction2 = new BABYLON.Vector3(7, 8, -3); particleSystem.minAngularSpeed = 0; particleSystem.maxAngularSpeed = Math.PI; particleSystem.minEmitPower = 1; particleSystem.maxEmitPower = 3; particleSystem.updateSpeed = 0.005; particleSystem.start(); var keys = []; var animation = new BABYLON.Animation("animation", "rotation.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE); // At the animation key 0, the value of scaling is "1" keys.push({ frame: 0, value: 0 }); // At the animation key 50, the value of scaling is "0.2" keys.push({ frame: 50, value: Math.PI }); // At the animation key 100, the value of scaling is "1" keys.push({ frame: 100, value: 0 }); // Launch animation animation.setKeys(keys); ground.animations.push(animation); //scene.beginAnimation(ground, 0, 100, true); return scene; }; var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
Output
Demo with Animation
<!doctype html> <html> <head> <meta charset = "utf-8"> <title>BabylonJs - Ball/Ground Demo</title> <script src = "babylon.js"></script> <style> canvas {width: 100%; height: 100%;} </style> </head> <body> <canvas id = "renderCanvas"></canvas> <script type = "text/javascript"> var canvas = document.getElementById("renderCanvas"); var engine = new BABYLON.Engine(canvas, true); var createScene = function() { var scene = new BABYLON.Scene(engine); scene.clearColor = new BABYLON.Color3( .5, .5, .5); var camera = new BABYLON.ArcRotateCamera("camera1", 0, 0, 0, new BABYLON.Vector3(0, 0, -0), scene); camera.setPosition(new BABYLON.Vector3(-100, 0, -100)); camera.attachControl(canvas, true); var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 0.5, 0), scene); var pl = new BABYLON.PointLight("pl", new BABYLON.Vector3(0, 0, 0), scene); var gmat = new BABYLON.StandardMaterial("mat1", scene); gmat.alpha = 1.0; var ground = BABYLON.Mesh.CreateGround("ground", 100, 100, 20, scene); ground.material = gmat; gmat.wireframe = true; var particleSystem = new BABYLON.ParticleSystem("particles", 2000, scene); particleSystem.particleTexture = new BABYLON.Texture("images/dot.jpg", scene); particleSystem.textureMask = new BABYLON.Color4(0.1, 0.8, 0.8, 1.0); particleSystem.emitter = ground; particleSystem.minEmitBox = new BABYLON.Vector3(-1, 0, 0); // Starting all from particleSystem.maxEmitBox = new BABYLON.Vector3(1, 0, 0); // To... particleSystem.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0); particleSystem.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0); particleSystem.colorDead = new BABYLON.Color4(0, 0, 0.2, 0.0); particleSystem.minSize = 0.1; particleSystem.maxSize = 0.5; particleSystem.minLifeTime = 0.3; particleSystem.maxLifeTime = 1.5; particleSystem.emitRate = 1500; particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE; particleSystem.gravity = new BABYLON.Vector3(0, -9.81, 0);//gravity for the particle. particleSystem.direction1 = new BABYLON.Vector3(-7, 8, 3); particleSystem.direction2 = new BABYLON.Vector3(7, 8, -3); //random direction for the particles on the scene particleSystem.minAngularSpeed = 0; particleSystem.maxAngularSpeed = Math.PI; particleSystem.minEmitPower = 1; particleSystem.maxEmitPower = 3; particleSystem.updateSpeed = 0.005; particleSystem.start(); var keys = []; var animation = new BABYLON.Animation("animation", "rotation.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE); // At the animation key 0, the value of scaling is "1" keys.push({ frame: 0, value: 0 }); // At the animation key 50, the value of scaling is "0.2" keys.push({ frame: 50, value: Math.PI }); // At the animation key 100, the value of scaling is "1" keys.push({ frame: 100, value: 0 }); // Launch animation animation.setKeys(keys); ground.animations.push(animation); scene.beginAnimation(ground, 0, 100, true); return scene; }; var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
Output
The above line of code generates the following output −
Explanation
The above demo shows a ground with wireframe material and the particle system is produced from the center.
Next Topic : Click Here