2014-09-03 34 views
1

我有一个全景播放器,3D声音我试图在three.js所实施并试图遵循这一http://www.html5rocks.com/en/tutorials/webaudio/positional_audio/网络音频与three.js所定位的3D声音

摄像头是固定的,并且3D声音放置在场景周围,我希望如果相机面对场景,音量将受到影响。

的声音被如此产生(其中,位置是一个的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); 
} 

这种运作良好。

在渲染循环

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); 

我打电话这一点,用相机目标

function updateSceneSounds(target) 
{ 

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

     console.log(distance); 

    } 
} 

的距离现身在数400-500之间,这将没有真正的帮助我不认为,可能是错误的使用价值。

任何线索或想法是最好的方式去做这件事?

回答