shadermetal

How to convert float4 color to half3 in Metal surface shader


In a Metal surface shader (for a custom material), the color = params.geometry().color() call returns a float4 type. I guess that color is the interpolated color for the fragment in the triangle.

I'd like to set that color to params.surface().set_base_color(color), however set_base_color expects a half3 parameter.

How to convert from float4 to half3, and how to combine the values ? (well, I only 1.0 alfa but still...).


Solution

  • See Metal Shader Language Specification, section 2.2.1 Accessing Vector Components.

    You can use .xyz (or .zyw or any other combination) on a float4 to turn it into float3

    Then, when you have the right number of components in a floatN vector, you must explicitly convert it to halfN.

    So what you want to do is half3(float4value.xyz)