SurFace材质的编写(卡通渲染)

Shader "Custom/cartoon"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,50)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
        _BumpMap("BumpMap",2D)="bump"{}//法线
        _BumpScale("_BumpScale",float)=1
        _ColorTint("ColorTint",Color)=(1,1,1,1)
        _RimColor("RimColor",Color)=(1,0,0,1)
        _RimPower("RimPower",Range(0.1,8.0))=1
        _Steps("Steps",Range(1,30))=1
        _ToonEffect("ToonEffect",Range(0,1))=0.5
        _Outline("Outline",Range(0,2))=0.5
        _OutlineColor("OutlineColor",COLOR)=(0,0,0,0)
        _XRayPower("XRayPower",Range(0.0001,3))=0.5
        _XRayColor("XRayColor",COLOR)=(1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
        //X-RAY
        Pass{
            blend SrcAlpha one
            ZWrite off
            ZTest Greater
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            float _XRayPower;
            fixed4 _XRayColor;
            struct v2f
            {
                float4 vertex:SV_POSITION;
                float3 worldPos:TEXCOORD0;
                float3 worldNormal:TEXCOORD1;
            };
            v2f vert(appdata_base v)
            {
                v2f o;
                o.vertex=UnityObjectToClipPos(v.vertex);
                o.worldNormal=normalize(UnityObjectToWorldNormal(v.normal));
                o.worldPos=mul(unity_ObjectToWorld,v.vertex);
                return o;
            };
            fixed4 frag(v2f i):SV_TARGET
            {   
                float3 ViewDir=UnityWorldSpaceViewDir(i.worldPos);
                float rim=1- dot( normalize(i.worldNormal),normalize(ViewDir));
                return rim*_XRayPower*_XRayColor;

                
            }
            ENDCG
        
        }

        //描边
        Pass{
        Name "Outline"
        Cull Front
        CGPROGRAM
       #pragma vertex vert
	    #pragma fragment frag
		#include "UnityCG.cginc"

        float _Outline;
        fixed4 _OutlineColor;
        struct v2f
        {
            float4 vertex:SV_POSITION;
        };
        v2f vert(appdata_base v)
        {
            v2f o;
            o.vertex=UnityObjectToClipPos(v.vertex);
            float3 normal=normalize(mul((float3x3)UNITY_MATRIX_IT_MV,v.normal));//模型空间转换到视角空间 
            float2 viewNormal=TransformViewToProjection(normal.xy);
            o.vertex.xy+=viewNormal*_Outline;
            return o;
        }
        float4 frag(v2f i):SV_TARGET
        {
            return _OutlineColor; 
        }

        ENDCG
        
        }
        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Toon fullforwardshadows finalcolor:final
 
        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0
 
        sampler2D _MainTex;
        sampler2D _BumpMap;
 
        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpMap;
            float3 viewDir; 
        };
        float _BumpScale;
        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        fixed4 _ColorTint;
        fixed4 _RimColor;
        float _RimPower;
        float _Steps;
        float _ToonEffect;
 
        // 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)
        
        half4 LightingToon(SurfaceOutput s ,half lightDir,half3 viewDir,half atten)
        {
            float difLight=dot(lightDir,s.Normal)*0.5+0.5;
            difLight=smoothstep(0,1,difLight);
            float toon=floor(difLight*_Steps)/_Steps;
            difLight=lerp(difLight,toon,_ToonEffect);
            fixed3 diffuse=_LightColor0*s.Albedo*difLight;
            fixed3 halfdir=normalize(lightDir+viewDir);
            fixed3 specular=_LightColor0.rgb*_Color.rgb*pow(max(0,dot(s.Normal,halfdir)),s.Gloss);
            return fixed4(diffuse+specular,1);

        }
        void surf (Input IN, inout SurfaceOutput o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Specular = _Metallic;
            o.Gloss = _Glossiness;
            o.Alpha = c.a;
           
            float3 normal=UnpackNormal(tex2D(_BumpMap,IN.uv_BumpMap));
            normal.xy*=_BumpScale;
            o.Normal=normal;
            half rim=1-dot(normalize(IN.viewDir),o.Normal);
            o.Emission=_RimColor*pow(rim,1/_RimPower);
           
        }
        void final(Input IN, SurfaceOutput  o,inout fixed4  color )
        {
            color*=_ColorTint;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值