[英]Can't Set Local Origin For OBJMTLLoader Object in Scene
I am creating a scene using Three.js where I can move objects around. 我正在使用Three.js创建一个场景,可以在其中移动对象。 I used the OBJMTLLoader to load in the objects.
我使用OBJMTLLoader加载对象。 However, these objects don't have a local origin, they still store their original origins.
但是,这些对象没有本地原点,它们仍存储其原始原点。 When I go to move an object, the object first moves back to its original origin, and then you can move the object from there.
当我去移动一个对象时,该对象首先移回其原始位置,然后可以从那里移动该对象。 I have found lots of examples of using centroids and bounding boxes, but I always get that something is undefined.
我发现了很多使用质心和边界框的示例,但是我总是得到一些未定义的信息。 Any help would be greatly appreciated!!
任何帮助将不胜感激!!
loader.load('example.obj', 'example.mtl', function (object) {
object.position.y = -50; // I want this to be the new origin of the object
object.scale.set(scale, scale, scale);
});
And then my code to move the object: 然后我的代码移动对象:
function onDocumentMouseMove(event) {
event.preventDefault();
mouse.x = ( (event.clientX - container.offsetLeft) / container.clientWidth ) * 2 - 1;
mouse.y = -( (event.clientY - container.offsetTop ) / container.clientHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
if (SELECTED) {
var intersects = raycaster.intersectObject(plane);
SELECTED.position.copy(intersects[0].point.sub(offset));
return;
}
var intersects = raycaster.intersectObjects(objects);
if (intersects.length > 0) {
if (INTERSECTED != intersects[0].object) {
INTERSECTED = intersects[0].object;
plane.position.copy(INTERSECTED.position);
plane.lookAt(camera.position);
updateLightPosition();
}
container.style.cursor = 'pointer';
} else {
INTERSECTED = null;
container.style.cursor = 'auto';
}
}
function onDocumentMouseDown(event) {
event.preventDefault();
var vector = new THREE.Vector3(mouse.x, mouse.y, 1).unproject(camera);
raycaster.set(camera.position, vector.sub(camera.position).normalize());
if (selectingTargetPos) {
var intersects = raycaster.intersectObjects(background);
if (intersects.length > 0) {
selectedObject.light.target = new THREE.Object3D();
selectedObject.light.target.position.copy(intersects[0].point);
selectedObject.light.target.updateMatrixWorld();
selectingTargetPos = false;
container.style.cursor = 'auto';
}
} else if (selectingTargetObj) {
var intersects = raycaster.intersectObjects(objects);
if (intersects.length > 0) {
selectedObject.light.target = intersects[0].object;
selectedObject.light.target.updateMatrixWorld();
selectingTargetObj = false;
container.style.cursor = 'auto';
}
} else {
var intersects = raycaster.intersectObjects(objects, true);
if (intersects.length > 0) {
SELECTED = intersects[0].object.userData.rootObject;
selectedObject = SELECTED;
var intersects = raycaster.intersectObject(plane);
offset.copy(intersects[0].point).sub(plane.position);
container.style.cursor = 'move';
}
}
}
function onDocumentMouseUp(event) {
event.preventDefault();
SELECTED = null;
container.style.cursor = 'auto';
}
Because the object position is based off of the plane and the objects are grouped, the problem lies in the INTERSECTED
block of the onDocumentMouseMove()
event. 因为对象位置基于平面,并且对象已分组,所以问题出在
onDocumentMouseMove()
事件的INTERSECTED
块中。
This does not get triggered because the raycaster is not recursively checking the objects array. 这不会被触发,因为光线投射器没有递归检查对象数组。 By changing the
intersectObjects()
function to have a second parameter of true, the problem will be solved. 通过将
intersectObjects()
函数更改为第二个参数true,可以解决该问题。
var intersects = raycaster.intersectObjects(objects, true);
if (intersects.length > 0) {
if (INTERSECTED != intersects[0].object) {
INTERSECTED = intersects[0].object;
plane.position.copy(INTERSECTED.position);
plane.lookAt(camera.position);
updateLightPosition();
}
container.style.cursor = 'pointer';
} else {
INTERSECTED = null;
container.style.cursor = 'auto';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.