Improve performance of fireworks

This commit is contained in:
Ryan Huffman 2015-11-04 12:10:42 -08:00
parent d0d3aaa09c
commit bed7a1f5c3

View file

@ -2,24 +2,28 @@ Script.include("http://rawgit.com/huffman/hifi/baseball/examples/baseball/utils.
var emitters = []; var emitters = [];
var currentIdx = 0;
var smokeTrailEmitters = [];
var burstEmitters = [];
var smokeTrailSettings = { var smokeTrailSettings = {
"name":"ParticlesTest Emitter", "name":"ParticlesTest Emitter",
"type": "ParticleEffect", "type": "ParticleEffect",
"color":{"red":205,"green":84.41176470588235,"blue":84.41176470588235}, "color":{"red":205,"green":84.41176470588235,"blue":84.41176470588235},
"maxParticles":1000, "maxParticles":1000,
"velocity": { x: 0, y: 18.0, z: 0 }, "velocity": { x: 0, y: 18.0, z: 0 },
"lifetime": 20, //"lifetime": 20,
"lifespan":3, "lifespan":3,
"emitRate":1000, "emitRate":1000,
"emitSpeed":0.5, "emitSpeed":0.5,
"speedSpread":1, "speedSpread":0,
"emitOrientation":{"x":-0.2,"y":0,"z":0,"w":0.7000000000000001}, "emitOrientation":{"x":0,"y":0,"z":0,"w":1},
"emitDimensions":{"x":0,"y":0,"z":0}, "emitDimensions":{"x":0,"y":0,"z":0},
"emitRadiusStart":0.5, "emitRadiusStart":0.5,
"polarStart":1, "polarStart":1,
"polarFinish":1, "polarFinish":1,
"azimuthStart":-Math.PI, "azimuthStart":0,
"azimuthFinish":Math.PI, "azimuthFinish":0,
"emitAcceleration":{"x":0,"y":-0.70000001192092896,"z":0}, "emitAcceleration":{"x":0,"y":-0.70000001192092896,"z":0},
"accelerationSpread":{"x":0,"y":0,"z":0}, "accelerationSpread":{"x":0,"y":0,"z":0},
"particleRadius":0.03999999910593033, "particleRadius":0.03999999910593033,
@ -41,8 +45,8 @@ var fireworkSettings = {
"type": "ParticleEffect", "type": "ParticleEffect",
"color":{"red":205,"green":84.41176470588235,"blue":84.41176470588235}, "color":{"red":205,"green":84.41176470588235,"blue":84.41176470588235},
"maxParticles":1000, "maxParticles":1000,
"lifetime": 20, //"lifetime": 20,
"lifespan":6, "lifespan":4,
"emitRate":1000, "emitRate":1000,
"emitSpeed":1.5, "emitSpeed":1.5,
"speedSpread":1.0, "speedSpread":1.0,
@ -67,7 +71,6 @@ var fireworkSettings = {
"alphaStart":0, "alphaStart":0,
"alphaFinish":1, "alphaFinish":1,
"textures":"https://hifi-public.s3.amazonaws.com/alan/Particles/spark_2.png", "textures":"https://hifi-public.s3.amazonaws.com/alan/Particles/spark_2.png",
//"textures": "https://hifi-public.s3.amazonaws.com/alan/Particles/spark.png"
}; };
var popSounds = getSounds([ var popSounds = getSounds([
@ -84,48 +87,41 @@ var fireSounds = getSounds([
]) ])
function playRandomSound(sounds, options) { function playRandomSound(sounds, options) {
if (options === undefined) {
options = {
volume: 1.0,
position: MyAvatar.position,
}
}
Audio.playSound(sounds[randomInt(sounds.length)], options); Audio.playSound(sounds[randomInt(sounds.length)], options);
} }
function shootFirework(position, color, options) { function shootFirework(position, color, options) {
smokeTrailSettings.position = position; var updatedSmokeProperties = {
smokeTrailSettings.velocity = randomVec3(-5, 5, 10, 20, 10, 15); position: position,
smokeTrailSettings.gravity = randomVec3(-5, 5, -9.8, -9.8, 20, 40); velocity: randomVec3(-5, 5, 10, 20, 10, 15),
gravity: randomVec3(-5, 5, -9.8, -9.8, 20, 40),
emitRate: 100
};
var idx = currentIdx;
currentIdx = (currentIdx + 1) % MAX_SIMULTANEOUS_FIREWORKS;
playRandomSound(fireSounds, { position: {x: 0, y: 0 , z: 0}, volume: 3.0 }); playRandomSound(fireSounds, { position: {x: 0, y: 0 , z: 0}, volume: 3.0 });
var smokeID = Entities.addEntity(smokeTrailSettings); var smokeID = smokeTrailEmitters[idx];
emitters.push(smokeID); var burstID = burstEmitters[idx];
Script.scriptEnding.connect(function() { Entities.editEntity(smokeID, updatedSmokeProperties);
Entities.deleteEntity(smokeID);
});
Script.setTimeout(function() { Script.setTimeout(function() {
Entities.editEntity(smokeID, { emitRate: 0 }); Entities.editEntity(smokeID, { emitRate: 0 });
var position = Entities.getEntityProperties(smokeID, ['position']).position; var position = Entities.getEntityProperties(smokeID, ['position']).position;
Vec3.print("pos", position); var updatedBurstProperties = {
options.position = position; position: position,
options.colorStart = color; colorStart: color,
options.colorFinish = color; colorFinish: color,
var id = Entities.addEntity(options); emitRate: 1000
emitters.push(id); };
Entities.editEntity(burstID, updatedBurstProperties);
playRandomSound(popSounds, { position: {x: 0, y: 0 , z: 0}, volume: 3.0 }); playRandomSound(popSounds, { position: {x: 0, y: 0 , z: 0}, volume: 3.0 });
Script.setTimeout(function() { Script.setTimeout(function() {
Entities.editEntity(id, { emitRate: 0 }); Entities.editEntity(burstID, { emitRate: 0 });
Entities.deleteEntity(smokeID);
}, 500); }, 500);
}, 2000); }, 2000);
} }
Script.scriptEnding.connect(function() {
for (var i = 0; i < emitters.lengths; ++i) {
Entities.deleteEntity(emitters[i]);
}
});
playFireworkShow = function(numberOfFireworks, duration) { playFireworkShow = function(numberOfFireworks, duration) {
var position = { x: 0, y: 0, z: -78.0 }; var position = { x: 0, y: 0, z: -78.0 };
@ -141,4 +137,19 @@ playFireworkShow = function(numberOfFireworks, duration) {
} }
} }
//playFireworkShow(50, 10000); var MAX_SIMULTANEOUS_FIREWORKS = 50;
smokeTrailSettings.emitRate = 0;
fireworkSettings.emitRate = 0;
for (var i = 0; i < MAX_SIMULTANEOUS_FIREWORKS; ++i) {
smokeTrailEmitters.push(Entities.addEntity(smokeTrailSettings));
burstEmitters.push(Entities.addEntity(fireworkSettings));
}
Script.scriptEnding.connect(function() {
for (var i = 0; i < MAX_SIMULTANEOUS_FIREWORKS; ++i) {
Entities.deleteEntity(smokeTrailEmitters[i]);
Entities.deleteEntity(burstEmitters[i]);
}
});
//playFireworkShow(30, 2000);