第三章 (2)创建卡通着色

                                                创建卡通着色 


 

这是有两种不同的方式实现的:

但都是利用 自定义 光照模型来做的

第一种方式:通过一张 阶梯图 来约束 光照  能实现 三种阶梯 效果

/// LightingToon 光照模型
/// 输入的第一个参数  是 surf 的返回值  SurfaceOutput
/// 第二个参数是  入射 光光线角度
/// 第三个参数为  光线 衰减率 0-1
half4 LightingToon( SurfaceOutput s, half3 lightDir, half atten)
{
	half NdotL = dot(s.Normal, lightDir);
	NdotL = tex2D(_RampTex, fixed2(NdotL,0.5));
	fixed4 c;
	c.rgb = s.Albedo * _LightColor0.rgb * NdotL * atten;
	c.a = s.Alpha;
	return c;
}

 

第二种方式: 通过代码来实现的

/// LightingToon2 光照模型
/// 输入的第一个参数  是 surf 的返回值  SurfaceOutput
/// 第二个参数是  入射 光光线角度
/// 第三个参数为  光线 衰减率 0-1
half4 LightingToon2(SurfaceOutput s, half3 lightDir, half atten)
{
	half NdotL = dot(s.Normal, lightDir); 
	half toon = (floor(NdotL * _ToonShadingLevels)) / (_ToonShadingLevels - 0.5);
	fixed4 c;
	c.rgb = s.Albedo * _LightColor0.rgb * toon * atten;
	c.a = s.Alpha;
	return c;
}

 

Shader "Custom/chapter03/ToonShader"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
		_RampTex("RampTex (RGB)", 2D) = "white" {}

		_ToonShadingLevels("_ToonShadingLevels",Range(2,10)) = 10
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types

		//两种 光照模型的 切换
		#pragma surface surf Toon
        //#pragma surface surf Toon2 

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
		sampler2D _RampTex;
		float _ToonShadingLevels;

        struct Input
        {
            float2 uv_MainTex;
        };


        // 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 SurfaceOutput o)
        { 
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
 
            o.Alpha = c.a;
        }

		/// LightingToon 光照模型
		/// 输入的第一个参数  是 surf 的返回值  SurfaceOutput
		/// 第二个参数是  入射 光光线角度
		/// 第三个参数为  光线 衰减率 0-1
		half4 LightingToon( SurfaceOutput s, half3 lightDir, half atten)
		{
			half NdotL = dot(s.Normal, lightDir);
			NdotL = tex2D(_RampTex, fixed2(NdotL,0.5));
			fixed4 c;
			c.rgb = s.Albedo * _LightColor0.rgb * NdotL * atten;
			c.a = s.Alpha;
			return c;
		}

		/// LightingToon2 光照模型
		/// 输入的第一个参数  是 surf 的返回值  SurfaceOutput
		/// 第二个参数是  入射 光光线角度
		/// 第三个参数为  光线 衰减率 0-1
		half4 LightingToon2(SurfaceOutput s, half3 lightDir, half atten)
		{
			half NdotL = dot(s.Normal, lightDir); 
			half toon = (floor(NdotL * _ToonShadingLevels)) / (_ToonShadingLevels - 0.5);
			fixed4 c;
			c.rgb = s.Albedo * _LightColor0.rgb * toon * atten;
			c.a = s.Alpha;
			return c;
		}

        ENDCG
    }
    FallBack "Diffuse"
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章