Unity Shader 实现(纹理法线贴图)

Unity Shader 顶点片段Shader实现(纹理法线贴图)

Shader "Custom/MyLastShader1"
{
    Properties
    {
		_MainColor("MainColor",COLOR) = (1,1,1,1)
		_NorTexture("NormalTexture",2D) = "white"{}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
			Tags{"LightMode"="ForwardBase"}
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
			#include "Lighting.cginc"
			float4 _MainColor;
			sampler2D _NorTexture;

            struct v2f
            {
                float4 vertex : SV_POSITION;
				float3 nor:NORMAL;
				float4 pos : POSITION1;
				float3 tan:POSITION2;
				float2 nortexcoord:TEXCOORD0;
            };


            v2f vert (appdata_tan v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
				o.nor = v.normal;
				o.pos = v.vertex;

				//float3 tmp = cross(v.tangent.xyz,v.normal);
				//float3x3 rotation = float3x3(v.tangent.xyz,tmp,v.normal);
				//使用如下宏代替以上注释代码
				TANGENT_SPACE_ROTATION;
				o.tan = mul(rotation,WorldSpaceLightDir(v.vertex));
				o.nortexcoord = v.texcoord.xy;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				//环境光
				float4 col = unity_AmbientSky * 2;
				//漫反射
				float3 N = normalize(UnpackNormal(tex2D(_NorTexture,i.nortexcoord)));
				float3 L = normalize(i.tan);
				float ndot = saturate(dot(L,N));
				col += _LightColor0 * ndot * _MainColor;
				//Light
				float3 worldPos = mul(unity_ObjectToWorld,i.pos);
				col.rgb += Shade4PointLights(unity_4LightPosX0,unity_4LightPosY0,unity_4LightPosZ0,
											unity_LightColor[0],unity_LightColor[1],unity_LightColor[2],unity_LightColor[3],
											unity_4LightAtten0,
											worldPos,N);
                return col;
            }
            ENDCG
        }
		//==========================
		Pass
        {
			Tags{"LightMode"="ForwardAdd"}
			Blend one one
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
			#include "Lighting.cginc"
			float4 _MainColor;
			sampler2D _NorTexture;

            struct v2f
            {
                float4 vertex : SV_POSITION;
				float3 nor:NORMAL;
				float4 pos : POSITION1;
				float3 tan:POSITION2;
				float2 nortexcoord:TEXCOORD0;
            };


            v2f vert (appdata_tan v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
				o.nor = v.normal;
				o.pos = v.vertex;

				//float3 tmp = cross(v.tangent.xyz,v.normal);
				//float3x3 rotation = float3x3(v.tangent.xyz,tmp,v.normal);
				//使用如下宏代替以上注释代码
				TANGENT_SPACE_ROTATION;
				o.tan = mul(rotation,WorldSpaceLightDir(v.vertex));
				o.nortexcoord = v.texcoord.xy;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				//环境光
				float4 col = unity_AmbientSky * 2;
				//漫反射
				float3 N = normalize(UnpackNormal(tex2D(_NorTexture,i.nortexcoord)));
				float3 L = normalize(i.tan);
				float ndot = saturate(dot(L,N));
				float t = 0;
				if(_WorldSpaceLightPos0.w!=0){
					t = 1.0/length(i.tan);
				}
				col += _LightColor0 * ndot * _MainColor * t;
                return col;
            }
            ENDCG
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值