-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsAssign1.fx
1777 lines (1378 loc) · 64.3 KB
/
GraphicsAssign1.fx
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
//--------------------------------------------------------------------------------------
// File: GraphicsAssign1.fx
//
// Shaders Graphics Assignment
// Add further models using different shader techniques
// See assignment specification for details
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Structures
//--------------------------------------------------------------------------------------
// Standard input geometry data, more complex techniques (e.g. normal mapping) may need more
struct VS_BASIC_INPUT
{
float3 Pos : POSITION;
float3 Normal : NORMAL;
float2 UV : TEXCOORD0;
};
struct VS_DEPTH_INPUT
{
float3 Pos : POSITION;
float3 Normal : NORMAL;
float2 UV : TEXCOORD0;
};
struct VS_SHADOW_INPUT
{
float3 Pos : POSITION;
float3 Normal : NORMAL;
float2 UV : TEXCOORD0;
};
struct VS_NORMALMAP_INPUT
{
float3 Pos : POSITION;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float2 UV : TEXCOORD0;
};
struct VS_PARALLAXMAP_INPUT
{
float3 Pos : POSITION;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float2 UV : TEXCOORD0;
};
// Data output from vertex shader to pixel shader for simple techniques. Again different techniques have different requirements
struct VS_OUTPUT
{
float4 ProjPos : SV_POSITION;
float Colour : COLOR0;
float2 UV : TEXCOORD0;
};
struct VS_BASIC_OUTPUT
{
float4 ProjPos : SV_POSITION;
float3 DiffuseLight : COLOR0;
float3 WorldPos : POSITION;
float3 WorldNormal : NORMAL;
float2 UV : TEXCOORD0;
float ClipDist : SV_ClipDistance;
};
struct VS_WIGGLE_OUTPUT
{
float4 ProjPos : SV_POSITION;
float3 DiffuseLight : COLOR0;
float3 WorldPos : POSITION;
float3 WorldNormal : NORMAL;
float2 UV : TEXCOORD0;
float ClipDist : SV_ClipDistance;
};
struct VS_LIGHTING_OUTPUT
{
float4 ProjPos : SV_POSITION;
float3 WorldPos : POSITION;
float3 WorldNormal : NORMAL;
float2 UV : TEXCOORD0;
float ClipDist : SV_ClipDistance;
};
struct VS_NORMALMAP_OUTPUT
{
float4 ProjPos : SV_POSITION;
float3 WorldPos : POSITION;
float3 ModelNormal : NORMAL;
float3 ModelTangent : TANGENT;
float2 UV : TEXCOORD0;
float ClipDist : SV_ClipDistance;
};
struct VS_PARALLAXMAP_OUTPUT
{
float4 ProjPos : SV_POSITION;
float3 WorldPos : POSITION;
float3 ModelNormal : NORMAL;
float3 ModelTangent : TANGENT;
float2 UV : TEXCOORD0;
float ClipDist : SV_ClipDistance;
};
struct VS_TEXONLY_OUTPUT
{
float4 ProjPos : SV_POSITION; // 2D "projected" position for vertex (required output for vertex shader)
float2 UV : TEXCOORD0;
float ClipDist : SV_ClipDistance;
};
struct VS_BLACK_OUTLINE_OUTPUT
{
float4 ProjPos : SV_POSITION;
float ClipDist : SV_ClipDistance;
};
struct VS_SHADOW_OUTPUT
{
float4 ProjPos : SV_POSITION;
float3 WorldPos : POSITION;
float3 WorldNormal : NORMAL;
float2 UV : TEXCOORD0;
float ClipDist : SV_ClipDistance;
};
struct VS_DEPTH_OUTPUT
{
float4 ProjPos : SV_POSITION;
float3 DiffuseLight : COLOR0;
float3 WorldPos : POSITION;
float3 WorldNormal : NORMAL;
float2 UV : TEXCOORD0;
float ClipDist : SV_ClipDistance;
};
struct VS_MIRROR_OUTPUT
{
float4 ProjPos : SV_POSITION;
float3 DiffuseLight : COLOR0;
float3 WorldPos : POSITION;
float3 WorldNormal : NORMAL;
float2 UV : TEXCOORD0;
float ClipDist : SV_ClipDistance;
};
struct PS_DEPTH_OUTPUT
{
float4 Colour : SV_Target;
float Depth : SV_Depth;
};
//--------------------------------------------------------------------------------------
// Global Variables
//--------------------------------------------------------------------------------------
// All these variables are created & manipulated in the C++ code and passed into the shader here
// The matrices (4x4 matrix of floats) for transforming from 3D model to 2D projection (used in vertex shader)
float4x4 WorldMatrix;
float4x4 ViewMatrix;
float4x4 ProjMatrix;
float4x4 ViewProjMatrix; // NG.
//NG. Wiggle effect variable
float Wiggle;
// Variable to tint colours
float3 TintColour;
// A single colour for an entire model - used for light models and the intial basic shader
float3 ModelColour;
// Variable used to for constant colour output (cell shading outline and tint for light models)
float3 ConstantColour;
// Controls thickness of outlines for cell shading
float OutlineThickness;
float4 ClipPlane;
//NG. Lighting information
float3 Light1Pos;
float3 Light1Colour;
float3 Light1Facing;
float4x4 Light1ViewMatrix;
float4x4 Light1ProjMatrix;
float Light1CosHalfAngle;
float3 Light2Pos;
float3 Light2Colour;
float3 Light2Facing;
float4x4 Light2ViewMatrix;
float4x4 Light2ProjMatrix;
float Light2CosHalfAngle;
float3 Light3Pos;
float3 Light3Colour;
float3 Light3Facing;
float4x4 Light3ViewMatrix;
float4x4 Light3ProjMatrix;
float Light3CosHalfAngle;
float3 AmbientColour;
float SpecularPower;
float3 CameraPos;
// Diffuse texture map (the main texture colour) - may contain specular map in alpha channel
Texture2D DiffuseMap;
Texture2D NormalMap;
Texture2D ShadowMap1;
Texture2D ShadowMap2;
Texture2D ShadowMap3;
Texture2D CellMap;
float ParallaxDepth;
// NG. Sampler
SamplerState TrilinearWrap
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
SamplerState PointClamp
{
Filter = MIN_MAG_MIP_POINT;
AddressU = Clamp;
AddressV = Clamp;
};
//--------------------------------------------------------------------------------------
// Vertex Shaders
//--------------------------------------------------------------------------------------
// Basic vertex shader to transform 3D model vertices to 2D and pass UVs to the pixel shader
//
VS_BASIC_OUTPUT BasicTransform(VS_BASIC_INPUT vIn)
{
//// Use world matrix passed from C++ to transform the input model vertex position into world space
VS_BASIC_OUTPUT vOut;
// Use world matrix passed from C++ to transform the input model vertex position into world space
float4 modelPos = float4(vIn.Pos, 1.0f); // Promote to 1x4 so we can multiply by 4x4 matrix, put 1.0 in 4th element for a point (0.0 for a vector)
float4 worldPos = mul(modelPos, WorldMatrix);
float4 viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
// Pass texture coordinates (UVs) on to the pixel shader
vOut.UV = vIn.UV;
vOut.ClipDist = dot(worldPos, ClipPlane);
return vOut;
}
VS_DEPTH_OUTPUT DepthTransform(VS_DEPTH_INPUT vIn)
{
VS_DEPTH_OUTPUT vOut;
// Use world matrix passed from C++ to transform the input model vertex position into world space
float4 modelPos = float4(vIn.Pos, 1.0f); // Promote to 1x4 so we can multiply by 4x4 matrix, put 1.0 in 4th element for a point (0.0 for a vector)
float4 worldPos = mul(modelPos, WorldMatrix);
float4 viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
// Pass texture coordinates (UVs) on to the pixel shader
vOut.UV = vIn.UV;
vOut.ClipDist = dot(worldPos, ClipPlane);
return vOut;
}
VS_LIGHTING_OUTPUT VertexLightingTex(VS_BASIC_INPUT vIn)
{
VS_LIGHTING_OUTPUT vOut;
// Use world matrix passed from C++ to transform the input model vertex position into world space
float4 modelPos = float4(vIn.Pos, 1.0f); // Promote to 1x4 so we can multiply by 4x4 matrix, put 1.0 in 4th element for a point (0.0 for a vector)
float4 worldPos = mul(modelPos, WorldMatrix);
vOut.WorldPos = worldPos.xyz;
float4 modelnorm = float4(vIn.Normal, 0.0f); // promote to 1x4 (since normal is a vector we add 0.0f)
float4 worldNormal = mul(modelnorm, WorldMatrix);
float4 viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
// Transform the vertex normal from model space into world space (almost same as first lines of code above)
float4 modelNormal = float4(vIn.Normal, 0.0f); // Set 4th element to 0.0 this time as normals are vectors
vOut.WorldNormal = mul(modelNormal, WorldMatrix).xyz;
// Pass texture coordinates (UVs) on to the pixel shader, the vertex shader doesn't need them
vOut.UV = vIn.UV;
vOut.ClipDist = dot(worldPos, ClipPlane);
return vOut;
}
VS_WIGGLE_OUTPUT WiggleTransform(VS_BASIC_INPUT vIn)
{
VS_WIGGLE_OUTPUT vOut;
float4 modelPos = float4(vIn.Pos, 1.0f); // Promote to 1x4 so we can multiply by 4x4 matrix, put 1.0 in 4th element for a point (0.0 for a vector)
float4 worldPos = mul(modelPos, WorldMatrix);
vOut.WorldPos = worldPos.xyz;
//NG.
float4 modelnorm = float4(vIn.Normal, 0.0f); // promote to 1x4 (since normal is a vector we add 0.0f)
float4 worldNormal = mul(modelnorm, WorldMatrix);
float4 modelNormal = float4(vIn.Normal, 0.0f);
vOut.WorldNormal = mul(modelNormal, WorldMatrix).xyz;
worldPos.x += sin(modelPos.y + Wiggle) * 0.1f;
worldPos.y += sin(modelPos.x + Wiggle) * 0.1f;
worldPos += worldNormal * (sin(Wiggle) + 1.0f) * 0.1f;
//vOut.ProjPos = mul(worldPos, ViewProjMatrix);
// todo?
float4 viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
// Pass texture coordinates (UVs) on to the pixel shader
vOut.UV = vIn.UV;
vOut.ClipDist = dot(worldPos, ClipPlane);
return vOut;
}
VS_WIGGLE_OUTPUT Teapot2DiffuseSpec(VS_BASIC_INPUT vIn)
{
VS_WIGGLE_OUTPUT vOut;
float4 modelPos = float4(vIn.Pos, 1.0f); // Promote to 1x4 so we can multiply by 4x4 matrix, put 1.0 in 4th element for a point (0.0 for a vector)
float4 worldPos = mul(modelPos, WorldMatrix);
vOut.WorldPos = worldPos.xyz;
//NG.
float4 modelnorm = float4(vIn.Normal, 0.0f); // promote to 1x4 (since normal is a vector we add 0.0f)
float4 worldNormal = mul(modelnorm, WorldMatrix);
float4 modelNormal = float4(vIn.Normal, 0.0f);
vOut.WorldNormal = mul(modelNormal, WorldMatrix).xyz;
float4 viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
// Pass texture coordinates (UVs) on to the pixel shader
vOut.UV = vIn.UV;
vOut.ClipDist = dot(worldPos, ClipPlane);
return vOut;
}
VS_OUTPUT SphereScroll(VS_BASIC_INPUT vIn)
{
VS_OUTPUT vOut;
float3 LightColour = { 1.0f, 0.8f, 0.4f };
float3 LightDir = { 0.707f, 0.707f, -0.707f };
// Use matrices to transform the geometry to 2D
float4 worldPos = mul(float4(vIn.Pos, 1.0f), WorldMatrix);
float4 viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
// Calculate lighting on this vertex
float3 worldNormal = (float3)mul(vIn.Normal, (float3x3)WorldMatrix);
float3 diffuseColour = AmbientColour + LightColour * saturate(dot(normalize(worldNormal), LightDir));
// Lit the Cube (Nikos Gkaltzidis)
vOut.Colour = diffuseColour;
//vOut.Colour = mul(float3(vIn.UV, 1.0f), diffuseColour);
vOut.UV = vIn.UV;
vOut.UV.x += Wiggle / 5;
return vOut;
}
VS_NORMALMAP_OUTPUT NormalMapTransform(VS_NORMALMAP_INPUT vIn)
{
VS_NORMALMAP_OUTPUT vOut;
// Use world matrix passed from C++ to transform the input model vertex position into world space
float4 modelPos = float4(vIn.Pos, 1.0f); // Promote to 1x4 so we can multiply by 4x4 matrix, put 1.0 in 4th element for a point (0.0 for a vector)
float4 worldPos = mul(modelPos, WorldMatrix);
vOut.WorldPos = worldPos.xyz;
// Use camera matrices to further transform the vertex from world space into view space (camera's point of view) and finally into 2D "projection" space for rendering
float4 viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
// Just send the model's normal and tangent untransformed (in model space). The pixel shader will do the matrix work on normals
vOut.ModelNormal = vIn.Normal;
vOut.ModelTangent = vIn.Tangent;
// Pass texture coordinates (UVs) on to the pixel shader, the vertex shader doesn't need them
vOut.UV = vIn.UV;
vOut.ClipDist = dot(worldPos, ClipPlane);
return vOut;
}
VS_PARALLAXMAP_OUTPUT ParallaxMapTransform(VS_PARALLAXMAP_INPUT vIn)
{
VS_PARALLAXMAP_OUTPUT vOut;
// Use world matrix passed from C++ to transform the input model vertex position into world space
float4 modelPos = float4(vIn.Pos, 1.0f); // Promote to 1x4 so we can multiply by 4x4 matrix, put 1.0 in 4th element for a point (0.0 for a vector)
float4 worldPos = mul(modelPos, WorldMatrix);
vOut.WorldPos = worldPos.xyz;
// Use camera matrices to further transform the vertex from world space into view space (camera's point of view) and finally into 2D "projection" space for rendering
float4 viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
// Just send the model's normal and tangent untransformed (in model space). The pixel shader will do the matrix work on normals
vOut.ModelNormal = vIn.Normal;
vOut.ModelTangent = vIn.Tangent;
// Pass texture coordinates (UVs) on to the pixel shader, the vertex shader doesn't need them
vOut.UV = vIn.UV;
vOut.ClipDist = dot(worldPos, ClipPlane);
return vOut;
}
VS_BLACK_OUTLINE_OUTPUT ExpandOutline(VS_BASIC_INPUT vIn)
{
VS_BLACK_OUTLINE_OUTPUT vOut;
// Transform model-space vertex position to world-space
float4 modelPos = float4(vIn.Pos, 1.0f); // Promote to 1x4 so we can multiply by 4x4 matrix, put 1.0 in 4th element for a point (0.0 for a vector)
float4 worldPos = mul(modelPos, WorldMatrix);
// Next the usual transform from world space to camera space - but we don't go any further here - this will be used to help expand the outline
// The result "viewPos" is the xyz position of the vertex as seen from the camera. The z component is the distance from the camera - that's useful...
float4 viewPos = mul(worldPos, ViewMatrix);
// Transform model normal to world space, using the normal to expand the geometry, not for lighting
float4 modelNormal = float4(vIn.Normal, 0.0f); // Set 4th element to 0.0 this time as normals are vectors
float4 worldNormal = normalize(mul(modelNormal, WorldMatrix)); // Normalise in case of world matrix scaling
// Now we return to the world position of this vertex and expand it along the world normal - that will expand the geometry outwards.
// Use the distance from the camera to decide how much to expand. Use this distance together with a sqrt to creates an outline that
// gets thinner in the distance, but always remains clear. Overall thickness is also controlled by the constant "OutlineThickness"
worldPos += OutlineThickness * sqrt(viewPos.z) * worldNormal;
// Transform new expanded world-space vertex position to viewport-space and output
viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
vOut.ClipDist = dot(worldPos, ClipPlane);
return vOut;
}
VS_SHADOW_OUTPUT ShadowTransformTex(VS_SHADOW_INPUT vIn)
{
VS_SHADOW_OUTPUT vOut;
// Use world matrix passed from C++ to transform the input model vertex position into world space
float4 modelPos = float4(vIn.Pos, 1.0f); // Promote to 1x4 so we can multiply by 4x4 matrix, put 1.0 in 4th element for a point (0.0 for a vector)
float4 worldPos = mul(modelPos, WorldMatrix);
vOut.WorldPos = worldPos.xyz;
// Use camera matrices to further transform the vertex from world space into view space (camera's point of view) and finally into 2D "projection" space for rendering
float4 viewPos = mul(worldPos, ViewMatrix);
vOut.ProjPos = mul(viewPos, ProjMatrix);
// Transform the vertex normal from model space into world space (almost same as first lines of code above)
float4 modelNormal = float4(vIn.Normal, 0.0f); // Set 4th element to 0.0 this time as normals are vectors
vOut.WorldNormal = mul(modelNormal, WorldMatrix).xyz;
// Pass texture coordinates (UVs) on to the pixel shader, the vertex shader doesn't need them
vOut.UV = vIn.UV;
vOut.ClipDist = dot(worldPos, ClipPlane);
return vOut;
}
//--------------------------------------------------------------------------------------
// Pixel Shaders
//--------------------------------------------------------------------------------------
// A pixel shader that just outputs a single fixed colour
float4 OneColour(VS_BASIC_OUTPUT vOut) : SV_Target
{
return float4(ModelColour, 1.0); // Set alpha channel to 1.0 (opaque)
}
float4 ScrollTexturePS(VS_OUTPUT vOut) : SV_Target // The ": SV_Target" bit just indicates that the returned float4 colour goes to the render target (i.e. it's a colour to render)
{
/*float SinY = sin(vOut.UV.y * radians(360.0f) + Wiggle);
vOut.UV.x += 0.1f * SinY;
float SinX = sin(vOut.UV.x * radians(360.0f) + Wiggle);
vOut.UV.y += 0.1f * SinX;*/
/*float SinZ = sin(vOut.UV.z * radians(360.0f) + Wiggle);
vOut.UV.z += 0.1f * SinZ;*/
float4 TexColour = DiffuseMap.Sample(TrilinearWrap, vOut.UV);
TexColour += sin(Wiggle);
float4 colour;
colour.rgb = (vOut.Colour + TexColour);
colour.a = 1.0f;
return colour;
}
float4 OneColourCell(VS_BLACK_OUTLINE_OUTPUT vOut) : SV_Target
{
return float4(ConstantColour, 1.0); // Set alpha channel to 1.0 (opaque)
}
float4 DiffuseMapOnly(VS_BASIC_OUTPUT vOut) : SV_Target
{
// Extract diffuse material colour for this pixel from a texture
float4 diffuseMapColour = DiffuseMap.Sample(TrilinearWrap, vOut.UV);
return diffuseMapColour;
}
float4 DiffuseMapOnlyMoodle(VS_BASIC_OUTPUT vOut) : SV_Target
{
// Extract diffuse material colour for this pixel from a texture
float4 diffuseMapColour = DiffuseMap.Sample(TrilinearWrap, vOut.UV);
//NG.
if (diffuseMapColour.a < 0.5)
discard;
return diffuseMapColour;
}
float4 TexturedColour(VS_BASIC_OUTPUT vOut) : SV_Target
{
// NG. Diffuse material colour from a texture
float4 DiffuseMaterial = DiffuseMap.Sample(TrilinearWrap, vOut.UV);
float3 worldNormal = normalize(vOut.WorldNormal);
///////////////////////
// Calculate lighting
// Calculate direction of camera
float3 CameraDir = normalize(CameraPos - vOut.WorldPos.xyz); // Position of camera - position of current vertex (or pixel) (in world space)
//// LIGHT 1
float3 Light1Dir = normalize(Light1Pos - vOut.WorldPos.xyz); // Direction for each light is different
float3 Light1Distance = length(Light1Pos - vOut.WorldPos.xyz); // Calculate Distance of Light1 NG.
float3 DiffuseLight1 = Light1Colour * saturate(dot(worldNormal.xyz, Light1Dir)) * 1 / Light1Distance;
float3 halfway = normalize(Light1Dir + CameraDir);
float3 SpecularLight1 = DiffuseLight1 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
float3 SpecularMaterial = DiffuseMaterial.a;
float4 CombinedColour;
CombinedColour.rgb = DiffuseMaterial * DiffuseLight1 + SpecularMaterial * SpecularLight1;
CombinedColour.a = 1.0;
return CombinedColour; // Set alpha channel to 1.0 (opaque)
}
float4 DiffuseSpecular(VS_BASIC_OUTPUT vOut) : SV_Target
{
// NG. Diffuse material colour from a texture
float4 DiffuseMaterial = DiffuseMap.Sample(TrilinearWrap, vOut.UV);
float3 worldNormal = normalize(vOut.WorldNormal);
///////////////////////
// Calculate lighting
// Calculate direction of camera
float3 CameraDir = normalize(CameraPos - vOut.WorldPos.xyz); // Position of camera - position of current vertex (or pixel) (in world space)
// LIGHT 1
float3 Light1Dir = normalize(Light1Pos - vOut.WorldPos.xyz); // Direction for each light is different
float3 Light1Distance = length(Light1Pos - vOut.WorldPos.xyz); // Calculate Distance of Light1 NG.
float3 DiffuseLight1 = Light1Colour * saturate(dot(worldNormal.xyz, Light1Dir)) * 1 / Light1Distance;
float3 halfway = normalize(Light1Dir + CameraDir);
float3 SpecularLight1 = DiffuseLight1 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
// LIGHT 2
float3 Light2Dir = normalize(Light2Pos - vOut.WorldPos.xyz); // Direction for each light is different
float3 Light2Distance = length(Light2Pos - vOut.WorldPos.xyz); // Calculate Distance of Light1 NG.
float3 DiffuseLight2 = Light2Colour * saturate(dot(worldNormal.xyz, Light2Dir)) * 1 / Light2Distance;
halfway = normalize(Light2Dir + CameraDir);
float3 SpecularLight2 = DiffuseLight2 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
// LIGHT 3
float3 Light3Dir = normalize(Light3Pos - vOut.WorldPos.xyz); // Direction for each light is different
float3 Light3Distance = length(Light3Pos - vOut.WorldPos.xyz); // Calculate Distance of Light1 NG.
float3 DiffuseLight3 = Light3Colour * saturate(dot(worldNormal.xyz, Light3Dir)) * 1 / Light3Distance;
halfway = normalize(Light3Dir + CameraDir);
float3 SpecularLight3 = DiffuseLight3 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
float3 SpecularMaterial = DiffuseMaterial.a;
float DiffuseLight = DiffuseLight1 + DiffuseLight2 + DiffuseLight3;
float SpecularLight = SpecularLight1 + SpecularLight2 + SpecularLight3;
float4 CombinedColour;
CombinedColour.rgb = DiffuseMaterial * DiffuseLight + SpecularMaterial * SpecularLight;
CombinedColour.a = 1.0;
return CombinedColour; // Set alpha channel to 1.0 (opaque)
}
float4 LightColourPS(VS_LIGHTING_OUTPUT vOut) : SV_Target
{
float3 worldNormal = normalize(vOut.WorldNormal);
///////////////////////
// Calculate lighting
// Calculate direction of camera
float3 CameraDir = normalize(CameraPos - vOut.WorldPos.xyz); // Position of camera - position of current vertex (or pixel) (in world space)
//// LIGHT 1
float3 Light1Dir = normalize(Light1Pos - vOut.WorldPos.xyz); // Direction for each light is different
float3 Light1Distance = length(Light1Pos - vOut.WorldPos.xyz); // Calculate Distance of Light1 NG.
float3 DiffuseLight1 = Light1Colour * saturate(dot(worldNormal.xyz, Light1Dir)) * 1 / Light1Distance;
float3 halfway = normalize(Light1Dir + CameraDir);
float3 SpecularLight1 = DiffuseLight1 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
//// LIGHT 2
float3 Light2Dir = normalize(Light2Pos - vOut.WorldPos.xyz);
float3 Light2Distance = length(Light2Pos - vOut.WorldPos.xyz); // Calculate Distance of Light2 NG.
float3 DiffuseLight2 = Light2Colour * saturate(dot(worldNormal.xyz, Light2Dir)) * 1 / Light2Distance;
halfway = normalize(Light2Dir + CameraDir);
float3 SpecularLight2 = DiffuseLight2 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
//// LIGHT 3
float3 Light3Dir = normalize(Light3Pos - vOut.WorldPos.xyz);
float3 Light3Distance = length(Light3Pos - vOut.WorldPos.xyz); // Calculate Distance of Light3 NG.
float3 DiffuseLight3 = Light3Colour * saturate(dot(worldNormal.xyz, Light3Dir)) * 1 / Light3Distance;
halfway = normalize(Light3Dir + CameraDir);
float3 SpecularLight3 = DiffuseLight3 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
// Sum the effect of the two lights - add the ambient at this stage rather than for each light (or we will get twice the ambient level)
float3 DiffuseLight = AmbientColour + DiffuseLight1 + DiffuseLight2 + DiffuseLight3;
float3 SpecularLight = SpecularLight1 + SpecularLight2 + SpecularLight3;
////////////////////
// Sample texture
// Extract diffuse material colour for this pixel from a texture (using float3, so we get RGB - i.e. ignore any alpha in the texture)
float4 DiffuseMaterial = DiffuseMap.Sample(TrilinearWrap, vOut.UV);
// Assume specular material colour is white (i.e. highlights are a full, untinted reflection of light)
float3 SpecularMaterial = DiffuseMaterial.a;
////////////////////
// Combine colours
// Combine maps and lighting for final pixel colour
float4 combinedColour;
combinedColour.rgb = DiffuseMaterial * DiffuseLight + SpecularMaterial * SpecularLight;
combinedColour.a = 1.0f; // No alpha processing in this shader, so just set it to 1
return combinedColour;
}
//NG. A pixel shader that just tints a (diffuse) texture with a fixed colour
float4 TintDiffuseMap(VS_BASIC_OUTPUT vOut) : SV_Target
{
// Extract diffuse material colour for this pixel from a texture
float4 diffuseMapColour = DiffuseMap.Sample(TrilinearWrap, vOut.UV);
// Tint by global colour (set from C++)
diffuseMapColour.rgb *= TintColour / 20;
return diffuseMapColour;
}
float4 NormalMapLighting(VS_NORMALMAP_OUTPUT vOut) : SV_Target
{
//************************
// Normal Map Extraction
//************************
// Will use the model normal/tangent to calculate matrix for tangent space. The normals for each pixel are *interpolated* from the
// vertex normals/tangents. This means they will not be length 1, so they need to be renormalised (same as per-pixel lighting issue)
float3 modelNormal = normalize(vOut.ModelNormal);
float3 modelTangent = normalize(vOut.ModelTangent);
// Calculate bi-tangent to complete the three axes of tangent space - then create the *inverse* tangent matrix to convert *from*
// tangent space into model space. This is just a matrix built from the three axes (very advanced note - by default shader matrices
// are stored as columns rather than in rows as in the C++. This means that this matrix is created "transposed" from what we would
// expect. However, for a 3x3 rotation matrix the transpose is equal to the inverse, which is just what we require)
float3 modelBiTangent = cross(modelNormal, modelTangent);
float3x3 invTangentMatrix = float3x3(modelTangent, modelBiTangent, modelNormal);
// Get the texture normal from the normal map. The r,g,b pixel values actually store x,y,z components of a normal. However, r,g,b
// values are stored in the range 0->1, whereas the x, y & z components should be in the range -1->1. So some scaling is needed
float3 textureNormal = 2.0f * NormalMap.Sample(TrilinearWrap, vOut.UV) - 1.0f; // Scale from 0->1 to -1->1
textureNormal.z = textureNormal.z / 20.0f;
// Now convert the texture normal into model space using the inverse tangent matrix, and then convert into world space using the world
// matrix. Normalise, because of the effects of texture filtering and in case the world matrix contains scaling
float3 worldNormal = normalize(mul(mul(textureNormal, invTangentMatrix), WorldMatrix));
// Now use this normal for lighting calculations in world space as usual - the remaining code same as per-pixel lighting
///////////////////////
// Calculate lighting
// Calculate direction of camera
float3 CameraDir = normalize(CameraPos - vOut.WorldPos.xyz); // Position of camera - position of current vertex (or pixel) (in world space)
//// LIGHT 1
float3 Light1Dir = normalize(Light1Pos - vOut.WorldPos.xyz); // Direction for each light is different
float3 Light1Distance = length(Light1Pos - vOut.WorldPos.xyz); // Calculate Distance of Light1 NG.
float3 DiffuseLight1 = Light1Colour * saturate(dot(worldNormal.xyz, Light1Dir)) * 1 / Light1Distance;
float3 halfway = normalize(Light1Dir + CameraDir);
float3 SpecularLight1 = DiffuseLight1 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
//// LIGHT 2
float3 Light2Dir = normalize(Light2Pos - vOut.WorldPos.xyz);
float3 Light2Distance = length(Light2Pos - vOut.WorldPos.xyz); // Calculate Distance of Light2 NG.
float3 DiffuseLight2 = Light2Colour * saturate(dot(worldNormal.xyz, Light2Dir)) * 1 / Light2Distance;
halfway = normalize(Light2Dir + CameraDir);
float3 SpecularLight2 = DiffuseLight2 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
//// LIGHT 3
float3 Light3Dir = normalize(Light3Pos - vOut.WorldPos.xyz);
float3 Light3Distance = length(Light3Pos - vOut.WorldPos.xyz); // Calculate Distance of Light3 NG.
float3 DiffuseLight3 = Light3Colour * saturate(dot(worldNormal.xyz, Light3Dir)) * 1 / Light3Distance;
halfway = normalize(Light3Dir + CameraDir);
float3 SpecularLight3 = DiffuseLight3 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
// Sum the effect of the two lights - add the ambient at this stage rather than for each light (or we will get twice the ambient level)
float3 DiffuseLight = AmbientColour + DiffuseLight1 + DiffuseLight2 + DiffuseLight3;
float3 SpecularLight = SpecularLight1 + SpecularLight2 + SpecularLight3;
////////////////////
// Sample texture
// Extract diffuse material colour for this pixel from a texture (using float3, so we get RGB - i.e. ignore any alpha in the texture)
float4 DiffuseMaterial = DiffuseMap.Sample(TrilinearWrap, vOut.UV);
// Assume specular material colour is white (i.e. highlights are a full, untinted reflection of light)
float3 SpecularMaterial = DiffuseMaterial.a;
////////////////////
// Combine colours
// Combine maps and lighting for final pixel colour
float4 combinedColour;
combinedColour.rgb = DiffuseMaterial * DiffuseLight + SpecularMaterial * SpecularLight;
combinedColour.a = 1.0f; // No alpha processing in this shader, so just set it to 1
return combinedColour;
}
float4 ParallaxMapLighting(VS_PARALLAXMAP_OUTPUT vOut) : SV_Target
{
//************************
// Normal Map Extraction
//************************
// Will use the model normal/tangent to calculate matrix for tangent space. The normals for each pixel are *interpolated* from the
// vertex normals/tangents. This means they will not be length 1, so they need to be renormalised (same as per-pixel lighting issue)
float3 modelNormal = normalize(vOut.ModelNormal);
float3 modelTangent = normalize(vOut.ModelTangent);
// Calculate bi-tangent to complete the three axes of tangent space - then create the *inverse* tangent matrix to convert *from*
// tangent space into model space. This is just a matrix built from the three axes (very advanced note - by default shader matrices
// are stored as columns rather than in rows as in the C++. This means that this matrix is created "transposed" from what we would
// expect. However, for a 3x3 rotation matrix the transpose is equal to the inverse, which is just what we require)
float3 modelBiTangent = cross(modelNormal, modelTangent);
float3x3 invTangentMatrix = float3x3(modelTangent, modelBiTangent, modelNormal);
//****| INFO |**********************************************************************************//
// The following few lines are the parallax mapping. Converts the camera direction into model
// space and adjusts the UVs based on that and the bump depth of the texel we are looking at
// Although short, this code involves some intricate matrix work / space transformations
//**********************************************************************************************//
// Get normalised vector to camera for parallax mapping and specular equation (this vector was calculated later in previous shaders)
float3 CameraDir = normalize(CameraPos - vOut.WorldPos.xyz);
// Transform camera vector from world into model space. Need *inverse* world matrix for this.
// Only need 3x3 matrix to transform vectors, to invert a 3x3 matrix we transpose it (flip it about its diagonal)
float3x3 invWorldMatrix = transpose(WorldMatrix);
float3 cameraModelDir = normalize(mul(CameraDir, invWorldMatrix)); // Normalise in case world matrix is scaled
// Then transform model-space camera vector into tangent space (texture coordinate space) to give the direction to offset texture
// coordinate, only interested in x and y components. Calculated inverse tangent matrix above, so invert it back for this step
float3x3 tangentMatrix = transpose(invTangentMatrix);
float2 textureOffsetDir = mul(cameraModelDir, tangentMatrix);
// Get the depth info from the normal map's alpha channel at the given texture coordinate
// Rescale from 0->1 range to -x->+x range, x determined by ParallaxDepth setting
float texDepth = ParallaxDepth * (NormalMap.Sample(TrilinearWrap, vOut.UV).a - 0.5f);
// Use the depth of the texture to offset the given texture coordinate - this corrected texture coordinate will be used from here on
float2 offsetTexCoord = vOut.UV + texDepth * textureOffsetDir;
//*******************************************
//****| INFO |**********************************************************************************//
// The above chunk of code is used only to calculate "offsetTexCoord", which is the offset in
// which part of the texture we see at this pixel due to it being bumpy. The remaining code is
// exactly the same as normal mapping, but uses offsetTexCoord instead of the usual vOut.UV
//**********************************************************************************************//
// Get the texture normal from the normal map. The r,g,b pixel values actually store x,y,z components of a normal. However, r,g,b
// values are stored in the range 0->1, whereas the x, y & z components should be in the range -1->1. So some scaling is needed
float3 textureNormal = 2.0f * NormalMap.Sample(TrilinearWrap, offsetTexCoord) - 1.0f; // Scale from 0->1 to -1->1
textureNormal.z = textureNormal.z / 5.0f;
// Now convert the texture normal into model space using the inverse tangent matrix, and then convert into world space using the world
// matrix. Normalise, because of the effects of texture filtering and in case the world matrix contains scaling
float3 worldNormal = normalize(mul(mul(textureNormal, invTangentMatrix), WorldMatrix));
// Now use this normal for lighting calculations in world space as usual - the remaining code same as per-pixel lighting
///////////////////////
// Calculate lighting
// Calculate direction of camera
//float3 CameraDir = normalize(CameraPos - vOut.WorldPos.xyz); // Position of camera - position of current vertex (or pixel) (in world space)
//// LIGHT 1
float3 Light1Dir = normalize(Light1Pos - vOut.WorldPos.xyz); // Direction for each light is different
float3 Light1Distance = length(Light1Pos - vOut.WorldPos.xyz); // Calculate Distance of Light1 NG.
float3 DiffuseLight1 = Light1Colour * saturate(dot(worldNormal.xyz, Light1Dir)) * 1 / Light1Distance;
float3 halfway = normalize(Light1Dir + CameraDir);
float3 SpecularLight1 = DiffuseLight1 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
//// LIGHT 2
float3 Light2Dir = normalize(Light2Pos - vOut.WorldPos.xyz);
float3 Light2Distance = length(Light2Pos - vOut.WorldPos.xyz); // Calculate Distance of Light2 NG.
float3 DiffuseLight2 = Light2Colour * saturate(dot(worldNormal.xyz, Light2Dir)) * 1 / Light2Distance;
halfway = normalize(Light2Dir + CameraDir);
float3 SpecularLight2 = DiffuseLight2 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
//// LIGHT 3
float3 Light3Dir = normalize(Light3Pos - vOut.WorldPos.xyz);
float3 Light3Distance = length(Light3Pos - vOut.WorldPos.xyz); // Calculate Distance of Light3 NG.
float3 DiffuseLight3 = Light3Colour * saturate(dot(worldNormal.xyz, Light3Dir)) * 1 / Light3Distance;
halfway = normalize(Light3Dir + CameraDir);
float3 SpecularLight3 = DiffuseLight3 * pow(saturate(dot(worldNormal.xyz, halfway)), SpecularPower);
// Sum the effect of the two lights - add the ambient at this stage rather than for each light (or we will get twice the ambient level)
float3 DiffuseLight = AmbientColour + DiffuseLight1 + DiffuseLight2 + DiffuseLight3;
float3 SpecularLight = SpecularLight1 + SpecularLight2 + SpecularLight3;
////////////////////
// Sample texture
// Extract diffuse material colour for this pixel from a texture (using float3, so we get RGB - i.e. ignore any alpha in the texture)
float4 DiffuseMaterial = DiffuseMap.Sample(TrilinearWrap, offsetTexCoord);
// Assume specular material colour is white (i.e. highlights are a full, untinted reflection of light)
float3 SpecularMaterial = DiffuseMaterial.a;
////////////////////
// Combine colours
// Combine maps and lighting for final pixel colour
float4 combinedColour;
combinedColour.rgb = DiffuseMaterial * DiffuseLight + SpecularMaterial * SpecularLight;
combinedColour.a = 1.0f; // No alpha processing in this shader, so just set it to 1
return combinedColour;
}
float4 VertexLitDiffuseMap(VS_LIGHTING_OUTPUT vOut) : SV_Target // The ": SV_Target" bit just indicates that the returned float4 colour goes to the render target (i.e. it's a colour to render)
{
// Can't guarantee the normals are length 1 now (because the world matrix may contain scaling), so renormalise
// If lighting in the pixel shader, this is also because the interpolation from vertex shader to pixel shader will also rescale normals
float3 worldNormal = normalize(vOut.WorldNormal);
///////////////////////
// Calculate lighting
// Calculate direction of camera
float3 CameraDir = normalize(CameraPos - vOut.WorldPos.xyz); // Position of camera - position of current vertex (or pixel) (in world space)
//// LIGHT 1
float3 Light1Dir = normalize(Light1Pos - vOut.WorldPos.xyz); // Direction for each light is different
float3 Light1Dist = length(Light1Pos - vOut.WorldPos.xyz);
//****| INFO |*************************************************************************************//
// To make a cartoon look to the lighting, we clamp the basic light level to just a small range of
// colours. This is done by using the light level itself as the U texture coordinate to look up
// a colour in a special 1D texture (a single line). This could be done with if statements, but
// GPUs are much faster at looking up small textures than if statements
//*************************************************************************************************//
float DiffuseLevel1 = max(dot(worldNormal.xyz, Light1Dir), 0);
float CellDiffuseLevel1 = CellMap.Sample(PointClamp, DiffuseLevel1).r;
float3 DiffuseLight1 = Light1Colour * CellDiffuseLevel1 / Light1Dist;
// Do same for specular light and further lights
float3 halfway = normalize(Light1Dir + CameraDir);
float SpecularLevel1 = pow(max(dot(worldNormal.xyz, halfway), 0), SpecularPower);
float CellSpecularLevel1 = CellMap.Sample(PointClamp, SpecularLevel1).r;
float3 SpecularLight1 = DiffuseLight1 * CellSpecularLevel1;
//// LIGHT 2
float3 Light2Dir = normalize(Light2Pos - vOut.WorldPos.xyz);
float3 Light2Dist = length(Light2Pos - vOut.WorldPos.xyz);
float DiffuseLevel2 = max(dot(worldNormal.xyz, Light2Dir), 0);
float CellDiffuseLevel2 = CellMap.Sample(PointClamp, DiffuseLevel2).r;
float3 DiffuseLight2 = Light2Colour * CellDiffuseLevel2 / Light2Dist;
halfway = normalize(Light2Dir + CameraDir);
float SpecularLevel2 = pow(max(dot(worldNormal.xyz, halfway), 0), SpecularPower);
float CellSpecularLevel2 = CellMap.Sample(PointClamp, SpecularLevel2).r;
float3 SpecularLight2 = DiffuseLight2 * CellSpecularLevel2;
//// LIGHT 2
float3 Light3Dir = normalize(Light3Pos - vOut.WorldPos.xyz);
float3 Light3Dist = length(Light3Pos - vOut.WorldPos.xyz);
float DiffuseLevel3= max(dot(worldNormal.xyz, Light3Dir), 0);
float CellDiffuseLevel3 = CellMap.Sample(PointClamp, DiffuseLevel2).r;
float3 DiffuseLight3 = Light3Colour * CellDiffuseLevel3 / Light3Dist;
halfway = normalize(Light3Dir + CameraDir);
float SpecularLevel3 = pow(max(dot(worldNormal.xyz, halfway), 0), SpecularPower);
float CellSpecularLevel3 = CellMap.Sample(PointClamp, SpecularLevel3).r;
float3 SpecularLight3 = DiffuseLight3 * CellSpecularLevel3;
// Sum the effect of the two lights - add the ambient at this stage rather than for each light (or we will get twice the ambient level)
float3 DiffuseLight = AmbientColour + DiffuseLight1 + DiffuseLight2 + DiffuseLight3;
float3 SpecularLight = SpecularLight1 + SpecularLight2 + SpecularLight3;
////////////////////
// Sample texture
// Extract diffuse material colour for this pixel from a texture (using float3, so we get RGB - i.e. ignore any alpha in the texture)
float4 DiffuseMaterial = DiffuseMap.Sample(TrilinearWrap, vOut.UV);
// Assume specular material colour is white (i.e. highlights are a full, untinted reflection of light)
float3 SpecularMaterial = DiffuseMaterial.a;
////////////////////
// Combine colours
// Combine maps and lighting for final pixel colour
float4 combinedColour;
combinedColour.rgb = DiffuseMaterial * DiffuseLight + SpecularMaterial * SpecularLight;
combinedColour.a = 1.0f; // No alpha processing in this shader, so just set it to 1
return combinedColour;
}
float4 ShadowMapTex(VS_SHADOW_OUTPUT vOut) : SV_Target // The ": SV_Target" bit just indicates that the returned float4 colour goes to the render target (i.e. it's a colour to render)
{
// Slight adjustment to calculated depth of pixels so they don't shadow themselves
const float DepthAdjust = 0.001f;
// Can't guarantee the normals are length 1 now (because the world matrix may contain scaling), so renormalise