具有Three.js的Web音频定位3D声音

bee*_*eek 1 javascript html5-audio three.js

我有一个3D声音的全景播放器我试图在Three.js中实现并试图遵循这个http://www.html5rocks.com/en/tutorials/webaudio/positional_audio/

相机是固定的,3D声音被放置在场景周围,如果相机面向场景,我希望音量受到影响.

因此创建声音(其中position是vector3)

function playSound(buffer, looping, position, volume) {
    var sound = {};
    sound.source = context.createBufferSource();
    sound.volume = context.createGain();
    sound.source.connect(sound.volume);
    sound.volume.connect(mainVolume);

     var gainNode = context.createGain();
     gainNode.gain.value = volume;
     sound.source.connect(gainNode);


    sound.source.buffer = buffer;
     if(looping)
         sound.source.loop = true;
     sound.source.connect(context.destination);
     sound.source.start();

     sound.panner = context.createPanner();
     sound.position = position;

     //sound.panner.updateMatrixWorld();
     sound.volume.connect(sound.panner);
     sound.panner.connect(mainVolume);

     sceneSounds.push( sound );
 }
Run Code Online (Sandbox Code Playgroud)

这很好用.

在渲染循环上

   lat = Math.max(-85, Math.min(85, lat));
    phi = THREE.Math.degToRad(90 - lat);
    theta = THREE.Math.degToRad(lon);

    target.x = 512 * Math.sin(phi) * Math.cos(theta);
    target.y = 512 * Math.cos(phi);
    target.z = 512 * Math.sin(phi) * Math.sin(theta);

    camera.lookAt(target);

    if(sceneSounds.length > 0)
        updateSceneSounds( target );
Run Code Online (Sandbox Code Playgroud)

我用相机目标打电话给我

function updateSceneSounds( target )
{

    for(var s in sceneSounds){
        var sound = sceneSounds[s];
        distance = sound.position.distanceTo( target );

        console.log( distance );

    }
}
Run Code Online (Sandbox Code Playgroud)

距离是在400-500之间的数字出来,这对我没有想到的真正帮助,并且可能是错误的使用价值.

任何线索或想法是最好的方法吗?