下面再来说一下如何动态改变颜色。颜色的变化,文章开始的图片中的颜色是由点的坐标位置决定的。坐标 x 值决定颜色的 r 通道,坐标 y 值决定颜色的 g 通道。所以,在 Shader 中首先要知道方块的坐标,然后坐标 x,y 值后,就可以设定颜色。(先写一个 Shader,然后新建一个 Material,并且使用我们写的 Shader,然后将 Material 赋予 Cube 的 Prefab)
CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0
struct Input { // 这一句是我们自己写的 float3 worldPos; };
half _Glossiness; half _Metallic;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o) { // 这一句也是我们自己写的 o.Albedo.rg = IN.worldPos.xy * 0.5 + 0.5; // Metallic and smoothness come from slider variables o.Metallic = _Metallic; o.Smoothness = _Glossiness;