在世界空间下
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
Shader "Custom/testLightShader"
{
Properties
{
_Color("Color",Color) = (1,1,1,1)
_MainTex("Main Tex",2D) = "white" {}
_BumpMap("Normal Map",2D) = "bump" {}
_BumpScale("Bump Scale",float) = 1.0
_Specular("Specular", Color) = (1,1,1,1)
_Gloss("Gloss", Range(8.0,256)) = 20
}
SubShader
{
Tags {"RenderType" = "Opaque" "LightMode" = "UniversalForward" "RenderPipeline" = "UniversalPipeline"}
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
CBUFFER_START(UnityPerMaterial)
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_BumpMap);
SAMPLER(sampler_BumpMap);
float4 _MainTex_ST;
float4 _BumpMap_ST;
float _BumpScale;
half4 _Color;
half4 _Specular;
half _Gloss;
CBUFFER_END
ENDHLSL
LOD 100
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
float4 TtoW0 : TEXCOORD1;
float4 TtoW1 : TEXCOORD2;
float4 TtoW2 : TEXCOORD3;
};
v2f vert(appdata v)
{
v2f o;
ZERO_INITIALIZE(v2f, o);
o.pos = TransformObjectToHClip(v.vertex.xyz);
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);
float3 worldPos = TransformObjectToWorld(v.vertex.xyz);
float3 worldNormal = TransformObjectToWorldNormal(v.normal);
float3 worldTangent = TransformObjectToWorldDir(v.tangent.xyz);
half3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
return o;
}
half4 frag(v2f i) : SV_TARGET
{
float3 worldPos = float3(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w);
Light light = GetMainLight();
half3 lightDir = normalize(light.direction);
half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - worldPos);
half3 bump = UnpackNormal(SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, i.uv.zw));
bump.xy *= _BumpScale;
bump.z = sqrt(1.0 - saturate(dot(bump.xy, bump.xy)));
bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));
float3 albedo = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv).rgb * _Color.rgb;
half3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
half3 diffuse = light.color * albedo * max(0,dot(bump, lightDir));//半兰伯特
half3 halfDir = normalize(lightDir + viewDir);
half3 specular = light.color.rgb * _Specular.rgb * pow(saturate(dot(bump, halfDir)), _Gloss); //blinnPhong模型
half3 color = ambient + diffuse + specular;
return half4(color, 1.0);
}
ENDHLSL
}
}
FallBack "Packages/com.unity.render-pipelines.universal/FallbackError"
}



