Trail Mesh
How To Use TrailMesh
TrailMesh creates a new Mesh that trails another.
import { TrailMesh } from "@babylonjs/core/Meshes/trailMesh";let options = { diameter: 1.5, length: 100, segments: 10, sections: 4, doNotTaper: false, autoStart: true};const trail = new TrailMesh(name, generator, scene, options);trail.start(); // Starts the trailing mesh.trail.stop(); // Stops the trailing mesh.| variable | description |
|---|---|
| name | The value used by scene.getMeshByName() to do a lookup. |
| generator | The mesh to generate a trail. |
| scene | The scene to add this mesh to. |
| options | The object containing trail parameters. |
There are a variety of parameters you can set to create different types of trails to suit your needs. If you do not specify segments, it will default to the length of the trail; i.e., a trail with a length of 60 will have 60 segments.
| options property | value | default value |
|---|---|---|
| diameter (optional) | (number) Diameter of trailing mesh. | 1 |
| length (optional) | (number) Length of trailing mesh. | 60 |
| segments (optional) | (number) Segments of trailing mesh. | length |
| sections (optional) | (number) Cross-section of trailing mesh. Min 2. | 4 |
| doNoTaper (optional) | (boolean) Whether the trail should taper. | false |
| autoStart (optional) | (boolean) Automatically starts the trailing mesh. | true |
TrailMesh will be affected by all modifications to the generator mesh. Using bakeCurrentTransformIntoVertices on the generator after scaling (and before a position shift) will prevent the TrailMesh from scaling.
Resetting the TrailMesh
As of 7.21.0, you can now reset the TrailMesh via trail.reset(). Use this in conjunction with trail.stop() and trail.start(). This is ideal when the trail appears distorted because of sudden, large changes in the generator position, for example when firing and reloading projectile meshes.
Example Usage
import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial";import { Color3 } from "@babylonjs/core/Maths/math.color";import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";import { TrailMesh } from "@babylonjs/core/Meshes/trailMesh";let alpha = Math.PI;// Create a mesh for the trail to follow.const cube = MeshBuilder.CreateBox("cube", {}, scene);cube.scaling.y = 2;cube.bakeCurrentTransformIntoVertices();cube.position.x = Math.sin(alpha) * 10;cube.position.z = Math.cos(alpha) * 10;cube.computeWorldMatrix(true);// Trail optionslet options = { diameter: 1.5, length: 100, segments: 10, sections: 4, doNotTaper: false, autoStart: true};const trail = new TrailMesh("new", cube, scene, options);const sourceMat = new StandardMaterial("sourceMat", scene);sourceMat.emissiveColor = sourceMat.diffuseColor = new Color3.Red();sourceMat.specularColor = new Color3.Black();trail.material = sourceMat;const observer = scene.onBeforeRenderObservable.add(animate);function animate() { alpha += Math.PI / 120; cube.position.x = Math.sin(alpha) * 10; cube.position.z = Math.cos(alpha) * 10; cube.rotation.x = (Math.PI * alpha) / 2; cube.rotation.y = alpha;}