Three.js - 如何切换VR / StereoEffect模式

时间:2017-11-01 18:42:43

标签: three.js virtual-reality

初始场景很好。 VR模式有效。但我不确定如何恢复正常模式。

var scene = new THREE.Scene()
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 )
camera.position.set(70,0,70)
scene.add(camera)
var renderer = new THREE.WebGLRenderer( { alpha: true } )
renderer.setSize( window.innerWidth, window.innerHeight )
document.body.appendChild( renderer.domElement )
var controls = new THREE.OrbitControls( camera, renderer.domElement )
var effect = new THREE.StereoEffect(renderer)

var squares = []
var geometry = new THREE.BoxGeometry( .1, 3, 3 )
var radius = 50
var VR = false

for ( var lon = 1; lon < 20; lon ++ ) {
  for ( var lat = 1; lat < 20; lat ++ ) {

    var square = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { transparent: true, opacity: 0.9, color: 0xffffff } ) )
    squares.push ( square )
    scene.add( square )

    var x = radius * Math.sin( lon ) * Math.cos( lat )
    var y = radius * Math.cos( lon )
    var z = radius * Math.sin( lon ) * Math.sin( lat )

    square.position.x = x
    square.position.y = y
    square.position.z = z

  }
}

function animate() {
  requestAnimationFrame( animate )

  if ( VR ) {
    effect.render(scene, camera)
  } else {
    renderer.render(scene, camera)
  }

}

function toggleVR() {
  if ( VR ) {
    VR = false
  } else {
    VR = true
  }
}

animate()

这是我的代码:

https://codepen.io/megagumby/pen/RjWZPJ

点击按钮两次,你会看到我在说什么。提前谢谢。

1 个答案:

答案 0 :(得分:1)

THREE.StereoEffect会更改THREE.WebGLRenderer的大小。当你切换回来时,你必须按setSize重置渲染器的大小。

function toggleVR() {
    if ( VR ) {
        VR = false
        renderer.setSize( window.innerWidth, window.innerHeight )
    } else {
        VR = true
    }
}