forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastConfigurable.shader
More file actions
392 lines (330 loc) · 15.2 KB
/
FastConfigurable.shader
File metadata and controls
392 lines (330 loc) · 15.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Very fast shader that uses the Unity light system.
// Compiles down to only performing the operations you're actually using.
// Uses material property drawers rather than a custom editor for ease of maintenance.
// Textured ambient+diffuse:
// Stats for Vertex shader:
// d3d11: 24 avg math (9..44)
// Stats for Fragment shader:
// d3d11: 2 avg math (1..5), 0 avg texture (0..2)
Shader "HoloToolkit/Fast Configurable"
{
Properties
{
[Header(Base Texture and Color)]
[Indent]
[Toggle] _UseVertexColor("Vertex Color Enabled?", Float) = 0
[Toggle] _UseMainColor("Main Color Enabled?", Float) = 0
_Color("Main Color", Color) = (1,1,1,1)
[Toggle] _UseMainTex("Main Texture Enabled?", Float) = 0
[NoScaleOffset]_MainTex("Main Texture", 2D) = "red" {}
[Toggle] _UseOcclusionMap("Occlusion/Detail Texture Enabled?", Float) = 0
[NoScaleOffset]_OcclusionMap("Occlusion/Detail Texture", 2D) = "blue" {}
[Dedent]
[Space(12)]
[Header(Lighting)]
[Indent]
[Toggle] _UseAmbient("Ambient Lighting Enabled?", Float) = 1
[Toggle] _UseDiffuse("Diffuse Lighting Enabled?", Float) = 1
[Toggle] _UseSpecular("Specular Lighting Enabled?", Float) = 0
[Indent]
_SpecularColor("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
[PowerSlider(5.0)]_Specular("Specular (Specular Power)", Range(1.0, 100.0)) = 10.0
_Gloss("Gloss (Specular Scale)", Range(0.1, 10.0)) = 1.0
[Toggle] _UseGlossMap("Use Gloss Map? (per-pixel)", Float) = 0
[NoScaleOffset]_MetallicGlossMap("Gloss Map", 2D) = "white" {}
[Dedent]
[Toggle] _Shade4("Use additional lighting data? (Expensive!)", Float) = 0
[Toggle] _UseBumpMap("Normal Map Enabled? (per-pixel)", Float) = 0
[NoScaleOffset][Normal] _BumpMap("Normal Map", 2D) = "bump" {}
[Dedent]
[Space(12)]
[Header(Emission)]
[Indent]
[Toggle] _UseEmissionColor("Emission Color Enabled?", Float) = 0
_EmissionColor("Emission Color", Color) = (1,1,1,1)
[Toggle] _UseEmissionTex("Emission Texture Enabled?", Float) = 0
[NoScaleOffset] _EmissionTex("Emission Texture", 2D) = "blue" {}
[Dedent]
[Space(12]
[Header(Texture Scale and Offset)]
[Indent]
[Toggle(_MainTex_SCALE_ON)] _MainTex_SCALE("Use Texture Scale? (Applies to all textures)", Float) = 0
[Toggle(_MainTex_OFFSET_ON)] _MainTex_OFFSET("Use Texture Offset? (Applies to all textures)", Float) = 0
_TextureScaleOffset("Texture Scale (XY) and Offset (ZW)", Vector) = (1, 1, 0, 0)
[Dedent]
[Space(12)]
[Header(Alpha Blending)]
[Indent]
[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("SrcBlend", Float) = 1 //"One"
[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("DestBlend", Float) = 0 //"Zero"
[Enum(UnityEngine.Rendering.BlendOp)] _BlendOp("BlendOp", Float) = 0 //"Add"
[Dedent]
[Space(12)]
[Header(Misc.)]
[Indent]
[Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull", Float) = 2 //"Back"
[Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 4 //"LessEqual"
[Enum(Off,0,On,1)] _ZWrite("ZWrite", Float) = 1 //"On"
[Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorWriteMask("ColorWriteMask", Float) = 15 //"All"
}
SubShader
{
Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
LOD 100
Blend[_SrcBlend][_DstBlend]
BlendOp[_BlendOp]
ZTest[_ZTest]
ZWrite[_ZWrite]
Cull[_Cull]
ColorMask[_ColorWriteMask]
Pass
{
Name "FORWARD"
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//compiles all variants needed by ForwardBase (forward rendering base) pass type. The variants deal with different lightmap types and main directional light having shadows on or off.
#pragma multi_compile_fwdbase
//expands to several variants to handle different fog types
#pragma multi_compile_fog
//We only target the HoloLens (and the Unity editor), so take advantage of shader model 5.
#pragma target 5.0
#pragma only_renderers d3d11
//shader features are only compiled if a material uses them
#pragma shader_feature _USEMAINCOLOR_ON
#pragma shader_feature _USEMAINTEX_ON
#pragma shader_feature _USESOCCLUSIONMAP_ON
#pragma shader_feature _USEBUMPMAP_ON
#pragma shader_feature _USEAMBIENT_ON
#pragma shader_feature _USEDIFFUSE_ON
#pragma shader_feature _USESPECULAR_ON
#pragma shader_feature _USEGLOSSMAP_ON
#pragma shader_feature _SHADE4_ON
#pragma shader_feature _USEEMISSIONCOLOR_ON
#pragma shader_feature _USEEMISSIONTEX_ON
//scale and offset will apply to all
#pragma shader_feature _MainTex_SCALE_ON
#pragma shader_feature _MainTex_OFFSET_ON
//may be set from script so generate both paths
#pragma multi_compile __ _NEAR_PLANE_FADE_ON
#include "Lighting.cginc"
#include "AutoLight.cginc"
#include "HoloToolkitCommon.cginc"
#include "macro.cginc"
#define USE_PER_PIXEL (_USEBUMPMAP_ON || _USEGLOSSMAP_ON)
#define PIXEL_SHADER_USES_WORLDPOS (USE_PER_PIXEL && (_USESPECULAR_ON || _SHADE4_ON))
#define USES_TEX_XY (_USEMAINTEX_ON || _USEOCCLUSIONMAP_ON || _USEEMISSIONTEX_ON || _USEBUMPMAP_ON || _USEGLOSSMAP_ON)
#if _USEMAINCOLOR_ON
float4 _Color;
#endif
#if _USEMAINTEX_ON
UNITY_DECLARE_TEX2D(_MainTex);
#endif
#if _USEMAINTEX_ON
UNITY_DECLARE_TEX2D(_SecondaryTex);
#endif
#if _USEBUMPMAP_ON
UNITY_DECLARE_TEX2D(_BumpMap);
#endif
#if _USESPECULAR_ON
float3 _SpecularColor;
float _Specular;
float _Gloss;
#endif
#if _USEGLOSSMAP_ON
UNITY_DECLARE_TEX2D(_MetallicGlossMap);
#endif
#if _USEEMISSIONCOLOR_ON
float4 _EmissionColor;
#endif
#if _USEEMISSIONTEX_ON
UNITY_DECLARE_TEX2D(_EmissionTex);
#endif
float4 _TextureScaleOffset;
struct a2v
{
float4 vertex : POSITION;
#if _USEBUMPMAP_ON
#else
float3 normal : NORMAL;
#endif
#if _USEVERTEXCOLOR_ON
float4 color : COLOR;
#endif
#if USES_TEX_XY
float2 texcoord : TEXCOORD0;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 pos : SV_POSITION;
#if _USEVERTEXCOLOR_ON
float4 color : COLOR;
#endif
#if _USEBUMPMAP_ON
#else
#if _USEGLOSSMAP_ON
float3 normal : NORMAL;
#endif
#endif
#if USES_TEX_XY || _NEAR_PLANE_FADE_ON
float3 texXYFadeZ : TEXCOORD0;
#endif
#if LIGHTMAP_ON
float2 lmap : TEXCOORD1;
#else
float3 vertexLighting : TEXCOORD1;
#endif
#if PIXEL_SHADER_USES_WORLDPOS
float3 worldPos: TEXCOORD2;
#endif
LIGHTING_COORDS(3, 4)
UNITY_FOG_COORDS(5)
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert(a2v v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_OUTPUT(v2f, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.pos = UnityObjectToClipPos(v.vertex);
#if _USEVERTEXCOLOR_ON
#if _USEMAINCOLOR_ON
o.color = v.color * _Color;
#else
o.color = v.color;
#endif
#endif
#if (_USESPECULAR_ON && USE_PER_PIXEL) || _SHADE4_ON
float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
#endif
#if PIXEL_SHADER_USES_WORLDPOS
o.worldPos = worldPos;
#endif
#if USES_TEX_XY
o.texXYFadeZ.xy = TRANSFORM_TEX_MAINTEX(v.texcoord.xy, _TextureScaleOffset);
#endif
#if LIGHTMAP_ON
o.lmap.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
#else
#if USE_PER_PIXEL
//no bump maps, do per-vertex lighting
#else
float3 normalWorld = UnityObjectToWorldNormal(v.normal);
#if _USEAMBIENT_ON
//grab ambient color from Unity's spherical harmonics
o.vertexLighting += ShadeSH9(float4(normalWorld, 1.0));
#endif
#if _USEDIFFUSE_ON
o.vertexLighting += HoloTKLightingLambertian(normalWorld, _WorldSpaceLightPos0.xyz, _LightColor0.rgb);
#endif
#if _USESPECULAR_ON
o.vertexLighting += HoloTKLightingBlinnPhong(normalWorld, _WorldSpaceLightPos0.xyz, _LightColor0.rgb, UnityWorldSpaceViewDir(mul(unity_ObjectToWorld, v.vertex)), _Specular, _Gloss, _SpecularColor);
#endif
#if _SHADE4_ON
//handles point and spot lights
o.vertexLighting += Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
unity_4LightAtten0, worldPos, normalWorld);
#endif
#endif
#endif
#if _USEBUMPMAP_ON
#else
#if _USEGLOSSMAP_ON
o.normal = v.normal;
#endif
#endif
//fade away objects closer to the camera
#if _NEAR_PLANE_FADE_ON
o.texXYFadeZ.z = ComputeNearPlaneFadeLinear(v.vertex);
#endif
TRANSFER_VERTEX_TO_FRAGMENT(o);
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
float4 frag(v2f IN) : SV_Target
{
#if _USEMAINTEX_ON
float4 color = UNITY_SAMPLE_TEX2D(_MainTex, IN.texXYFadeZ.xy);
#else
float4 color = 1;
#endif
#if _USEOCCLUSIONMAP_ON
color *= UNITY_SAMPLE_TEX2D(_OcclusionMap, IN.texXYFadeZ.xy);
#endif
#if _USEVERTEXCOLOR_ON
color *= IN.color;
//if vertex color is on, we've already scaled it by the main color if needed in the vertex shader
#elif _USEMAINCOLOR_ON
color *= _Color;
#endif
//light attenuation from shadows cast onto the object
float lightAttenuation = SHADOW_ATTENUATION(IN);
float3 lightColorShadowAttenuated = 0;
#if LIGHTMAP_ON
float3 lightmapResult = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, IN.lmap.xy));
#ifdef SHADOWS_SCREEN
lightColorShadowAttenuated = min(lightmapResult, lightAttenuation * 2);
#else
lightColorShadowAttenuated = lightmapResult;
#endif
#else //not using lightmapping
#if USE_PER_PIXEL
//if a normal map is on, it makes sense to do most calculations per-pixel
//unpack can be expensive if normal map is dxt5
float3 normalObject;
#if (_USEBUMPMAP_ON)
normalObject = UnpackNormal(UNITY_SAMPLE_TEX2D(_BumpMap, IN.texXYFadeZ.xy));
#else
normalObject = IN.normal;
#endif
float3 normalWorld = UnityObjectToWorldNormal(normalObject);
#if _USEAMBIENT_ON
//grab ambient color from Unity's spherical harmonics
lightColorShadowAttenuated += ShadeSH9(float4(normalWorld, 1.0));
#endif
#if _USEDIFFUSE_ON
lightColorShadowAttenuated += HoloTKLightingLambertian(normalWorld, _WorldSpaceLightPos0.xyz, _LightColor0.rgb);
#endif
#if _USESPECULAR_ON
float gloss = _Gloss;
#if _USEGLOSSMAP_ON
gloss *= UNITY_SAMPLE_TEX2D(_MetallicGlossMap, IN.texXYFadeZ.xy).y;
#endif
lightColorShadowAttenuated += HoloTKLightingBlinnPhong(normalWorld, _WorldSpaceLightPos0.xyz, _LightColor0.rgb, UnityWorldSpaceViewDir(IN.worldPos), _Specular, gloss, _SpecularColor);
#endif
#if _SHADE4_ON
//This handles point and directional lights
lightColorShadowAttenuated += Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
unity_4LightAtten0, IN.worldPos, normalWorld);
#endif
#else
//no normal map, so vertex lighting is sufficient
lightColorShadowAttenuated = IN.vertexLighting;
#endif
//could save some work here in the 0 case
lightColorShadowAttenuated *= lightAttenuation;
#endif
color.rgb *= lightColorShadowAttenuated;
#if _USEEMISSIONTEX_ON
color.rgb += UNITY_SAMPLE_TEX2D(_EmissionTex, IN.texXYFadeZ.xy);
#endif
#if _USEEMISSIONCOLOR_ON
color.rgb += _EmissionColor;
#endif
#if _NEAR_PLANE_FADE_ON
color.rgb *= IN.texXYFadeZ.z;
#endif
UNITY_APPLY_FOG(IN.fogCoord, color);
return color;
}
ENDCG
}
}
Fallback "VertexLit" //for shadows
}