简体   繁体   English

ThreeJS不使用setAnimationLoop渲染立方体

[英]ThreeJS not rendering Cube with setAnimationLoop

So I'm working on a ThreeJS WebVR Page. 因此,我正在研究ThreeJS WebVR页面。 (I'm quite new to three.js) (我对three.js相当陌生)

So I tired to make a basic scene to test some stuff. 所以我很疲倦地制作一个基本场景来测试一些东西。 But when I load the page with renderer.setAnimationLoop(render) I get my green cube for 1 Frame and then it disappears. 但是,当我用renderer.setAnimationLoop(render)加载页面时,我得到了1帧的绿色立方体,然后消失了。

(I got it working with requestAnimationFrame() but this will not work with WebVR) (我将其与requestAnimationFrame()一起使用,但这不适用于WebVR)

This is my code for my test sandbox: 这是我的测试沙箱代码:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 10);

var renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight); 
renderer.vr.enabled = true;

document.body.appendChild(renderer.domElement);

var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshBasicMaterial({
  color: 0x00ff00
});
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);

camera.position.z = 5;

function render() {
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;

  renderer.render(scene, camera);
}

function animate() {
  renderer.setAnimationLoop(render);
}

animate();

//Button vor start VR Session
document.body.appendChild(WEBVR.createButton(renderer));

Not the problem with setAnimationLoop The problem is you cannot set camera position when vr is enabled check this stack overflow question Unable to change camera position when using VRControls setAnimationLoop不是问题,问题是启用vr时无法设置摄像机位置,请检查此堆栈溢出问题。使用VRControls时无法更改摄像机位置

Check this CodePen Link where i fixed your code by changing position of cube instead of camera 检查此CodePen链接 ,我在其中通过更改立方体而不是相机的位置来固定代码

var cube = new THREE.Mesh(geometry, material);
cube.position.z = -5; //added this instead of camera.position.z = 5;

if you want to move the camera you need make it as child of another object and set the position to that object 如果要移动相机,则需要将其作为另一个对象的子对象并将其位置设置为该对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM