Hello, I have a simple problem. I have a sorted array of a million 2D points. I would like to use SceneKit Point Cloud Rendering to display the points that leverages GPU cycles for a great user experience. Can i get some help on how this can be done? I have a sample program I have below for rendering a small array of points and also a sample sphere but none of the points actually render. Only the sphere renders. I worked with Apple support to resolve this to no avail. Any help is greatly appreciated!
#import “ViewController.h”
typedef struct {
float x, y, z; // position
float r, g, b; // color
} MyVertex;
typedef struct {
float x, y, z; // position
} MyPoint;
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// SCNScene *scene = [SCNScene sceneNamed:@“scene.scnassets/scene.scn”];
SCNScene *scene = [SCNScene scene];
if(scene == nil) {
return;
}
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.position = SCNVector3Make(0, 0, 10);
[scene.rootNode addChildNode:cameraNode];
SCNNode *lightNode = [SCNNode node];
lightNode.light = [SCNLight light];
lightNode.light.type = SCNLightTypeOmni;
lightNode.position = SCNVector3Make(0, 3, 3);
[scene.rootNode addChildNode:lightNode];
// test sphere
SCNNode *sphere = [SCNNode node];
sphere.geometry = [SCNSphere sphereWithRadius:0.3];
sphere.position = SCNVector3Make(1, -2, -10);
[scene.rootNode addChildNode:sphere];
// test point cloud
// vertex data
MyVertex vertices[5] = {
{-0.0378297, 0.12794, 0, 1.0, 0.0, 0.0},
{-0.0447794, 0.128887, 0, 1.0, 0.0, 0.0},
{-0.0680095, 0.151244, 0, 1.0, 0.0, 0.0},
{-0.00228741, 0.13015, 0, 1.0, 0.0, 0.0},
{-0.0226054, 0.126675, 0, 1.0, 0.0, 0.0}
};
MyPoint points[5] = {
{-0.0378297, 0.12794, 0},
{-0.0447794, 0.128887, 0},
{-0.0680095, 0.151244, 0},
{-0.00228741, 0.13015, 0},
{-0.0226054, 0.126675, 0}
};
NSData *vertexData = [NSData dataWithBytes:vertices length:sizeof(vertices)];
NSData *pointData = [NSData dataWithBytes:points length:sizeof(points)];
// SCNGeometrySource objects for vertex and color
SCNGeometrySource *vertexSource, *colorSource;
vertexSource = [SCNGeometrySource geometrySourceWithData:vertexData semantic:SCNGeometrySourceSemanticVertex vectorCount:5 floatComponents:YES componentsPerVector:3 bytesPerComponent:sizeof(float) dataOffset:offsetof(MyVertex, x) dataStride:sizeof(MyVertex)];
colorSource = [SCNGeometrySource geometrySourceWithData:vertexData semantic:SCNGeometrySourceSemanticColor vectorCount:5 floatComponents:YES componentsPerVector:3 bytesPerComponent:sizeof(float) dataOffset:offsetof(MyVertex, r) dataStride:sizeof(MyVertex)];
// SCNGeometryElement
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:pointData primitiveType:SCNGeometryPrimitiveTypePoint primitiveCount:5 bytesPerIndex:sizeof(float)];
element.pointSize = 1;
element.maximumPointScreenSpaceRadius = 3;
element.minimumPointScreenSpaceRadius = 1;
NSMutableArray<SCNGeometrySource *> *sources;
[sources addObject:vertexSource];
[sources addObject:colorSource];
NSMutableArray<SCNGeometryElement *> *elements;
[elements addObject:element];
SCNGeometry *geometry = [SCNGeometry geometryWithSources:sources elements:elements];
SCNNode *pcNode = [SCNNode nodeWithGeometry:geometry];
pcNode.position = SCNVector3Make(1, 2, 0);
[scene.rootNode addChildNode:pcNode];
sceneView.allowsCameraControl = TRUE;
sceneView.scene = scene;
}
-
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}