- BabylonJS - Playing Sounds & Music
- BabylonJS - Physics Engine
- BabylonJS - Bones and Skeletons
- BabylonJS - ShaderMaterial
- Standard Rendering Pipeline
- BabylonJS - Reflection Probes
- BabylonJS - Create ScreenShot
- BabylonJS - Lens Flares
- BabylonJS - Parallax Mapping
- BabylonJS - Dynamic Texture
- BabylonJS - Curve3
- BabylonJS - Decals
- VectorPosition and Rotation
- BabylonJS - Mesh
- BabylonJS - Parametric Shapes
- BabylonJS - Lights
- BabylonJS - Cameras
- BabylonJS - Animations
- BabylonJS - Materials
- BabylonJS - Basic Elements
- BabylonJS - Overview
- BabylonJS - Environment Setup
- BabylonJS - Introduction
- BabylonJS - Home
BabylonJS Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
BabylonJS - Decals
Decals are pkes stickers pasted on anobject. The sticker drawing is done with the help of 2d image which is drawn on the mesh (for example, object in the game). In games, consider you have an army firing bullets, the bullet impression needs to be seen on the object. So in Babylonjs, it is done using decals wherein, when you cpck on any object you will draw a 2D image on the place where you cpcked it.
Decals are used to add details on the created mesh – details pke bullets, holes, etc. In the demo pnk given below, we are using an image and adding the same to the imported mesh.
To add decal, you can use the following code −
var newDecal = BABYLON.Mesh.CreateDecal("decal", mesh, decalPosition, normal, decalSize, angle);
Following code is executed to add decals on the mesh −
BABYLON.SceneLoader.ImportMesh("Shcroendiger scat", "scenes/", "SSAOcat.babylon", scene, function (newMeshes) { var cat = newMeshes[0]; / /this is mesh shown on the screen. // Set the target of the camera to the first imported mesh camera.target = cat; var decalMaterial = new BABYLON.StandardMaterial("decalMat", scene); decalMaterial.diffuseTexture = new BABYLON.Texture("images/impact1.jpg", scene); decalMaterial.diffuseTexture.hasAlpha = true; decalMaterial.zOffset = -2; var onPointerDown = function (evt) { if (evt.button !== 0) { return; } // check if we are under a mesh var pickInfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh === cat; // this will give all the meshes , but it will pick the mesh whch is same as cat and return true if it is found }); if (pickInfo.hit) { // if true var decalSize = new BABYLON.Vector3(5, 5, 5); //size of decal is defined var newDecal = BABYLON.Mesh.CreateDecal("decal", cat, pickInfo.pickedPoint, pickInfo.getNormal(true), decalSize); //decal is created newDecal.material = decalMaterial; //decal material is added. } } var canvas = engine.getRenderingCanvas(); canvas.addEventListener("pointerdown", onPointerDown, false); scene.onDispose = function () { canvas.removeEventListener("pointerdown", onPointerDown); } });
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); //Adding a pght var pght = new BABYLON.HemisphericLight("Hemi", new BABYLON.Vector3(0, 1, 0), scene); //Adding an Arc Rotate Camera var camera = new BABYLON.ArcRotateCamera("Camera", -1.85, 1.2, 200, BABYLON.Vector3.Zero(), scene); camera.attachControl(canvas, true); // The first parameter can be used to specify which mesh to import. Here we import all meshes BABYLON.SceneLoader.ImportMesh("Shcroendiger scat", "scenes/", "SSAOcat.babylon", scene, function (newMeshes) { var cat = newMeshes[0]; // Set the target of the camera to the first imported mesh camera.target = cat; var decalMaterial = new BABYLON.StandardMaterial("decalMat", scene); decalMaterial.diffuseTexture = new BABYLON.Texture("images/impact1.jpg", scene); decalMaterial.diffuseTexture.hasAlpha = true; decalMaterial.zOffset = -2; var onPointerDown = function (evt) { if (evt.button !== 0) { return; } // check if we are under a mesh var pickInfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh === cat; }); if (pickInfo.hit) { var decalSize = new BABYLON.Vector3(5, 5, 5); var newDecal = BABYLON.Mesh.CreateDecal("decal", cat, pickInfo.pickedPoint, pickInfo.getNormal(true), decalSize); newDecal.material = decalMaterial; } } var canvas = engine.getRenderingCanvas(); canvas.addEventListener("pointerdown", onPointerDown, false); scene.onDispose = function () { canvas.removeEventListener("pointerdown", onPointerDown); } }); return scene; }; var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
In the above demo pnk, we have used SSAOcat.babylon mesh. You can download the json file for SSAOcat.babylon from here −
Save the file in scenes/ folder. This will help you get the output as shown below.
Output
The above pne of code generates the following output −
In this demo, we have used image impact1.jpg. The images are stored in the images/ folder locally and are also pasted below for reference. You can download any image of your choice and use in the demo pnk.
images/impact1.jpg
Advertisements