Hi and welcome to the next Quick Tip of this week!
In this video I show you how to use the script "SimpleFlow" with a simple scene inside CreativeExperience .
You can use this script for example to simulate objects on a conveyor belt or for any object that should follow a path with a certain amount of objects.
Please copy the script here:
var particles = []; //local copy of rock pool
var startingScales = []; //memorized size of rock
var isRunning = true;
var zero = new DSMath.Vector3D();
var rotateSpeedVect = new DSMath.Vector3D(0, 0, 1);
var timer = 0;
var speedInPerc = 0;
var particleTemplate;
beScript.onStart = function () {
console.log('start');
particleTemplate = STU.Experience.getCurrent().getActorTemplateByName(this.TemplateName);
speedInPerc = this.Speed / this.actor.getLength();
rotateSpeedVect = new DSMath.Vector3D(0, 0, this.RotateSpeed / 100);
beScript.reset();
};
/*
CAPACITY to reset
call this from NL when switching
*/
beScript.reset = function () {
particles = [];
for (var i = 0; i < this.StartCount; ++i) {
particleTemplate.createDynamicInstance({
name: "MyActor" + i,
onInstantiationSuccess: function (actor) {
particles.push(actor);
}
});
}
console.log(particles.length);
startingScales = [];
// this.RockPool.visible = true;
//startingPosition = this.Emmiter.getPosition();
for (var i = 0; i < particles.length; i++) {
console.log(particles[i].getBoundingSphere().radius);
startingScales.push(particles[i].getScale());
particles[i].life = 3000;
particles[i].index = i;
particles[i].visible = false;
particles[i].progress = 0;
}
}
beScript.switchEmmiter = function (em) {
this.Emmiter = em;
beScript.reset();
}
beScript.onStop = function () {
//Will be called on experience stop.
};
beScript.startFlow = function () {
this.isRunning = true;
beScript.reset();
}
beScript.stopFlow = function () {
this.isRunning = false;
}
beScript.execute = function (context) {
if (!isRunning) {
return;
}
var emitOne = true;
timer += context.deltaTime;
for (var i = 0; i < particles.length; i++) {
if (!particles[i].visible) {
if (timer > this.FlowSpeed && emitOne) {
//console.log(particles[i].life);
if (!particles[i].visible) {
particles[i].visible = true;
emitOne = false;
timer = 0;
}
else {
continue; //perf
}
} else {
continue; //perf
}
}
particles[i].life += context.deltaTime;
particles[i].progress += speedInPerc * context.deltaTime / 10;
if (particles[i].progress > 0.95) { //reset
// console.log(particles[i].getPosition().z);
beScript.emitParticle(particles[i], 0, startingScales[i]);
} else {
particles[i].setPosition(this.actor.getValue(particles[i].progress));
particles[i].rotate(rotateSpeedVect);
// // make smaller
// if (this.SpeedOfCrush > 0) {
// var newScale = particles[i].getScale() - context.deltaTime * this.SpeedOfCrush / 100000;
// particles[i].setScale(newScale);
// //particles[i].RigidBody.createSphere(this.particlesize * newScale);
// }
//split in half
if (this.TimeToSplit > 0 && particles[i].life > this.TimeToSplit) {
var r1 = beScript.getFreeParticle();
var r2 = beScript.getFreeParticle();
if (r1 == null) {
continue;
}
if (r2 == null) {
r1.visible = false;
continue;
}
console.log('split');
var newScale = particles[i].getScale() + Math.random() * 0.3;
beScript.emitParticle(r1, particles[i].progress, newScale);
r1.delay = 0;
r1.visible = true;
beScript.emitParticle(r2, particles[i].progress + speedInPerc * 5, 1 - newScale);
r2.delay = 0;
r2.visible = true;
beScript.emitParticle(particles[i], 0, startingScales[i]);
}
}
}
};
beScript.getFreeParticle = function () {
for (var i = 0; i < particles.length; i++) {
if (!particles[i].visible) {
particles[i].visible = true;
return particles[i];
}
}
return null;
}
beScript.emitParticle = function (particle, progress, scale) {
particle.setScale(scale);
particle.progress = progress;
particle.life = 0;
particle.visible = false;
}
