UnityShader--非真实感渲染(内置渲染管线)---15

卡通风格:

Shader "Unlit/34"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}
		_Color("Color",Color) = (1,1,1,1)
		_Ramp("RampTex",2D) = "white"{}
		_Outline("Outline",Range(0,1)) = 0
		_OutlineColor("OutlineColor",Color) = (1,1,1,1)
		_Specular("Specular",Color) = (1,1,1,1)
		_SpecularScale("SpecularScale",Range(0,1)) = 0
	}
	SubShader
	{
		Pass
		{
			NAME "Outline"

			Cull Front

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"	

			fixed4 _OutlineColor;
			fixed _Outline;

			struct v2f
			{
				float4 pos:SV_POSITION;
			};

			v2f vert(appdata_base v)
			{
				v2f o;

				float4 pos = mul(UNITY_MATRIX_MV,v.vertex);

				float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV,v.normal);

				normal.z = -0.5;

				pos = pos + float4(normalize(normal), 0) * _Outline;

				o.pos = mul(UNITY_MATRIX_P, pos);

				return o;
			}

			fixed4 frag(v2f i) :SV_Target
			{
				return fixed4(_OutlineColor.rgb,1);
			}

			ENDCG
		}

		Pass
		{
			Tags{"LightMode" = "ForwardBase"}

			Cull Back

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_fwdbase
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			#include "AutoLight.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _Ramp;
			fixed4 _Color;
			fixed4 _Specular;
			fixed4 _OutlineColor;
			fixed _Outline;
			fixed _SpecularScale;

			struct v2f
			{
				float4 pos:SV_POSITION;
				float2 uv:TEXCOORD0;
				float3 worldNormal:TEXCOORD1;
				float3 worldPos:TEXCOORD2;
				UNITY_SHADOW_COORDS(3)
			};

			v2f vert(appdata_base v)
			{
				v2f o;

				o.pos = UnityObjectToClipPos(v.vertex);

				o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				//o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);

				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

				TRANSFER_SHADOW(o);

				return o;
			}

			fixed4 frag(v2f i) :SV_Target
			{
				fixed3 worldNormal = normalize(i.worldNormal);
				fixed3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldPos));
				fixed3 worldView = normalize(UnityWorldSpaceViewDir(i.worldPos));
				fixed3 halfDir = normalize(worldView + worldLight);

				fixed3 albedo = tex2D(_MainTex,i.uv).rgb * _Color.rgb;

				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb * albedo;

				UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);

				float halfLambert = (dot(worldNormal, worldLight) * 0.5 + 0.5) * atten;

				fixed3 diffuse = _LightColor0.rgb * albedo * tex2D(_Ramp,float2(halfLambert,halfLambert)).rgb;

				fixed spec = dot(worldNormal,halfDir);

				//高光抗锯齿处理
				fixed w = fwidth(spec) * 2;					
				fixed t = smoothstep(-w,w,spec + _SpecularScale -1) * step(0.001,_SpecularScale);
				spec = lerp(0,1,t);

				fixed3 specular = _LightColor0.rgb * _Specular.rgb * spec;

				return fixed4(ambient + diffuse + specular,1);
			}

			ENDCG
		}
	}
	Fallback "Diffuse"
}

素描风格:

Shader "Unlit/35"
{
    Properties
    {
        _Color("Color",Color)=(1,1,1,1)
        _TileFactor("TileFactor",Float)=1
        _Outline("Outline",Range(0,1))=0
        _P1("P1",2D) = "white"{}
        _P2("P2",2D) = "white"{}
        _P3("P3",2D) = "white"{}
        _P4("P4",2D) = "white"{}
        _P5("P5",2D) = "white"{}
        _P6("P6",2D) = "white"{}
    }
        SubShader
    {
        Tags{"RenderType" = "Opaque" "Queue" = "Geometry"}

        UsePass "Unlit/34/Outline"

        Pass
        {
            Tags{"LightMode" = "ForwardBase"}

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            #include "AutoLight.cginc"

            fixed4 _Color;
            float _TileFactor;
            fixed _Outline;
            sampler2D _P1;
            sampler2D _P2;
            sampler2D _P3;
            sampler2D _P4;
            sampler2D _P5;
            sampler2D _P6;
            
            struct v2f
            {
               float4 pos:SV_POSITION;
               float2 uv:TEXCOORD0;
               float3 pWeight1:TEXCOORD1;
               float3 pWeight2:TEXCOORD2;
               float3 worldPos:TEXCOORD3;
               UNITY_SHADOW_COORDS(4)
            };

            v2f vert(appdata_base v)
            {
                v2f o;

                o.pos = UnityObjectToClipPos(v.vertex);

                o.uv = v.texcoord.xy * _TileFactor;

                fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
                fixed3 worldLight = normalize(WorldSpaceLightDir(v.vertex));
                fixed diff = saturate(dot(worldNormal,worldLight));

                o.pWeight1 = fixed3(0, 0, 0);
                o.pWeight2 = fixed3(0, 0, 0);

                float hatchFactor = diff * 7;

                if (hatchFactor>6)
                {

                }
                else if(hatchFactor > 5)
                {
                    o.pWeight1.x = hatchFactor - 5;
                }
                else if (hatchFactor > 4)
                {
                    o.pWeight1.x = hatchFactor - 4;
                    o.pWeight1.y = 1 - o.pWeight1.x;
                }
                else if (hatchFactor > 3)
                {
                    o.pWeight1.y = hatchFactor - 3;
                    o.pWeight1.z = 1 - o.pWeight1.y;
                }
                else if (hatchFactor > 2)
                {
                    o.pWeight1.z = hatchFactor - 2;
                    o.pWeight2.x = 1 - o.pWeight1.z;
                }
                else if (hatchFactor > 1)
                {
                    o.pWeight2.x = hatchFactor - 1;
                    o.pWeight2.y = 1 - o.pWeight2.x;
                }
                else
                {
                    o.pWeight2.y = hatchFactor;
                    o.pWeight2.z = 1 - o.pWeight2.y;
                }

                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;

                TRANSFER_SHADOW(o);

                return o;
            }

            fixed4 frag(v2f i) :SV_Target
            {
                fixed4 t1 = tex2D(_P1, i.uv) * i.pWeight1.x;
                fixed4 t2 = tex2D(_P2, i.uv) * i.pWeight1.y;
                fixed4 t3 = tex2D(_P3, i.uv) * i.pWeight1.z;
                fixed4 t4 = tex2D(_P4, i.uv) * i.pWeight2.x;
                fixed4 t5 = tex2D(_P5, i.uv) * i.pWeight2.y;
                fixed4 t6 = tex2D(_P6, i.uv) * i.pWeight2.z;
                fixed4 whiteColor = fixed4(1, 1, 1, 1)* (1 - i.pWeight1.x - i.pWeight1.y - i.pWeight1.z - i.pWeight2.x - i.pWeight2.y - i.pWeight2.z);

                fixed4 hatchColor = t1 + t2 + t3 + t4 + t5 + t6 + whiteColor;

                UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);

                return fixed4(hatchColor.rgb * _Color.rgb * atten,1.0);
            }
            ENDCG
        }       
    }
    Fallback "Diffuse"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值