SCN shader modifier in metal - pass uniform to sha

2019-05-17 19:30发布

问题:

I'm trying to use shaders modifiers with Metal. I cannot figure out to declare uniforms... So far my fragment modifier is:

// color changes
#pragma arguments
float4x4 u_color_transformation;

#pragma body
_output.color.rgb = vec3(1.0) - (u_color_transformation * _output.color).rgb;

This outputs a purple texture, with no log.. If I just have _output.color.rgb = (u_color_transformation * _output.color).rgb, things are ok. I think I'm following the doc but maybe not!

The uniform is set with:

material.shaderModifiers =
        [ SCNShaderModifierEntryPoint.fragment: theShaderAbove ]

material.setValue(NSValue(mat4: GLKMatrix4Identity), forKey: "u_color_transformation")

All this was working fine with openGL

Update:

Apparently, SceneKit shader modifiers use glsl code, and take care of converting it to metal. So I changed the shader to:

// color changes
#pragma arguments
uniform mat4 u_color_transformation;

#pragma body
_output.color.rgb = vec3(1.0) - (u_color_transformation * _output.color).rgb;

The output is now white (suggesting that at least it compiled), suggesting that u_color_transformation is not passed to the shader, and is resolved to a null matrix in the shader, which gives black which, once inverted, is white.

回答1:

SceneKit assumed you were using glsl because you had vec3(1.0) in your shader body. If you changed that to float3(1.0), it would have used metal and worked without the uniform keyword.