2014-09-03 51 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之間,這將沒有真正的幫助我不認爲,可能是錯誤的使用價值。

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

回答