-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathforwardRendering.shader
140 lines (127 loc) · 5.02 KB
/
forwardRendering.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Shader "LX/forwardRendering"
{
Properties
{
_Diffuse("Diffuse",Color)=(1,1,1,1)
_MainTex("Main Tex",2D)="white"{}
_Specular("Specular",Color)=(1,1,1,1)
_Gloss("Gloss",Range(8,256))=20
}
SubShader
{
Pass
{
Tags
{
"LightMode"="ForwardBase"
}
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
sampler2D _MainTex;
float4 _MainTex_ST;
struct a2v
{
float4 position:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 position:SV_POSITION;
float3 worldNormal:TEXCOORD0;
float3 worldPosition:TEXCOORD1;
float2 uvMain:TEXCOORD2;
};
v2f vert(a2v vertData)
{
v2f o;
o.position = UnityObjectToClipPos(vertData.position);
o.worldNormal = normalize(UnityObjectToWorldNormal(vertData.normal));
o.worldPosition = mul(unity_ObjectToWorld, vertData.position);
o.uvMain = vertData.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
fixed4 frag(v2f i):SV_Target
{
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPosition));
fixed3 texColor = tex2D(_MainTex, i.uvMain).rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT * texColor;
fixed3 diffuse = _LightColor0.rgb * texColor * saturate(dot(i.worldNormal, worldLightDir));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPosition));
fixed3 halfDir = normalize(worldLightDir + viewDir);
fixed3 specular = texColor * _LightColor0.rgb * _Specular.rgb * pow(
saturate(dot(i.worldNormal, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1.0);
}
ENDCG
}
Pass
{
Tags
{
"LightMode"="ForwardAdd"
}
Blend One One
CGPROGRAM
#pragma multi_compile_fwdadd
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
sampler2D _MainTex;
float4 _MainTex_ST;
struct a2v
{
float4 position:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 position:SV_POSITION;
float3 worldNormal:TEXCOORD0;
float3 worldPosition:TEXCOORD1;
float2 uvMain:TEXCOORD2;
};
v2f vert(a2v vertData)
{
v2f o;
o.position = UnityObjectToClipPos(vertData.position);
o.worldNormal = normalize(UnityObjectToWorldNormal(vertData.normal));
o.worldPosition = mul(unity_ObjectToWorld, vertData.position);
o.uvMain = vertData.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
fixed4 frag(v2f i):SV_Target
{
#if defined (POINT)
float3 lightCoord = mul(unity_WorldToLight, float4(i.worldPosition, 1)).xyz;
fixed atten = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL;
#elif defined (SPOT)
float4 lightCoord = mul(unity_WorldToLight, float4(i.worldPosition, 1));
fixed atten = (lightCoord.z > 0) * tex2D(_LightTexture0, lightCoord.xy / lightCoord.w + 0.5).w * tex2D(_LightTextureB0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL;
#else
fixed atten = 1.0;
#endif
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPosition));
fixed3 texColor = tex2D(_MainTex, i.uvMain).rgb;
fixed3 diffuse = _LightColor0.rgb * texColor * saturate(dot(i.worldNormal, worldLightDir));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPosition));
fixed3 halfDir = normalize(worldLightDir + viewDir);
fixed3 specular = texColor * _LightColor0.rgb * _Specular.rgb * pow(
saturate(dot(i.worldNormal, halfDir)), _Gloss);
return fixed4((diffuse + specular) * atten, 1.0);
}
ENDCG
}
}
}