-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTree Creator Bark Optimized.shader
2435 lines (2008 loc) · 120 KB
/
Tree Creator Bark Optimized.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Shader "HDRPNaturebyCK/Tree Creator Bark Optimized"
{
Properties
{
[NoScaleOffset] _MainTex("Base (RGB) Alpha (A)", 2D) = "white" {}
[NoScaleOffset] _BumpSpecMap("Normalmap (GA) Spec (R)", 2D) = "white" {}
_Color("Main Color", Color) = (0.8355054,1,0.8349056,0)
_Cutoff("Alpha cutoff", Range(0, 1)) = 0
[HideInInspector] _EmissionColor("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags
{
"RenderPipeline"="HDRenderPipeline"
"RenderType"="Transparent"
"Queue"="Transparent+0"
}
Pass
{
// based on HDLitPass.template
Name "META"
Tags { "LightMode" = "META" }
//-------------------------------------------------------------------------------------
// Render Modes (Blend, Cull, ZTest, Stencil, etc)
//-------------------------------------------------------------------------------------
Blend One OneMinusSrcAlpha
Cull Off
ZTest LEqual
ZWrite Off
ZClip [_ZClip]
// Default Stencil
//-------------------------------------------------------------------------------------
// End Render Modes
//-------------------------------------------------------------------------------------
HLSLPROGRAM
#pragma target 4.5
#pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
//#pragma enable_d3d11_debug_symbols
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma multi_compile _ LOD_FADE_CROSSFADE
//-------------------------------------------------------------------------------------
// Variant Definitions (active field translations to HDRP defines)
//-------------------------------------------------------------------------------------
#define _SURFACE_TYPE_TRANSPARENT 1
#define _BLENDMODE_ALPHA 1
#define _ENABLE_FOG_ON_TRANSPARENT 1
#define _ENERGY_CONSERVING_SPECULAR 1
#define _DISABLE_DECALS 1
#define _DISABLE_SSR 1
//-------------------------------------------------------------------------------------
// End Variant Definitions
//-------------------------------------------------------------------------------------
#pragma vertex Vert
#pragma fragment Frag
// This will be enabled in an upcoming change.
// #define SURFACE_GRADIENT
// If we use subsurface scattering, enable output split lighting (for forward pass)
#if defined(_MATERIAL_FEATURE_SUBSURFACE_SCATTERING) && !defined(_SURFACE_TYPE_TRANSPARENT)
#define OUTPUT_SPLIT_LIGHTING
#endif
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/NormalSurfaceGradient.hlsl"
// define FragInputs structure
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/FragInputs.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPass.cs.hlsl"
//-------------------------------------------------------------------------------------
// Defines
//-------------------------------------------------------------------------------------
#define SHADERPASS SHADERPASS_LIGHT_TRANSPORT
// this translates the new dependency tracker into the old preprocessor definitions for the existing HDRP shader code
#define ATTRIBUTES_NEED_NORMAL
#define ATTRIBUTES_NEED_TANGENT
#define ATTRIBUTES_NEED_TEXCOORD0
#define ATTRIBUTES_NEED_TEXCOORD1
#define ATTRIBUTES_NEED_TEXCOORD2
#define ATTRIBUTES_NEED_COLOR
#define VARYINGS_NEED_TEXCOORD0
//-------------------------------------------------------------------------------------
// End Defines
//-------------------------------------------------------------------------------------
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#ifdef DEBUG_DISPLAY
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl"
#endif
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"
#if (SHADERPASS == SHADERPASS_FORWARD)
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/Lighting.hlsl"
#define HAS_LIGHTLOOP
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl"
#else
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl"
#endif
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinUtilities.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialUtilities.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/DecalUtilities.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDecalData.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderGraphFunctions.hlsl"
// Used by SceneSelectionPass
int _ObjectId;
int _PassValue;
//-------------------------------------------------------------------------------------
// Interpolator Packing And Struct Declarations
//-------------------------------------------------------------------------------------
// Generated Type: AttributesMesh
struct AttributesMesh {
float3 positionOS : POSITION;
float3 normalOS : NORMAL; // optional
float4 tangentOS : TANGENT; // optional
float4 uv0 : TEXCOORD0; // optional
float4 uv1 : TEXCOORD1; // optional
float4 uv2 : TEXCOORD2; // optional
float4 color : COLOR; // optional
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC;
#endif // UNITY_ANY_INSTANCING_ENABLED
};
// Generated Type: VaryingsMeshToPS
struct VaryingsMeshToPS {
float4 positionCS : SV_Position;
float4 texCoord0; // optional
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC;
#endif // UNITY_ANY_INSTANCING_ENABLED
};
struct PackedVaryingsMeshToPS {
float4 interp00 : TEXCOORD0; // auto-packed
float4 positionCS : SV_Position; // unpacked
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC; // unpacked
#endif // UNITY_ANY_INSTANCING_ENABLED
};
PackedVaryingsMeshToPS PackVaryingsMeshToPS(VaryingsMeshToPS input)
{
PackedVaryingsMeshToPS output;
output.positionCS = input.positionCS;
output.interp00.xyzw = input.texCoord0;
#if UNITY_ANY_INSTANCING_ENABLED
output.instanceID = input.instanceID;
#endif // UNITY_ANY_INSTANCING_ENABLED
return output;
}
VaryingsMeshToPS UnpackVaryingsMeshToPS(PackedVaryingsMeshToPS input)
{
VaryingsMeshToPS output;
output.positionCS = input.positionCS;
output.texCoord0 = input.interp00.xyzw;
#if UNITY_ANY_INSTANCING_ENABLED
output.instanceID = input.instanceID;
#endif // UNITY_ANY_INSTANCING_ENABLED
return output;
}
// Generated Type: VaryingsMeshToDS
struct VaryingsMeshToDS {
float3 positionRWS;
float3 normalWS;
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC;
#endif // UNITY_ANY_INSTANCING_ENABLED
};
struct PackedVaryingsMeshToDS {
float3 interp00 : TEXCOORD0; // auto-packed
float3 interp01 : TEXCOORD1; // auto-packed
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC; // unpacked
#endif // UNITY_ANY_INSTANCING_ENABLED
};
PackedVaryingsMeshToDS PackVaryingsMeshToDS(VaryingsMeshToDS input)
{
PackedVaryingsMeshToDS output;
output.interp00.xyz = input.positionRWS;
output.interp01.xyz = input.normalWS;
#if UNITY_ANY_INSTANCING_ENABLED
output.instanceID = input.instanceID;
#endif // UNITY_ANY_INSTANCING_ENABLED
return output;
}
VaryingsMeshToDS UnpackVaryingsMeshToDS(PackedVaryingsMeshToDS input)
{
VaryingsMeshToDS output;
output.positionRWS = input.interp00.xyz;
output.normalWS = input.interp01.xyz;
#if UNITY_ANY_INSTANCING_ENABLED
output.instanceID = input.instanceID;
#endif // UNITY_ANY_INSTANCING_ENABLED
return output;
}
//-------------------------------------------------------------------------------------
// End Interpolator Packing And Struct Declarations
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
// Graph generated code
//-------------------------------------------------------------------------------------
// Shared Graph Properties (uniform inputs)
CBUFFER_START(UnityPerMaterial)
float4 _Color;
float _Cutoff;
float4 _EmissionColor;
CBUFFER_END
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex); float4 _MainTex_TexelSize;
TEXTURE2D(_BumpSpecMap); SAMPLER(sampler_BumpSpecMap); float4 _BumpSpecMap_TexelSize;
// Pixel Graph Inputs
struct SurfaceDescriptionInputs {
float3 TangentSpaceNormal; // optional
float4 uv0; // optional
};
// Pixel Graph Outputs
struct SurfaceDescription
{
float3 Albedo;
float3 Normal;
float3 BentNormal;
float CoatMask;
float Metallic;
float3 Emission;
float Smoothness;
float Occlusion;
float Alpha;
float AlphaClipThreshold;
};
// Shared Graph Node Functions
void Unity_Multiply_float (float4 A, float4 B, out float4 Out)
{
Out = A * B;
}
void Unity_Multiply_float (float A, float B, out float Out)
{
Out = A * B;
}
// Pixel Graph Evaluation
SurfaceDescription SurfaceDescriptionFunction(SurfaceDescriptionInputs IN)
{
SurfaceDescription surface = (SurfaceDescription)0;
float4 _Property_CCC4877_Out = _Color;
float4 _SampleTexture2D_16D55C5C_RGBA = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv0.xy);
float _SampleTexture2D_16D55C5C_R = _SampleTexture2D_16D55C5C_RGBA.r;
float _SampleTexture2D_16D55C5C_G = _SampleTexture2D_16D55C5C_RGBA.g;
float _SampleTexture2D_16D55C5C_B = _SampleTexture2D_16D55C5C_RGBA.b;
float _SampleTexture2D_16D55C5C_A = _SampleTexture2D_16D55C5C_RGBA.a;
float4 _Multiply_1BBCBAF5_Out;
Unity_Multiply_float(_Property_CCC4877_Out, _SampleTexture2D_16D55C5C_RGBA, _Multiply_1BBCBAF5_Out);
float4 _SampleTexture2D_B633C7A5_RGBA = SAMPLE_TEXTURE2D(_BumpSpecMap, sampler_BumpSpecMap, IN.uv0.xy);
_SampleTexture2D_B633C7A5_RGBA.rgb = UnpackNormalmapRGorAG(_SampleTexture2D_B633C7A5_RGBA);
float _SampleTexture2D_B633C7A5_R = _SampleTexture2D_B633C7A5_RGBA.r;
float _SampleTexture2D_B633C7A5_G = _SampleTexture2D_B633C7A5_RGBA.g;
float _SampleTexture2D_B633C7A5_B = _SampleTexture2D_B633C7A5_RGBA.b;
float _SampleTexture2D_B633C7A5_A = _SampleTexture2D_B633C7A5_RGBA.a;
float _Multiply_BB93496C_Out;
Unity_Multiply_float(_SampleTexture2D_B633C7A5_R, 0.19, _Multiply_BB93496C_Out);
float _Property_855AD5CD_Out = _Cutoff;
surface.Albedo = (_Multiply_1BBCBAF5_Out.xyz);
surface.Normal = IN.TangentSpaceNormal;
surface.BentNormal = IN.TangentSpaceNormal;
surface.CoatMask = 0;
surface.Metallic = 0;
surface.Emission = float3(0, 0, 0);
surface.Smoothness = _Multiply_BB93496C_Out;
surface.Occlusion = 1;
surface.Alpha = _SampleTexture2D_16D55C5C_A;
surface.AlphaClipThreshold = _Property_855AD5CD_Out;
return surface;
}
//-------------------------------------------------------------------------------------
// End graph generated code
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
// TEMPLATE INCLUDE : SharedCode.template.hlsl
//-------------------------------------------------------------------------------------
FragInputs BuildFragInputs(VaryingsMeshToPS input)
{
FragInputs output;
ZERO_INITIALIZE(FragInputs, output);
// Init to some default value to make the computer quiet (else it output 'divide by zero' warning even if value is not used).
// TODO: this is a really poor workaround, but the variable is used in a bunch of places
// to compute normals which are then passed on elsewhere to compute other values...
output.worldToTangent = k_identity3x3;
output.positionSS = input.positionCS; // input.positionCS is SV_Position
output.texCoord0 = input.texCoord0;
#if SHADER_STAGE_FRAGMENT
#endif // SHADER_STAGE_FRAGMENT
return output;
}
SurfaceDescriptionInputs FragInputsToSurfaceDescriptionInputs(FragInputs input, float3 viewWS)
{
SurfaceDescriptionInputs output;
ZERO_INITIALIZE(SurfaceDescriptionInputs, output);
output.TangentSpaceNormal = float3(0.0f, 0.0f, 1.0f);
output.uv0 = input.texCoord0;
return output;
}
// existing HDRP code uses the combined function to go directly from packed to frag inputs
FragInputs UnpackVaryingsMeshToFragInputs(PackedVaryingsMeshToPS input)
{
UNITY_SETUP_INSTANCE_ID(input);
VaryingsMeshToPS unpacked= UnpackVaryingsMeshToPS(input);
return BuildFragInputs(unpacked);
}
//-------------------------------------------------------------------------------------
// END TEMPLATE INCLUDE : SharedCode.template.hlsl
//-------------------------------------------------------------------------------------
void BuildSurfaceData(FragInputs fragInputs, inout SurfaceDescription surfaceDescription, float3 V, PositionInputs posInput, out SurfaceData surfaceData, out float3 bentNormalWS)
{
// setup defaults -- these are used if the graph doesn't output a value
ZERO_INITIALIZE(SurfaceData, surfaceData);
// copy across graph values, if defined
surfaceData.baseColor = surfaceDescription.Albedo;
surfaceData.perceptualSmoothness = surfaceDescription.Smoothness;
surfaceData.ambientOcclusion = surfaceDescription.Occlusion;
surfaceData.metallic = surfaceDescription.Metallic;
surfaceData.coatMask = surfaceDescription.CoatMask;
#ifdef _HAS_REFRACTION
if (_EnableSSRefraction)
{
surfaceData.transmittanceMask = (1.0 - surfaceDescription.Alpha);
surfaceDescription.Alpha = 1.0;
}
else
{
surfaceData.ior = 1.0;
surfaceData.transmittanceColor = float3(1.0, 1.0, 1.0);
surfaceData.atDistance = 1.0;
surfaceData.transmittanceMask = 0.0;
surfaceDescription.Alpha = 1.0;
}
#else
surfaceData.ior = 1.0;
surfaceData.transmittanceColor = float3(1.0, 1.0, 1.0);
surfaceData.atDistance = 1.0;
surfaceData.transmittanceMask = 0.0;
#endif
// These static material feature allow compile time optimization
surfaceData.materialFeatures = MATERIALFEATUREFLAGS_LIT_STANDARD;
#ifdef _MATERIAL_FEATURE_SUBSURFACE_SCATTERING
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_SUBSURFACE_SCATTERING;
#endif
#ifdef _MATERIAL_FEATURE_TRANSMISSION
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_TRANSMISSION;
#endif
#ifdef _MATERIAL_FEATURE_ANISOTROPY
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_ANISOTROPY;
#endif
#ifdef _MATERIAL_FEATURE_IRIDESCENCE
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_IRIDESCENCE;
#endif
#ifdef _MATERIAL_FEATURE_SPECULAR_COLOR
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_SPECULAR_COLOR;
#endif
#if defined (_MATERIAL_FEATURE_SPECULAR_COLOR) && defined (_ENERGY_CONSERVING_SPECULAR)
// Require to have setup baseColor
// Reproduce the energy conservation done in legacy Unity. Not ideal but better for compatibility and users can unchek it
surfaceData.baseColor *= (1.0 - Max3(surfaceData.specularColor.r, surfaceData.specularColor.g, surfaceData.specularColor.b));
#endif
float3 doubleSidedConstants = float3(1.0, 1.0, 1.0);
// tangent-space normal
float3 normalTS = float3(0.0f, 0.0f, 1.0f);
normalTS = surfaceDescription.Normal;
// compute world space normal
GetNormalWS(fragInputs, normalTS, surfaceData.normalWS, doubleSidedConstants);
bentNormalWS = surfaceData.normalWS;
surfaceData.geomNormalWS = fragInputs.worldToTangent[2];
surfaceData.tangentWS = normalize(fragInputs.worldToTangent[0].xyz); // The tangent is not normalize in worldToTangent for mikkt. TODO: Check if it expected that we normalize with Morten. Tag: SURFACE_GRADIENT
surfaceData.tangentWS = Orthonormalize(surfaceData.tangentWS, surfaceData.normalWS);
// By default we use the ambient occlusion with Tri-ace trick (apply outside) for specular occlusion.
// If user provide bent normal then we process a better term
#if defined(_SPECULAR_OCCLUSION_CUSTOM)
// Just use the value passed through via the slot (not active otherwise)
#elif defined(_SPECULAR_OCCLUSION_FROM_AO_BENT_NORMAL)
// If we have bent normal and ambient occlusion, process a specular occlusion
surfaceData.specularOcclusion = GetSpecularOcclusionFromBentAO(V, bentNormalWS, surfaceData.normalWS, surfaceData.ambientOcclusion, PerceptualSmoothnessToPerceptualRoughness(surfaceData.perceptualSmoothness));
#elif defined(_AMBIENT_OCCLUSION) && defined(_SPECULAR_OCCLUSION_FROM_AO)
surfaceData.specularOcclusion = GetSpecularOcclusionFromAmbientOcclusion(ClampNdotV(dot(surfaceData.normalWS, V)), surfaceData.ambientOcclusion, PerceptualSmoothnessToRoughness(surfaceData.perceptualSmoothness));
#else
surfaceData.specularOcclusion = 1.0;
#endif
#if HAVE_DECALS
if (_EnableDecals)
{
DecalSurfaceData decalSurfaceData = GetDecalSurfaceData(posInput, surfaceDescription.Alpha);
ApplyDecalToSurfaceData(decalSurfaceData, surfaceData);
}
#endif
#ifdef _ENABLE_GEOMETRIC_SPECULAR_AA
surfaceData.perceptualSmoothness = GeometricNormalFiltering(surfaceData.perceptualSmoothness, fragInputs.worldToTangent[2], surfaceDescription.SpecularAAScreenSpaceVariance, surfaceDescription.SpecularAAThreshold);
#endif
#ifdef DEBUG_DISPLAY
if (_DebugMipMapMode != DEBUGMIPMAPMODE_NONE)
{
// TODO: need to update mip info
surfaceData.metallic = 0;
}
// We need to call ApplyDebugToSurfaceData after filling the surfarcedata and before filling builtinData
// as it can modify attribute use for static lighting
ApplyDebugToSurfaceData(fragInputs.worldToTangent, surfaceData);
#endif
}
void GetSurfaceAndBuiltinData(FragInputs fragInputs, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
{
#ifdef LOD_FADE_CROSSFADE // enable dithering LOD transition if user select CrossFade transition in LOD group
uint3 fadeMaskSeed = asuint((int3)(V * _ScreenSize.xyx)); // Quantize V to _ScreenSize values
LODDitheringTransition(fadeMaskSeed, unity_LODFade.x);
#endif
float3 doubleSidedConstants = float3(1.0, 1.0, 1.0);
ApplyDoubleSidedFlipOrMirror(fragInputs, doubleSidedConstants);
SurfaceDescriptionInputs surfaceDescriptionInputs = FragInputsToSurfaceDescriptionInputs(fragInputs, V);
SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
// Perform alpha test very early to save performance (a killed pixel will not sample textures)
// TODO: split graph evaluation to grab just alpha dependencies first? tricky..
DoAlphaTest(surfaceDescription.Alpha, surfaceDescription.AlphaClipThreshold);
float3 bentNormalWS;
BuildSurfaceData(fragInputs, surfaceDescription, V, posInput, surfaceData, bentNormalWS);
// Builtin Data
// For back lighting we use the oposite vertex normal
InitBuiltinData(surfaceDescription.Alpha, bentNormalWS, -fragInputs.worldToTangent[2], fragInputs.positionRWS, fragInputs.texCoord1, fragInputs.texCoord2, builtinData);
builtinData.emissiveColor = surfaceDescription.Emission;
// TODO: Handle depth offset
//builtinData.depthOffset = 0.0;
#if (SHADERPASS == SHADERPASS_DISTORTION)
builtinData.distortion = surfaceDescription.Distortion;
builtinData.distortionBlur = surfaceDescription.DistortionBlur;
#else
builtinData.distortion = float2(0.0, 0.0);
builtinData.distortionBlur = 0.0;
#endif
PostInitBuiltinData(V, posInput, surfaceData, builtinData);
}
//-------------------------------------------------------------------------------------
// Pass Includes
//-------------------------------------------------------------------------------------
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassLightTransport.hlsl"
//-------------------------------------------------------------------------------------
// End Pass Includes
//-------------------------------------------------------------------------------------
ENDHLSL
}
Pass
{
// based on HDLitPass.template
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }
//-------------------------------------------------------------------------------------
// Render Modes (Blend, Cull, ZTest, Stencil, etc)
//-------------------------------------------------------------------------------------
Blend One Zero
Cull Back
ZTest LEqual
ZWrite On
ZClip [_ZClip]
// Default Stencil
ColorMask 0
//-------------------------------------------------------------------------------------
// End Render Modes
//-------------------------------------------------------------------------------------
HLSLPROGRAM
#pragma target 4.5
#pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
//#pragma enable_d3d11_debug_symbols
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma multi_compile _ LOD_FADE_CROSSFADE
//-------------------------------------------------------------------------------------
// Variant Definitions (active field translations to HDRP defines)
//-------------------------------------------------------------------------------------
#define _SURFACE_TYPE_TRANSPARENT 1
#define _BLENDMODE_ALPHA 1
#define _ENABLE_FOG_ON_TRANSPARENT 1
#define _ENERGY_CONSERVING_SPECULAR 1
#define _DISABLE_DECALS 1
#define _DISABLE_SSR 1
//-------------------------------------------------------------------------------------
// End Variant Definitions
//-------------------------------------------------------------------------------------
#pragma vertex Vert
#pragma fragment Frag
// This will be enabled in an upcoming change.
// #define SURFACE_GRADIENT
// If we use subsurface scattering, enable output split lighting (for forward pass)
#if defined(_MATERIAL_FEATURE_SUBSURFACE_SCATTERING) && !defined(_SURFACE_TYPE_TRANSPARENT)
#define OUTPUT_SPLIT_LIGHTING
#endif
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/NormalSurfaceGradient.hlsl"
// define FragInputs structure
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/FragInputs.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPass.cs.hlsl"
//-------------------------------------------------------------------------------------
// Defines
//-------------------------------------------------------------------------------------
#define SHADERPASS SHADERPASS_SHADOWS
#define USE_LEGACY_UNITY_MATRIX_VARIABLES
// this translates the new dependency tracker into the old preprocessor definitions for the existing HDRP shader code
#define ATTRIBUTES_NEED_TEXCOORD0
#define VARYINGS_NEED_TEXCOORD0
//-------------------------------------------------------------------------------------
// End Defines
//-------------------------------------------------------------------------------------
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#ifdef DEBUG_DISPLAY
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl"
#endif
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"
#if (SHADERPASS == SHADERPASS_FORWARD)
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/Lighting.hlsl"
#define HAS_LIGHTLOOP
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl"
#else
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl"
#endif
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinUtilities.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialUtilities.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/DecalUtilities.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDecalData.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderGraphFunctions.hlsl"
// Used by SceneSelectionPass
int _ObjectId;
int _PassValue;
//-------------------------------------------------------------------------------------
// Interpolator Packing And Struct Declarations
//-------------------------------------------------------------------------------------
// Generated Type: AttributesMesh
struct AttributesMesh {
float3 positionOS : POSITION;
float4 uv0 : TEXCOORD0; // optional
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC;
#endif // UNITY_ANY_INSTANCING_ENABLED
};
// Generated Type: VaryingsMeshToPS
struct VaryingsMeshToPS {
float4 positionCS : SV_Position;
float4 texCoord0; // optional
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC;
#endif // UNITY_ANY_INSTANCING_ENABLED
};
struct PackedVaryingsMeshToPS {
float4 interp00 : TEXCOORD0; // auto-packed
float4 positionCS : SV_Position; // unpacked
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC; // unpacked
#endif // UNITY_ANY_INSTANCING_ENABLED
};
PackedVaryingsMeshToPS PackVaryingsMeshToPS(VaryingsMeshToPS input)
{
PackedVaryingsMeshToPS output;
output.positionCS = input.positionCS;
output.interp00.xyzw = input.texCoord0;
#if UNITY_ANY_INSTANCING_ENABLED
output.instanceID = input.instanceID;
#endif // UNITY_ANY_INSTANCING_ENABLED
return output;
}
VaryingsMeshToPS UnpackVaryingsMeshToPS(PackedVaryingsMeshToPS input)
{
VaryingsMeshToPS output;
output.positionCS = input.positionCS;
output.texCoord0 = input.interp00.xyzw;
#if UNITY_ANY_INSTANCING_ENABLED
output.instanceID = input.instanceID;
#endif // UNITY_ANY_INSTANCING_ENABLED
return output;
}
// Generated Type: VaryingsMeshToDS
struct VaryingsMeshToDS {
float3 positionRWS;
float3 normalWS;
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC;
#endif // UNITY_ANY_INSTANCING_ENABLED
};
struct PackedVaryingsMeshToDS {
float3 interp00 : TEXCOORD0; // auto-packed
float3 interp01 : TEXCOORD1; // auto-packed
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC; // unpacked
#endif // UNITY_ANY_INSTANCING_ENABLED
};
PackedVaryingsMeshToDS PackVaryingsMeshToDS(VaryingsMeshToDS input)
{
PackedVaryingsMeshToDS output;
output.interp00.xyz = input.positionRWS;
output.interp01.xyz = input.normalWS;
#if UNITY_ANY_INSTANCING_ENABLED
output.instanceID = input.instanceID;
#endif // UNITY_ANY_INSTANCING_ENABLED
return output;
}
VaryingsMeshToDS UnpackVaryingsMeshToDS(PackedVaryingsMeshToDS input)
{
VaryingsMeshToDS output;
output.positionRWS = input.interp00.xyz;
output.normalWS = input.interp01.xyz;
#if UNITY_ANY_INSTANCING_ENABLED
output.instanceID = input.instanceID;
#endif // UNITY_ANY_INSTANCING_ENABLED
return output;
}
//-------------------------------------------------------------------------------------
// End Interpolator Packing And Struct Declarations
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
// Graph generated code
//-------------------------------------------------------------------------------------
// Shared Graph Properties (uniform inputs)
CBUFFER_START(UnityPerMaterial)
float4 _Color;
float _Cutoff;
float4 _EmissionColor;
CBUFFER_END
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex); float4 _MainTex_TexelSize;
TEXTURE2D(_BumpSpecMap); SAMPLER(sampler_BumpSpecMap); float4 _BumpSpecMap_TexelSize;
// Pixel Graph Inputs
struct SurfaceDescriptionInputs {
float3 TangentSpaceNormal; // optional
float4 uv0; // optional
};
// Pixel Graph Outputs
struct SurfaceDescription
{
float Alpha;
float AlphaClipThreshold;
};
// Shared Graph Node Functions
// Pixel Graph Evaluation
SurfaceDescription SurfaceDescriptionFunction(SurfaceDescriptionInputs IN)
{
SurfaceDescription surface = (SurfaceDescription)0;
float4 _SampleTexture2D_16D55C5C_RGBA = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv0.xy);
float _SampleTexture2D_16D55C5C_R = _SampleTexture2D_16D55C5C_RGBA.r;
float _SampleTexture2D_16D55C5C_G = _SampleTexture2D_16D55C5C_RGBA.g;
float _SampleTexture2D_16D55C5C_B = _SampleTexture2D_16D55C5C_RGBA.b;
float _SampleTexture2D_16D55C5C_A = _SampleTexture2D_16D55C5C_RGBA.a;
float _Property_855AD5CD_Out = _Cutoff;
surface.Alpha = _SampleTexture2D_16D55C5C_A;
surface.AlphaClipThreshold = _Property_855AD5CD_Out;
return surface;
}
//-------------------------------------------------------------------------------------
// End graph generated code
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
// TEMPLATE INCLUDE : SharedCode.template.hlsl
//-------------------------------------------------------------------------------------
FragInputs BuildFragInputs(VaryingsMeshToPS input)
{
FragInputs output;
ZERO_INITIALIZE(FragInputs, output);
// Init to some default value to make the computer quiet (else it output 'divide by zero' warning even if value is not used).
// TODO: this is a really poor workaround, but the variable is used in a bunch of places
// to compute normals which are then passed on elsewhere to compute other values...
output.worldToTangent = k_identity3x3;
output.positionSS = input.positionCS; // input.positionCS is SV_Position
output.texCoord0 = input.texCoord0;
#if SHADER_STAGE_FRAGMENT
#endif // SHADER_STAGE_FRAGMENT
return output;
}
SurfaceDescriptionInputs FragInputsToSurfaceDescriptionInputs(FragInputs input, float3 viewWS)
{
SurfaceDescriptionInputs output;
ZERO_INITIALIZE(SurfaceDescriptionInputs, output);
output.TangentSpaceNormal = float3(0.0f, 0.0f, 1.0f);
output.uv0 = input.texCoord0;
return output;
}
// existing HDRP code uses the combined function to go directly from packed to frag inputs
FragInputs UnpackVaryingsMeshToFragInputs(PackedVaryingsMeshToPS input)
{
UNITY_SETUP_INSTANCE_ID(input);
VaryingsMeshToPS unpacked= UnpackVaryingsMeshToPS(input);
return BuildFragInputs(unpacked);
}
//-------------------------------------------------------------------------------------
// END TEMPLATE INCLUDE : SharedCode.template.hlsl
//-------------------------------------------------------------------------------------
void BuildSurfaceData(FragInputs fragInputs, inout SurfaceDescription surfaceDescription, float3 V, PositionInputs posInput, out SurfaceData surfaceData, out float3 bentNormalWS)
{
// setup defaults -- these are used if the graph doesn't output a value
ZERO_INITIALIZE(SurfaceData, surfaceData);
// copy across graph values, if defined
#ifdef _HAS_REFRACTION
if (_EnableSSRefraction)
{
surfaceData.transmittanceMask = (1.0 - surfaceDescription.Alpha);
surfaceDescription.Alpha = 1.0;
}
else
{
surfaceData.ior = 1.0;
surfaceData.transmittanceColor = float3(1.0, 1.0, 1.0);
surfaceData.atDistance = 1.0;
surfaceData.transmittanceMask = 0.0;
surfaceDescription.Alpha = 1.0;
}
#else
surfaceData.ior = 1.0;
surfaceData.transmittanceColor = float3(1.0, 1.0, 1.0);
surfaceData.atDistance = 1.0;
surfaceData.transmittanceMask = 0.0;
#endif
// These static material feature allow compile time optimization
surfaceData.materialFeatures = MATERIALFEATUREFLAGS_LIT_STANDARD;
#ifdef _MATERIAL_FEATURE_SUBSURFACE_SCATTERING
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_SUBSURFACE_SCATTERING;
#endif
#ifdef _MATERIAL_FEATURE_TRANSMISSION
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_TRANSMISSION;
#endif
#ifdef _MATERIAL_FEATURE_ANISOTROPY
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_ANISOTROPY;
#endif
#ifdef _MATERIAL_FEATURE_IRIDESCENCE
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_IRIDESCENCE;
#endif
#ifdef _MATERIAL_FEATURE_SPECULAR_COLOR
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_LIT_SPECULAR_COLOR;
#endif
#if defined (_MATERIAL_FEATURE_SPECULAR_COLOR) && defined (_ENERGY_CONSERVING_SPECULAR)
// Require to have setup baseColor
// Reproduce the energy conservation done in legacy Unity. Not ideal but better for compatibility and users can unchek it
surfaceData.baseColor *= (1.0 - Max3(surfaceData.specularColor.r, surfaceData.specularColor.g, surfaceData.specularColor.b));
#endif
float3 doubleSidedConstants = float3(1.0, 1.0, 1.0);
// tangent-space normal
float3 normalTS = float3(0.0f, 0.0f, 1.0f);
// compute world space normal
GetNormalWS(fragInputs, normalTS, surfaceData.normalWS, doubleSidedConstants);
bentNormalWS = surfaceData.normalWS;
surfaceData.geomNormalWS = fragInputs.worldToTangent[2];
surfaceData.tangentWS = normalize(fragInputs.worldToTangent[0].xyz); // The tangent is not normalize in worldToTangent for mikkt. TODO: Check if it expected that we normalize with Morten. Tag: SURFACE_GRADIENT
surfaceData.tangentWS = Orthonormalize(surfaceData.tangentWS, surfaceData.normalWS);
// By default we use the ambient occlusion with Tri-ace trick (apply outside) for specular occlusion.
// If user provide bent normal then we process a better term
#if defined(_SPECULAR_OCCLUSION_CUSTOM)
// Just use the value passed through via the slot (not active otherwise)
#elif defined(_SPECULAR_OCCLUSION_FROM_AO_BENT_NORMAL)
// If we have bent normal and ambient occlusion, process a specular occlusion
surfaceData.specularOcclusion = GetSpecularOcclusionFromBentAO(V, bentNormalWS, surfaceData.normalWS, surfaceData.ambientOcclusion, PerceptualSmoothnessToPerceptualRoughness(surfaceData.perceptualSmoothness));
#elif defined(_AMBIENT_OCCLUSION) && defined(_SPECULAR_OCCLUSION_FROM_AO)
surfaceData.specularOcclusion = GetSpecularOcclusionFromAmbientOcclusion(ClampNdotV(dot(surfaceData.normalWS, V)), surfaceData.ambientOcclusion, PerceptualSmoothnessToRoughness(surfaceData.perceptualSmoothness));
#else
surfaceData.specularOcclusion = 1.0;
#endif
#if HAVE_DECALS
if (_EnableDecals)
{
DecalSurfaceData decalSurfaceData = GetDecalSurfaceData(posInput, surfaceDescription.Alpha);
ApplyDecalToSurfaceData(decalSurfaceData, surfaceData);
}
#endif
#ifdef _ENABLE_GEOMETRIC_SPECULAR_AA
surfaceData.perceptualSmoothness = GeometricNormalFiltering(surfaceData.perceptualSmoothness, fragInputs.worldToTangent[2], surfaceDescription.SpecularAAScreenSpaceVariance, surfaceDescription.SpecularAAThreshold);
#endif
#ifdef DEBUG_DISPLAY
if (_DebugMipMapMode != DEBUGMIPMAPMODE_NONE)
{
// TODO: need to update mip info
surfaceData.metallic = 0;
}
// We need to call ApplyDebugToSurfaceData after filling the surfarcedata and before filling builtinData
// as it can modify attribute use for static lighting
ApplyDebugToSurfaceData(fragInputs.worldToTangent, surfaceData);
#endif
}
void GetSurfaceAndBuiltinData(FragInputs fragInputs, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
{
#ifdef LOD_FADE_CROSSFADE // enable dithering LOD transition if user select CrossFade transition in LOD group
uint3 fadeMaskSeed = asuint((int3)(V * _ScreenSize.xyx)); // Quantize V to _ScreenSize values
LODDitheringTransition(fadeMaskSeed, unity_LODFade.x);
#endif
float3 doubleSidedConstants = float3(1.0, 1.0, 1.0);
ApplyDoubleSidedFlipOrMirror(fragInputs, doubleSidedConstants);
SurfaceDescriptionInputs surfaceDescriptionInputs = FragInputsToSurfaceDescriptionInputs(fragInputs, V);
SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
// Perform alpha test very early to save performance (a killed pixel will not sample textures)
// TODO: split graph evaluation to grab just alpha dependencies first? tricky..
DoAlphaTest(surfaceDescription.Alpha, surfaceDescription.AlphaClipThreshold);
float3 bentNormalWS;
BuildSurfaceData(fragInputs, surfaceDescription, V, posInput, surfaceData, bentNormalWS);
// Builtin Data
// For back lighting we use the oposite vertex normal
InitBuiltinData(surfaceDescription.Alpha, bentNormalWS, -fragInputs.worldToTangent[2], fragInputs.positionRWS, fragInputs.texCoord1, fragInputs.texCoord2, builtinData);
// TODO: Handle depth offset
//builtinData.depthOffset = 0.0;
#if (SHADERPASS == SHADERPASS_DISTORTION)
builtinData.distortion = surfaceDescription.Distortion;
builtinData.distortionBlur = surfaceDescription.DistortionBlur;
#else
builtinData.distortion = float2(0.0, 0.0);
builtinData.distortionBlur = 0.0;
#endif
PostInitBuiltinData(V, posInput, surfaceData, builtinData);
}
//-------------------------------------------------------------------------------------
// Pass Includes
//-------------------------------------------------------------------------------------
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl"
//-------------------------------------------------------------------------------------
// End Pass Includes
//-------------------------------------------------------------------------------------
ENDHLSL
}
Pass
{
// based on HDLitPass.template
Name "SceneSelectionPass"
Tags { "LightMode" = "SceneSelectionPass" }
//-------------------------------------------------------------------------------------
// Render Modes (Blend, Cull, ZTest, Stencil, etc)
//-------------------------------------------------------------------------------------
Blend One OneMinusSrcAlpha
Cull Back
ZTest LEqual
ZWrite Off
ZClip [_ZClip]
// Default Stencil