Reputation: 358
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/[email protected]/manual/Screen-Position-Node.html
I would be really thankful for any hints. Attached is a little visualization.
Upvotes: 2
Views: 178
Reputation: 31782
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;
Upvotes: 4