SMD01 SMD01 - 1 year ago 140
iOS Question

SceneKit nodesInsideFrustumWithPointOfView: unexpectedly returning empty

I am using the following method to determine which SCNNodes are visible by the camera.

[self.scnView nodesInsideFrustumWithPointOfView:cameraNode];

However the returned array is always empty.
I set up the scene up as follows:

scene = [SCNScene scene];

cameraNode = [SCNNode node]; = [SCNCamera camera];
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 0, 0);

[scene.rootNode addChildNode:cameraNode];

self.scnView.scene = scene;
self.scnView.showsStatistics = YES;
self.scnView.backgroundColor = [UIColor clearColor];

At a random time, after the scene is created, I add a SCNNode to the scene:

testnode = [Testnode createNode];
testnode.position = SCNVector3Make(0, 0, -10);
[self.scnView.scene.rootNode addChildNode:testnode];

On my device, the node "testnode" is visible on my screen yet nodesInsideFrustumWithPointOfView: returns nothing.

EDIT: I tried changing the point of view to a spot light object and test whether "testnode" is inside its frustum. Here is what I see on screen: Yet the array still returns empty. The testnode is the white cube.

Answer Source

Adding this for better visibility. Thank Crashalot for this.

Basically, if the SCNNode you are trying to detect using [scnView nodesInsideFrustumWithPointOfView:] and [scnView isNodeInsideFrustum: withPointOfView:] is the child of a node with empty geometry, it won't be detected.

In my case, I added a plane geometry and set the material to transparent:

SCNNode *emptynode = [SCNNode node];
node.geometry = [SCNPlane planeWithWidth:1.0f height:2.0f];
node.geometry.firstMaterial.transparency = 0.0f;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download