简体   繁体   English

如何使用鼠标旋转3D对象而不用three.js在屏幕外旋转它?

[英]How do I rotate a 3D object using a mouse without rotating it off screen with three.js?

I am making an application very similar to what's on this shutterfly customization page , except more concise. 除了更加简洁之外,我正在制作一个与该shutterfly自定义页面非常相似的应用程序。 I really like the preview, but I need guidance on how to rotate the object the way they do in their preview. 我真的很喜欢预览,但是我需要有关如何在预览中旋转对象的指导。 Currently, I'm using THREE.OrbitControls() , but my object rolls out of view and the camera isn't focused on the actual object. 当前,我正在使用THREE.OrbitControls() ,但是我的对象从视线中移出,并且相机未聚焦在实际对象上。 I tried focusing on the object, but I get an error. 我尝试着眼于对象,但出现错误。

Edited to add: My object is a .ply file so I'm using PLYLoader. 编辑添加:我的对象是一个.ply文件,所以我正在使用PLYLoader。

I found this other perfect example of what I want to do that uses mouse events: https://jsfiddle.net/n6u6asza/1735/ 我找到了另一个我想做的使用鼠标事件的完美示例: https : //jsfiddle.net/n6u6asza/1735/

It's clever, but I want to use three.js libraries instead of these mouse events because my object keeps disappearing when I remove the controls and if I can keep my code shorter, that'd be ideal. 这很聪明,但是我想使用three.js库而不是这些鼠标事件,因为删除控件时我的对象不断消失,而且如果我可以使代码更短,那将是理想的选择。 I've spent several hours reading about OrbitControls(), but nothing I've tried has kept the object focused so it seems I'm only rotating the object. 我已经花了几个小时阅读有关OrbitControls()的信息,但是我尝试过的所有操作都没有使对象聚焦,因此看来我只是在旋转对象。

The control code: 控制代码:

controls = new THREE.OrbitControls( camera, renderer.domElement);
 controls.minPolarAngle = toRadians(0); //was trying to limit the object 
 controls.maxPolarAngle = toRadians(45); //movement with these
controls.enablePan = false;
controls.enableZoom = false;
//controls.enabled = false; 
controls.update();

I also have a basic animate function. 我还具有基本的动画功能。 I want to later implement auto rotate as a button option. 我想稍后将自动旋转实现为按钮选项。

function animate() {
    requestAnimationFrame( animate );
    controls.update();
    renderer.render( scene, camera );
}

Just to recap, my question is: How do I keep focus on an object when rotating the camera? 概括地说,我的问题是:旋转相机时如何保持聚焦在某个物体上? OR How do I rotate the object without moving the camera? 或如何在不移动相机的情况下旋转对象?

Edited to add my object code: 编辑后添加我的目标代码:

var loader = new THREE.PLYLoader();
loader.load( 'sleeve.ply', function ( geometry ) {
     geometry.computeVertexNormals();
     var material = new THREE.MeshStandardMaterial( { color: 0xffffff } );
     sleeve = new THREE.Mesh( geometry, material );
     scene.add( sleeve );
});

To keep focus on an object you should use https://threejs.org/docs/#api/en/core/Object3D.lookAt 要专注于某个对象,您应该使用https://threejs.org/docs/#api/en/core/Object3D.lookAt

object.lookAt(x,y,z) or object.lookAt(vector);

For controls you should change the target https://threejs.org/docs/#examples/controls/OrbitControls.target 对于控件,您应该更改目标https://threejs.org/docs/#examples/controls/OrbitControls.target

controls.target = object;

Or you can modify transform controls, if you want to rotate object and not rotate camera around object. 或者,如果要旋转对象而不要围绕对象旋转摄像机,则可以修改变换控件。

https://threejs.org/examples/?q=control#misc_controls_transform https://threejs.org/examples/?q=control#misc_controls_transform

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

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