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

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

问题描述

是否可以通过重新计算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)和eulerangle(x -90deg)设置为正确显示.一切都很好,但是缩放和旋转使线下的逻辑混乱了,因为该逻辑也使用旋转,从而覆盖了前一个逻辑...:(

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]];

这只是一个粗略的示例,它仅更新顶点源,并重复使用普通和彩色几何源以及geometryElement,这在每个模型中可能会有很大差异.

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天全站免登陆