gpt4 book ai didi

collada - 从场景中提取顶点

转载 作者:行者123 更新时间:2023-12-03 02:55:27 24 4
gpt4 key购买 nike

我在理解场景包几何时遇到问题。

我有 Blender 中的默认立方体,我导出为 collada (DAE),并且可以将其带入 scenekit...一切都很好。

现在我想查看立方体的顶点。在 DAE 中,我可以看到“Cube-mesh-positions-array”的以下内容,

“1 1 -1 1 -1 -1 -1 -0.9999998 -1 -0.9999997 1 -1 1 0.9999995 1 0.9999994 -1.000001 1 -1 -0.9999997 1 -1 1 1”

现在我想在 scenekit 中做的是使用如下内容取回顶点:

SCNGeometrySource *vertexBuffer = [[cubeNode.geometry geometrySourcesForSemantic:SCNGeometrySourceSemanticVertex] objectAtIndex:0];

如果我处理 vertexBuffer(我尝试了多种查看数据的方法),它似乎不正确。

有人可以解释一下“SCNGeometrySourceSemanticVertex”给了我什么,以及如何正确提取顶点数据吗?我希望看到的是:

X = "float"
Y = "float"
Z = "float"

我还在研究以下类/方法,它们看起来很有希望(这里有一些很好的数据值),但是来自 gmpe 的数据显示为空,有人能够解释“SCNGeometryElement”的数据属性包含什么吗?

SCNGeometryElement *gmpe = [theCurrentNode.geometry geometryElementAtIndex:0];

谢谢,非常感谢您的帮助,

D

最佳答案

几何源

当您调用geometrySourcesForSemantic:时,您将返回一个SCNGeometrySource对象数组,在您的情况下具有给定的语义(顶点数据的源)。

此数据可以通过多种不同的方式进行编码,并且多个源可以使用具有不同步幅偏移的相同数据。源本身有很多属性,让您能够解码数据,例如

  • dataStride
  • 数据偏移
  • 向量计数
  • componentsPerVector
  • bytesPerComponent

您可以使用这些组合来确定要读取数据的哪些部分并从中创建顶点。

解码

步长告诉您应该步进多少字节才能到达下一个向量,偏移量告诉您在到达该向量的相关数据部分之前应该从该向量的开头偏移多少字节。您应该为每个向量读取的字节数为 ComponentsPerVector * bytesPerComponent

读取单个几何源的所有顶点的代码如下所示

// Get the vertex sources
NSArray *vertexSources = [geometry geometrySourcesForSemantic:SCNGeometrySourceSemanticVertex];

// Get the first source
SCNGeometrySource *vertexSource = vertexSources[0]; // TODO: Parse all the sources

NSInteger stride = vertexSource.dataStride; // in bytes
NSInteger offset = vertexSource.dataOffset; // in bytes

NSInteger componentsPerVector = vertexSource.componentsPerVector;
NSInteger bytesPerVector = componentsPerVector * vertexSource.bytesPerComponent;
NSInteger vectorCount = vertexSource.vectorCount;

SCNVector3 vertices[vectorCount]; // A new array for vertices

// for each vector, read the bytes
for (NSInteger i=0; i<vectorCount; i++) {

// Assuming that bytes per component is 4 (a float)
// If it was 8 then it would be a double (aka CGFloat)
float vectorData[componentsPerVector];

// The range of bytes for this vector
NSRange byteRange = NSMakeRange(i*stride + offset, // Start at current stride + offset
bytesPerVector); // and read the lenght of one vector

// Read into the vector data buffer
[vertexSource.data getBytes:&vectorData range:byteRange];

// At this point you can read the data from the float array
float x = vectorData[0];
float y = vectorData[1];
float z = vectorData[2];

// ... Maybe even save it as an SCNVector3 for later use ...
vertices[i] = SCNVector3Make(x, y, z);

// ... or just log it
NSLog(@"x:%f, y:%f, z:%f", x, y, z);
}

几何元素

这将为您提供所有顶点,但不会告诉您如何使用它们来构造几何图形。为此,您需要管理顶点索引的几何元素。

您可以从geometryElementCount属性获取一个几何图形的几何元素数量。然后您可以使用geometryElementAtIndex:获取不同的元素。

该元素可以告诉您顶点是否使用单个三角形或三角形带。它还告诉您每个索引的字节数(索引可能是 intshort ,这是解码其数据所必需的。

关于collada - 从场景中提取顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17250501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com