如何使用SceneKit和SCNGeometryPrimitiveTypePoint

时间:2016-07-08 00:38:11

标签: ios scenekit point-clouds

我正在编写一个iOS应用程序,它使用自定义几何体在SceneKit中渲染一个pointcloud。 This post让我在那里非常有帮助(虽然我把它翻译成了Objective-C),就像DavidRönnqvist的书中使用SceneKit的3D图形一样(参见自定义几何图章章节)。代码工作得很好,但是我想让点数渲染得更大 - 目前点数非常小。

根据OpenGL文档,您可以通过调用glPointSize()来完成此操作。根据我的理解,SceneKit是建立在OpenGL之上的,所以我希望有一种方法来访问这个函数或者使用SceneKit做同等的事情。任何建议将不胜感激!

我的代码如下。我还在bitbucket access here上发布了一个小示例应用程序。

// set the number of points
NSUInteger numPoints = 10000;

// set the max distance points
int randomPosUL = 2;
int scaleFactor = 10000; // because I want decimal points
                         // but am getting random values using arc4random_uniform

PointcloudVertex pointcloudVertices[numPoints];

for (NSUInteger i = 0; i < numPoints; i++) {

    PointcloudVertex vertex;

    float x = (float)(arc4random_uniform(randomPosUL * 2 * scaleFactor));
    float y = (float)(arc4random_uniform(randomPosUL * 2 * scaleFactor));
    float z = (float)(arc4random_uniform(randomPosUL * 2 * scaleFactor));

    vertex.x = (x - randomPosUL * scaleFactor) / scaleFactor;
    vertex.y = (y - randomPosUL * scaleFactor) / scaleFactor;
    vertex.z = (z - randomPosUL * scaleFactor) / scaleFactor;

    vertex.r = arc4random_uniform(255) / 255.0;
    vertex.g = arc4random_uniform(255) / 255.0;
    vertex.b = arc4random_uniform(255) / 255.0;

    pointcloudVertices[i] = vertex;

    //        NSLog(@"adding vertex #%lu with position - x: %.3f y: %.3f z: %.3f | color - r:%.3f g: %.3f b: %.3f",
    //              (long unsigned)i,
    //              vertex.x,
    //              vertex.y,
    //              vertex.z,
    //              vertex.r,
    //              vertex.g,
    //              vertex.b);
}

// convert array to point cloud data (position and color)
NSData *pointcloudData = [NSData dataWithBytes:&pointcloudVertices length:sizeof(pointcloudVertices)];

// create vertex source
SCNGeometrySource *vertexSource = [SCNGeometrySource geometrySourceWithData:pointcloudData
                                                                   semantic:SCNGeometrySourceSemanticVertex
                                                                vectorCount:numPoints
                                                            floatComponents:YES
                                                        componentsPerVector:3
                                                          bytesPerComponent:sizeof(float)
                                                                 dataOffset:0
                                                                 dataStride:sizeof(PointcloudVertex)];

// create color source
SCNGeometrySource *colorSource = [SCNGeometrySource geometrySourceWithData:pointcloudData
                                                                  semantic:SCNGeometrySourceSemanticColor
                                                               vectorCount:numPoints
                                                           floatComponents:YES
                                                       componentsPerVector:3
                                                         bytesPerComponent:sizeof(float)
                                                                dataOffset:sizeof(float) * 3
                                                                dataStride:sizeof(PointcloudVertex)];

// create element
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:nil
                                                            primitiveType:SCNGeometryPrimitiveTypePoint
                                                           primitiveCount:numPoints
                                                            bytesPerIndex:sizeof(int)];

// create geometry
SCNGeometry *pointcloudGeometry = [SCNGeometry geometryWithSources:@[ vertexSource, colorSource ] elements:@[ element]];

// add pointcloud to scene
SCNNode *pointcloudNode = [SCNNode nodeWithGeometry:pointcloudGeometry];
[self.myView.scene.rootNode addChildNode:pointcloudNode];

1 个答案:

答案 0 :(得分:1)

我正在寻找自己在ios中渲染点云,并通过“vade”在twitter上找到解决方案,并想到我在这里将其发布给其他人:

ProTip:SceneKit着色器修改器非常有用:

mat.shaderModifiers = @{SCNShaderModifierEntryPointGeometry : @"gl_PointSize = 16.0;"};