-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicroSplat.shader
1665 lines (1371 loc) · 61.7 KB
/
MicroSplat.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
//////////////////////////////////////////////////////
// MicroSplat
// Copyright (c) Jason Booth
//
// Auto-generated shader code, don't hand edit!
// Compiled with MicroSplat 2.6
// Unity : 5.6.6f2
// Platform : OSXEditor
// RenderLoop : Surface Shader
//////////////////////////////////////////////////////
Shader "MicroSplat/Example" {
Properties {
[HideInInspector] _Control0 ("Control0", 2D) = "red" {}
[HideInInspector] _Control1 ("Control1", 2D) = "black" {}
// Splats
[NoScaleOffset]_Diffuse ("Diffuse Array", 2DArray) = "white" {}
[NoScaleOffset]_NormalSAO ("Normal Array", 2DArray) = "bump" {}
[NoScaleOffset]_PerTexProps("Per Texture Properties", 2D) = "black" {}
[NoScaleOffset]_PerPixelNormal("Per Pixel Normal", 2D) = "bump" {}
_Contrast("Blend Contrast", Range(0.01, 0.99)) = 0.4
_UVScale("UV Scales", Vector) = (45, 45, 0, 0)
}
CGINCLUDE
ENDCG
SubShader {
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry+100" "SplatCount" = "8"}
Cull Back
ZTest LEqual
CGPROGRAM
#pragma exclude_renderers d3d9
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityStandardBRDF.cginc"
#pragma surface surf Standard vertex:vert fullforwardshadows addshadow
#pragma target 3.5
#define _MICROSPLAT 1
#define _USEGRADMIP 1
#define _MAX8TEXTURES 1
#define _MSRENDERLOOP_SURFACESHADER 1
#define _MSRENDERLOOP_SURFACESHADER 1
struct TriGradMipFormat
{
float4 d0;
float4 d1;
float4 d2;
};
#if _VSSHADOWMAP
float4 gVSSunDirection;
#endif
#if _TRIPLANAR
#if _USEGRADMIP
#define MIPFORMAT TriGradMipFormat
#define INITMIPFORMAT (TriGradMipFormat)0;
#define MIPFROMATRAW float4
#else
#define MIPFORMAT float3
#define INITMIPFORMAT 0;
#define MIPFROMATRAW float3
#endif
#else
#if _USEGRADMIP
#define MIPFORMAT float4
#define INITMIPFORMAT 0;
#define MIPFROMATRAW float4
#else
#define MIPFORMAT float
#define INITMIPFORMAT 0;
#define MIPFROMATRAW float
#endif
#endif
#if _MESHSUBARRAY
half4 _MeshSubArrayIndexes;
#endif
// splat
UNITY_DECLARE_TEX2DARRAY(_Diffuse);
float4 _Diffuse_TexelSize;
UNITY_DECLARE_TEX2DARRAY(_NormalSAO);
float4 _NormalSAO_TexelSize;
#if _PACKINGHQ
UNITY_DECLARE_TEX2DARRAY(_SmoothAO);
#endif
#if _USEEMISSIVEMETAL
UNITY_DECLARE_TEX2DARRAY(_EmissiveMetal);
float4 _EmissiveMetal_TexelSize;
#endif
half _Contrast;
UNITY_DECLARE_TEX2D_NOSAMPLER(_PerPixelNormal);
UNITY_DECLARE_TEX2D(_Control0);
#if _CUSTOMSPLATTEXTURES
UNITY_DECLARE_TEX2D(_CustomControl0);
#if !_MAX4TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_CustomControl1);
#endif
#if !_MAX4TEXTURES && !_MAX8TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_CustomControl2);
#endif
#if !_MAX4TEXTURES && !_MAX8TEXTURES && !_MAX12TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_CustomControl3);
#endif
#if _MAX20TEXTURES || _MAX24TEXTURES || _MAX28TEXTURES || _MAX32TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_CustomControl4);
#endif
#if _MAX24TEXTURES || _MAX28TEXTURES || _MAX32TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_CustomControl5);
#endif
#if _MAX28TEXTURES || _MAX32TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_CustomControl6);
#endif
#if _MAX32TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_CustomControl7);
#endif
#else
#if !_MAX4TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_Control1);
#endif
#if !_MAX4TEXTURES && !_MAX8TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_Control2);
#endif
#if !_MAX4TEXTURES && !_MAX8TEXTURES && !_MAX12TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_Control3);
#endif
#if _MAX20TEXTURES || _MAX24TEXTURES || _MAX28TEXTURES || _MAX32TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_Control4);
#endif
#if _MAX24TEXTURES || _MAX28TEXTURES || _MAX32TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_Control5);
#endif
#if _MAX28TEXTURES || _MAX32TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_Control6);
#endif
#if _MAX32TEXTURES
UNITY_DECLARE_TEX2D_NOSAMPLER(_Control7);
#endif
#endif
sampler2D_float _PerTexProps;
float4 _UVScale; // scale and offset
struct Input
{
float2 uv_Control0;
#if _MICROMESH && _MESHUV2
float2 uv2_Diffuse;
#endif
float3 viewDir;
float3 worldPos;
float3 worldNormal;
#if _TERRAINBLENDING || _VSSHADOWMAP || _WINDSHADOWS || _SNOWSHADOWS
fixed4 color : COLOR;
#endif
#if _MSRENDERLOOP_SURFACESHADER
INTERNAL_DATA
#else
float3x3 TBN;
#endif
};
struct TriplanarConfig
{
float3x3 uv0;
float3x3 uv1;
float3x3 uv2;
float3x3 uv3;
half3 pN;
half3 pN0;
half3 pN1;
half3 pN2;
half3 pN3;
half3 axisSign;
Input IN;
};
struct Config
{
float2 uv;
float3 uv0;
float3 uv1;
float3 uv2;
float3 uv3;
half4 cluster0;
half4 cluster1;
half4 cluster2;
half4 cluster3;
};
struct MicroSplatLayer
{
half3 Albedo;
half3 Normal;
half Smoothness;
half Occlusion;
half Metallic;
half Height;
half3 Emission;
#if _MESHOVERLAYSPLATS || _MESHCOMBINED
half Alpha;
#endif
};
struct appdata
{
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
float2 texcoord2 : TEXCOORD2;
#if _TERRAINBLENDING || _VSSHADOWMAP || _WINDSHADOWS || _SNOWSHADOWS
half4 color : COLOR;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
// raw, unblended samples from arrays
struct RawSamples
{
half4 albedo0;
half4 albedo1;
half4 albedo2;
half4 albedo3;
half4 normSAO0;
half4 normSAO1;
half4 normSAO2;
half4 normSAO3;
#if _USEEMISSIVEMETAL || _GLOBALEMIS || _GLOBALSMOOTHAOMETAL
half4 emisMetal0;
half4 emisMetal1;
half4 emisMetal2;
half4 emisMetal3;
#endif
};
void InitRawSamples(inout RawSamples s)
{
s.normSAO0 = half4(0,0,0,1);
s.normSAO1 = half4(0,0,0,1);
s.normSAO2 = half4(0,0,0,1);
s.normSAO3 = half4(0,0,0,1);
}
#if _MAX2LAYER
inline half BlendWeights(half s1, half s2, half s3, half s4, half4 w) { return s1 * w.x + s2 * w.y; }
inline half2 BlendWeights(half2 s1, half2 s2, half2 s3, half2 s4, half4 w) { return s1 * w.x + s2 * w.y; }
inline half3 BlendWeights(half3 s1, half3 s2, half3 s3, half3 s4, half4 w) { return s1 * w.x + s2 * w.y; }
inline half4 BlendWeights(half4 s1, half4 s2, half4 s3, half4 s4, half4 w) { return s1 * w.x + s2 * w.y; }
#elif _MAX3LAYER
inline half BlendWeights(half s1, half s2, half s3, half s4, half4 w) { return s1 * w.x + s2 * w.y + s3 * w.z; }
inline half2 BlendWeights(half2 s1, half2 s2, half2 s3, half2 s4, half4 w) { return s1 * w.x + s2 * w.y + s3 * w.z; }
inline half3 BlendWeights(half3 s1, half3 s2, half3 s3, half3 s4, half4 w) { return s1 * w.x + s2 * w.y + s3 * w.z; }
inline half4 BlendWeights(half4 s1, half4 s2, half4 s3, half4 s4, half4 w) { return s1 * w.x + s2 * w.y + s3 * w.z; }
#else
inline half BlendWeights(half s1, half s2, half s3, half s4, half4 w) { return s1 * w.x + s2 * w.y + s3 * w.z + s4 * w.w; }
inline half2 BlendWeights(half2 s1, half2 s2, half2 s3, half2 s4, half4 w) { return s1 * w.x + s2 * w.y + s3 * w.z + s4 * w.w; }
inline half3 BlendWeights(half3 s1, half3 s2, half3 s3, half3 s4, half4 w) { return s1 * w.x + s2 * w.y + s3 * w.z + s4 * w.w; }
inline half4 BlendWeights(half4 s1, half4 s2, half4 s3, half4 s4, half4 w) { return s1 * w.x + s2 * w.y + s3 * w.z + s4 * w.w; }
#endif
#if _MAX3LAYER
#define SAMPLE_PER_TEX(varName, pixel, config, defVal) \
half4 varName##0 = defVal; \
half4 varName##1 = defVal; \
half4 varName##2 = defVal; \
half4 varName##3 = defVal; \
varName##0 = tex2Dlod(_PerTexProps, float4(config.uv0.z/32, pixel/16, 0, 0)); \
varName##1 = tex2Dlod(_PerTexProps, float4(config.uv1.z/32, pixel/16, 0, 0)); \
varName##2 = tex2Dlod(_PerTexProps, float4(config.uv2.z/32, pixel/16, 0, 0)); \
#elif _MAX2LAYER
#define SAMPLE_PER_TEX(varName, pixel, config, defVal) \
half4 varName##0 = defVal; \
half4 varName##1 = defVal; \
half4 varName##2 = defVal; \
half4 varName##3 = defVal; \
varName##0 = tex2Dlod(_PerTexProps, float4(config.uv0.z/32, pixel/16, 0, 0)); \
varName##1 = tex2Dlod(_PerTexProps, float4(config.uv1.z/32, pixel/16, 0, 0)); \
#else
#define SAMPLE_PER_TEX(varName, pixel, config, defVal) \
half4 varName##0 = tex2Dlod(_PerTexProps, float4(config.uv0.z/32, pixel/16, 0, 0)); \
half4 varName##1 = tex2Dlod(_PerTexProps, float4(config.uv1.z/32, pixel/16, 0, 0)); \
half4 varName##2 = tex2Dlod(_PerTexProps, float4(config.uv2.z/32, pixel/16, 0, 0)); \
half4 varName##3 = tex2Dlod(_PerTexProps, float4(config.uv3.z/32, pixel/16, 0, 0)); \
#endif
half3 BlendNormal3(half3 n1, half3 n2)
{
n1.z += 1;
n2.xy = -n2.xy;
return n1 * dot(n1, n2) / n1.z - n2;
}
half2 TransformTriplanarNormal(Input IN, float3x3 t2w, half3 axisSign, half3 absVertNormal,
half3 pN, half2 a0, half2 a1, half2 a2)
{
a0 = a0 * 2 - 1;
a1 = a1 * 2 - 1;
a2 = a2 * 2 - 1;
a0.x *= axisSign.x;
a1.x *= axisSign.y;
a2.x *= axisSign.z;
half3 n0 = half3(a0.xy, 1);
half3 n1 = half3(a1.xy, 1);
half3 n2 = half3(a2.xy, 1);
n0 = BlendNormal3(half3(IN.worldNormal.zy, absVertNormal.x), n0);
n1 = BlendNormal3(half3(IN.worldNormal.xz, absVertNormal.y), n1);
n2 = BlendNormal3(half3(IN.worldNormal.xy, absVertNormal.z), n2);
n0.z *= axisSign.x;
n1.z *= axisSign.y;
n2.z *= -axisSign.z;
half3 worldNormal = (n0.zyx * pN.x + n1.xzy * pN.y + n2.xyz * pN.z );
return mul(t2w, worldNormal).xy;
}
// funcs
float2 Hash2D( float2 x )
{
float2 k = float2( 0.3183099, 0.3678794 );
x = x*k + k.yx;
return -1.0 + 2.0*frac( 16.0 * k*frac( x.x*x.y*(x.x+x.y)) );
}
float Noise2D(float2 p )
{
float2 i = floor( p );
float2 f = frac( p );
float2 u = f*f*(3.0-2.0*f);
return lerp( lerp( dot( Hash2D( i + float2(0.0,0.0) ), f - float2(0.0,0.0) ),
dot( Hash2D( i + float2(1.0,0.0) ), f - float2(1.0,0.0) ), u.x),
lerp( dot( Hash2D( i + float2(0.0,1.0) ), f - float2(0.0,1.0) ),
dot( Hash2D( i + float2(1.0,1.0) ), f - float2(1.0,1.0) ), u.x), u.y);
}
float FBM2D(float2 uv)
{
float f = 0.5000*Noise2D( uv ); uv *= 2.01;
f += 0.2500*Noise2D( uv ); uv *= 1.96;
f += 0.1250*Noise2D( uv );
return f;
}
float3 Hash3D( float3 p )
{
p = float3( dot(p,float3(127.1,311.7, 74.7)),
dot(p,float3(269.5,183.3,246.1)),
dot(p,float3(113.5,271.9,124.6)));
return -1.0 + 2.0*frac(sin(p)*437.5453123);
}
float Noise3D( float3 p )
{
float3 i = floor( p );
float3 f = frac( p );
float3 u = f*f*(3.0-2.0*f);
return lerp( lerp( lerp( dot( Hash3D( i + float3(0.0,0.0,0.0) ), f - float3(0.0,0.0,0.0) ),
dot( Hash3D( i + float3(1.0,0.0,0.0) ), f - float3(1.0,0.0,0.0) ), u.x),
lerp( dot( Hash3D( i + float3(0.0,1.0,0.0) ), f - float3(0.0,1.0,0.0) ),
dot( Hash3D( i + float3(1.0,1.0,0.0) ), f - float3(1.0,1.0,0.0) ), u.x), u.y),
lerp( lerp( dot( Hash3D( i + float3(0.0,0.0,1.0) ), f - float3(0.0,0.0,1.0) ),
dot( Hash3D( i + float3(1.0,0.0,1.0) ), f - float3(1.0,0.0,1.0) ), u.x),
lerp( dot( Hash3D( i + float3(0.0,1.0,1.0) ), f - float3(0.0,1.0,1.0) ),
dot( Hash3D( i + float3(1.0,1.0,1.0) ), f - float3(1.0,1.0,1.0) ), u.x), u.y), u.z );
}
float FBM3D(float3 uv)
{
float f = 0.5000*Noise3D( uv ); uv *= 2.01;
f += 0.2500*Noise3D( uv ); uv *= 1.96;
f += 0.1250*Noise3D( uv );
return f;
}
half2 BlendNormal2(half2 base, half2 blend) { return normalize(float3(base.xy + blend.xy, 1)).xy; }
half3 BlendOverlay(half3 base, half3 blend) { return (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend))); }
half3 BlendMult2X(half3 base, half3 blend) { return (base * (blend * 2)); }
half4 ComputeWeights(half4 iWeights, half h0, half h1, half h2, half h3, half contrast)
{
#if _DISABLEHEIGHTBLENDING
return iWeights;
#else
// compute weight with height map
//half4 weights = half4(iWeights.x * h0, iWeights.y * h1, iWeights.z * h2, iWeights.w * h3);
half4 weights = half4(iWeights.x * max(h0,0.001), iWeights.y * max(h1,0.001), iWeights.z * max(h2,0.001), iWeights.w * max(h3,0.001));
// Contrast weights
half maxWeight = max(max(weights.x, max(weights.y, weights.z)), weights.w);
half transition = max(contrast * maxWeight, 0.0001);
half threshold = maxWeight - transition;
half scale = 1.0 / transition;
weights = saturate((weights - threshold) * scale);
// Normalize weights.
half weightScale = 1.0f / (weights.x + weights.y + weights.z + weights.w);
weights *= weightScale;
return weights;
#endif
}
half HeightBlend(half h1, half h2, half slope, half contrast)
{
#if _DISABLEHEIGHTBLENDING
return slope;
#else
h2 = 1 - h2;
half tween = saturate((slope - min(h1, h2)) / max(abs(h1 - h2), 0.001));
half blend = saturate( ( tween - (1-contrast) ) / max(contrast, 0.001));
return blend;
#endif
}
#if _MAX4TEXTURES
#define TEXCOUNT 4
#elif _MAX8TEXTURES
#define TEXCOUNT 8
#elif _MAX12TEXTURES
#define TEXCOUNT 12
#elif _MAX20TEXTURES
#define TEXCOUNT 20
#elif _MAX24TEXTURES
#define TEXCOUNT 24
#elif _MAX28TEXTURES
#define TEXCOUNT 28
#elif _MAX32TEXTURES
#define TEXCOUNT 32
#else
#define TEXCOUNT 16
#endif
void Setup(out half4 weights, float2 uv, out Config config, fixed4 w0, fixed4 w1, fixed4 w2, fixed4 w3, fixed4 w4, fixed4 w5, fixed4 w6, fixed4 w7, float3 worldPos)
{
config = (Config)0;
half4 indexes = 0;
config.uv = uv;
#if _WORLDUV
uv = worldPos.xz;
#endif
float2 scaledUV = uv * _UVScale.xy + _UVScale.zw;
// if only 4 textures, and blending 4 textures, skip this whole thing..
// this saves about 25% of the ALU of the base shader on low end
float4 defaultIndexes = float4(0,1,2,3);
#if _MESHSUBARRAY
defaultIndexes = _MeshSubArrayIndexes;
#endif
#if _MESHSUBARRAY || (_MAX4TEXTURES && !_MAX3LAYER && !_MAX2LAYER)
weights = w0;
config.uv0 = float3(scaledUV, defaultIndexes.x);
config.uv1 = float3(scaledUV, defaultIndexes.y);
config.uv2 = float3(scaledUV, defaultIndexes.z);
config.uv3 = float3(scaledUV, defaultIndexes.w);
return;
#endif
fixed splats[TEXCOUNT];
splats[0] = w0.x;
splats[1] = w0.y;
splats[2] = w0.z;
splats[3] = w0.w;
#if !_MAX4TEXTURES
splats[4] = w1.x;
splats[5] = w1.y;
splats[6] = w1.z;
splats[7] = w1.w;
#endif
#if !_MAX4TEXTURES && !_MAX8TEXTURES
splats[8] = w2.x;
splats[9] = w2.y;
splats[10] = w2.z;
splats[11] = w2.w;
#endif
#if !_MAX4TEXTURES && !_MAX8TEXTURES && !_MAX12TEXTURES
splats[12] = w3.x;
splats[13] = w3.y;
splats[14] = w3.z;
splats[15] = w3.w;
#endif
#if _MAX20TEXTURES || _MAX24TEXTURES || _MAX28TEXTURES || _MAX32TEXTURES
splats[16] = w4.x;
splats[17] = w4.y;
splats[18] = w4.z;
splats[19] = w4.w;
#endif
#if _MAX24TEXTURES || _MAX28TEXTURES || _MAX32TEXTURES
splats[20] = w5.x;
splats[21] = w5.y;
splats[22] = w5.z;
splats[23] = w5.w;
#endif
#if _MAX28TEXTURES || _MAX32TEXTURES
splats[24] = w6.x;
splats[25] = w6.y;
splats[26] = w6.z;
splats[27] = w6.w;
#endif
#if _MAX32TEXTURES
splats[28] = w7.x;
splats[29] = w7.y;
splats[30] = w7.z;
splats[31] = w7.w;
#endif
weights[0] = 0;
weights[1] = 0;
weights[2] = 0;
weights[3] = 0;
indexes[0] = 0;
indexes[1] = 0;
indexes[2] = 0;
indexes[3] = 0;
int i = 0;
for (i = 0; i < TEXCOUNT; ++i)
{
fixed w = splats[i];
if (w >= weights[0])
{
weights[3] = weights[2];
indexes[3] = indexes[2];
weights[2] = weights[1];
indexes[2] = indexes[1];
weights[1] = weights[0];
indexes[1] = indexes[0];
weights[0] = w;
indexes[0] = i;
}
else if (w >= weights[1])
{
weights[3] = weights[2];
indexes[3] = indexes[2];
weights[2] = weights[1];
indexes[2] = indexes[1];
weights[1] = w;
indexes[1] = i;
}
else if (w >= weights[2])
{
weights[3] = weights[2];
indexes[3] = indexes[2];
weights[2] = w;
indexes[2] = i;
}
else if (w >= weights[3])
{
weights[3] = w;
indexes[3] = i;
}
}
// clamp and renormalize
#if _MAX2LAYER
weights.zw = 0;
weights.xy *= (1.0 / (weights.x + weights.y));
#endif
#if _MAX3LAYER
weights.w = 0;
weights.xyz *= (1.0 / (weights.x + weights.y + weights.z));
#endif
config.uv0 = float3(scaledUV, indexes.x);
config.uv1 = float3(scaledUV, indexes.y);
config.uv2 = float3(scaledUV, indexes.z);
config.uv3 = float3(scaledUV, indexes.w);
}
float ComputeMipLevel(float2 uv, float2 textureSize)
{
uv *= textureSize;
float2 dx_vtc = ddx(uv);
float2 dy_vtc = ddy(uv);
float delta_max_sqr = max(dot(dx_vtc, dx_vtc), dot(dy_vtc, dy_vtc));
return 0.5 * log2(delta_max_sqr);
}
inline fixed2 UnpackNormal2(fixed4 packednormal)
{
#if defined(UNITY_NO_DXT5nm)
return packednormal.xy * 2 - 1;
#else
return packednormal.wy * 2 - 1;
#endif
}
half3 TriplanarHBlend(half h0, half h1, half h2, half3 pN, half contrast)
{
half3 blend = pN / dot(pN, half3(1,1,1));
float3 heights = float3(h0, h1, h2) + (blend * 3.0);
half height_start = max(max(heights.x, heights.y), heights.z) - contrast;
half3 h = max(heights - height_start.xxx, half3(0,0,0));
blend = h / dot(h, half3(1,1,1));
return blend;
}
// man I wish unity would wrap everything instead of only what they use. Just seems like a landmine for
// people like myself.. especially as they keep changing things around and I have to figure out all the new defines
// and handle changes across Unity versions, which would be automatically handled if they just wrapped these themselves without
// as much complexity..
#if (UNITY_VERSION >= 201810 && (defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(UNITY_COMPILER_HLSLCC) || defined(SHADER_API_PSSL) || (SHADER_TARGET_SURFACE_ANALYSIS && !SHADER_TARGET_SURFACE_ANALYSIS_MOJOSHADER))) || (UNITY_VERSION < 201810 && (defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(UNITY_COMPILER_HLSLCC) || defined(SHADER_API_PSSL)))
#define MICROSPLAT_SAMPLE_TEX2D_LOD(tex,coord, lod) tex.SampleLevel (sampler##tex,coord, lod)
#define MICROSPLAT_SAMPLE_TEX2D_SAMPLER_LOD(tex,samplertex,coord, lod) tex.SampleLevel (sampler##samplertex,coord, lod)
#else
#define MICROSPLAT_SAMPLE_TEX2D_LOD(tex,coord,lod) tex2D (tex,coord,0,lod)
#define MICROSPLAT_SAMPLE_TEX2D_SAMPLER_LOD(tex,samplertex,coord,lod) tex2D (tex,coord,0,lod)
#endif
#if (UNITY_VERSION >= 201810 && (defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(UNITY_COMPILER_HLSLCC) || defined(SHADER_API_PSSL) || (SHADER_TARGET_SURFACE_ANALYSIS && !SHADER_TARGET_SURFACE_ANALYSIS_MOJOSHADER))) || (UNITY_VERSION < 201810 && (defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(UNITY_COMPILER_HLSLCC) || defined(SHADER_API_PSSL)))
#define MICROSPLAT_SAMPLE_TEX2D_GRAD(tex,coord,dx,dy) tex.SampleGrad (sampler##tex,coord,dx,dy)
#elif defined(SHADER_API_D3D9)
#define MICROSPLAT_SAMPLE_TEX2D_GRAD(tex,coord,dx,dy) half4(0,1,0,0)
#elif defined(UNITY_COMPILER_HLSL2GLSL) || defined(SHADER_TARGET_SURFACE_ANALYSIS)
#define MICROSPLAT_SAMPLE_TEX2D_GRAD(tex,coord,dx,dy) texCUBEgrad (tex,coord,float3(dx.x,dx.y,0),float3(dy.x,dy.y,0))
#elif defined(SHADER_API_GLES)
#define MICROSPLAT_SAMPLE_TEX2D_GRAD(tex,coord,dx,dy) half4(1,1,0,0)
#elif defined(SHADER_API_D3D11_9X)
#define MICROSPLAT_SAMPLE_TEX2D_GRAD(tex,coord,dx,dy) half4(0,1,1,0)
#else
#define MICROSPLAT_SAMPLE_TEX2D_GRAD(tex,coord,dx,dy) half4(0,0,1,0)
#endif
#if _USELODMIP
#define MICROSPLAT_SAMPLE(tex, u, l) UNITY_SAMPLE_TEX2DARRAY_LOD(tex, u, l.x)
#elif _USEGRADMIP
#define MICROSPLAT_SAMPLE(tex, u, l) MICROSPLAT_SAMPLE_TEX2D_GRAD(tex, u, l.xy, l.zw)
#else
#define MICROSPLAT_SAMPLE(tex, u, l) UNITY_SAMPLE_TEX2DARRAY(tex, u)
#endif
#define MICROSPLAT_SAMPLE_DIFFUSE(u, cl, l) MICROSPLAT_SAMPLE(_Diffuse, u, l)
#define MICROSPLAT_SAMPLE_EMIS(u, cl, l) MICROSPLAT_SAMPLE(_EmissiveMetal, u, l)
#define MICROSPLAT_SAMPLE_DIFFUSE_LOD(u, cl, l) UNITY_SAMPLE_TEX2DARRAY_LOD(_Diffuse, u, l)
#if _PACKINGHQ
#define MICROSPLAT_SAMPLE_NORMAL(u, cl, l) half4(MICROSPLAT_SAMPLE(_NormalSAO, u, l).ga, MICROSPLAT_SAMPLE(_SmoothAO, u, l).ga).brag
#else
#define MICROSPLAT_SAMPLE_NORMAL(u, cl, l) MICROSPLAT_SAMPLE(_NormalSAO, u, l)
#endif
#if UNITY_VERSION >= 201830 && !_TERRAINBLENDABLESHADER && !_MICROMESH
#if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X)
sampler2D _TerrainHeightmapTexture;
sampler2D _TerrainNormalmapTexture;
float4 _TerrainHeightmapRecipSize; // float4(1.0f/width, 1.0f/height, 1.0f/(width-1), 1.0f/(height-1))
float4 _TerrainHeightmapScale; // float4(hmScale.x, hmScale.y / (float)(kMaxHeight), hmScale.z, 0.0f)
#endif
#endif
#if UNITY_VERSION >= 201830 && !_TERRAINBLENDABLESHADER && !_MICROMESH
UNITY_INSTANCING_BUFFER_START(Terrain)
UNITY_DEFINE_INSTANCED_PROP(float4, _TerrainPatchInstanceData) // float4(xBase, yBase, skipScale, ~)
UNITY_INSTANCING_BUFFER_END(Terrain)
#endif
// surface shaders + tessellation, do not pass go, or
// collect $500 - sucks it up and realize you can't use
// an Input struct, so you have to hack UV coordinates
// and live with only the magic keywords..
void vert (inout appdata i)
{
#if UNITY_VERSION >= 201830 && !_TERRAINBLENDABLESHADER && !_MICROMESH && defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X)
float2 patchVertex = i.vertex.xy;
float4 instanceData = UNITY_ACCESS_INSTANCED_PROP(Terrain, _TerrainPatchInstanceData);
float4 uvscale = instanceData.z * _TerrainHeightmapRecipSize;
float4 uvoffset = instanceData.xyxy * uvscale;
uvoffset.xy += 0.5f * _TerrainHeightmapRecipSize.xy;
float2 sampleCoords = (patchVertex.xy * uvscale.xy + uvoffset.xy);
float hm = UnpackHeightmap(tex2Dlod(_TerrainHeightmapTexture, float4(sampleCoords, 0, 0)));
i.vertex.xz = (patchVertex.xy + instanceData.xy) * _TerrainHeightmapScale.xz * instanceData.z; //(x + xBase) * hmScale.x * skipScale;
i.vertex.y = hm * _TerrainHeightmapScale.y;
i.vertex.w = 1.0f;
i.texcoord.xy = (patchVertex.xy * uvscale.zw + uvoffset.zw);
i.texcoord2 = sampleCoords;
i.texcoord2 = i.texcoord1 = i.texcoord;
i.normal = float3(0,1,0);
#elif _PERPIXNORMAL
i.normal = float3(0,1,0);
#endif
#if !_MICROMESH
float4 tangent;
tangent.xyz = cross(UnityObjectToWorldNormal( i.normal ), float3(0,0,1));
tangent.w = -1;
i.tangent = tangent;
#endif
#if _VSSHADOWMAP || _WINDSHADOWS || _SNOWSHADOWS
float3 N = mul((float3x3)unity_ObjectToWorld, i.normal);
float3 T = mul((float3x3)unity_ObjectToWorld, i.tangent.xyz);
float3 B = cross(N,T) * i.tangent.w;
float3x3 worldToTangent = float3x3(T,B,N);
#if _VSSHADOWMAP
i.color.rgb = mul( worldToTangent, gVSSunDirection.xyz ).xyz;
#else
i.color.rgb = mul( worldToTangent, normalize(_WorldSpaceLightPos0.xyz) ).xyz;
#endif
#endif
}
void SampleAlbedo(inout Config config, TriplanarConfig tc, inout RawSamples s, MIPFORMAT mipLevel)
{
#if _TRIPLANAR
#if _USEGRADMIP
float4 d0 = mipLevel.d0;
float4 d1 = mipLevel.d1;
float4 d2 = mipLevel.d2;
#elif _USELODMIP
float d0 = mipLevel.x;
float d1 = mipLevel.y;
float d2 = mipLevel.z;
#else
MIPFORMAT d0 = mipLevel;
MIPFORMAT d1 = mipLevel;
MIPFORMAT d2 = mipLevel;
#endif
half4 contrasts = _Contrast.xxxx;
#if _PERTEXTRIPLANARCONTRAST
SAMPLE_PER_TEX(ptc, 5.5, config, half4(1,0.5,0,0));
contrasts = half4(ptc0.y, ptc1.y, ptc2.y, ptc3.y);
#endif
{
half4 a0 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv0[0], config.cluster0, d0);
half4 a1 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv0[1], config.cluster0, d1);
half4 a2 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv0[2], config.cluster0, d2);
half3 bf = tc.pN0;
#if _TRIPLANARHEIGHTBLEND
bf = TriplanarHBlend(a0.a, a1.a, a2.a, tc.pN0, contrasts.x);
tc.pN0 = bf;
#endif
s.albedo0 = a0 * bf.x + a1 * bf.y + a2 * bf.z;
}
{
half4 a0 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv1[0], config.cluster1, d0);
half4 a1 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv1[1], config.cluster1, d1);
half4 a2 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv1[2], config.cluster1, d2);
half3 bf = tc.pN1;
#if _TRIPLANARHEIGHTBLEND
bf = TriplanarHBlend(a0.a, a1.a, a2.a, tc.pN1, contrasts.x);
tc.pN1 = bf;
#endif
s.albedo1 = a0 * bf.x + a1 * bf.y + a2 * bf.z;
}
#if !_MAX2LAYER
{
half4 a0 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv2[0], config.cluster2, d0);
half4 a1 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv2[1], config.cluster2, d1);
half4 a2 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv2[2], config.cluster2, d2);
half3 bf = tc.pN2;
#if _TRIPLANARHEIGHTBLEND
bf = TriplanarHBlend(a0.a, a1.a, a2.a, tc.pN2, contrasts.x);
tc.pN2 = bf;
#endif
s.albedo2 = a0 * bf.x + a1 * bf.y + a2 * bf.z;
}
#endif
#if !_MAX3LAYER || !_MAX2LAYER
{
half4 a0 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv3[0], config.cluster3, d0);
half4 a1 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv3[1], config.cluster3, d1);
half4 a2 = MICROSPLAT_SAMPLE_DIFFUSE(tc.uv3[2], config.cluster3, d2);
half3 bf = tc.pN3;
#if _TRIPLANARHEIGHTBLEND
bf = TriplanarHBlend(a0.a, a1.a, a2.a, tc.pN3, contrasts.x);
tc.pN3 = bf;
#endif
s.albedo3 = a0 * bf.x + a1 * bf.y + a2 * bf.z;
}
#endif
#else
s.albedo0 = MICROSPLAT_SAMPLE_DIFFUSE(config.uv0, config.cluster0, mipLevel);
s.albedo1 = MICROSPLAT_SAMPLE_DIFFUSE(config.uv1, config.cluster1, mipLevel);
#if !_MAX2LAYER
s.albedo2 = MICROSPLAT_SAMPLE_DIFFUSE(config.uv2, config.cluster2, mipLevel);
#endif
#if !_MAX3LAYER || !_MAX2LAYER
s.albedo3 = MICROSPLAT_SAMPLE_DIFFUSE(config.uv3, config.cluster3, mipLevel);
#endif
#endif
#if _PERTEXHEIGHTOFFSET || _PERTEXHEIGHTCONTRAST
SAMPLE_PER_TEX(ptHeight, 10.5, config, 1);
#if _PERTEXHEIGHTOFFSET
s.albedo0.a = saturate(s.albedo0.a + ptHeight0.b - 1);
s.albedo1.a = saturate(s.albedo1.a + ptHeight1.b - 1);
s.albedo2.a = saturate(s.albedo2.a + ptHeight2.b - 1);
s.albedo3.a = saturate(s.albedo3.a + ptHeight3.b - 1);
#endif
#if _PERTEXHEIGHTCONTRAST
s.albedo0.a = saturate(pow(s.albedo0.a + 0.5, ptHeight0.a) - 0.5);
s.albedo1.a = saturate(pow(s.albedo1.a + 0.5, ptHeight1.a) - 0.5);
s.albedo2.a = saturate(pow(s.albedo2.a + 0.5, ptHeight2.a) - 0.5);
s.albedo3.a = saturate(pow(s.albedo3.a + 0.5, ptHeight3.a) - 0.5);
#endif
#endif
}
void SampleNormal(Config config, TriplanarConfig tc, inout RawSamples s, MIPFORMAT mipLevel)
{
#if _NONOMALMAP
s.normSAO0 = half4(0,0, 0, 1);
s.normSAO1 = half4(0,0, 0, 1);
s.normSAO2 = half4(0,0, 0, 1);
s.normSAO3 = half4(0,0, 0, 1);
return;
#endif
#if _TRIPLANAR
#if _USEGRADMIP
float4 d0 = mipLevel.d0;
float4 d1 = mipLevel.d1;
float4 d2 = mipLevel.d2;
#elif _USELODMIP
float d0 = mipLevel.x;
float d1 = mipLevel.y;
float d2 = mipLevel.z;
#else
MIPFORMAT d0 = mipLevel;
MIPFORMAT d1 = mipLevel;
MIPFORMAT d2 = mipLevel;
#endif
half3 absVertNormal = abs(tc.IN.worldNormal);
float3 t2w0 = WorldNormalVector(tc.IN, float3(1,0,0));
float3 t2w1 = WorldNormalVector(tc.IN, float3(0,1,0));
float3 t2w2 = WorldNormalVector(tc.IN, float3(0,0,1));
float3x3 t2w = float3x3(t2w0, t2w1, t2w2);
{
half4 a0 = MICROSPLAT_SAMPLE_NORMAL(tc.uv0[0], config.cluster0, d0).garb;
half4 a1 = MICROSPLAT_SAMPLE_NORMAL(tc.uv0[1], config.cluster0, d1).garb;
half4 a2 = MICROSPLAT_SAMPLE_NORMAL(tc.uv0[2], config.cluster0, d2).garb;
s.normSAO0.xy = TransformTriplanarNormal(tc.IN, t2w, tc.axisSign, absVertNormal, tc.pN0, a0.xy, a1.xy, a2.xy);
s.normSAO0.zw = a0.zw * tc.pN0.x + a1.zw * tc.pN0.y + a2.zw * tc.pN0.z;
}
{
half4 a0 = MICROSPLAT_SAMPLE_NORMAL(tc.uv1[0], config.cluster1, d0).garb;
half4 a1 = MICROSPLAT_SAMPLE_NORMAL(tc.uv1[1], config.cluster1, d1).garb;
half4 a2 = MICROSPLAT_SAMPLE_NORMAL(tc.uv1[2], config.cluster1, d2).garb;
s.normSAO1.xy = TransformTriplanarNormal(tc.IN, t2w, tc.axisSign, absVertNormal, tc.pN1, a0.xy, a1.xy, a2.xy);
s.normSAO1.zw = a0.zw * tc.pN1.x + a1.zw * tc.pN1.y + a2.zw * tc.pN1.z;
}
#if !_MAX2LAYER
{
half4 a0 = MICROSPLAT_SAMPLE_NORMAL(tc.uv2[0], config.cluster2, d0).garb;
half4 a1 = MICROSPLAT_SAMPLE_NORMAL(tc.uv2[1], config.cluster2, d1).garb;
half4 a2 = MICROSPLAT_SAMPLE_NORMAL(tc.uv2[2], config.cluster2, d2).garb;
s.normSAO2.xy = TransformTriplanarNormal(tc.IN, t2w, tc.axisSign, absVertNormal, tc.pN2, a0.xy, a1.xy, a2.xy);
s.normSAO2.zw = a0.zw * tc.pN2.x + a1.zw * tc.pN2.y + a2.zw * tc.pN2.z;
}
#endif
#if !_MAX3LAYER || !_MAX2LAYER
{
half4 a0 = MICROSPLAT_SAMPLE_NORMAL(tc.uv3[0], config.cluster3, d0).garb;
half4 a1 = MICROSPLAT_SAMPLE_NORMAL(tc.uv3[1], config.cluster3, d1).garb;
half4 a2 = MICROSPLAT_SAMPLE_NORMAL(tc.uv3[2], config.cluster3, d2).garb;
s.normSAO3.xy = TransformTriplanarNormal(tc.IN, t2w, tc.axisSign, absVertNormal, tc.pN3, a0.xy, a1.xy, a2.xy);
s.normSAO3.zw = a0.zw * tc.pN3.x + a1.zw * tc.pN3.y + a2.zw * tc.pN3.z;
}
#endif
#else
s.normSAO0 = MICROSPLAT_SAMPLE_NORMAL(config.uv0, config.cluster0, mipLevel).garb;
s.normSAO1 = MICROSPLAT_SAMPLE_NORMAL(config.uv1, config.cluster1, mipLevel).garb;
s.normSAO0.xy = s.normSAO0.xy * 2 - 1;
s.normSAO1.xy = s.normSAO1.xy * 2 - 1;
#if !_MAX2LAYER
s.normSAO2 = MICROSPLAT_SAMPLE_NORMAL(config.uv2, config.cluster2, mipLevel).garb;
s.normSAO2.xy = s.normSAO2.xy * 2 - 1;
#endif
#if !_MAX3LAYER || !_MAX2LAYER
s.normSAO3 = MICROSPLAT_SAMPLE_NORMAL(config.uv3, config.cluster3, mipLevel).garb;
s.normSAO3.xy = s.normSAO3.xy * 2 - 1;
#endif
#endif
}
void SampleEmis(Config config, TriplanarConfig tc, inout RawSamples s, MIPFORMAT mipLevel)
{
#if _USEEMISSIVEMETAL
#if _TRIPLANAR
#if _USEGRADMIP
float4 d0 = mipLevel.d0;
float4 d1 = mipLevel.d1;
float4 d2 = mipLevel.d2;
#elif _USELODMIP
float d0 = mipLevel.x;
float d1 = mipLevel.y;
float d2 = mipLevel.z;
#else
MIPFORMAT d0 = mipLevel;
MIPFORMAT d1 = mipLevel;
MIPFORMAT d2 = mipLevel;
#endif
{
half4 a0 = MICROSPLAT_SAMPLE_EMIS(tc.uv0[0], config.cluster0, d0);
half4 a1 = MICROSPLAT_SAMPLE_EMIS(tc.uv0[1], config.cluster0, d1);
half4 a2 = MICROSPLAT_SAMPLE_EMIS(tc.uv0[2], config.cluster0, d2);