我正在使用轨迹球控件处理一个 Three.js 场景,其中我以编程方式移动摄像机,我注意到如果我将camera.position
和controls.target
设置为相同的值,控件将完全冻结,用户无法再缩放/平移等。
// Create the scene and a camera to view it
var scene = new THREE.Scene();
/**
* Camera
**/
// Specify the portion of the scene visiable at any time (in degrees)
var fieldOfView = 75;
// Specify the camera's aspect ratio
var aspectRatio = window.innerWidth / window.innerHeight;
// Specify the near and far clipping planes. Only objects
// between those planes will be rendered in the scene
// (these values help control the number of items rendered
// at any given time)
var nearPlane = 0.1;
var farPlane = 1000;
// Use the values specified above to create a camera
var camera = new THREE.PerspectiveCamera(
fieldOfView, aspectRatio, nearPlane, farPlane
);
// Finally, set the camera's position in the z-dimension
camera.position.z = 5;
/**
* Renderer
**/
// Create the canvas with a renderer
var renderer = new THREE.WebGLRenderer();
// Specify the size of the canvas
renderer.setSize( window.innerWidth, window.innerHeight );
// Add the canvas to the DOM
document.body.appendChild( renderer.domElement );
/**
* Controls
**/
var controls = new THREE.TrackballControls(camera, renderer.domElement);
setTimeout(function() {
controls.target = new THREE.Vector3(1, 1, 1)
camera.position.set(1, 1, 1)
alert('freeze!')
}, 2500)
/**
* Cube
**/
// Create a cube with width, height, and depth set to 1
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
// Use a simple material with a specified hex color
var material = new THREE.MeshBasicMaterial({ color: 0xffff00 });
// Combine the geometry and material into a mesh
var cube = new THREE.Mesh( geometry, material );
// Add the mesh to our scene
scene.add( cube );
/**
* Render!
**/
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
controls.update();
}
animate();
* {
margin: 0;
padding: 0;
background: black;
*
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/88/three.min.js"></script>
<script src="https://s3.amazonaws.com/duhaime/blog/tsne-webgl/assets/js/trackball-controls.js"></script>
为了解决这个问题,我可以将camera.position
和controls.target
设置为略有不同的值,但我想了解为什么当这些值设置相同时控件会冻结。
有谁知道在这种情况下可能导致控件冻结的原因?如果其他人对这个问题有任何建议,我将不胜感激。
如果您查看THREE.TrackballControls()
的源代码,例如在这里,您会注意到内部变量_eye
将具有坐标[0, 0, 0]
,并且当您将相机和控件的目标位置设置为相同的坐标时,其长度将0
。
这导致所有带有_eye
的交叉乘积都将产生长度为零的[0, 0, 0]
向量。实际上,所有使用此变量的计算(cross()
、.crossVectors()
、.setFromAxisAndAngle()
)的结果都是零向量、零长度或零四元数。因此,没有平移,没有缩放,没有旋转。
附言如果我错了,也许更大的专家会添加更多信息甚至纠正我)