基于变换重新计算 SCNNode 几何 [英] Recalculate SCNNode geometry based on transformation

查看:20
本文介绍了基于变换重新计算 SCNNode 几何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过重新计算几何来巩固 SCNNode 的比例和方向?

Is there a way to solidify a SCNNode scale and orientation by recalculating its geometry?

基本上,我是从 scn 文件中加载 SCNNode,该 scn 文件是从导出到 DAE 文件的 Sketchup 文件转换而来的.因为 ARKit 以米为单位工作并且具有不同的轴方向,所以我必须设置加载的 SCNNode 的比例(到 0.0254)和欧拉角(x -90 度)以正确显示它.这一切都很好,但是因此缩放和旋转会弄乱一些逻辑,因为这个逻辑也使用旋转,从而覆盖了前一个...... :(

Basically I am loading a SCNNode out of a scn file that was converted from a sketchup file that was exported to a DAE file. Because ARKit works in meters and with a different axis orientation I have to set the loaded SCNNode’s scale (to 0.0254) and eulerangle (x -90deg) to correctly show it. This all works fine however thus scaling and rotation messes up some logic down the line because this logic also uses rotation thus overriding the previous one... :(

我认为,如果我能简单地告诉 SCNNode 根据其当前比例、方向...(基本上是它的变换矩阵)重新计算其几何形状,这将导致 SCNNode 的变换矩阵为零矩阵,那就太好了...

I think it would be great if I could simply tell the SCNNode to recalculate its geometry based on its current scale, orientation... (basically its transformation matrix) which would result in an SCNNode with transformation matrix the zero matrix....

推荐答案

如果您只是"想告诉节点永久缩放其顶点,则必须编写一个函数来执行此操作,因为没有标准选项.

If you "simply" want to tell the node to scale its vertices permanently, you will have to write a function to do so as there is no standard option.

该函数应该将节点几何的顶点源读入一个向量数组,然后使用 GLKMatrix4MultiplyAndProjectVector3 将变换应用于每个向量,然后以新的顶点作为源创建一个新的 SCNGeometry.

That function should read the vertex source of the node’s geometry into an array of vectors, then use GLKMatrix4MultiplyAndProjectVector3 to apply the transformation to each vector, and then create a new SCNGeometry with the new verts as a source.

GLKMatrix4 scalemat = GLKMatrix4MakeScale(aNode.scale.x, aNode.scale.y, aNode.scale.z);

for (HEVertex* vert in toBeTransformedVerts) {
     vert.pos = SCNVector3FromGLKVector3(GLKMatrix4MultiplyAndProjectVector3(scalemat, SCNVector3ToGLKVector3(vert.pos)) );
}

//reset node scale property.
aNode.scale = SCNVector3Make(1.0, 1.0, 1.0);

HEVertex 是我用来存储顶点的类,pos 属性是 SCNVector3.在您的情况下,您必须将 SCNNode 的 .geometry 的顶点源读入 SCNVector3 数组,然后循环遍历它们.

HEVertex is a class I use to store vertices, the pos property is a SCNVector3. In your case you would have to read the vertex source of the .geometry of the SCNNode into an array of SCNVector3 and loop through those instead.

变换每个顶点的位置后,需要更新节点的几何体.IE.像这样:

After transforming the position of each vertex, you need to update the node's geometry. I.e. something like this:

SCNGeometrySource *_vertexSource =
        [SCNGeometrySource geometrySourceWithVertices:_meshVertices count:_vertexCount];

aNode.geometry = [SCNGeometry geometryWithSources:@[_vertexSource, _aNode.geometry.geometrySources[1], _aNode.geometry.geometrySources[2]] elements:@[_aNode.geometry.geometryElements.firstObject]];

这只是一个粗略的例子,它只更新顶点源,并重用法线和颜色几何源以及几何元素,每个模型的差异很大.

That is just a rough example, it updates only the vertex source, and reuses the normal and color geometry sources and the geometryElement, which can differ highly per model.

非常可行,但不像重新导出具有适当大小的模型那么简单.

Very doable, but not nearly as simple as re-exporting the model with the appropriate size.

这篇关于基于变换重新计算 SCNNode 几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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