简体   繁体   English

如何在 Three.js 工作区中围绕随机 3d 对象/网格/3d 线的轴旋转网格组?

[英]How do I rotate a mesh group about the axis of a random 3d object/mesh/3d line within the Three.js workspace?

I'm trying to learn more about three.js so I can perform testing on a simple mechanism.我正在尝试了解更多有关three.js 的信息,以便我可以对一个简单的机制进行测试。 Ultimately the desired output would be board tilt (front view) to axle orientation (plan view) for different designs.对于不同的设计,最终所需的输出将是板倾斜(前视图)到轴方向(平面图)。 (XY plot), but that's obviously step five or six.. I'm stuck on step one, trying to understand how to obtain an axis of rotation, represented by a small diameter cylinder. (XY 图),但这显然是第五步或第六步……我被困在第一步,试图了解如何获得由小直径圆柱体表示的旋转轴。

Obviously this photo represents a fail.显然这张照片代表了失败。 总成旋转失败

  • I'm hoping to utilize the existing matrix tooling within Three.js to perform data charting / system analysis.我希望利用 Three.js 中现有的矩阵工具来执行数据图表/系统分析。
  • Its not clear on what the worldMatrix object within a mesh object really represents.不清楚网格对象中的 worldMatrix 对象真正代表什么。 I would think that contains location, orientation and scaling factors, but it not clear on how to extract that info for translation / 3d rotations of other components in the world space.我认为它包含位置、方向和缩放因子,但不清楚如何提取该信息以用于世界空间中其他组件的平移/3d 旋转。
  • I'm guessing I'm better off making a copy of original component locations and matrix values, then working off of the copies.我猜我最好制作原始组件位置和矩阵值的副本,然后处理副本。 When you tilt left and back again, you don't end up in the original place.当你向左和向后倾斜时,你不会回到原来的地方。

The model represents a simple skateboard.该模型代表一个简单的滑板。 I want to rotate the front truck hanger (which contains an axle and two wheels) about a tilted axis, according to a user selected slider element.我想根据用户选择的滑块元素围绕倾斜轴旋转前卡车吊架(包含一个轴和两个轮子)。

Sandbox here... 沙箱在这里...

 var euler;
 const rotateMeshGroup =( meshGroup, rotationAxis, angleOfRotation) =>{
     euler = new THREE.Euler().setFromRotationMatrix(rotationAxis.matrixWorld);
     meshGroup.children.forEach(item => {
         item.rotateOnAxis(euler, THREE.MathUtils.degToRad(angleOfRotation));
     }) 
 }

I don't understand how one creates an axis of rotation.我不明白人们如何创建旋转轴。 The 3D angle I understand, but what about distance from the axis?我理解的 3D 角度,但是与轴的距离呢? Is that a matrix cross product calculation (shortest distance to a 3D axis, with distance perpendicular to the axis? )那是矩阵叉积计算吗(到 3D 轴的最短距离,距离垂直于轴?)

edit: Still trying to figure this one out.编辑:仍在试图弄清楚这一点。 Been wading thru lots of console.logs and I don't see anything in the canned matrices that would work.一直在通过大量的 console.logs 涉水,我在罐头矩阵中看不到任何有用的东西。 I've converted the pivot axis from a small diameter cylinder mesh object into a simple line defined by two vector endpoints.我已将枢轴从小直径圆柱体网格对象转换为由两个矢量端点定义的简单线。 I'm now thinking delta-x, delta-y, delta-z calculations (translate, rotation, translate back) within RotateMeshGroup function.我现在正在考虑在 RotateMeshGroup 函数中进行 delta-x、delta-y、delta-z 计算(平移、旋转、平移回来)。

Any tips and hints here?这里有什么提示和提示吗?

So, yeah, this was a work in progress.所以,是的,这是一项正在进行的工作。 And I've now gone far beyond that.而我现在已经远远不止于此。 For me this was a learning exercise in using Three.js for engineering related analysis.对我来说,这是使用 Three.js 进行工程相关分析的学习练习。 I'd like to share some tips I've learned.我想分享一些我学到的技巧。

  1. I found this posting with answer from "Stranger in the Q" The code is of a rotating Solar System with Sun, Earth (with revolving moon and rocks) and Mars (with two other planets.) I went through this example very carefully, understanding, really understanding all that was going on.我发现这篇文章的答案来自“Q 中的陌生人”代码是一个旋转的太阳系,有太阳、地球(有旋转的月亮和岩石)和火星(有另外两个行星)。我非常仔细地阅读了这个例子,理解,真正了解正在发生的一切。 The program made clear how rotations are propagated through to children (Moon rotates around the Earth, while the Earth rotates around the Sun, etc...) The content was managed by a simple object external to the ThreeJS scene.该程序清楚地说明了旋转是如何传播给孩子的(月球绕地球旋转,而地球绕太阳旋转等等......)内容由 ThreeJS 场景外部的一个简单对象管理。 The program uses some recursive functions to manage actions by children elements in the animation functioning.该程序使用一些递归函数来管理动画功能中子元素的动作。 There are some updates required, eg the constructor for THREE.Object3d no longer takes names.需要进行一些更新,例如,THREE.Object3d 的构造函数不再采用名称。 The corrective action was to simple add a name value after object creation.纠正措施是在对象创建后简单地添加一个名称值。 When you name everything accurately, its much easier to understand the entire child hierarchy.当您准确地命名所有内容时,更容易理解整个子层次结构。 Again this example was instrumental in helping me to understand some of this stuff.这个例子再次帮助我理解其中的一些东西。

  2. The other thing I found was a posting with an answer from BadAskTechie The response was simple, and directly to the point.我发现的另一件事是一个来自 BadAskTechie 的回答的帖子回复很简单,而且直截了当

     //rotates a mesh's geometry about a specified axis and pivot //the axis is a normalized Vector3 const rotateAbout = (mesh, axis, axisPosition, angle) => { mesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(mesh.position.x-axisPosition.x, mesh.position.y-axisPosition.y, mesh.position.z-axisPosition.z)); //translate geometry to axis location mesh.geometry.applyMatrix(new THREE.Matrix4().makeRotationAxis(axis, angle)); //rotate geometry about axis mesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(axisPosition.x-mesh.position.x, axisPosition.y-mesh.position.y, axisPosition.z-mesh.position.z)); //translate geometry back to original location }
  3. One tip for complicated nested 3d meshes.复杂嵌套 3d 网格的一个技巧。 Do all the geometry work, including component part orientation / translation / rotation, in the THREE.abcGeometry object stage.在 THREE.abcGeometry 对象阶段完成所有几何工作,包括部件的方向/平移/旋转。 Don't create the meshs and then try to orient objects in the scene space.不要创建网格,然后尝试在场景空间中定位对象。 When you do the translations and rotations in geometry, everything lines up perfectly when it comes time for animation.当您在几何体中进行平移和旋转时,当需要制作动画时,一切都会完美排列。 Until I tried that nothing was making sense.直到我试过才觉得没有任何意义。 The axis directions all seemed mixed up.轴方向似乎都被混淆了。

  4. There was one technique I found useful.我发现一种技术很有用。 I wanted to know where my elements were located in 3d space during rotation/animation functions.我想知道在旋转/动画功能期间我的元素在 3d 空间中的位置。 I could not figure out how to do that on 3d mesh objects.我不知道如何在 3d 网格对象上做到这一点。 There are too many vertices to be useful.有太多的顶点没有用。 Instead I created a simple line object, and maneuver that along with the rest of the assembly during rotations.相反,我创建了一个简单的线对象,并在旋转过程中与组件的其余部分一起操作。 After an interim move, I could query the line by checking its two vertices, create a new 3d vector.normalize() and utilize that in future calculations.在临时移动之后,我可以通过检查它的两个顶点来查询该线,创建一个新的 3d vector.normalize() 并在以后的计算中使用它。 This seemed to work pretty well.这似乎工作得很好。

  5. The question I've asked above is really superseded by lots of progress on the whole skateboard truck model problem.我在上面提出的问题确实被整个滑板卡车模型问题的大量进展所取代。 Some of the code in the sandbox above is terrible.上面沙箱中的一些代码很糟糕。 Please don't even waste time looking at it.请不要浪费时间看它。 Instead, I think what I'll do is open source the completed project, and list the github repo link and demo here, so others can get a glimpse on to my progress on same.相反,我认为我要做的是将已完成的项目开源,并在此处列出 github repo 链接和演示,以便其他人可以一瞥我的进度。

Perhaps that would be of use to others learning ThreeJS.也许这对其他人学习 ThreeJS 有用。

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

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