added ability to rotate entity and sound direction

This commit is contained in:
Philip Rosedale 2016-12-21 13:13:59 -08:00
parent 52e418481e
commit cf3a260646

View file

@ -8,6 +8,9 @@
// userData.range should be an integer for the max distance away from the entity where the sound will be audible.
// userData.volume is the max volume at which the clip should play. Defaults to 1.0 full volume)
//
// The rotation of the entity is copied to the ambisonic field, so by rotating the entity you will rotate the
// direction in-which a certain sound comes from.
//
// Remember that the entity has to be visible to the user for the sound to play at all, so make sure the entity is
// large enough to be loaded at the range you set, particularly for large ranges.
//
@ -27,6 +30,7 @@
var range = DEFAULT_RANGE;
var maxVolume = DEFAULT_VOLUME;
var UPDATE_INTERVAL_MSECS = 100;
var rotation;
var entity;
var ambientSound;
@ -92,23 +96,28 @@
this.maybeUpdate = function() {
// Every UPDATE_INTERVAL_MSECS, update the volume of the ambient sound based on distance from my avatar
_this.updateSettings();
var props = Entities.getEntityProperties(entity);
var HYSTERESIS_FRACTION = 0.1;
var props = Entities.getEntityProperties(entity, [ "position" ]);
var props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
center = props.position;
rotation = props.rotation;
var distance = Vec3.length(Vec3.subtract(MyAvatar.position, center));
if (distance <= range) {
var volume = (1.0 - distance / range) * maxVolume;
if (!soundPlaying && ambientSound.downloaded) {
soundPlaying = Audio.playSound(ambientSound, { loop: true, localOnly: true, volume: volume });
soundPlaying = Audio.playSound(ambientSound, { loop: true,
localOnly: true,
rotation: rotation,
volume: volume });
debugPrint("Starting ambient sound, volume: " + volume);
if (WANT_COLOR_CHANGE) {
Entities.editEntity(entity, { color: COLOR_ON });
}
} else if (soundPlaying && soundPlaying.playing) {
soundPlaying.setOptions( { volume: volume } );
debugPrint("Setting volume and rotation: " + Quat.safeEulerAngles(rotation).y);
soundPlaying.setOptions( { volume: volume, orientation: rotation } );
}
} else if (soundPlaying && soundPlaying.playing && (distance > range * HYSTERESIS_FRACTION)) {
soundPlaying.stop();
soundPlaying = false;