You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
3.9 KiB
149 lines
3.9 KiB
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' |
|
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' |
|
|
|
Shader "Unity Shaders Book/Chapter 9/Attenuation And Shadow Use Build-in Functions" { |
|
Properties { |
|
_Diffuse ("Diffuse", Color) = (1, 1, 1, 1) |
|
_Specular ("Specular", Color) = (1, 1, 1, 1) |
|
_Gloss ("Gloss", Range(8.0, 256)) = 20 |
|
} |
|
SubShader { |
|
Tags { "RenderType"="Opaque" } |
|
|
|
Pass { |
|
// Pass for ambient light & first pixel light (directional light) |
|
Tags { "LightMode"="ForwardBase" } |
|
|
|
CGPROGRAM |
|
|
|
// Apparently need to add this declaration |
|
#pragma multi_compile_fwdbase |
|
|
|
#pragma vertex vert |
|
#pragma fragment frag |
|
|
|
// Need these files to get built-in macros |
|
#include "Lighting.cginc" |
|
#include "AutoLight.cginc" |
|
|
|
fixed4 _Diffuse; |
|
fixed4 _Specular; |
|
float _Gloss; |
|
|
|
struct a2v { |
|
float4 vertex : POSITION; |
|
float3 normal : NORMAL; |
|
}; |
|
|
|
struct v2f { |
|
float4 pos : SV_POSITION; |
|
float3 worldNormal : TEXCOORD0; |
|
float3 worldPos : TEXCOORD1; |
|
SHADOW_COORDS(2) |
|
}; |
|
|
|
v2f vert(a2v v) { |
|
v2f o; |
|
o.pos = UnityObjectToClipPos(v.vertex); |
|
|
|
o.worldNormal = UnityObjectToWorldNormal(v.normal); |
|
|
|
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; |
|
|
|
// Pass shadow coordinates to pixel shader |
|
TRANSFER_SHADOW(o); |
|
|
|
return o; |
|
} |
|
|
|
fixed4 frag(v2f i) : SV_Target { |
|
fixed3 worldNormal = normalize(i.worldNormal); |
|
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos)); |
|
|
|
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; |
|
|
|
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal, worldLightDir)); |
|
|
|
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos)); |
|
fixed3 halfDir = normalize(worldLightDir + viewDir); |
|
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss); |
|
|
|
// UNITY_LIGHT_ATTENUATION not only compute attenuation, but also shadow infos |
|
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos); |
|
|
|
return fixed4(ambient + (diffuse + specular) * atten, 1.0); |
|
} |
|
|
|
ENDCG |
|
} |
|
|
|
Pass { |
|
// Pass for other pixel lights |
|
Tags { "LightMode"="ForwardAdd" } |
|
|
|
Blend One One |
|
|
|
CGPROGRAM |
|
|
|
// Apparently need to add this declaration |
|
#pragma multi_compile_fwdadd |
|
// Use the line below to add shadows for point and spot lights |
|
// #pragma multi_compile_fwdadd_fullshadows |
|
|
|
#pragma vertex vert |
|
#pragma fragment frag |
|
|
|
#include "Lighting.cginc" |
|
#include "AutoLight.cginc" |
|
|
|
fixed4 _Diffuse; |
|
fixed4 _Specular; |
|
float _Gloss; |
|
|
|
struct a2v { |
|
float4 vertex : POSITION; |
|
float3 normal : NORMAL; |
|
}; |
|
|
|
struct v2f { |
|
float4 pos : SV_POSITION; |
|
float3 worldNormal : TEXCOORD0; |
|
float3 worldPos : TEXCOORD1; |
|
SHADOW_COORDS(2) |
|
}; |
|
|
|
v2f vert(a2v v) { |
|
v2f o; |
|
o.pos = UnityObjectToClipPos(v.vertex); |
|
|
|
o.worldNormal = UnityObjectToWorldNormal(v.normal); |
|
|
|
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; |
|
|
|
// Pass shadow coordinates to pixel shader |
|
TRANSFER_SHADOW(o); |
|
|
|
return o; |
|
} |
|
|
|
fixed4 frag(v2f i) : SV_Target { |
|
fixed3 worldNormal = normalize(i.worldNormal); |
|
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos)); |
|
|
|
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal, worldLightDir)); |
|
|
|
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos)); |
|
fixed3 halfDir = normalize(worldLightDir + viewDir); |
|
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss); |
|
|
|
// UNITY_LIGHT_ATTENUATION not only compute attenuation, but also shadow infos |
|
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos); |
|
|
|
return fixed4((diffuse + specular) * atten, 1.0); |
|
} |
|
|
|
ENDCG |
|
} |
|
} |
|
FallBack "Specular" |
|
}
|
|
|