2

I'm fairly new to Shader Development and currently working on a SCNProgram to replace the rendering of a plane geometry.

Within the programs vertex shader I'd like to access the position (or basically anchor position) point of the node/mesh as a clip space coordinate. Is there an easy way to accomplish that, maybe through the supplied Node Buffer? I got kinda close with:

xCoordinate = scn_node.modelViewProjectionTransform[3].x / povZPosition
yCoordinate = scn_node.modelViewProjectionTransform[3].y / povZPosition

The pov z position is being injected from outside through a custom buffer. This breaks though, when the POV is facing the scene at an angle.

I figured that I could probably just calculate the node position by myself via:

renderer.projectPoint(markerNode.presentation.worldPosition)

and then passing that through my shader via »program.handleBinding(ofBufferNamed: …« on every frame. I hope there is a better way though.

While digging through Google the Unity equivalent would probably be: https://docs.unity3d.com/Packages/com.unity.shadergraph@6.9/manual/Screen-Position-Node.html

I would be really thankful for any hints. Attached is a little visualization.

Access Nodes Clip Position

arthurschiller
  • 251
  • 2
  • 9

1 Answers1

3

If I'm reading you correctly, it sounds like you actually want the NDC position of the center of the node. This differs subtly from the clip-space position, but both are computable in the vertex shader as:

float4 clipSpaceNodeCenter = scn_node.modelViewProjectionTransform[3];
float2 ndcNodeCenter = clipSpaceNodeCenter.xy / clipSpaceNodeCenter.w;
warrenm
  • 28,472
  • 4
  • 71
  • 102
  • Thanks a lot Warren – that works perfectly! Also appreciate the clarification regarding NDC. It's sometimes a little hard to find good Metal learning resources. – arthurschiller Feb 08 '20 at 23:38