Unity Shader (五)Surface Shader示例

本文介绍了一种使用 Unity Shader 实现材质颜色渐变的方法。通过定义两个颜色参数 _MainColor 和 _SecondColor,结合顶点位置信息,实现从一种颜色到另一种颜色的平滑过渡效果。该 Shader 支持金属度和光滑度调节。

1、替换颜色

Shader "Custom/Example_Frag_5" {
    Properties {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _MainColor("MainColor",color)=(1,1,1,1)
        _SecondColor("SecondColor",color)=(1,1,1,1)
        _Center("Center",range(-2.7,2.7))=0
        _R("R",range(0,0.5))=0.2 //颜色渐变的半径
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
        
        CGPROGRAM
    
        #pragma surface surf Standard vertex:vert fullforwardshadows

        #pragma target 3.0

        sampler2D _MainTex;
        half _Glossiness;
        half _Metallic;
        float4 _MainColor;
        float4 _SecondColor;
        float _R;
        float _Center;


        struct Input {
            float2 uv_MainTex;
            float x;
        };

        void vert(inout appdata_full v,out Input o)
        {
            o.uv_MainTex=v.texcoord.xy;
            o.x=v.vertex.x;
        }



        void surf (Input IN, inout SurfaceOutputStandard o) {
            
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;

            float d=IN.x-_Center;
            float s=abs(d);
            d=d/s;
            float f=s/_R;
            f=saturate(f);
            d*=f;
            d=d/2+0.5;
            o.Albedo*=lerp(_MainColor,_SecondColor,d)*2;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

转载于:https://www.cnblogs.com/Jason-c/p/8582238.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值