shader 边缘发光

当视角方向与法线方向垂直时,顶点坐标位置即为边缘位置。

在Properties中定义以下变量:
_XRayColor(“xray color”,Color) = (1,1,1,1)
_XRayPower(“xray power”,Range(0.0001,3)) = 0.1

在Pass中定义以下变量:
float4 _RimColor;
float _RimPower;

定义一个结构体:
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
};

在顶点着色器中求出世界空间的法线和世界空间顶点的位置:
v2f vert (appdata_base v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}

在片元着色器中进行颜色输出:
fixed4 frag (v2f i) : SV_Target
{
fixed3 view = normalize(UnityWorldSpaceViewDir(i.worldPos));
//视角与法线垂直时,即为边缘位置,此时点积为0,用1减去它表示 值越趋于1,就越靠近边缘。
float rim = 1-saturate(dot(view, i.worldNormal));
fixed3 rimColor = _RimColor.rgb * pow(rim, 1 / _RimPower);
return fixed4(diffuse + ambient + rimColor, 1);//此处的diffuse和ambient需自己另外求。
}

总的代码如下:

Shader "MyShader/NewShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
		_Diffuse("diffuse",Color) = (1,1,1,1)
		_Outline("outline",Range(0,0.2)) = 0.003
		_OutlineColor("outline color",Color)=(0,0,0,0)
		_Steps("steps",Range(1,30))=1
		_ToonEffect("toon effect",Range(0,1))=0.5
		//_RampTex("ramp tex",2D)="white"{}//渐进纹理
		_RimColor("rim color",Color)=(1,1,1,1)
		_RimPower("rim power",Range(0.0001,3))=0.1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

		

		Pass//描边通道,可不看
		{
			Name "Outline"
			Cull Front
			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			float _Outline;
			float4 _OutlineColor;

			struct v2f
			{
				float4 vertex:SV_POSITION;
			};

			v2f vert(appdata_base v)
			{
				v2f o;

				//模型空间法线外拓
				v.vertex.xyz += v.normal*_Outline;
				o.vertex = UnityObjectToClipPos(v.vertex);

				//视角空间法线外拓
				//float4 pos= mul(UNITY_MATRIX_V, mul(unity_ObjectToWorld, v.vertex));//模型顶点坐标转换到视角坐标
				//float3 normal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.normal));//得到视角坐标的法线
				//pos = pos + float4(normal,0) * _Outline;
				//o.vertex= mul(UNITY_MATRIX_P, pos);//从视角空间转换到裁剪空间

				//裁剪空间法线外拓
				//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;
			}

			fixed4 frag(v2f i) :SV_Target
			{
				return _OutlineColor;
			}

			ENDCG
		}
		

        Pass//边缘发光通道!!!
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

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


            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
				float3 worldNormal : TEXCOORD1;
				float3 worldPos : TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
			float4 _Diffuse;
			float _Steps;
			float _ToonEffect;
			//sampler2D _RampTex;
			float4 _RimColor;
			float _RimPower;

            v2f vert (appdata_base v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
				fixed3 worldLight = UnityWorldSpaceLightDir(i.worldPos);
				fixed3 view = normalize(UnityWorldSpaceViewDir(i.worldPos));
				//fixed3 view = normalize(_WorldSpaceCameraPos - i.worldPos);

                fixed4 albedo = tex2D(_MainTex, i.uv);
				fixed difLight = dot(worldLight, i.worldNormal)*0.5 + 0.5;

				//渐进纹理颜色值
				//fixed4 rampColor = tex2D(_RampTex, fixed2(difLight, difLight));

				difLight = smoothstep(0, 1, difLight);
				float toon = floor(difLight*_Steps) / _Steps;
				difLight = lerp(difLight, toon, _ToonEffect);

				//视角与法线垂直时,即为边缘位置,此时点积为0,用1减去它表示 值越趋于1,就越靠近边缘。
				float rim = 1-saturate(dot(view, i.worldNormal));
				fixed3 rimColor = _RimColor.rgb * pow(rim, 1 / _RimPower);

				fixed3 diffuse = _LightColor0.rgb*_Diffuse.rgb*albedo.rgb*difLight;//*rampColor;
				return fixed4(diffuse + ambient + rimColor, 1);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

也可以这样写,效果差不多,以下是单纯的边缘发光效果。

Shader "MyShader/XRaySimpleVF" {
	Properties {
		_RimColor("RimColor",Color) = (1,1,0,1)
		_RimPower ("Rim Power", Range(0.1,8.0)) = 3.0
 
	}
	SubShader {
		Tags { "Queue"="Transparent" "RenderType"="Opaque" }
		LOD 200
		
		Pass
		{
			Blend SrcAlpha One
			ZWrite off
			Lighting off
 
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
 
			float4 _RimColor;
			float _RimPower;
			
			struct appdata_t {
				float4 vertex : POSITION;
				float2 texcoord : TEXCOORD0;
				float4 color:COLOR;
				float4 normal:NORMAL;
			};
 
			struct v2f {
				float4  pos : SV_POSITION;
				float4	color:COLOR;
			} ;
			v2f vert (appdata_t v)
			{
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
				float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                float rim = 1 - saturate(dot(viewDir,v.normal ));
               	o.color = _RimColor*pow(rim,_RimPower);
				return o;
			}
			float4 frag (v2f i) : COLOR
			{
				return i.color; 
			}
			ENDCG
		}
		
	} 
	FallBack "Diffuse"
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值