-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathShaderCheatsheet.shader
685 lines (521 loc) · 25.3 KB
/
ShaderCheatsheet.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
███████╗██╗ ██╗ █████╗ ██████╗ ███████╗██████╗ ██╗ ██╗███████╗██╗ ██████╗ ██╗
██╔════╝██║ ██║██╔══██╗██╔══██╗██╔════╝██╔══██╗ ██║ ██║██╔════╝██║ ██╔══██╗██║
███████╗███████║███████║██║ ██║█████╗ ██████╔╝ ███████║█████╗ ██║ ██████╔╝██║
╚════██║██╔══██║██╔══██║██║ ██║██╔══╝ ██╔══██╗ ██╔══██║██╔══╝ ██║ ██╔═══╝ ╚═╝
███████║██║ ██║██║ ██║██████╔╝███████╗██║ ██║ ██║ ██║███████╗███████╗██║ ██╗
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═╝
// - Or - I will never learn how to write shaders by heart
// by Nothke, contact: [email protected] or https://twitter.com/Nothke
// All comments are written preceeding with // so that shader highlighting can be used in a text editor (like sublime)
// All categories mentioned in comments for searching are written in caps, for example "see MY TITLE"
// To navigate to a category, like MY TITLE find it with hashtag like "#MY TITLE"
// This cheatsheet primarily focuses on VERTEX/FRAMGENT shaders and not on SURFACE shaders,
// but some of the surface shader things are mentioned in a separate category on the bottom. see SURFACE
// #STRUCTURE
// #VERT/FRAG shader
Shader "Custom/Name"
{
Properties // See PROPERTIES
{
}
Tags // See TAGS
Blend // See BLEND MODES
Pass
{
CGPROGRAM
#include // See INCLUDES
#pragma vertex vert // See PRAGMAS
#pragma fragment frag
// custom variables
struct appdata { }; // not necessary if using one of predefined ones, see #APPDATA
struct v2f { }; // see #STRUCTS
v2f vert (appdata v) { };
fixed4 frag (v2f i) : SV_Target { };
ENDCG
}
Pass // Another pass...
{
}
}
// #PROPERTIES
Properties
{
// Syntax: _VariableName ("Label in the inspector", Type) = default value
_RangedFloat ("Ranged Float", Range (min, max)) = number
_Float ("Float", Float) = number
_Int ("Int", Int) = number
_Color ("Some Color", Color) = (1,1,1,1)
_Vector ("Some Vector", Vector) = (0,0,0,0)
_Texture ("Texture", 2D) = "white" {} // "", “white” (1,1,1,1), “black” (0,0,0,0), “gray” (0.5,0.5,0.5,0.5), “bump” (0.5,0.5,1,0.5) or “red” (1,0,0,0)
_Cubemap ("Cubemap", CUBE) = "" {}
_3DTexture ("3DTexture", 3D) = "defaulttexture" {}
// Standard properties:
// Unity's builtin shaders use _MainTex ss the main diffuse/albedo slot,
// so it is advised to use this
_MainTex ("Main Tex", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
// Attiributes:
[HideInInspector]
[MaterialToggle] // Makes a Float parameter act as a bool toggle in inspector. Not to be confused with [Toggle(THINGY)] (see below)
[NoScaleOffset] // no texture tiling/offset fields in inspector for samplers
[Normal] // expects a normal map and adds a "Fix Now" button if the texture is not set to NormalMap
[HDR] // expects an HDR
[Gamma] // indicates that a float/vector property is specified as sRGB value in the UI (just like colors are), and possibly needs conversion according to color space used
[PerRendererData] // indicates that a texture property will be coming from per-renderer data in the form of a MaterialPropertyBlock. Material inspector changes the texture slot UI for these properties.
// Sliders
[PowerSlider(3.0)] _Shininess ("Shininess", Range (0.01, 1)) = 0.08 // A slider with "power of" (in this case ^3) response curve
[IntRange] _Alpha ("Alpha", Range (0, 255)) = 100 // integer slider for specified range (0 to 255)
// Decorators
[Space(50)] _Prop2 ("Prop2", Float) = 0 // creates space before property
[Header(A group of things)] _Prop1 ("Prop1", Float) = 0 // creates a header before property
[Enum(UnityEngine.Rendering.BlendMode)] _Blend ("Blend mode", Float) = 1 // blend mode popup menu
[Enum(One,1,SrcAlpha,5)] _Blend2 ("Blend mode subset", Float) = 1 // a subset of blend mode values, just "One" (value 1) and "SrcAlpha" (value 5)
// multi_compile Shader Keywords
[Toggle(THINGY)] _Fancy ("Thingy", Float) = 0 // toggles a THINGY multi_compile shader keyword, see #MULTI_COMPILE
[KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0
}
// They would be accessed in HSLS code as:
half _Float;
fixed _Float;
float _Float;
int _Int;
uint _Int;
// half, fixed, float + 2,3,4 are all possible combinations for example:
fixed4 _Color; // low precision type is usually enough for colors
float4 _Vector;
float4x4 _Matrix;
sampler2D _Texture;
samplerCUBE _Cubemap;
sampler3D _3DTexture;
// Tiling and Offset information can be obtain by [TEXTURE NAME]_ST, that needs to be defined separately
float4 _Texture_ST; // (x = x tiling, y = y tiling, z = x offset, w = y offset)
// Texture size can be obtained through the Vector4 [TEXTURE NAME]_TexelSize
float4 _Texture_TexelSize; // x = 1.0/width, y = 1.0/height, z = width, w = height
// Never personally used, see in official docs
float4 _Texture_HDR
// #TAGS
Tags {
"Queue" = "Background" | "Geometry" | "AlphaTest" | "Transparent" | "Overlay"
// special cases with +number, e.g. "Geometry+1"
"RenderType" = "Transparent" | "TransparentCutout" | "Background" | "Overlay"
| "TreeOpaque" | "TreeTransparentCutout" | "TreeBillboard" | "Grass" | "GrassBillboard"
"IgnoreProjector" = "True"
"DisableBatching" = "True" | "False" | "LODFading"
"ForceNoShadowCasting" = "True"
"IgnoreProjector" = "True"
"CanUseSpriteAtlas" = "False" // if the shader is meant for sprites, and will not work when they are packed into atlases
"PreviewType" = "Sphere" | "Plane" | "Skybox" // how should the inspector preview the material, Plane is 2D
}
// incomplete
// #BLEND MODES
Blend Off
Blend SrcAlpha OneMinusSrcAlpha // Traditional transparency
Blend One OneMinusSrcAlpha // Premultiplied transparency
Blend One One // Additive
Blend OneMinusDstColor One // Soft Additive
Blend DstColor Zero // Multiplicative
Blend DstColor SrcColor // 2x Multiplicative
BlendOp colorOp // provide a custom operation
BlendOp colorOp, alphaOp
AlphaToMask On | Off // aka Alpha to coverage, allows MSAA to be used on pixels of an alpha test shader
// #Culling & Z
Cull Back | Front | Off
ZWrite On | Off
ZTest Less | Greater | LEqual | GEqual | Equal | NotEqual | Always
Offset Factor, Units
Name "PassName" // Name a pass so that it can be reused by calling UsePass from other shaders
ColorMask RGB | A | 0 | any combination of R, G, B, A // used to select which channel to use with blending
// #PRAGMAS ==
// Pragmas are compilation directives
// They written inside the CGPROGRAM ENDCG block
#pragma vertex name // function that acts on every mesh vertex
#pragma fragment name // function that acts on every visible pixel
#pragma geometry name // DX10 geometry shader function. Acts per triangle. Turns on #pragma target 4.0
// Used for tessellation:
#pragma hull name // DX11 hull shader function. Acts once per patch. Turns on #pragma target 5.0
#pragma domain name // DX11 domain shader function. Turns on #pragma target 5.0
// Shader keywords:
#pragma multi_compile KEYWORD1 KEYWORD2 // enables multiple shader variants, see MULTI_COMPILE
#pragma skip_variants KEYWORD2 KEYWORD3 // skips compiling variants that you are sure are not used in the project
#pragma hardware_tier_variants renderer // automatically creates 3 variants: UNITY_HARDWARE_TIER1, UNITY_HARDWARE_TIER2 and UNITY_HARDWARE_TIER3
#pragma shader_feature KEYWORD // similar to multi_compile but unused variants will not be included in the build
// #VERTEX STRUCTS
// built-in shortcut appdatas:
appdata_base // position, normal and one texture coordinate.
appdata_tan // position, tangent, normal and one texture coordinate.
appdata_full // position, tangent, normal, four texture coordinates and color.
appdata_img // vertex shader input with position and one texture coordinate.
// or custom struct like:
struct appdata {
// built in types:
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0; // or some people write "uv"
float3 mycustomthing : TEXCOORD1; // for other custom things use TEXCOORD-number
};
// this is what appdata_full has:
struct appdata_full {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;
fixed4 color : COLOR;
#if defined(SHADER_API_XBOX360)
half4 texcoord2 : TEXCOORD2;
half4 texcoord3 : TEXCOORD3;
half4 texcoord4 : TEXCOORD4;
half4 texcoord5 : TEXCOORD5;
#endif
};
// #STRUCTS
// #VERTEX TO FRAGMENT STRUCT
// COLOR0, COLOR2.. etc is used for low precision 0-1 values
// TEXCOORD0, TEXCOORD1.. etc is used for high precision
// ^ this only matters on older, dx9 hardware
// Note: COLOR is the same as COLOR0, the same goes for TEXCOORD and TEXCOORD0 (TODO: check if true)
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR; // this didn't neeed to be COLOR (see above)
float4 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
uint vid : SV_VertexID // vertex ID, needs to be uint, requires #pragma target 3.5
}; // NOTE THE SEMICOLON HERE AFTER STRUCT!
// #VERTEX SHADER
// If you only need position, you can return just a float4 in clip space
float4 vert (float4 vertex : POSITION) : SV_POSITION
{
return UnityObjectToClipPos(vertex);
}
// if you have more data, you need to return a custom structure (see VERTEX TO FRAGMENT STRUCT)
// The values output from the vertex shader will be interpolated across the face of the rendered triangles,
// and the values at each pixel will be passed as inputs to the fragment shader.
v2f vert (appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex); // clip pos, NOT object position
// uv
o.uv = float4( v.texcoord.xy, 0, 0 );
// vertex color
o.color = v.color;
// VIEW (camera) - camera view
o.viewDir = normalize(WorldSpaceViewDir(v.vertex));
return o;
} // NO SEMICOLON AFTER FUNCTIONS!
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
vs
o.vertex = UnityObjectToClipPos(v.vertex);
?
// #FRAGMENT SHADER
// usually, you would use : SV_Target semantic, like:
fixed4 frag (v2f i) : SV_Target
{
return i.color // to visualize vertex color for eg.
}
// ---------
// but you can also return custom structure if you need additional colors (as in multiple render targets):
struct fragOutput
{
fixed4 color : SV_Target;
fixed4 color2 : SV_Target0; // for additional render targets .. add as many SV_TargetN as you need
fixed4 depth : SV_Depth; // overrides the z buffer value. Note on some GPUs it turns off depth buffer optimizations
};
fragOutput frag (v2f i)
{
fragOutput o;
o.color = fixed4(i.uv, 0, 0);
o.color2 = i.color;
o.depth = 0;
return o;
}
// ---------
// You can use VPOS to obtain screen space vertex position in pixels
// requires #pragma target 3.0
fixed4 frag (v2f i, UNITY_VPOS_TYPE screenPos : VPOS) : SV_Target
{
return fixed4(screenPos.xy, 0, 0); // X Y is in pixels!
}
// You can use VFACE to obtain face facing
// VFACE input positive for frontbaces,negative for backfaces
// requires #pragma target 3.0
fixed4 frag (fixed facing : VFACE) : SV_Target
{
return facing > 0 ? _ColorFront : _ColorBack;
}
// ---------
// #BUILT-IN FUNCTIONS
// #INCLUDES
#include HLSLSupport.cginc // (automatically included) Helper macros and definitions for cross-platform shader compilation.
#include UnityShaderVariables.cginc // (automatically included) Commonly used global variables.
#include UnityCG.cginc // commonly used helper functions.
#include AutoLight.cginc // lighting & shadowing functionality, e.g. surface shaders use this file internally.
#include Lighting.cginc // standard surface shader lighting models; automatically included when you’re writing surface shaders.
#include TerrainEngine.cginc // helper functions for Terrain & Vegetation shaders.
// Vertex transformation functions in UnityCG.cginc
float4 UnityObjectToClipPos(float3 pos) // Transforms a point from object space to the camera’s clip space in homogeneous coordinates. This is the equivalent of mul(UNITY_MATRIX_MVP, float4(pos, 1.0)), and should be used in its place.
float3 UnityObjectToViewPos(float3 pos) // Transforms a point from object space to view space. This is the equivalent of mul(UNITY_MATRIX_MV, float4(pos, 1.0)).xyz, and should be used in its place.
// Generic helper functions in UnityCG.cginc
float3 WorldSpaceViewDir (float4 v) // Returns world space direction (not normalized) from given object space vertex position towards the camera.
float3 ObjSpaceViewDir (float4 v) // Returns object space direction (not normalized) from given object space vertex position towards the camera.
float2 ParallaxOffset (half h, half height, half3 viewDir) // calculates UV offset for parallax normal mapping.
fixed Luminance (fixed3 c) // Converts color to luminance (grayscale).
fixed3 DecodeLightmap (fixed4 color) // Decodes color from Unity lightmap (RGBM or dLDR depending on platform).
float4 EncodeFloatRGBA (float v) // Encodes [0..1) range float into RGBA color, for storage in low precision render target.
float DecodeFloatRGBA (float4 enc) // Decodes RGBA color into a float.
float2 EncodeFloatRG (float v) // Encodes [0..1) range float into a float2.
float DecodeFloatRG (float2 enc) // Decodes a previously-encoded RG float.
float2 EncodeViewNormalStereo (float3 n) // Encodes view space normal into two numbers in 0..1 range.
float3 DecodeViewNormalStereo (float4 enc4) // Decodes view space normal from enc4.xy.
// Only in forward rendering
float3 WorldSpaceLightDir (float4 v) //Computes world space direction (not normalized) to light, given object space vertex position.
float3 ObjSpaceLightDir (float4 v) //Computes object space direction (not normalized) to light, given object space vertex position.
float3 Shade4PointLights (...) //Computes illumination from four point lights, with light data tightly packed into vectors. Forward rendering uses this to compute per-vertex lighting
// Screen-space helper functions in UnityCG.cginc
float4 ComputeScreenPos (float4 clipPos) // Computes texture coordinate for doing a screenspace-mapped texture sample. Input is clip space position. The function takes care of platform differences in render texture coordinates.
float4 ComputeGrabScreenPos (float4 clipPos) //Computes texture coordinate for sampling a GrabPass texure. Input is clip space position. The function takes care of platform differences in render texture coordinates.
float3 ShadeVertexLights (float4 vertex, float3 normal) // Computes illumination from four per-vertex lights and ambient, given object space position & normal.
UnityObjectToWorldNormal(normal); // To convert normals to world space
tex2D(_Tex, float2); // Texture sampling
// #Depth
float depth = SAMPLE_DEPTH_TEXTURE(_DepthTexture, uv); // Extracts depth from depth texture, the value is non-linear 1 for near plane, 0 for far plane
Linear01Depth(depth) // Converts raw depth to linear value where 0 is near plane and 1 is far plane
LinearEyeDepth(depth) // Converts raw depth to world units, 0 being the camera position and 1 unit corresponds to 1 world unit from the camera
// #BUILT-IN VARIABLES
// #Transformations
// TODOL CHeck if these still work
UNITY_MATRIX_MVP // Current model * view * projection matrix.
UNITY_MATRIX_MV // Current model * view matrix.
UNITY_MATRIX_V // Current view matrix.
UNITY_MATRIX_P // Current projection matrix.
UNITY_MATRIX_VP // Current view * projection matrix.
UNITY_MATRIX_T_MV // Transpose of model * view matrix.
UNITY_MATRIX_IT_MV // Inverse transpose of model * view matrix.
unity_WorldToObject // Inverse of current world matrix. _World2Object is obsolete
unity_ObjectToWorld // Current model matrix. _Object2World is obsolete
// To get UVs
newUV = TRANSFORM_TEX(oldUV, _MainTex); // replace with the name of your texture
// also remember to declare the float4 _MainTex_ST; variable before using the macro
// #Camera & Screen
_WorldSpaceCameraPos // float3 World space position of the camera.
_ProjectionParams // float4 x is 1.0 (or –1.0 if currently rendering with a flipped projection matrix), y is the camera’s near plane, z is the camera’s far plane and w is 1/FarPlane.
_ScreenParams // float4 x is the camera’s render target width in pixels, y is the camera’s render target height in pixels, z is 1.0 + 1.0/width and w is 1.0 + 1.0/height.
_ZBufferParams // float4 Used to linearize Z buffer values. x is (1-far/near), y is (far/near), z is (x/far) and w is (y/far).
unity_OrthoParams // float4 x is orthographic camera’s width, y is orthographic camera’s height, z is unused and w is 1.0 when camera is orthographic, 0.0 when perspective.
unity_CameraProjection // float4x4 Camera’s projection matrix.
unity_CameraInvProjection // float4x4 Inverse of camera’s projection matrix.
unity_CameraWorldClipPlanes[6] // float4 Camera frustum plane world space equations, in this order: left, right, bottom, top, near, far.
// #Time
_Time // float4 Time since level load (t/20, t, t*2, t*3), use to animate things inside the shaders.
_SinTime // float4 Sine of time: (t/8, t/4, t/2, t).
_CosTime // float4 Cosine of time: (t/8, t/4, t/2, t).
unity_DeltaTime // float4 Delta time: (dt, 1/dt, smoothDt, 1/smoothDt).
// #Lights
_LightColor0 // (declared in Lighting.cginc) fixed4 Light color
_WorldSpaceLightPos0 // float4 Directional lights: (world space direction, 0). Other lights: (world space position, 1).
_LightMatrix0 // (declared in AutoLight.cginc) float4x4 World-to-light matrix. Used to sample cookie & attenuation textures.
// forward only:
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0 // float4 (ForwardBase pass only) world space positions of first four non-important point lights.
unity_4LightAtten0 // float4 (ForwardBase pass only) attenuation factors of first four non-important point lights.
unity_LightColor // half4[4] (ForwardBase pass only) colors of of first four non-important point lights.
// deffered:
_LightColor //float4 Light color.
_LightMatrix0 //float4x4 World-to-light matrix. Used to sample cookie & attenuation textures.
// vertex lit:
unity_LightColor // half4[8] Light colors.
unity_LightPosition // float4[8] View-space light positions. (-direction,0) for directional lights; (position,1) for point/spot lights.
unity_LightAtten // half4[8] Light attenuation factors. x is cos(spotAngle/2) or –1 for non-spot lights; y is 1/cos(spotAngle/4) or 1 for non-spot lights; z is quadratic attenuation; w is squared light range.
unity_SpotDirection // float4[8] View-space spot light positions; (0,0,1,0) for non-spot lights.
// #Ambient
unity_AmbientSky // fixed4 Sky ambient lighting color in gradient ambient lighting case.
unity_AmbientEquator // fixed4 Equator ambient lighting color in gradient ambient lighting case.
unity_AmbientGround // fixed4 Ground ambient lighting color in gradient ambient lighting case.
// #Fog
// UNITY_LIGHTMODEL_AMBIENT // fixed4 Ambient lighting color (sky color in gradient ambient case). Legacy variable.
unity_FogColor // fixed4 Fog color.
unity_FogParams // float4 Parameters for fog calculation: (density / sqrt(ln(2)), density / ln(2), –1/(end-start), end/(end-start)). x is useful for Exp2 fog mode, y for Exp mode, z and w for Linear mode.
// #LOD
unity_LODFade // float4 Level-of-detail fade when using LODGroup. x is fade (0..1), y is fade quantized to 16 levels, z and w unused.
//-------------------
//-- #TRANSPARENCY --
//-------------------
// #ALPHA TEST TRANSPARENCY == // (aka #CUTOFF, #CUTOUT)
// To make a thing transparent:
// Make sure you use have these 2 tags:
Tags {
"Queue" = "AlphaTest"
"RenderType" = "TransparentCutout"
}
// In frag shader add:
clip(col.a - 0.5); // this 0.5 is the cutoff value, you can put it in a variable or property
// #ALPHA BLEND TRANSPARENCY
Tags {
"Queue"="Transparent"
"RenderType"="Transparent"
}
// Use one of the Blend modes see BLEND MODES
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB
ZWrite Off
//Cull Off // optional
//----------
//-- #FOG --
//----------
// Unity provides some useful macros for implementing built-in fog that can be configured in Lighting tab
#pragma multi_compile_fog
#include "UnityCG.cginc"
// inside your v2f struct add:
struct v2f {
...
UNITY_FOG_COORDS(1) // the number (1) is an unused TEXCOORD-number, so if you already have TEXCOORD1, use (2)
}
// in the vert shader add UNITY_TRANSFER_FOG
v2f vert (appdata_t v) {
v2f o;
...
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
// add UNITY_APPLY_FOG to your frag shader like
fixed4 frag (v2f i) : SV_Target {
...
UNITY_APPLY_FOG(i.fogCoord, col); // i.fogCoord is generated by UNITY_FOG_COORDS
return col;
}
//-----------------------------------
//-- #GPU INSTANCING - #INSTANCING --
//-----------------------------------
#pragma multi_compile_instancing
#include "UnityCG.cginc"
struct appdata_t {
...
UNITY_VERTEX_INPUT_INSTANCE_ID
};
// It seems that this block required even tho you don't have any per instance properties
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color) // Make _Color an instanced property (i.e. an array)
#define _Color_arr Props
UNITY_INSTANCING_BUFFER_END(Props)
v2f vert (appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
...
return o;
}
//-------------------------------
//-- #VR - #STEREO SINGLE PASS --
//-------------------------------
// To support stereo single pass you need to put these macros to pass the eye index to the fragment shader.
// For example, if you’re using any of the view or projection matrices or the camera position in the fragment shader
// these need the eye index to give the correct value, otherwise you’ll always access the left eye.
#include "UnityCG.cginc"
struct appdata_t {
...
UNITY_VERTEX_INPUT_INSTANCE_ID
}
struct v2f {
...
UNITY_VERTEX_OUTPUT_STEREO
}
v2f vert (appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(); // for single pass instanced. Not necessary?
UNITY_INITIALIZE_OUTPUT(v2f, o); // for single pass instanced. Not necessary?
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
...
return o;
}
// in frag shader, eye can be accessed with
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i)
// but it is not necessary, unless you are writing a post shader for example
//---------------------------------------------------
//-- #MULTI_COMPILE #SHADER KEYWORDS #KEYWORDS ------
//---------------------------------------------------
// using multi_compile directives enables making multiple shader variants #pragma multi_compile VARIANT_1 VARIANT_2 etc..
//-------------
// 2 VARIANTS
// with 2 variants, using __ will assume the "off" variant, you can write it like:
#pragma multi_compile __ THINGY
// use in code as:
#if defined(THINGY)
// do something when enabled
#else
// do something when disabled
#endif
// to create a property that will toggle it:
[Toggle(THINGY)] _Thingy("Thingy", Float) = 0 // the name of the property (_Thingy in this case) doesn't seem to be relevant
// or you can toggle from code using Shader.EnableKeyword/DisableKeyword or Material.EnableKeyword/DisableKeyword
//--------------------
// MULTIPLE VARIANTS
#pragma multi_compile _SOMETHING_THIS _SOMETHING_ELSE _SOMETHING_OTHERWISE
// use similar to the above:
#if defined(_SOMETHING_THIS)
// do something only when SOMETHING_1 is defined
#endif
// Auto-property for more than 2 variants.
// Each name will enable "property name" + underscore + "enum name", uppercased, shader keyword. Up to 9 names can be provided.
[KeywordEnum(This, Else, Otherwise)] _Something("Something?", Float) = 0 // Here the property name IS relevant
//-------------------------------
//-- #SHADOW CASTING ------------
//-------------------------------
// incomplete, not tested
Pass
{
Tags{ "LightMode" = "ShadowCaster" }
Fog{ Mode Off }
ZWrite On ZTest Less Cull Off
Offset 1, 1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f
{
V2F_SHADOW_CASTER;
};
v2f vert(appdata_base v)
{
v2f o;
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
return o;
}
float4 frag(v2f i) : COLOR
{
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
//-----------------------------------------------//
//-----------------------------------------------//
//---------- SURFACE SHADERS ------------//
//-----------------------------------------------//
//-----------------------------------------------//
// #SURFACE SHADERS
Shader "Example/Diffuse Simple"
{
Properties // See PROPERTIES
{
}
SubShader // Note, SubShader, unlike "Pass" in vert/frag shaders
{
Tags // see tags. Note, tags inside the SubShader {} unlike vert/frag shaders
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float4 color : COLOR;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = 1;
}
ENDCG
}
Fallback "Diffuse"
}
//
IN.worldNormal
IN.viewDir