Merge pull request #7104 from howard-stearns/update-orbitingSound

update-orbitingSound.js
This commit is contained in:
Brad Hefta-Gaub 2016-02-12 17:44:47 -08:00
commit 48fe638312

View file

@ -17,7 +17,7 @@ var SPEED = 1.0;
var currentPosition = { x: 0, y: 0, z: 0 }; var currentPosition = { x: 0, y: 0, z: 0 };
var trailingLoudness = 0.0; var trailingLoudness = 0.0;
var soundClip = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Tabla+Loops/Tabla1.wav"); var soundClip = SoundCache.getSound("http://public.highfidelity.io/sounds/Rats_Squeaks_Active.wav");
var properties = { var properties = {
type: "Box", type: "Box",
@ -27,18 +27,26 @@ var properties = {
}; };
var objectId = Entities.addEntity(properties); var objectId = Entities.addEntity(properties);
var sound = Audio.playSound(soundClip, { position: orbitCenter, loop: true, volume: 0.5 }); var soundOptions = { position: orbitCenter, loop: true, volume: 0.5 };
var sound;
function update(deltaTime) { function update(deltaTime) {
if (!soundClip.downloaded) {
return;
}
if (!sound) {
sound = Audio.playSound(soundClip, soundOptions); // Not until downloaded
}
time += deltaTime; time += deltaTime;
currentPosition = { x: orbitCenter.x + Math.cos(time * SPEED) * RADIUS, y: orbitCenter.y, z: orbitCenter.z + Math.sin(time * SPEED) * RADIUS }; currentPosition = { x: orbitCenter.x + Math.cos(time * SPEED) * RADIUS, y: orbitCenter.y, z: orbitCenter.z + Math.sin(time * SPEED) * RADIUS };
trailingLoudness = 0.9 * trailingLoudness + 0.1 * sound.loudness; trailingLoudness = 0.9 * trailingLoudness + 0.1 * sound.loudness;
Entities.editEntity( objectId, { position: currentPosition, color: { red: Math.min(trailingLoudness * 2000, 255), green: 0, blue: 0 } } ); Entities.editEntity( objectId, { position: currentPosition, color: { red: Math.min(trailingLoudness * 2000, 255), green: 0, blue: 0 } } );
sound.options = { position: currentPosition }; soundOptions.position = currentPosition;
sound.setOptions(soundOptions);
} }
Script.scriptEnding.connect(function() { Script.scriptEnding.connect(function() {
Entities.deleteEntity(objectId); Entities.deleteEntity(objectId);
}); });
Script.update.connect(update); Script.update.connect(update);