I'm trying to write a shader to colorize a custom geometry in SceneKit. I want to set the colors so that horizontal surfaces that are facing up are white, horizontal surfaces that are facing downward are black, and all other surfaces in between are shades of gray based on their orientation. I used the material's surface entry point to call the following shader
vec4 normal = vec4(_surface.normal, 1.0);
// Assume value is [-1, 1], scale to [0, 1]
float color = normal.z * 0.5 + 0.5;
_surface.diffuse = vec4(color, color, color, 1.0);
It appears that the normal.z
is relative to the camera (or view). I assume I need to transform the value, so that it's in another space. I tried multiple transforms (and combinations of transforms) such as u_inverseViewTransform
, but the results all seem to be in view space. Does anyone know how to colorize a geometry based on the orientation of its surfaces?