-
Notifications
You must be signed in to change notification settings - Fork 246
/
Render.ts
1738 lines (1456 loc) · 71 KB
/
Render.ts
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
import * as Viewer from '../viewer.js';
import { TextureHolder, LoadedTexture, TextureMapping } from '../TextureHolder.js';
import { GfxDevice, GfxSampler, GfxWrapMode, GfxMipFilterMode, GfxTexFilterMode, GfxCullMode, GfxCompareMode, GfxInputLayout, GfxBuffer, GfxBufferUsage, GfxFormat, GfxVertexAttributeDescriptor, GfxVertexBufferFrequency, GfxVertexBufferDescriptor, GfxBindingLayoutDescriptor, GfxBlendMode, GfxBlendFactor, GfxProgram, GfxMegaStateDescriptor, GfxIndexBufferDescriptor, GfxInputLayoutBufferDescriptor, makeTextureDescriptor2D, GfxChannelWriteMask } from '../gfx/platform/GfxPlatform.js';
import * as BNTX from '../fres_nx/bntx.js';
import { surfaceToCanvas } from '../Common/bc_texture.js';
import { translateImageFormat, deswizzle, decompress, getImageFormatString } from '../fres_nx/tegra_texture.js';
import { FMDL, FSHP, FMAT, FMAT_RenderInfo, FMAT_RenderInfoType, FVTX, FSHP_Mesh, FRES, FVTX_VertexAttribute, FVTX_VertexBuffer, parseFMAT_ShaderParam_Float4, FMAT_ShaderParam, parseFMAT_ShaderParam_Color3, parseFMAT_ShaderParam_Float, parseFMAT_ShaderParam_Texsrt, parseFMAT_ShaderParam_Float2, FMAT_ShaderAssign } from '../fres_nx/bfres.js';
import { GfxRenderInst, makeSortKey, GfxRendererLayer, setSortKeyDepth, GfxRenderInstManager, GfxRenderInstList } from '../gfx/render/GfxRenderInstManager.js';
import { TextureAddressMode, FilterMode, IndexFormat, AttributeFormat, getChannelFormat, getTypeFormat } from '../fres_nx/nngfx_enum.js';
import { nArray, assert, assertExists, fallbackUndefined } from '../util.js';
import { makeStaticDataBuffer, makeStaticDataBufferFromSlice } from '../gfx/helpers/BufferHelpers.js';
import { fillMatrix4x4, fillMatrix4x3, fillVec4v, fillColor, fillVec3v, fillMatrix4x2, fillVec4 } from '../gfx/helpers/UniformBufferHelpers.js';
import { mat4, ReadonlyMat4, vec2, vec3, vec4 } from 'gl-matrix';
import { computeViewSpaceDepthFromWorldSpaceAABB } from '../Camera.js';
import { AABB } from '../Geometry.js';
import { reverseDepthForCompareMode } from '../gfx/helpers/ReversedDepthHelpers.js';
import { DeviceProgram } from '../Program.js';
import { GfxRenderCache } from '../gfx/render/GfxRenderCache.js';
import { GfxRenderHelper } from '../gfx/render/GfxRenderHelper.js';
import { makeBackbufferDescSimple, standardFullClearRenderPassDescriptor } from '../gfx/helpers/RenderGraphHelpers.js';
import { GfxrAttachmentSlot } from '../gfx/render/GfxRenderGraph.js';
import ArrayBufferSlice from '../ArrayBufferSlice.js';
import { GfxShaderLibrary, glslGenerateFloat } from '../gfx/helpers/GfxShaderLibrary.js';
import { getMatrixTranslation, MathConstants, Vec3Zero } from '../MathHelpers.js';
import * as SARC from "../fres_nx/sarc.js";
import * as AGLLightMap from './AGLParameter_LightMap.js';
import * as AGLEnv from './AGLParameter_Env.js';
import { colorNewCopy, colorScale, OpaqueBlack, White } from '../Color.js';
import { IS_DEVELOPMENT } from '../BuildVersion.js';
export class BRTITextureHolder extends TextureHolder<BNTX.BRTI> {
public addFRESTextures(device: GfxDevice, fres: FRES): void {
const bntxFile = fres.externalFiles.find((f) => f.name === 'textures.bntx');
if (bntxFile !== undefined)
this.addBNTXFile(device, bntxFile.buffer);
}
public addBNTXFile(device: GfxDevice, buffer: ArrayBufferSlice): void {
const bntx = BNTX.parse(buffer);
this.addTextures(device, bntx.textures);
}
public loadTexture(device: GfxDevice, textureEntry: BNTX.BRTI): LoadedTexture | null {
const gfxTexture = device.createTexture(makeTextureDescriptor2D(translateImageFormat(textureEntry.imageFormat), textureEntry.width, textureEntry.height, textureEntry.mipBuffers.length));
const canvases: HTMLCanvasElement[] = [];
const channelFormat = getChannelFormat(textureEntry.imageFormat);
for (let i = 0; i < textureEntry.mipBuffers.length; i++) {
const mipLevel = i;
const buffer = textureEntry.mipBuffers[i];
const width = Math.max(textureEntry.width >>> mipLevel, 1);
const height = Math.max(textureEntry.height >>> mipLevel, 1);
const depth = 1;
const blockHeightLog2 = textureEntry.blockHeightLog2;
deswizzle({ buffer, width, height, channelFormat, blockHeightLog2 }).then((deswizzled) => {
const rgbaTexture = decompress({ ...textureEntry, width, height, depth }, deswizzled);
const rgbaPixels = rgbaTexture.pixels;
device.uploadTextureData(gfxTexture, mipLevel, [rgbaPixels]);
const canvas = document.createElement('canvas');
surfaceToCanvas(canvas, rgbaTexture);
canvases.push(canvas);
});
}
const extraInfo = new Map<string, string>();
extraInfo.set('Format', getImageFormatString(textureEntry.imageFormat));
const viewerTexture: Viewer.Texture = { name: textureEntry.name, surfaces: canvases, extraInfo };
return { viewerTexture, gfxTexture };
}
}
function translateAddressMode(addrMode: TextureAddressMode): GfxWrapMode {
switch (addrMode) {
case TextureAddressMode.Repeat:
return GfxWrapMode.Repeat;
case TextureAddressMode.ClampToEdge:
case TextureAddressMode.ClampToBorder:
return GfxWrapMode.Clamp;
case TextureAddressMode.Mirror:
return GfxWrapMode.Mirror;
case TextureAddressMode.MirrorClampToEdge:
// TODO(jstpierre): This requires GL_ARB_texture_mirror_clamp_to_edge
return GfxWrapMode.Mirror;
default:
throw "whoops";
}
}
function translateMipFilterMode(filterMode: FilterMode): GfxMipFilterMode {
switch (filterMode) {
case FilterMode.Linear:
return GfxMipFilterMode.Linear;
case FilterMode.Point:
return GfxMipFilterMode.Nearest;
case 0:
return GfxMipFilterMode.NoMip;
default:
throw "whoops";
}
}
function translateTexFilterMode(filterMode: FilterMode): GfxTexFilterMode {
switch (filterMode) {
case FilterMode.Linear:
return GfxTexFilterMode.Bilinear;
case FilterMode.Point:
return GfxTexFilterMode.Point;
default:
throw "whoops";
}
}
class TurboUBER extends DeviceProgram {
public static a_Orders = [ '_p0', '_c0', '_u0', '_u1', '_u2', '_u3', '_n0', '_t0' ];
public static s_Orders = ['_a0', '_s0', '_n0', '_n1', '_e0', '_b0', '_b1', '_a1', '_a2', '_a3', '_t0' ];
public static ub_SceneParams = 0;
public static ub_MaterialParams = 1;
public static NumEnvLightParams = 2;
constructor(public fmat: FMAT) {
super();
this.name = this.fmat.name;
assert(this.fmat.samplerInfo.length <= 8);
this.frag = this.generateFrag();
}
public static globalDefinitions = `
precision mediump float;
layout(std140) uniform ub_ShapeParams {
Mat4x4 u_ProjectionView;
vec4 u_CameraPosWorld;
};
struct EnvLightParam {
vec4 BacksideColor;
vec4 DiffuseColor;
vec4 Direction;
};
layout(std140) uniform ub_MaterialParams {
Mat4x3 u_Model;
Mat4x2 u_TexCoordSRT0;
vec4 u_TexCoordBake0ScaleBias;
vec4 u_TexCoordBake1ScaleBias;
Mat4x2 u_TexCoordSRT2;
Mat4x2 u_TexCoordSRT3;
vec4 u_AlbedoColorAndTransparency;
vec4 u_EmissionColorAndNormalMapWeight;
vec4 u_SpecularColorAndIntensity;
vec4 u_BakeLightScaleAndRoughness;
vec4 u_TransmissionColorAndIntensity;
vec4 u_MultiTexReg[3];
vec4 u_Misc[1];
EnvLightParam u_EnvLightParams[${TurboUBER.NumEnvLightParams}];
};
#define u_AlbedoColor (u_AlbedoColorAndTransparency.rgb)
#define u_Transparency (u_AlbedoColorAndTransparency.a)
#define u_EmissionColor (u_EmissionColorAndNormalMapWeight.rgb)
#define u_NormalMapWeight (u_EmissionColorAndNormalMapWeight.a)
#define u_SpecularColor (u_SpecularColorAndIntensity.rgb)
#define u_SpecularIntensity (u_SpecularColorAndIntensity.a)
#define u_BakeLightScale (u_BakeLightScaleAndRoughness.rgb)
#define u_SpecularRoughness (u_BakeLightScaleAndRoughness.a)
#define u_IndirectMag (u_Misc[0].xy)
${GfxShaderLibrary.CalcScaleBias}
uniform sampler2D u_TextureAlbedo0; // _a0
uniform sampler2D u_TextureSpecMask; // _s0
uniform sampler2D u_TextureNormal0; // _n0
uniform sampler2D u_TextureNormal1; // _n1
uniform sampler2D u_TextureEmission0; // _e0
uniform sampler2D u_TextureBake0; // _b0
uniform sampler2D u_TextureBake1; // _b1
uniform sampler2D u_TextureMultiA; // _a1
uniform sampler2D u_TextureMultiB; // _a2
uniform sampler2D u_TextureIndirect; // _a3
uniform sampler2D u_TextureTransmission; // _t0
`;
public override both = TurboUBER.globalDefinitions;
public override vert = `
layout(location = ${this.getAttrLocation('_p0')}) in vec3 a_p0; // _p0
layout(location = ${this.getAttrLocation('_c0')}) in vec4 a_c0; // _c0
layout(location = ${this.getAttrLocation('_u0')}) in vec2 a_u0; // _u0
layout(location = ${this.getAttrLocation('_u1')}) in vec2 a_u1; // _u1
layout(location = ${this.getAttrLocation('_u2')}) in vec2 a_u2; // _u2
layout(location = ${this.getAttrLocation('_u3')}) in vec2 a_u3; // _u3
layout(location = ${this.getAttrLocation('_n0')}) in vec4 a_n0; // _n0
layout(location = ${this.getAttrLocation('_t0')}) in vec4 a_t0; // _t0
#define a_Position (a${this.getAttrAssign('_p0')})
#define a_Color (a${this.getAttrAssign('_c0')})
#define a_TexCoord0 (a${this.getAttrAssign('_u0')})
#define a_TexCoord1 (a${this.getAttrAssign('_u1')})
#define a_TexCoord2 (a${this.getAttrAssign('_u2')})
#define a_TexCoord3 (a${this.getAttrAssign('_u3')})
#define a_Normal (a${this.getAttrAssign('_n0')})
#define a_Tangent (a${this.getAttrAssign('_t0')})
out vec3 v_PositionWorld;
out vec2 v_TexCoord0;
out vec4 v_TexCoordBake;
out vec4 v_TexCoord23;
out vec4 v_VtxColor;
out vec3 v_NormalWorld;
out vec4 v_TangentWorld;
void main() {
gl_Position = Mul(u_ProjectionView, Mul(_Mat4x4(u_Model), vec4(a_Position, 1.0)));
v_PositionWorld = a_Position.xyz;
bool gsys_invalidate_texture_srt = ${this.shaderOptionBool('gsys_invalidate_texture_srt')};
if (gsys_invalidate_texture_srt) {
v_TexCoord0 = a_TexCoord0.xy;
v_TexCoord23.xy = a_TexCoord2.xy;
v_TexCoord23.zw = a_TexCoord3.xy;
} else {
v_TexCoord0 = Mul(u_TexCoordSRT0, vec4(a_TexCoord0.xy, 1.0, 1.0));
v_TexCoord23.xy = Mul(u_TexCoordSRT2, vec4(a_TexCoord2.xy, 1.0, 1.0));
v_TexCoord23.zw = Mul(u_TexCoordSRT3, vec4(a_TexCoord3.xy, 1.0, 1.0));
}
v_TexCoordBake.xy = CalcScaleBias(a_TexCoord1.xy, u_TexCoordBake0ScaleBias);
v_TexCoordBake.zw = CalcScaleBias(a_TexCoord1.xy, u_TexCoordBake1ScaleBias);
v_VtxColor = a_Color;
v_NormalWorld.xyz = normalize(a_Normal.xyz);
v_TangentWorld.xyzw = a_Tangent.xyzw;
}
`;
private getAttrAssign(attrName: string): string {
const attrAssign = this.fmat.shaderAssign.attrAssign;
return fallbackUndefined(attrAssign.get(attrName), attrName);
}
private getAttrLocation(attrName: string): number {
const index = TurboUBER.a_Orders.indexOf(attrName);
assert(index >= 0);
return index;
}
private isTranslucent(): boolean {
const render_state_mode = getRenderInfoSingleString(this.fmat.renderInfo.get('gsys_render_state_mode')!);
return render_state_mode === 'translucent';
}
private shaderOptionBool(name: string, fallback: boolean = false): boolean {
let v = this.fmat.shaderAssign.shaderOption.get(name);
if (v !== undefined) {
assert(v === '0' || v === '1');
return v === '1';
} else {
return fallback;
}
}
private shaderOptionInt(name: string, fallback: number = -1): string {
let v = this.fmat.shaderAssign.shaderOption.get(name);
if (v !== undefined) {
return v;
} else {
return glslGenerateFloat(fallback);
}
}
public generateFrag() {
return `
precision mediump float;
${GfxShaderLibrary.saturate}
in vec3 v_PositionWorld;
in vec2 v_TexCoord0;
in vec4 v_TexCoordBake;
in vec4 v_TexCoord23;
in vec4 v_VtxColor;
in vec3 v_NormalWorld;
in vec4 v_TangentWorld;
struct BakeResult {
vec3 IndirectLight;
float Shadow;
float AO;
};
struct LightResult {
vec3 DiffuseColor;
vec3 SpecularColor;
};
struct DirectionalLight {
vec3 Color;
vec3 BacksideColor;
vec3 Direction;
bool Wrapped;
bool VisibleInShadow;
};
struct SurfaceLightParams {
vec3 SurfaceNormal;
vec3 SurfacePointToEyeDir;
vec3 SpecularColor;
float IntensityFromShadow;
float SpecularRoughness;
};
float G1V(float NoV, float k) {
return 1.0 / (NoV * (1.0 - k) + k);
}
void CalcDirectionalLight(inout LightResult t_Result, in SurfaceLightParams t_SurfaceLightParams, in DirectionalLight t_Light) {
vec3 N = t_SurfaceLightParams.SurfaceNormal.xyz;
// Surface point to light
vec3 L = normalize(-t_Light.Direction.xyz);
// Surface point to eye
vec3 V = t_SurfaceLightParams.SurfacePointToEyeDir.xyz;
float NoL = dot(N, L);
float t_Intensity = t_SurfaceLightParams.IntensityFromShadow;
if (t_Light.VisibleInShadow)
t_Intensity = 1.0;
vec3 t_LightColor = t_Light.Color * t_Intensity;
vec3 t_BacksideColor = t_Light.BacksideColor * t_Intensity;
// Diffuse
{
float t_LightVisibility = NoL;
// Wrapped lighting
if (t_Light.Wrapped)
t_LightVisibility = t_LightVisibility * 0.5 + 0.5;
else
t_LightVisibility = saturate(t_LightVisibility);
t_Result.DiffuseColor += mix(t_BacksideColor, t_LightColor, t_LightVisibility);
}
// Specular
// TODO(jstpierre): Replace with cubemaps
if (!t_Light.VisibleInShadow) {
// Stolen from: http://filmicworlds.com/blog/optimizing-ggx-update/
vec3 H = normalize(L + V);
float NoV = saturate(dot(N, V));
float NoH = saturate(dot(N, H));
float LoH = saturate(dot(L, H));
float r = t_SurfaceLightParams.SpecularRoughness;
float a = r * r;
float a2 = a * a;
// D
float D = a2 / (3.14159 * pow(NoH * NoH * (a2 - 1.0) + 1.0, 2.0));
// F
// Stolen from: https://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
// float LoH5 = exp2((-5.55473 * LoH - 6.98316) * LoH);
vec3 F0 = vec3(0.05);
float LoH5 = pow(1.0 - LoH, 5.0);
vec3 F = F0 + (1.0 - F0) * LoH5;
// vis / G
float k = a / 2.0;
float vis = G1V(NoL, k) * G1V(NoV, k);
vec3 t_SpecularResponse = D * F * vis;
t_Result.SpecularColor += saturate(NoL) * t_SpecularResponse.rgb * t_LightColor.rgb * t_SurfaceLightParams.SpecularColor.rgb;
}
}
void CalcEnvLight(out LightResult t_Result, in SurfaceLightParams t_SurfaceLightParams) {
t_Result.DiffuseColor = vec3(0.0);
t_Result.SpecularColor = vec3(0.0);
for (int i = 0; i < 2; i++) {
EnvLightParam t_EnvLightParam = u_EnvLightParams[i];
DirectionalLight t_Light;
t_Light.Color = t_EnvLightParam.DiffuseColor.rgb;
t_Light.BacksideColor = t_EnvLightParam.BacksideColor.rgb;
t_Light.Direction = t_EnvLightParam.Direction.xyz;
t_Light.Wrapped = bool(t_EnvLightParam.BacksideColor.a != 0.0);
t_Light.VisibleInShadow = bool(t_EnvLightParam.DiffuseColor.a != 0.0);
CalcDirectionalLight(t_Result, t_SurfaceLightParams, t_Light);
}
}
void CalcBakeResult(out BakeResult t_Result, in vec4 t_TexCoordBake) {
bool enable_bake_texture = ${this.shaderOptionBool('enable_bake_texture')};
int bake_light_type = enable_bake_texture ? ${this.shaderOptionInt('bake_light_type')} : -1;
if (bake_light_type == 0) {
// Lightmap.
vec4 t_Bake1Sample = texture(SAMPLER_2D(u_TextureBake1), t_TexCoordBake.zw);
vec3 t_Bake1Color = t_Bake1Sample.rgb * t_Bake1Sample.a;
t_Result.IndirectLight = t_Bake1Color * u_BakeLightScale.rgb;
} else {
// Unknown.
t_Result.IndirectLight = vec3(0.0);
}
int bake_shadow_type = enable_bake_texture ? ${this.shaderOptionInt('bake_shadow_type')} : -1;
if (bake_shadow_type == 0) {
float t_BakeSample = texture(SAMPLER_2D(u_TextureBake0), t_TexCoordBake.xy).r;
t_Result.AO = t_BakeSample;
t_Result.Shadow = 1.0;
} else if (bake_shadow_type == 1) {
float t_BakeSample = texture(SAMPLER_2D(u_TextureBake0), t_TexCoordBake.xy).r;
t_Result.AO = 1.0;
t_Result.Shadow = t_BakeSample;
} else if (bake_shadow_type == 2) {
vec2 t_BakeSample = texture(SAMPLER_2D(u_TextureBake0), t_TexCoordBake.xy).rg;
t_Result.AO = t_BakeSample.r;
t_Result.Shadow = t_BakeSample.g;
} else {
// Unknown.
t_Result.AO = 1.0;
t_Result.Shadow = 1.0;
}
}
vec2 SelectTexCoord(in int t_Selection) {
if (t_Selection == 0)
return v_TexCoord0.xy;
else if (t_Selection == 2)
return v_TexCoord23.xy;
else if (t_Selection == 3)
return v_TexCoord23.zw;
else
return vec2(0.0); // error!
}
void Indirect(inout vec2 t_TexCoord, bool t_Condition) {
if (!t_Condition)
return;
vec2 t_IndTexCoord = SelectTexCoord(${this.shaderOptionInt('texcoord_select_indirectA')});
vec2 t_IndOffset = texture(SAMPLER_2D(u_TextureIndirect), t_IndTexCoord).rg;
bool indirect_texture_is_BC5s = ${this.shaderOptionBool('indirect_texture_is_BC5s')};
if (!indirect_texture_is_BC5s) {
t_IndOffset = t_IndOffset * 2.0 - 1.0;
}
t_IndOffset *= u_IndirectMag.xy;
t_TexCoord += t_IndOffset;
}
vec4 SampleMultiTexA() {
vec2 t_TexCoord = SelectTexCoord(${this.shaderOptionInt('texcoord_select_multiA')});
Indirect(t_TexCoord.xy, ${this.shaderOptionBool('indirect_effect_multiA')});
return texture(SAMPLER_2D(u_TextureMultiA), t_TexCoord.xy);
}
vec4 SampleMultiTexB() {
vec2 t_TexCoord = SelectTexCoord(${this.shaderOptionInt('texcoord_select_multiB')});
Indirect(t_TexCoord.xy, ${this.shaderOptionBool('indirect_effect_multiB')});
return texture(SAMPLER_2D(u_TextureMultiB), t_TexCoord.xy);
}
float CalcGeoMultiAlpha(PD_SAMPLER_2D(t_Texture)) {
int geo_multi_alpha_type = ${this.shaderOptionInt('geo_multi_alpha_type')};
if (geo_multi_alpha_type == 1) {
return texture(PU_SAMPLER_2D(t_Texture), SelectTexCoord(0)).a;
} else if (geo_multi_alpha_type == 2) {
return texture(PU_SAMPLER_2D(t_Texture), SelectTexCoord(2)).a;
} else if (geo_multi_alpha_type == 3) {
return texture(PU_SAMPLER_2D(t_Texture), SelectTexCoord(3)).a;
} else {
return v_VtxColor.a * u_Transparency;
}
}
void CalcGeoMultiTextureSpec(inout vec3 t_SpecMask) {
bool enable_geo_multi = ${this.shaderOptionBool('enable_geo_multi')};
if (!enable_geo_multi)
return;
int geo_multi_specmask_calc_type = ${this.shaderOptionInt('geo_multi_specmask_calc_type')};
if (geo_multi_specmask_calc_type == 0) {
int multi_tex_calc_type_color = ${this.shaderOptionInt('multi_tex_calc_type_color')};
if (multi_tex_calc_type_color == 0)
t_SpecMask.rgb = mix(t_SpecMask.rgb, SampleMultiTexB().rgb, CalcGeoMultiAlpha(PP_SAMPLER_2D(u_TextureAlbedo0)));
if (multi_tex_calc_type_color == 7)
t_SpecMask.rgb = mix(t_SpecMask.rgb, SampleMultiTexB().rgb, CalcGeoMultiAlpha(PP_SAMPLER_2D(u_TextureMultiA)));
} else if (geo_multi_specmask_calc_type == 1) {
t_SpecMask.rgb *= SampleMultiTexB().rgb;
}
}
void CalcMultiTexture(in int t_OutputType, inout vec4 t_Sample) {
bool enable_multi_texture = ${this.shaderOptionBool('enable_multi_texture')};
if (!enable_multi_texture)
return;
int multi_tex_output_type = ${this.shaderOptionInt('multi_tex_output_type')};
if (t_OutputType != multi_tex_output_type)
return;
bool enable_geo_multi = ${this.shaderOptionBool('enable_geo_multi')};
if (enable_geo_multi) {
int geo_multi_alpha_type = ${this.shaderOptionInt('geo_multi_alpha_type')};
if (geo_multi_alpha_type == 1) {
t_Sample.rgb = mix(t_Sample.rgb, SampleMultiTexA().rgb, CalcGeoMultiAlpha(PP_SAMPLER_2D(u_TextureMultiA)));
} else if (geo_multi_alpha_type == 2) {
t_Sample.rgb = mix(t_Sample.rgb, SampleMultiTexA().rgb, CalcGeoMultiAlpha(PP_SAMPLER_2D(u_TextureAlbedo0)));
}
return;
}
int multi_tex_calc_type_color = ${this.shaderOptionInt('multi_tex_calc_type_color')};
if (multi_tex_calc_type_color == 0) {
// This space intentionally left blank.
} else if (multi_tex_calc_type_color == 1) {
t_Sample.rgb *= SampleMultiTexA().rgb;
} else if (multi_tex_calc_type_color == 2) {
t_Sample.rgb *= SampleMultiTexA().rgb * SampleMultiTexB().rgb;
} else if (multi_tex_calc_type_color == 3) {
t_Sample.rgb = mix(t_Sample.rgb - SampleMultiTexA().rgb, SampleMultiTexA().rgb, t_Sample.a);
} else if (multi_tex_calc_type_color == 5) {
t_Sample.rgb = saturate((t_Sample.rgb + SampleMultiTexA().rgb - u_MultiTexReg[0].r) * u_MultiTexReg[0].g);
} else if (multi_tex_calc_type_color == 6) {
t_Sample.rgb = saturate((t_Sample.rgb + SampleMultiTexB().rgb - u_MultiTexReg[0].r) * u_MultiTexReg[0].g);
} else if (multi_tex_calc_type_color == 7) {
t_Sample.rgb = mix(t_Sample.rgb, SampleMultiTexA().rgb, SampleMultiTexA().a);
} else if (multi_tex_calc_type_color == 8) {
vec3 t_Sum = saturate(t_Sample.rgb + SampleMultiTexA().rgb + SampleMultiTexB().rgb - u_MultiTexReg[0].r) * u_MultiTexReg[0].g;
t_Sample.rgb = mix(u_MultiTexReg[2].rgb, u_MultiTexReg[1].rgb, t_Sum);
} else if (multi_tex_calc_type_color == 11) {
t_Sample.rgb = mix(u_MultiTexReg[0].rgb, u_MultiTexReg[1].rgb, t_Sample.a);
} else if (multi_tex_calc_type_color == 12) {
t_Sample.rgb = saturate(t_Sample.rgb * SampleMultiTexA().rgb + SampleMultiTexB().rgb);
} else if (multi_tex_calc_type_color == 13) {
t_Sample.rgb = saturate(SampleMultiTexA().rgb + SampleMultiTexB().rgb - u_MultiTexReg[0].r) * u_MultiTexReg[0].g;
} else if (multi_tex_calc_type_color == 14) {
t_Sample.rgb = mix(u_MultiTexReg[0].rgb, t_Sample.rgb, t_Sample.a);
} else if (multi_tex_calc_type_color == 17) {
t_Sample.rgb = mix(SampleMultiTexA().rgb * u_MultiTexReg[0].rgb, t_Sample.rgb, u_MultiTexReg[0].a);
} else if (multi_tex_calc_type_color == 19) {
t_Sample.rgb = t_Sample.rgb * saturate(SampleMultiTexA().rgb + u_MultiTexReg[0].rgb);
} else if (multi_tex_calc_type_color == 20) {
t_Sample.rgb = mix(u_MultiTexReg[0].rgb, t_Sample.rgb, v_VtxColor.rgb);
} else if (multi_tex_calc_type_color == 21) {
t_Sample.rgb = mix(u_MultiTexReg[0].rgb, u_MultiTexReg[1].rgb, (t_Sample.r + t_Sample.g + t_Sample.b) / 3.0);
} else if (multi_tex_calc_type_color == 22) {
t_Sample.rgb = saturate(t_Sample.rgb + SampleMultiTexA().rgb * SampleMultiTexB().rgb);
} else if (multi_tex_calc_type_color == 24) {
t_Sample.rgb += mix(u_MultiTexReg[0].rgb, u_MultiTexReg[1].rgb, SampleMultiTexA().r) * SampleMultiTexA().r * u_MultiTexReg[0].a * u_MultiTexReg[1].a;
} else if (multi_tex_calc_type_color == 25) {
t_Sample.rgb += mix(u_MultiTexReg[0].rgb + u_MultiTexReg[1].rgb, u_MultiTexReg[2].rgb, SampleMultiTexA().r) * SampleMultiTexA().x * u_MultiTexReg[0].a * u_MultiTexReg[1].a;
} else if (multi_tex_calc_type_color == 27) {
t_Sample.rgb = saturate(SampleMultiTexA().rgb * u_MultiTexReg[0].rgb + t_Sample.rgb);
} else if (multi_tex_calc_type_color == 30) {
t_Sample.rgb *= SampleMultiTexA().rgb + SampleMultiTexB().rgb;
} else if (multi_tex_calc_type_color == 33) {
t_Sample.rgb = SampleMultiTexA().rgb * u_MultiTexReg[0].rgb + t_Sample.rgb;
} else if (multi_tex_calc_type_color == 34) {
t_Sample.rgb = SampleMultiTexA().rgb + t_Sample.rgb;
} else if (multi_tex_calc_type_color == 35) {
t_Sample.rgb = SampleMultiTexA().rgb + t_Sample.rgb; // same as 34?
} else if (multi_tex_calc_type_color == 38) {
t_Sample.rgb += (SampleMultiTexA().rgb * (vec3(1.0) - SampleMultiTexB().rgb)) * u_MultiTexReg[0].r;
} else if (multi_tex_calc_type_color == 39) {
t_Sample.rgb *= u_MultiTexReg[0].rgb + u_MultiTexReg[1].rgb * SampleMultiTexA().rgb;
} else if (multi_tex_calc_type_color == 40) {
t_Sample.rgb = SampleMultiTexA().rgb * SampleMultiTexA().a * v_VtxColor.r + t_Sample.rgb;
} else if (multi_tex_calc_type_color == 44) {
t_Sample.rgb = saturate((t_Sample.rgb - SampleMultiTexA().rgb - u_MultiTexReg[0].rrr) * u_MultiTexReg[0].ggg);
} else {
// Unknown multi texture calc type.
bool is_development = ${IS_DEVELOPMENT};
if (is_development)
t_Sample.rgb = vec3(1.0, 0.0, 1.0);
}
int multi_tex_calc_type_alpha = ${this.shaderOptionInt('multi_tex_calc_type_alpha')};
if (multi_tex_calc_type_alpha == 0) {
// This space intentionally left blank.
} else if (multi_tex_calc_type_alpha == 1) {
t_Sample.a *= SampleMultiTexA().a;
} else if (multi_tex_calc_type_alpha == 4) {
t_Sample.a = (SampleMultiTexA().a + SampleMultiTexA().b - u_MultiTexReg[0].b) * u_MultiTexReg[0].a;
} else if (multi_tex_calc_type_alpha == 6) {
t_Sample.a = SampleMultiTexA().a;
} else if (multi_tex_calc_type_alpha == 7) {
t_Sample.a = SampleMultiTexA().r;
} else if (multi_tex_calc_type_alpha == 8) {
t_Sample.a = SampleMultiTexB().r;
} else {
// Unknown multi texture calc type.
bool is_development = ${IS_DEVELOPMENT};
if (is_development)
t_Sample.rgb = vec3(1.0, 1.0, 0.0);
}
}
vec3 ReconstructNormal(in vec2 t_NormalXY) {
float t_NormalZ = sqrt(saturate(1.0 - dot(t_NormalXY.xy, t_NormalXY.xy)));
return vec3(t_NormalXY.xy, t_NormalZ);
}
vec3 UnpackNormalMap(in vec4 t_NormalMapSample) {
bool normalmap_bc1 = ${this.shaderOptionBool('gsys_normalmap_BC1')};
vec2 t_NormalXY = t_NormalMapSample.xy;
if (normalmap_bc1) {
t_NormalXY = t_NormalXY * 2.0 - 1.0;
}
return ReconstructNormal(t_NormalXY);
}
vec3 SampleNormalMap0() {
vec2 t_TexCoord = SelectTexCoord(${this.shaderOptionInt('texcoord_select_normal')});
Indirect(t_TexCoord.xy, ${this.shaderOptionBool('indirect_effect_normal')});
vec4 t_NormalMapSample = texture(SAMPLER_2D(u_TextureNormal0), t_TexCoord);
return UnpackNormalMap(t_NormalMapSample);
}
vec3 SampleNormalMap1() {
vec2 t_TexCoord = SelectTexCoord(${this.shaderOptionInt('texcoord_select_normal2')});
vec4 t_NormalMapSample = texture(SAMPLER_2D(u_TextureNormal0), t_TexCoord);
return UnpackNormalMap(t_NormalMapSample);
}
vec3 CalcTangentToWorld(in vec3 t_TangentNormal, in vec3 t_Basis0, in vec3 t_Basis1, in vec3 t_Basis2) {
return t_TangentNormal.xxx * t_Basis0 + t_TangentNormal.yyy * t_Basis1 + t_TangentNormal.zzz * t_Basis2;
}
vec3 CalcNormalWorld() {
bool enable_normal_map = ${this.shaderOptionBool('enable_normal_map')};
if (!enable_normal_map)
return v_NormalWorld.xyz;
vec3 t_Basis2 = v_NormalWorld.xyz;
vec3 t_Basis0 = v_TangentWorld.xyz;
vec3 t_Basis1 = cross(v_NormalWorld.xyz, v_TangentWorld.xyz) * v_TangentWorld.w;
// We now have our basis. Now sample the normal maps.
vec3 t_TangentNormal0 = SampleNormalMap0();
vec3 t_NormalWorld0 = CalcTangentToWorld(t_TangentNormal0, t_Basis0, t_Basis1, t_Basis2);
bool gsys_enable_normal_map2 = ${this.shaderOptionBool('gsys_enable_normal_map2')};
if (gsys_enable_normal_map2) {
vec3 t_TangentNormal1 = SampleNormalMap1();
vec3 t_NormalWorld1 = CalcTangentToWorld(t_TangentNormal1, t_Basis0, t_Basis1, t_Basis2);
return normalize(mix(t_NormalWorld0, t_NormalWorld1, u_NormalMapWeight));
} else {
return t_NormalWorld0;
}
}
void main() {
// ShaderOption settings.
bool enable_diffuse = ${this.shaderOptionBool('enable_diffuse')};
bool enable_diffuse2 = ${this.shaderOptionBool('enable_diffuse2')};
bool enable_albedo = ${this.shaderOptionBool('enable_albedo')};
bool enable_emission = ${this.shaderOptionBool('enable_emission')};
bool enable_emission_map = ${this.shaderOptionBool('enable_emission_map')};
bool enable_specular = ${this.shaderOptionBool('enable_specular')};
bool enable_specular_mask = ${this.shaderOptionBool('enable_specular_mask')};
bool enable_specular_mask_rougness = ${this.shaderOptionBool('enable_specular_mask_rougness')};
bool enable_specular_physical = ${this.shaderOptionBool('enable_specular_physical')};
bool enable_vtx_color_diff = ${this.shaderOptionBool('enable_vtx_color_diff')};
bool enable_vtx_color_emission = ${this.shaderOptionBool('enable_vtx_color_emission')};
bool enable_vtx_color_spec = ${this.shaderOptionBool('enable_vtx_color_spec')};
bool enable_vtx_alpha_trans = ${this.shaderOptionBool('enable_vtx_alpha_trans')};
bool enable_decal_ao = ${this.shaderOptionBool('enable_decal_ao')};
bool enable_opa_trans = ${this.shaderOptionBool('enable_opa_trans')};
vec4 t_PixelOut = vec4(0.0);
float t_Alpha = 1.0;
// Calculate incoming light.
vec3 t_IncomingLightDiffuse = vec3(0.0);
BakeResult t_BakeResult;
CalcBakeResult(t_BakeResult, v_TexCoordBake);
t_IncomingLightDiffuse += t_BakeResult.IndirectLight;
vec3 t_NormalWorld = CalcNormalWorld();
vec3 t_IncomingLightSpecular = vec3(0.0);
// Flip normals if we're backfacing (shouldn't happen except for transmission)
if (enable_opa_trans && !gl_FrontFacing)
t_NormalWorld = -t_NormalWorld;
vec4 t_AlbedoTex = vec4(1.0);
vec3 t_Albedo = u_AlbedoColor.rgb;
vec3 t_Emission = u_EmissionColor.rgb;
if (enable_diffuse2 && enable_albedo) {
vec2 t_AlbedoTexCoord = v_TexCoord0.xy;
Indirect(t_AlbedoTexCoord.xy, ${this.shaderOptionBool('indirect_effect_albedo')});
vec4 t_AlbedoSample = texture(SAMPLER_2D(u_TextureAlbedo0), t_AlbedoTexCoord.xy);
t_AlbedoTex.rgba = t_AlbedoSample.rgba;
CalcMultiTexture(0, t_AlbedoSample);
t_Albedo.rgb *= t_AlbedoSample.rgb;
t_Alpha *= t_AlbedoSample.a;
}
if (enable_vtx_color_diff) {
t_Albedo.rgb *= v_VtxColor.rgb;
}
vec3 t_SpecMask = vec3(1.0);
float t_SpecularRoughness = u_SpecularRoughness;
if (enable_specular) {
if (enable_specular_mask) {
vec2 t_SpecularTexCoord = SelectTexCoord(${this.shaderOptionInt('texcoord_select_specmask')});
Indirect(t_SpecularTexCoord.xy, ${this.shaderOptionBool('indirect_effect_specmask')});
vec4 t_SpecMaskSample = texture(SAMPLER_2D(u_TextureSpecMask), t_SpecularTexCoord.xy);
CalcMultiTexture(3, t_SpecMaskSample);
t_SpecMask.rgb = t_SpecMaskSample.rgb;
CalcGeoMultiTextureSpec(t_SpecMask);
if (enable_specular_mask_rougness) {
t_SpecularRoughness *= 1.0 - t_SpecMask.g;
}
}
if (enable_specular_physical) {
t_SpecularRoughness = 1.0 - u_SpecularIntensity;
}
}
if (enable_emission && enable_emission_map) {
vec2 t_EmissionTexCoord = SelectTexCoord(${this.shaderOptionInt('texcoord_select_emission')});
Indirect(t_EmissionTexCoord.xy, ${this.shaderOptionBool('indirect_effect_emission')});
vec4 t_EmissionSample = texture(SAMPLER_2D(u_TextureEmission0), t_EmissionTexCoord.xy);
CalcMultiTexture(1, t_EmissionSample);
t_Emission.rgb *= t_EmissionSample.rgb;
}
if (enable_emission && enable_vtx_color_emission) {
t_Emission.rgb *= v_VtxColor.rgb;
}
vec3 t_SurfacePointToEye = u_CameraPosWorld.xyz - v_PositionWorld.xyz;
vec3 t_SurfacePointToEyeDir = normalize(t_SurfacePointToEye.xyz);
SurfaceLightParams t_SurfaceLightParams;
t_SurfaceLightParams.SurfaceNormal = t_NormalWorld.xyz;
t_SurfaceLightParams.SurfacePointToEyeDir = t_SurfacePointToEyeDir.xyz;
t_SurfaceLightParams.SpecularRoughness = t_SpecularRoughness;
t_SurfaceLightParams.SpecularColor = u_SpecularColor * u_SpecularIntensity * 10.0;
if (enable_specular) {
if (enable_specular_mask_rougness) {
t_SurfaceLightParams.SpecularColor.rgb *= t_SpecMask.rrr;
} else {
t_SurfaceLightParams.SpecularColor.rgb *= t_SpecMask.rgb;
}
if (enable_vtx_color_spec) {
t_SurfaceLightParams.SpecularColor.rgb *= v_VtxColor.rgb;
}
}
t_SurfaceLightParams.IntensityFromShadow = t_BakeResult.Shadow;
if (enable_diffuse) {
LightResult t_LightResult;
CalcEnvLight(t_LightResult, t_SurfaceLightParams);
t_IncomingLightDiffuse += t_LightResult.DiffuseColor;
// TODO(jstpierre): Calculate specular light from cubemap instead of directional lights.
t_IncomingLightSpecular += max(t_LightResult.SpecularColor, vec3(0.0));
} else {
LightResult t_LightResult;
t_SurfaceLightParams.SurfaceNormal = vec3(0.0, 1.0, 0.0);
t_SurfaceLightParams.IntensityFromShadow = 0.0;
CalcEnvLight(t_LightResult, t_SurfaceLightParams);
t_IncomingLightDiffuse = mix(t_LightResult.DiffuseColor, vec3(1.0), vec3(t_BakeResult.Shadow));
}
if (enable_diffuse2) {
t_PixelOut.rgb += t_Albedo.rgb * t_IncomingLightDiffuse.rgb;
}
vec3 t_AOColor = vec3(t_BakeResult.AO);
t_PixelOut.rgb *= t_AOColor;
if (enable_opa_trans) {
bool enable_opa_trans_tex = ${this.shaderOptionBool('enable_opa_trans_tex')};
bool enable_opa_trans_albedo = ${this.shaderOptionBool('enable_opa_trans_albedo')};
vec3 t_Transmission = u_TransmissionColorAndIntensity.rgb * u_TransmissionColorAndIntensity.a;
if (enable_opa_trans_tex) {
vec2 t_TransTexCoord = SelectTexCoord(${this.shaderOptionInt('texcoord_select_transmitt')});
t_Transmission *= texture(u_TextureTransmission, t_TransTexCoord).rgb;
}
if (enable_opa_trans_albedo)
t_Transmission *= t_Albedo.rgb;
LightResult t_LightResult; // Backfacing light
t_SurfaceLightParams.SurfaceNormal = -t_NormalWorld.xyz;
CalcEnvLight(t_LightResult, t_SurfaceLightParams);
vec3 t_IncomingLightTransmission = t_Transmission * t_LightResult.DiffuseColor;
t_PixelOut.rgb += t_IncomingLightTransmission.rgb;
}
if (enable_emission) {
t_PixelOut.rgb += t_Emission.rgb;
}
if (enable_specular) {
t_PixelOut.rgb += t_IncomingLightSpecular.rgb;
}
bool is_xlu = ${this.isTranslucent()};
if (is_xlu) {
t_Alpha *= u_Transparency;
if (enable_vtx_alpha_trans) {
t_Alpha *= v_VtxColor.a;
}
}
if (enable_decal_ao) {
// Fake it for now...
vec4 t_StaticShadowSample = vec4(1.0);
float t_TrailValue = t_StaticShadowSample.w;
int decal_trail_type = ${this.shaderOptionInt('decal_trail_type')};
if (decal_trail_type == 2) {
t_TrailValue = saturate(t_TrailValue * -2.0 + 1.9);
t_PixelOut.rgb = mix( t_PixelOut.rgb, vec3(0.0), t_TrailValue);
} else if (decal_trail_type == 4) {
t_TrailValue = ((t_TrailValue - 0.5) * (1.0 + t_BakeResult.Shadow)) * 4.0 + 1.0;
t_PixelOut.rgb *= clamp(t_TrailValue, 0.2, 1.8);
}
}
t_PixelOut.a = t_Alpha;
${this.generateAlphaTest()}
// Gamma correct
t_PixelOut.rgb = pow(t_PixelOut.rgb, vec3(1.0 / 2.2));
gl_FragColor = t_PixelOut;
}
`;
}
private generateAlphaTestCompare(compare: GfxCompareMode, ref: string) {
switch (compare) {
case GfxCompareMode.Never: return `false`;
case GfxCompareMode.Less: return `t_PixelOut.a < ${ref}`;
case GfxCompareMode.Equal: return `t_PixelOut.a == ${ref}`;
case GfxCompareMode.LessEqual: return `t_PixelOut.a <= ${ref}`;
case GfxCompareMode.Greater: return `t_PixelOut.a > ${ref}`;
case GfxCompareMode.NotEqual: return `t_PixelOut.a != ${ref}`;
case GfxCompareMode.GreaterEqual: return `t_PixelOut.a >= ${ref}`;
case GfxCompareMode.Always: return `true`;
default: throw "whoops";
}
}
private generateAlphaTest(): string {
if (!getRenderInfoBoolean(this.fmat.renderInfo.get('gsys_alpha_test_enable')!))
return '';
const mode = getRenderInfoCompareMode(this.fmat.renderInfo.get('gsys_alpha_test_func')!);
const ref = getRenderInfoSingleFloat(this.fmat.renderInfo.get('gsys_alpha_test_value')!);
const compareExpr = this.generateAlphaTestCompare(mode, glslGenerateFloat(ref));
return `
if (!(${compareExpr}))
discard;
`;
}
}
function getRenderInfoSingleString(renderInfo: FMAT_RenderInfo): string {
assert(renderInfo.type === FMAT_RenderInfoType.String && renderInfo.values.length === 1);
return renderInfo.values[0] as string;
}
function getRenderInfoSingleFloat(renderInfo: FMAT_RenderInfo): number {
assert(renderInfo.type === FMAT_RenderInfoType.Float && renderInfo.values.length === 1);
return renderInfo.values[0] as number;
}
function getRenderInfoBoolean(renderInfo: FMAT_RenderInfo): boolean {
const value = getRenderInfoSingleString(renderInfo);
if (value === 'true' || value === '1')
return true;
else if (value === 'false' || value === '0')
return false;
else
throw "whoops";
}
function translateCullMode(fmat: FMAT): GfxCullMode {
const display_face = getRenderInfoSingleString(fmat.renderInfo.get('gsys_render_state_display_face')!);
if (display_face === 'front')
return GfxCullMode.Back;
else if (display_face === 'back')
return GfxCullMode.Front;
else if (display_face === 'both')
return GfxCullMode.None;
else
throw "whoops";
}
function translateDepthWrite(fmat: FMAT): boolean {
return getRenderInfoBoolean(fmat.renderInfo.get('gsys_depth_test_write')!);
}
function getRenderInfoCompareMode(renderInfo: FMAT_RenderInfo): GfxCompareMode {
const value = getRenderInfoSingleString(renderInfo);
if (value === 'lequal')
return GfxCompareMode.LessEqual;
else if (value === 'gequal')
return GfxCompareMode.GreaterEqual;
else if (value === 'greater')
return GfxCompareMode.Greater;
else
throw "whoops";
}
function translateDepthCompare(fmat: FMAT): GfxCompareMode {
if (getRenderInfoBoolean(fmat.renderInfo.get('gsys_depth_test_enable')!)) {
return getRenderInfoCompareMode(fmat.renderInfo.get('gsys_depth_test_func')!);
} else {
return GfxCompareMode.Always;
}
}
function getRenderInfoBlendMode(renderInfo: FMAT_RenderInfo): GfxBlendMode {
const value = getRenderInfoSingleString(renderInfo);
if (value === 'add')
return GfxBlendMode.Add;
else
throw "whoops";
}
function translateRenderInfoBlendFactor(renderInfo: FMAT_RenderInfo): GfxBlendFactor {
const value = getRenderInfoSingleString(renderInfo);
if (value === 'src_alpha')
return GfxBlendFactor.SrcAlpha;
else if (value === 'one_minus_src_alpha')
return GfxBlendFactor.OneMinusSrcAlpha;
else if (value === 'one')
return GfxBlendFactor.One;
else if (value === 'zero')
return GfxBlendFactor.Zero;
else
throw "whoops";
}
function findShaderParam(fmat: FMAT, name: string): FMAT_ShaderParam {
return fmat.shaderParam.find((p) => p.name === name)!;
}
function calcTexMtx_Maya(dst: mat4, scaleS: number, scaleT: number, rotation: number, translationS: number, translationT: number): void {
const theta = rotation * MathConstants.DEG_TO_RAD;
const sinR = Math.sin(theta);
const cosR = Math.cos(theta);
mat4.identity(dst);
dst[0] = scaleS * cosR;
dst[4] = scaleS * sinR;
dst[12] = scaleS * ((-0.5 * cosR) - (0.5 * sinR - 0.5) - translationS);
dst[1] = scaleT * -sinR;
dst[5] = scaleT * cosR;
dst[13] = scaleT * ((-0.5 * cosR) + (0.5 * sinR - 0.5) + translationT) + 1.0;
}
function calcTexMtx_Max(dst: mat4, scaleS: number, scaleT: number, rotation: number, translationS: number, translationT: number): void {