我有一个问题,当我创建PointerLockControls对象时,我的three.js场景无法渲染。我完全不知道这个问题可能是什么。

main.js:

const THREE = require("three");
var PointerLockControls = require('three-pointerlock');
//utils
import detect from "./detect"
import dialogs from "./dialogs"
import calc from "./calc"
import {Player} from "./charackters"


//3D stuff
import world from "./world"

//Scene creation
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );

var controls = new PointerLockControls(camera);

camera.position.y = -50;
camera.position.z = 40;
camera.rotation.x = calc.rad(90);


//WebGL Renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor(0x000000);
document.body.appendChild( renderer.domElement );

world.drawFloor(scene, THREE);
var cGeo = new THREE.BoxGeometry(10,10,10);
var cTexture = new THREE.MeshNormalMaterial();
var Cube = new THREE.Mesh(cGeo, cTexture);
scene.add(Cube);


//tick function
var clock = new THREE.Clock(true);
function animate() {
    requestAnimationFrame(animate);
    controls.update(clock.getDelta);
    renderer.render( scene, camera );
}

if(detect.webgl()){
  animate();
}else{
  dialogs.error("Your browser does not support WebGL. Please install a modern Browser such as Google Chrome or Mozilla Firefox to play AlphaWars!", "Warning:")
}


如果您需要我的其他文件,请发表评论。
提前致谢。

最佳答案

也许您以错误的方式使用了PointerLockControls。您应该添加
    scene.add(controls.getObject());

    var controls = new PointerLockControls(camera);
因为PointerLockControls对象具有属性pitchObject,所以您应该将其添加到场景中。

10-04 21:24