在Three.js中应用矩阵不是我所期望的 [英] Applying a matrix in Three.js does not what I expect

查看:175
本文介绍了在Three.js中应用矩阵不是我所期望的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在从事的项目,我试图获得在浏览器中可见的建筑物的3D模型.在建筑物的所有元素中,我有顶点,索引和matrix3d.这些信息来自使用OpenGL显示脱机程序中的元素的应用程序.

For a project I am working on I am trying to get get a 3D-model of a building visible in a browser. Of all the elements of the building I have vertices, indices and a matrix3d. This information comes from an application that uses OpenGL to show the elements in a offline program.

现在,我正在尝试将这些元素添加到Three.js场景中.

Now I am trying to add these elements to my Three.js scene.

我可以将元素添加到由顶点和索引定义的场景中,我可以通过使用材质和灯光来看到它们,但是我无法旋转并将它们平移到正确的位置.例如,我添加一个像这样的元素:

I am at the point that I can add elements to the scene defined by the vertices and indices an I can see them by using materials and lights, but I can not rotate and translate them into the right place. For example I add an element like this:

var m242242255255 = new THREE.MeshPhongMaterial({color:0xf2f2ff, transparent:true, opacity:1, side:THREE.DoubleSide});
var geometry = new THREE.BufferGeometry();
geometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array([821,-15,2825.1,-821,-15,2825.1,-821,-39,2825.1,821,-39,2825.1,-821,-39,54,-821,-15,54,821,-15,54,821,-39,54,-875,-54,0,-821,-54,54,-821,-54,2825.1,821,-54,54,-875,-54,2879.1,821,-54,2825.1,875,-54,0,875,-54,2879.1,875,0,0,821,0,54,821,0,2825.1,-821,0,54,875,0,2879.1,-821,0,2825.1,-875,0,0,-875,0,2879.1]), 3));
geometry.setIndex(new THREE.BufferAttribute(new Uint16Array([8,9,10,9,8,11,12,10,13,10,12,8,11,14,13,14,11,8,13,15,12,15,13,14,16,17,18,17,16,19,20,18,21,18,20,16,19,22,21,22,19,16,21,23,20,23,21,22,8,22,16,16,14,8,14,16,20,20,15,14,15,20,23,23,12,15,12,23,22,22,8,12,13,18,17,17,11,13,11,17,19,19,9,11,9,19,21,21,10,9,10,21,18,18,13,10]), 1));

var mesh = new THREE.Mesh(geometry, m242242255255);
mesh.matrixAutoUpdate = false;
mesh.applyMatrix(new THREE.Matrix4().set(0,0,-1,0,  -0.42262,-0.90631,0,0,  -0.90631,0.42262,0,0,  64754.68,15569.13,-4647.5,1));
mesh.updateMatrix();
scene.add(mesh);

该元素出现在我的场景中,看起来好像已经旋转了,但是没有平移到正确的位置.

The element shows up in my scene and it looks like is rotated but it is not translated to its correct position.

我可以在将网格物体添加到场景之前添加平移,但是感觉好像没有必要.

I can add the translation before the adding of the mesh to the scene, but it feels like it should not be necessary.

mesh.applyMatrix(new THREE.Matrix4().makeTranslation(-64754.68, -15569.13, -4647.5));
mesh.updateMatrix();

看起来元素也沿着错误的轴旋转.沿x轴而不是z轴旋转.有人可以告诉我我做错了什么吗?我应该先更改矩阵才能在Three.js中使用它吗?

It also looks like the element is rotated along the wrong axis. It is rotated along the x-axis instead of the z-axis. Can someone tell me what it is I am doing wrong? Should I changed the matrix first to be able to use it in Three.js?

我刚刚发现我必须反转矩阵才能纠正旋转问题.所以我现在有:

I just found out that I had to invert my matrix to correct the rotation-problem. So I now have:

var geometry = new THREE.BufferGeometry();
geometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array([821,-15,2825.1,-821,-15,2825.1,-821,-39,2825.1,821,-39,2825.1,-821,-39,54,-821,-15,54,821,-15,54,821,-39,54,-875,-54,0,-821,-54,54,-821,-54,2825.1,821,-54,54,-875,-54,2879.1,821,-54,2825.1,875,-54,0,875,-54,2879.1,875,0,0,821,0,54,821,0,2825.1,-821,0,54,875,0,2879.1,-821,0,2825.1,-875,0,0,-875,0,2879.1]), 3));
geometry.setIndex(new THREE.BufferAttribute(new Uint16Array([8,9,10,9,8,11,12,10,13,10,12,8,11,14,13,14,11,8,13,15,12,15,13,14,16,17,18,17,16,19,20,18,21,18,20,16,19,22,21,22,19,16,21,23,20,23,21,22,8,22,16,16,14,8,14,16,20,20,15,14,15,20,23,23,12,15,12,23,22,22,8,12,13,18,17,17,11,13,11,17,19,19,9,11,9,19,21,21,10,9,10,21,18,18,13,10]), 1));

var mesh = new THREE.Mesh(geometry, m242242255255);
mesh.matrixAutoUpdate = false;
var matrix = new THREE.Matrix4();
matrix.set(0,0,-1,0,-0.42262,-0.90631,0,0,-0.90631,0.42262,0,0,64754.68,15569.13,-4647.5,1);
matrix.getInverse(matrix);
mesh.applyMatrix( matrix );
mesh.updateMatrix();
mesh.applyMatrix( new THREE.Matrix4().makeTranslation( 64754.68, 15569.13, -4647.5 ) );
mesh.updateMatrix();
scene.add(mesh);

但是使用矩阵进行翻译仍然存在问题.如何避免两次更新网格?

But I still have a problem with translating using the matrix. How can I avoid updating the mesh twice?

推荐答案

来自

请注意,在这种情况下,必须将matrixAutoUpdate设置为false,并且应确保 not 不能调用updateMatrix.调用updateMatrix将破坏对矩阵所做的手动更改,从而根据位置,比例等重新计算矩阵.

Note that matrixAutoUpdate must be set to false in this case, and you should make sure not to call updateMatrix. Calling updateMatrix will clobber the manual changes made to the matrix, recalculating the matrix from position, scale, and so on.

您会发现,调用mesh.updateMatrix()后,网格变换矩阵将不同于您设置的矩阵.您可以通过将matrix.elementsmesh.matrixWorld.elements进行比较来验证这一点,在删除updateMatrix之后,该结果将是相同的.

You'll find that after you call mesh.updateMatrix(), the mesh transformation matrix will be different than the one you set. You verify this by comparing matrix.elements to mesh.matrixWorld.elements, which will be the same after you remove updateMatrix.

这篇关于在Three.js中应用矩阵不是我所期望的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆