Just in case you cant see playground code… Here is code to bootstrap Ammo.js on the playground:
class Playground {
public static async CreateScene(engine: BABYLON.Engine, canvas: HTMLCanvasElement): Promise<BABYLON.Scene> {
// This creates a basic scene object (non-mesh)
var scene = new BABYLON.Scene(engine);
// This initializes bullet physics (non-mesh)
const wnd:any = window;
if (!wnd.ammoLoaded) {
wnd.Ammo().then(function(){
wnd.ammoLoaded = true;
Playground.LoadScene(scene);
});
} else {
Playground.LoadScene(scene);
}
// This save current scene for debugging
(<any>window).scene = scene;
return scene;
}
public static async LoadScene(scene:BABYLON.Scene):Promise<void> {
let rootUrl:string = "https://mackeyk24.github.io/racing/test3/";
// Load Project Script Bundle (Authored With Babylon Toolkit Editor)
await BABYLON.Tools.LoadScriptAsync(rootUrl + "project.js");
// Load Authored Sample Race Track Scene (SceneManager Will Parse Unity Metadata)
await BABYLON.SceneLoader.AppendAsync(rootUrl, "SampleScene.gltf", scene);
// Get Mustang Car Transform Node From Authored Scene Content
const mustangTransform:BABYLON.TransformNode = BABYLON.SceneManager.GetTransformNode(scene, "Tester Car 1");
if (mustangTransform) {
// Get Reference To Project Standard Car Controller
const standardCarController:PROJECT.StandardCarController = BABYLON.SceneManager.FindScriptComponent(mustangTransform, "PROJECT.StandardCarController");
if (standardCarController != null) {
standardCarController.baseEnginePower = 3000;
standardCarController.burnoutCoefficient = 5.55;
standardCarController.burnoutDonutBoost = 9.25;
// Wheel Fountain Local Positions
const frontLeftPosition:BABYLON.Vector3 = BABYLON.Vector3.Zero();
const frontRightPosition:BABYLON.Vector3 = BABYLON.Vector3.Zero();
const backLeftPosition:BABYLON.Vector3 = BABYLON.Vector3.Zero();
const backRightPosition:BABYLON.Vector3 = BABYLON.Vector3.Zero();
// Wheel Hub Transform Nodes
let frontLeftWheelHub:BABYLON.TransformNode = null;
let frontRightWheelHub:BABYLON.TransformNode = null;
let backLeftWheelHub:BABYLON.TransformNode = null;
let backRightWheelHub:BABYLON.TransformNode = null;
////////////////////////////////////////////////////
// TODO: Setup GPU Particle Systems For Wheel Smoke
////////////////////////////////////////////////////
let backRigghtWheelEmitter:BABYLON.AbstractMesh = new BABYLON.AbstractMesh("backRigghtWheelEmitter");
//let psbr = new BABYLON.GPUParticleSystem("psbr",{capacity:30000}, scene);
let psbr = new BABYLON.ParticleSystem("psbr", 10000, scene);
psbr.particleTexture = new BABYLON.Texture("https://raw.githubusercontent.com/PatrickRyanMS/BabylonJStextures/master/forum/correctedSmoke.png", scene);
psbr.minAngularSpeed = -1.5;
psbr.maxAngularSpeed = 1.5;
psbr.minSize = 0.2;
psbr.maxSize = 1.3;
// changed lifetime to control shape of the cloud in conjunction with velocity gradient
psbr.minLifeTime = 3.0;
psbr.maxLifeTime = 5.0;
// adding velocity over time to prevent large bloom of particles by launching them fast and then slowing them down GPU particles don't take a gradient on velocity, but the reduction in velocity over time is worth the trade off for less randomization
// psbr.minEmitPower = 0.3;
// psbr.maxEmitPower = 0.7;
psbr.addVelocityGradient(0, 1);
psbr.addVelocityGradient(0.1, 0.7);
psbr.addVelocityGradient(0.7, 0.2);
psbr.addVelocityGradient(1.0, 0.05);
psbr.emitter = backRigghtWheelEmitter;
// reduced the emit rate to reduce bloom of particles in the center of mass
psbr.gravity = new BABYLON.Vector3(0, -0.1, 0);
psbr.minEmitBox = new BABYLON.Vector3(0, -0.25, 0);
psbr.maxEmitBox = new BABYLON.Vector3(0, -0.25, 0);
psbr.direction1 = new BABYLON.Vector3(-1, -1, -1);
psbr.direction2 = new BABYLON.Vector3(1, 1, 1);
// The color1, color2, colorDead pattern is overridden by color gradient so is unnecessary
psbr.color1 = new BABYLON.Color4(1.0, 1.0, 1.0, 0.1);
psbr.color2 = new BABYLON.Color4(1.0, 1.0, 1.0, 0.05);
psbr.colorDead = new BABYLON.Color4(1.0, 1.0, 1.0, 0.0);
// Changed the color gradient to add a second value for randomization and ramped the color down further to create more texture with alpha blending
//psbr.addColorGradient(0.0, new BABYLON.Color4(1, 1, 1, 0.0), new BABYLON.Color4(0.7, 0.7, 0.7, 0.0));
//psbr.addColorGradient(0.2, new BABYLON.Color4(0.7, 0.7, 0.7, 0.2), new BABYLON.Color4(0.6, 0.6, 0.6, 0.2));
//psbr.addColorGradient(0.6, new BABYLON.Color4(0.6, 0.6, 0.6, 0.1),new BABYLON.Color4(0.4, 0.4, 0.4, 0.1));
//psbr.addColorGradient(1.0, new BABYLON.Color4(0.3, 0.3, 0.3, 0.0),new BABYLON.Color4(0.1, 0.1, 0.1, 0.0));
psbr.blendMode = BABYLON.ParticleSystem.BLENDMODE_STANDARD;
let backLeftWheelEmitter:BABYLON.AbstractMesh = new BABYLON.AbstractMesh("backLeftWheelEmitter");
let psbl = psbr.clone("psbl", null);
psbl.emitter = backLeftWheelEmitter;
standardCarController.onUpdateVehicleInformation = (skid_FL:number, skid_FR:number, skid_RL:number, skid_RR:number) => {
//////////////////////////////////////////////////
// Validate Wheel Hub Transform Nodes Setup
//////////////////////////////////////////////////
if (frontLeftWheelHub == null) {
frontLeftWheelHub = standardCarController.getFrontLeftWheelNode();
if (frontLeftWheelHub != null) frontLeftPosition.copyFrom(frontLeftWheelHub.position);
// TODO: Create Front Left Fountion Mesh At Local Position - ???
}
if (frontRightWheelHub == null) {
frontRightWheelHub = standardCarController.getFrontRightWheelNode();
if (frontRightWheelHub != null) frontRightPosition.copyFrom(frontRightWheelHub.position);
// TODO: Create Front Right Fountion Mesh At Local Position - ???
}
if (backLeftWheelHub == null) {
backLeftWheelHub = standardCarController.getBackLeftWheelNode();
if (backLeftWheelHub != null){
backLeftPosition.copyFrom(backLeftWheelHub.position);
backLeftWheelEmitter.position = standardCarController.getBackLeftWheelNode().absolutePosition;
}
// TODO: Create Back Left Fountion Mesh At Local Position - ???
}
if (backRightWheelHub == null) {
backRightWheelHub = standardCarController.getBackRightWheelNode();
if (backRightWheelHub != null){
backRightPosition.copyFrom(backRightWheelHub.position);
backRigghtWheelEmitter.position = standardCarController.getBackRightWheelNode().absolutePosition;
}
// TODO: Create Back Right Fountion Mesh At Local Position - ???
}
//////////////////////////////////////////////////
// TODO: Emit Wheel Smoke Based On Skid Intenisty
//////////////////////////////////////////////////
if (frontLeftWheelHub != null) {
// Emit Front Left Wheel Smoke If Needed
}
if (frontRightWheelHub != null) {
// Emit Front Right Wheel Smoke If Needed
}
if (backLeftWheelHub != null) {
if(!psbl.isStarted()){
psbl.start()
}
let skid = skid_RL*skid_RL;
psbl.emitRate = 300 * skid;
psbl.minSize = 0.2 * skid + 0.2;
psbl.maxSize = 1.5 * skid + 1.5;
}
if (backRightWheelHub != null) {
if(!psbr.isStarted()){
psbr.start()
}
let skid = skid_RR*skid_RR;
psbr.emitRate = 300 * skid;
psbr.minSize = 0.2 * skid + 0.2;
psbr.maxSize = 1.5 * skid + 1.5;
}
};
}
} else {
console.warn("No Mustang Car Transform Found.");
}
}
}