码迷,mamicode.com
首页 > 其他好文 > 详细

边缘光shader

时间:2016-08-26 19:58:57      阅读:533      评论:0      收藏:0      [点我收藏+]

标签:

Shader "CM/RimLight" {
    Properties 
	{
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
		_IlluminPower("Illumin Power", Range(0, 2)) = 1
        _RimColor ("Rim Color", Color) = (1, 1, 1, 1)
        _RimPower ("Rim Power", Float) = 0.7
    }
    SubShader 
	{
        Pass 
		{
       		Lighting Off
            CGPROGRAM
			
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			uniform sampler2D _MainTex;
			uniform fixed4 _Color;
			uniform float4 _MainTex_ST;
			float _IlluminPower;
			uniform fixed4 _RimColor;
			float _RimPower;
			
			struct appdata {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				fixed3 color : COLOR;
			};

			v2f vert (appdata_base v) 
			{
				v2f o;
				o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

				float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
				float dotProduct = 1 - dot(v.normal, viewDir);
			   
				o.color = _RimColor * pow(dotProduct, _RimPower);//smoothstep(1 - _RimPower, 1.0, dotProduct);

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


			fixed4 frag(v2f i) : COLOR 
			{
				fixed4 texcol = tex2D(_MainTex, i.uv);
				texcol.rgb = texcol.rgb  * _Color.rgb * _IlluminPower + i.color;
				return texcol;
			}
			
            ENDCG
        }
    }
}

 

  

技术分享

冰冻效果

 

二、使用cap texture:

 

Shader "CM/Rimlight2" {
    Properties 
	{
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
		_IlluminPower("Illumin Power", Range(0, 2)) = 1

		_CapTex("Cap Tex (RGB)", 2D) = "white" {}
		_CapColor("Cap Color", Color) = (1,1,1,1)
		_CapIntensity("Cap Intensity", Range(0,3)) = 0
    }
    SubShader 
	{
        Pass 
		{
       		Lighting Off
            CGPROGRAM
			
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			uniform sampler2D _MainTex;
			uniform fixed4 _Color;
			uniform float4 _MainTex_ST;
			float _IlluminPower;

			
			float _CapIntensity;
			uniform sampler2D _CapTex;
			fixed4 _CapColor;
				
			
			struct appdata {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				fixed2 cap : COLOR;
			};

			v2f vert (appdata_base v) 
			{
				v2f o;
				o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

				half2 capCoord;
				capCoord.x = dot(UNITY_MATRIX_IT_MV[0].xyz,v.normal);
				capCoord.y = dot(UNITY_MATRIX_IT_MV[1].xyz,v.normal);
				o.cap = capCoord * 0.5 + 0.5;

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


			fixed4 frag(v2f i) : COLOR 
			{
				fixed4 texcol = tex2D(_MainTex, i.uv);
				fixed4 cap = tex2D(_CapTex, i.cap);
				
				texcol.rgb = texcol.rgb  * _Color.rgb * _IlluminPower + cap.rgb * _CapIntensity * _CapColor;
				return texcol;
			}
			
            ENDCG
        }
    }
}

 

 技术分享

 三、在光照射方向上存在边缘光

Shader "CM/Rimlight3" {
    Properties 
	{
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
		_IlluminPower("Illumin Power", Range(0, 2)) = 1
        _RimColor ("Rim Color", Color) = (0, 0.8, 0.8, 1)
        _RimPower ("Rim Power", Float) = 0.7
    }
    SubShader 
	{
        Pass 
		{
       		Lighting Off
            CGPROGRAM
			
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			uniform sampler2D _MainTex;
			uniform fixed4 _Color;
			uniform float4 _MainTex_ST;
			float _IlluminPower;
			uniform fixed4 _RimColor;
			float _RimPower;
			
			struct appdata {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				fixed3 color : COLOR;
			};

			v2f vert (appdata_base v) 
			{
				v2f o;
				o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

				float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
				float dotRim = 1 - dot(v.normal, viewDir);
				float3 lightDir = normalize(ObjSpaceLightDir(v.vertex));
				float dotDiff = dot(v.normal, lightDir);
				
				o.color = _RimColor * pow(dotRim, _RimPower) * dotDiff;

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


			fixed4 frag(v2f i) : COLOR 
			{
				fixed4 texcol = tex2D(_MainTex, i.uv);
				texcol.rgb = texcol.rgb  * _Color.rgb * _IlluminPower + i.color;
				return texcol;
			}
			
            ENDCG
        }
    }
}

  技术分享

 四、在光照射方向上产生边缘光  ----------------- 使用matcap 贴图  (shader 依然用上面的rimlight2), 只需要替换matcap贴图 即可:

技术分享

 

边缘光shader

标签:

原文地址:http://www.cnblogs.com/skylinee/p/5811457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!