From 45fe6a438c9aadf70377e13d5ccc7d2006ee315c Mon Sep 17 00:00:00 2001 From: bwent Date: Fri, 14 Aug 2015 16:18:07 -0700 Subject: [PATCH 1/2] Example particle scripts --- examples/fireworks.js | 251 ++++++++++++++++++++++++++++++++++++++ examples/particleDance.js | 154 +++++++++++++++++++++++ 2 files changed, 405 insertions(+) create mode 100644 examples/fireworks.js create mode 100644 examples/particleDance.js diff --git a/examples/fireworks.js b/examples/fireworks.js new file mode 100644 index 0000000000..b0a8591fa8 --- /dev/null +++ b/examples/fireworks.js @@ -0,0 +1,251 @@ +HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; +var fireSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/GUN-SHOT2.raw"); +var audioOptions = { + volume: 0.9, + position: Vec3.sum(Camera.getPosition(), Quat.getFront(Camera.getOrientation())) +}; + +var DISTANCE_FROM_CAMERA = 7.0; + +var bluePalette = [{ + red: 0, + green: 206, + blue: 209 +}, { + red: 173, + green: 216, + blue: 230 +}, { + red: 0, + green: 191, + blue: 255 +}]; + +var greenPalette = [{ + red: 152, + green: 251, + blue: 152 +}, { + red: 127, + green: 255, + blue: 0 +}, { + red: 50, + green: 205, + blue: 50 +}]; + +var redPalette = [{ + red: 255, + green: 20, + blue: 147 +}, { + red: 255, + green: 69, + blue: 0 +}, { + red: 255, + green: 90, + blue: 120 +}]; + + +var COLOR_RED = {red: 255, green: 0, blue: 0 }; +var COLOR_GREEN = {red: 0, green: 255, blue: 0}; +var COLOR_BLUE = {red: 0, green: 0, blue: 255}; +var iconsX = 700; +var iconsY = 660; +var ICON_SIZE = 30; + +var redIcon = Overlays.addOverlay("text", { + backgroundColor: COLOR_RED, + x: iconsX, + y: iconsY, + width: ICON_SIZE, + height: ICON_SIZE, + alpha: 0.0, + backgroundAlpha: 1.0, + visible: true + }); +var greenIcon = Overlays.addOverlay("text", { + backgroundColor: COLOR_GREEN, + x: iconsX + 50, + y: iconsY, + width: ICON_SIZE, + height: ICON_SIZE, + alpha: 0.0, + backgroundAlpha: 1.0, + visible: true + }); +var blueIcon = Overlays.addOverlay("text", { + backgroundColor: COLOR_BLUE, + x: iconsX + 100, + y: iconsY, + width: ICON_SIZE, + height: ICON_SIZE, + alpha: 0.0, + backgroundAlpha: 1.0, + visible: true + }); + + +var NUM_BURSTS = 11; +var SPEED = 6.0; + +var rockets = []; +Rocket = function(point, colorPalette) { + //default to blue palette if no palette passed in + this.colors = colorPalette; + this.point = point; + this.bursts = []; + this.burst = false; + + this.emitRate = randInt(80, 120); + this.emitStrength = randInt(5.0, 7.0); + + this.rocket = Entities.addEntity({ + type: "Sphere", + position: this.point, + dimensions: { + x: 0.07, + y: 0.07, + z: 0.07 + }, + color: { + red: 240, + green: 240, + blue: 240 + } + }); + + this.animationSettings = JSON.stringify({ + fps: 40, + frameIndex: 0, + running: true, + firstFrame: 0, + lastFrame: 20, + loop: false + }); + + this.direction = { + x: randFloat(-0.4, 0.4), + y: 1.0, + z: 0.0 + } + + this.time = 0.0; + this.timeout = randInt(15, 40); +}; + +Rocket.prototype.update = function(deltaTime) { + this.time++; + + Entities.editEntity(this.rocket, { + velocity: Vec3.multiply(SPEED, this.direction) + }); + var position = Entities.getEntityProperties(this.rocket).position; + + if (this.time > this.timeout) { + this.explode(position); + return; + } +}; + + +Rocket.prototype.explode = function(position) { + Audio.playSound(fireSound, audioOptions); + Entities.editEntity(this.rocket, { + velocity: { + x: 0, + y: 0, + z: 0 + } + }); + + var colorIndex = 0; + for (var i = 0; i < NUM_BURSTS; ++i) { + var color = this.colors[colorIndex]; + print(JSON.stringify(color)); + this.bursts.push(Entities.addEntity({ + type: "ParticleEffect", + animationSettings: this.animationSettings, + position: position, + textures: 'https://raw.githubusercontent.com/ericrius1/SantasLair/santa/assets/smokeparticle.png', + emitRate: this.emitRate, + emitStrength: this.emitStrength, + emitDirection: { + x: Math.pow(-1, i) * randFloat(0.0, 1.4), + y: 1.0, + z: 0.0 + }, + color: color, + lifespan: 1.0, + visible: true, + locked: false + })); + + if (colorIndex < this.colors.length - 1) { + colorIndex++; + } + } + + this.burst = true; + Entities.deleteEntity(this.rocket); +}; + +//var lastLoudness; +var LOUDNESS_RADIUS_RATIO = 10; + +function update(deltaTime) { + for (var i = 0; i < rockets.length; i++) { + if (!rockets[i].burst) { + rockets[i].update(); + } + } +} + +function randFloat(min, max) { + return Math.random() * (max - min) + min; +} + +function randInt(min, max) { + return Math.floor(Math.random() * (max - min)) + min; +} + +function computeWorldPoint(event) { + var pickRay = Camera.computePickRay(event.x, event.y); + var addVector = Vec3.multiply(Vec3.normalize(pickRay.direction), DISTANCE_FROM_CAMERA); + return Vec3.sum(Camera.getPosition(), addVector); +} + +function mousePressEvent(event) { + var clickedOverlay = Overlays.getOverlayAtPoint({ + x: event.x, + y: event.y + }); + if(clickedOverlay === redIcon) { + rockets.push(new Rocket(computeWorldPoint(event), redPalette)); + } else if (clickedOverlay === greenIcon) { + rockets.push(new Rocket(computeWorldPoint(event), greenPalette)); + } else if (clickedOverlay === blueIcon) { + rockets.push(new Rocket(computeWorldPoint(event), bluePalette)); + } + +} + +function cleanup() { + Overlays.deleteOverlay(redIcon); + Overlays.deleteOverlay(greenIcon); + Overlays.deleteOverlay(blueIcon); + for (var i = 0; i < rockets.length; ++i) { + Entities.deleteEntity(rockets[i].rocket); + for (var j = 0; j < NUM_BURSTS; ++j) { + Entities.deleteEntity(rockets[i].bursts[j]); + } + + } +} + +Script.update.connect(update); +Script.scriptEnding.connect(cleanup); +Controller.mousePressEvent.connect(mousePressEvent); \ No newline at end of file diff --git a/examples/particleDance.js b/examples/particleDance.js new file mode 100644 index 0000000000..d79b0ed17c --- /dev/null +++ b/examples/particleDance.js @@ -0,0 +1,154 @@ +(function() { + var NUM_BURSTS = 3; + var NUM_EMITTERS_PER_BURST = 11; + + var RANGE = 5.0; + var AUDIO_RANGE = 0.5 * RANGE; + var DIST_BETWEEN_BURSTS = 1.0; + + var LOUDNESS_RADIUS_RATIO = 10; + + var TEXTURE_PATH = 'https://raw.githubusercontent.com/ericrius1/SantasLair/santa/assets/smokeparticle.png'; + var cameraAxis = Quat.getFront(Camera.getOrientation()); + var center = Vec3.sum(Camera.getPosition(), Vec3.multiply(RANGE, cameraAxis)); + var audioPosition = Vec3.sum(Camera.getPosition(), Vec3.multiply(AUDIO_RANGE, cameraAxis)); + + var song = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/eric/sounds/songs/Made%20In%20Heights%20-%20Forgiveness.wav"); + var audioOptions = { + volume: 0.9, position: audioPosition + }; + + var DISTANCE_FROM_CAMERA = 7.0; + + var colorPalette = [{ + red: 0, + green: 206, + blue: 209 + }, { + red: 173, + green: 216, + blue: 230 + }, { + red: 0, + green: 191, + blue: 255 + }]; + + var bursts = []; + var audioStats; + + Burst = function(point) { + if (!audioStats) { + audioStats = Audio.playSound(song, audioOptions); + } + + this.point = point; + this.emitters = []; + + this.emitRate = randInt(80, 120); + this.emitStrength = randInt(4.0, 6.0); + + this.animationSettings = JSON.stringify({ + fps: 10, + frameIndex: 0, + running: true, + firstFrame: 0, + lastFrame: 50, + loop: true + }); + + this.direction = { + x: randFloat(-0.3, 0.3), + y: 1.0, + z: 0.0 + } + + this.base = Entities.addEntity({ + type: "Sphere", + position: this.point, + dimensions: { + x: 0.05, + y: 0.05, + z: 0.05 + }, + color: { + red: 240, + green: 240, + blue: 240 + } + }); + for (var i = 0; i < NUM_EMITTERS_PER_BURST; ++i) { + var colorIndex = randInt(0, colorPalette.length - 1); + var color = colorPalette[colorIndex]; + this.emitters.push(Entities.addEntity({ + type: "ParticleEffect", + animationSettings: this.animationSettings, + position: this.point, + textures: TEXTURE_PATH, + emitRate: this.emitRate, + emitStrength: this.emitStrength, + emitDirection: { + x: Math.pow(-1, i) * randFloat(0.0, 0.4), + y: 1.0, + z: 0.0 + }, + color: color, + lifespan: 1.0, + visible: true, + locked: false + })); + } + + }; + + var nextPosition = center; + var posOrNeg = -1; + + for (var i = 0; i < NUM_BURSTS; ++i) { + posOrNeg *= -1; + bursts.push(new Burst(nextPosition)); + var offset = { + x: RANGE/(i+2) * posOrNeg, + y: 0, + z: 0 + }; + var nextPosition = Vec3.sum(nextPosition, offset); + } + + function update(deltaTime) { + for (var i = 0; i < NUM_BURSTS; i++) { + if (audioStats && audioStats.loudness > 0.0) { + for (var j = 0; j < NUM_EMITTERS_PER_BURST; ++j) { + Entities.editEntity(bursts[i].emitters[j], { + particleRadius: audioStats.loudness / LOUDNESS_RADIUS_RATIO + }); + } + } + } + } + + function randFloat(min, max) { + return Math.random() * (max - min) + min; + } + + function randInt(min, max) { + return Math.floor(Math.random() * (max - min)) + min; + } + + this.cleanup = function() { + for (var i = 0; i < NUM_BURSTS; ++i) { + Entities.deleteEntity(bursts[i].base); + for (var j = 0; j < NUM_EMITTERS_PER_BURST; ++j) { + var emitter = bursts[i].emitters[j]; + Entities.deleteEntity(emitter); + } + + } + Audio.stop(); + } + + Script.update.connect(update); + + })(); + + Script.scriptEnding.connect(cleanup); \ No newline at end of file From 889d9c54a562231ebd2a2db45f27700b649bdc2d Mon Sep 17 00:00:00 2001 From: bwent Date: Fri, 14 Aug 2015 16:24:11 -0700 Subject: [PATCH 2/2] add newline at end of file --- examples/fireworks.js | 2 +- examples/particleDance.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/fireworks.js b/examples/fireworks.js index b0a8591fa8..621931ee36 100644 --- a/examples/fireworks.js +++ b/examples/fireworks.js @@ -248,4 +248,4 @@ function cleanup() { Script.update.connect(update); Script.scriptEnding.connect(cleanup); -Controller.mousePressEvent.connect(mousePressEvent); \ No newline at end of file +Controller.mousePressEvent.connect(mousePressEvent); diff --git a/examples/particleDance.js b/examples/particleDance.js index d79b0ed17c..e920fcfa41 100644 --- a/examples/particleDance.js +++ b/examples/particleDance.js @@ -151,4 +151,5 @@ })(); - Script.scriptEnding.connect(cleanup); \ No newline at end of file + Script.scriptEnding.connect(cleanup); + \ No newline at end of file