/
transform.js
128 lines (106 loc) · 3.53 KB
/
transform.js
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
export default /* glsl */`
#ifdef PIXELSNAP
uniform vec4 uScreenSize;
#endif
#ifdef SCREENSPACE
uniform float projectionFlipY;
#endif
#ifdef MORPHING
uniform vec4 morph_weights_a;
uniform vec4 morph_weights_b;
#endif
#ifdef MORPHING_TEXTURE_BASED
uniform vec2 morph_tex_params;
ivec2 getTextureMorphCoords() {
// turn morph_vertex_id into int grid coordinates
ivec2 textureSize = ivec2(morph_tex_params);
int morphGridV = int(morph_vertex_id) / textureSize.x;
int morphGridU = int(morph_vertex_id) - (morphGridV * textureSize.x);
#ifdef WEBGPU
// flipY
morphGridV = textureSize.y - morphGridV - 1;
#endif
return ivec2(morphGridU, morphGridV);
}
#endif
#ifdef MORPHING_TEXTURE_BASED_POSITION
uniform highp sampler2D morphPositionTex;
#endif
mat4 getModelMatrix() {
#ifdef DYNAMICBATCH
return getBoneMatrix(vertex_boneIndices);
#elif defined(SKIN)
return matrix_model * getSkinMatrix(vertex_boneIndices, vertex_boneWeights);
#elif defined(INSTANCING)
return mat4(instance_line1, instance_line2, instance_line3, instance_line4);
#else
return matrix_model;
#endif
}
vec4 getPosition() {
dModelMatrix = getModelMatrix();
vec3 localPos = vertex_position;
#ifdef NINESLICED
// outer and inner vertices are at the same position, scale both
localPos.xz *= outerScale;
// offset inner vertices inside
// (original vertices must be in [-1;1] range)
vec2 positiveUnitOffset = clamp(vertex_position.xz, vec2(0.0), vec2(1.0));
vec2 negativeUnitOffset = clamp(-vertex_position.xz, vec2(0.0), vec2(1.0));
localPos.xz += (-positiveUnitOffset * innerOffset.xy + negativeUnitOffset * innerOffset.zw) * vertex_texCoord0.xy;
vTiledUv = (localPos.xz - outerScale + innerOffset.xy) * -0.5 + 1.0; // uv = local pos - inner corner
localPos.xz *= -0.5; // move from -1;1 to -0.5;0.5
localPos = localPos.xzy;
#endif
#ifdef MORPHING
#ifdef MORPHING_POS03
localPos.xyz += morph_weights_a[0] * morph_pos0;
localPos.xyz += morph_weights_a[1] * morph_pos1;
localPos.xyz += morph_weights_a[2] * morph_pos2;
localPos.xyz += morph_weights_a[3] * morph_pos3;
#endif // MORPHING_POS03
#ifdef MORPHING_POS47
localPos.xyz += morph_weights_b[0] * morph_pos4;
localPos.xyz += morph_weights_b[1] * morph_pos5;
localPos.xyz += morph_weights_b[2] * morph_pos6;
localPos.xyz += morph_weights_b[3] * morph_pos7;
#endif // MORPHING_POS47
#endif // MORPHING
#ifdef MORPHING_TEXTURE_BASED_POSITION
ivec2 morphUV = getTextureMorphCoords();
vec3 morphPos = texelFetch(morphPositionTex, ivec2(morphUV), 0).xyz;
localPos += morphPos;
#endif
vec4 posW = dModelMatrix * vec4(localPos, 1.0);
#ifdef SCREENSPACE
posW.zw = vec2(0.0, 1.0);
#endif
dPositionW = posW.xyz;
vec4 screenPos;
#ifdef UV1LAYOUT
screenPos = vec4(vertex_texCoord1.xy * 2.0 - 1.0, 0.5, 1);
#ifdef WEBGPU
screenPos.y *= -1.0;
#endif
#else
#ifdef SCREENSPACE
screenPos = posW;
screenPos.y *= projectionFlipY;
#else
screenPos = matrix_viewProjection * posW;
#endif
#ifdef PIXELSNAP
// snap vertex to a pixel boundary
screenPos.xy = (screenPos.xy * 0.5) + 0.5;
screenPos.xy *= uScreenSize.xy;
screenPos.xy = floor(screenPos.xy);
screenPos.xy *= uScreenSize.zw;
screenPos.xy = (screenPos.xy * 2.0) - 1.0;
#endif
#endif
return screenPos;
}
vec3 getWorldPosition() {
return dPositionW;
}
`;