I'm following this tutorial: http://blog.bignerdranch.com/754-scenekit-in-mountain-lion/
I'm interested in using Scene Kit, but my scenes might potentially have thousands of spheres. To stress-test Scene Kit I tried this:
SCNSphere *sphere = [SCNSphere sphereWithRadius:0.5];
for (int i=0; i<10; i++) {
for(int j=0; j<10; j++){
for(int k=0; k<10; k++){
SCNNode *myNode = [SCNNode nodeWithGeometry:sphere];
myNode.position = SCNVector3Make(i,j,k);
[root addChildNode:myNode];
}
}
}
This works fine for, say, 1000 spheres (10^3) but fails (perhaps unsurprisingly) for 1,000,000 spheres (100^3). I don't mind not being able to use a million spheres, but I'd like to work out what the sensible upper bound is (5,000? 15,000?) and how to increase it.
What can I do to mitigate this? e.g. I've tried sphere.segmentCount = 3 and while that speeds up rendering, it doesn't have much effect on memory usage, which I suspect is the limiting factor.
Also, there doesn't seem to be a SCNPoint class. I was thinking about switching to just displaying a point when the number of spheres is too high, but I can't see from the SceneKit documentation how to display a simple point -- the simplest I can see is a triangle.
Any help is much appreciated.
Edit: @toyos suggested that the SCNSphere objects are merged into single SCNGeometry object (provided they don't need to be independently animated, which they don't), but I can't find an easy way to do this.
SCNGeometry is created by using [SCNGeometry geometryWithSources:(* NSArray)sources geometryWithElements:(* NSArray) elements]
as documented here, but I'm not clear as to how to create an SCNGeometry
object from my spheres.
e.g. for a single sphere, I could see using sphere.geometryElementCount
to get the number of elements and then using that to populate an array using [sphere geometryElementAtIndex:(NSInteger)elementIndex]
which would give me the elements, but I'm not sure how to get the "sources" (or what they even are). The method to get the geometry sources is [sphere geometrySourcesForSemantic:(NSString*) semantic]
, but what is this semantic string? (is it meant to be "normals" or "vertices" or is this something else? the documentation quite helpfully says the semantic is "The semantic value of the geometry source." without saying what possible values of the semantic are)
That's just for a single sphere, which would be fairly pointless (since SCNSphere
is just a subclass of SCNGeometry
anyway), so now I have to add multiple spheres. So would I have to manually translate the vertices of the sphere when adding them to my SCNGeometry
object?
I'm just trying to figure out the most sensible way to do this.