| 1 |
/* clang-format off */ |
| 2 |
#[modes] |
| 3 |
|
| 4 |
mode_base_pass = |
| 5 |
mode_blend_pass = #define MODE_BLEND_PASS |
| 6 |
|
| 7 |
#[specializations] |
| 8 |
|
| 9 |
MODE_2D = true |
| 10 |
USE_BLEND_SHAPES = false |
| 11 |
USE_SKELETON = false |
| 12 |
USE_NORMAL = false |
| 13 |
USE_TANGENT = false |
| 14 |
FINAL_PASS = false |
| 15 |
USE_EIGHT_WEIGHTS = false |
| 16 |
|
| 17 |
#[vertex] |
| 18 |
|
| 19 |
#include "stdlib_inc.glsl" |
| 20 |
|
| 21 |
#ifdef MODE_2D |
| 22 |
#define VFORMAT vec2 |
| 23 |
#else |
| 24 |
#define VFORMAT vec3 |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifdef FINAL_PASS |
| 28 |
#define OFORMAT vec2 |
| 29 |
#else |
| 30 |
#define OFORMAT uvec2 |
| 31 |
#endif |
| 32 |
|
| 33 |
// These come from the source mesh and the output from previous passes. |
| 34 |
layout(location = 0) in highp VFORMAT in_vertex; |
| 35 |
#ifdef MODE_BLEND_PASS |
| 36 |
#ifdef USE_NORMAL |
| 37 |
layout(location = 1) in highp uvec2 in_normal; |
| 38 |
#endif |
| 39 |
#ifdef USE_TANGENT |
| 40 |
layout(location = 2) in highp uvec2 in_tangent; |
| 41 |
#endif |
| 42 |
#else // MODE_BLEND_PASS |
| 43 |
#ifdef USE_NORMAL |
| 44 |
layout(location = 1) in highp vec2 in_normal; |
| 45 |
#endif |
| 46 |
#ifdef USE_TANGENT |
| 47 |
layout(location = 2) in highp vec2 in_tangent; |
| 48 |
#endif |
| 49 |
#endif // MODE_BLEND_PASS |
| 50 |
|
| 51 |
#ifdef USE_SKELETON |
| 52 |
#ifdef USE_EIGHT_WEIGHTS |
| 53 |
layout(location = 10) in highp uvec4 in_bone_attrib; |
| 54 |
layout(location = 11) in highp uvec4 in_bone_attrib2; |
| 55 |
layout(location = 12) in mediump vec4 in_weight_attrib; |
| 56 |
layout(location = 13) in mediump vec4 in_weight_attrib2; |
| 57 |
#else |
| 58 |
layout(location = 10) in highp uvec4 in_bone_attrib; |
| 59 |
layout(location = 11) in mediump vec4 in_weight_attrib; |
| 60 |
#endif |
| 61 |
|
| 62 |
uniform highp sampler2D skeleton_texture; // texunit:0 |
| 63 |
#endif |
| 64 |
|
| 65 |
/* clang-format on */ |
| 66 |
#ifdef MODE_BLEND_PASS |
| 67 |
layout(location = 3) in highp VFORMAT blend_vertex; |
| 68 |
#ifdef USE_NORMAL |
| 69 |
layout(location = 4) in highp vec2 blend_normal; |
| 70 |
#endif |
| 71 |
#ifdef USE_TANGENT |
| 72 |
layout(location = 5) in highp vec2 blend_tangent; |
| 73 |
#endif |
| 74 |
#endif // MODE_BLEND_PASS |
| 75 |
|
| 76 |
out highp VFORMAT out_vertex; //tfb: |
| 77 |
|
| 78 |
#ifdef USE_NORMAL |
| 79 |
flat out highp OFORMAT out_normal; //tfb:USE_NORMAL |
| 80 |
#endif |
| 81 |
#ifdef USE_TANGENT |
| 82 |
flat out highp OFORMAT out_tangent; //tfb:USE_TANGENT |
| 83 |
#endif |
| 84 |
|
| 85 |
#ifdef USE_BLEND_SHAPES |
| 86 |
uniform highp float blend_weight; |
| 87 |
uniform lowp float blend_shape_count; |
| 88 |
#endif |
| 89 |
|
| 90 |
#ifdef USE_SKELETON |
| 91 |
uniform mediump vec2 skeleton_transform_x; |
| 92 |
uniform mediump vec2 skeleton_transform_y; |
| 93 |
uniform mediump vec2 skeleton_transform_offset; |
| 94 |
|
| 95 |
uniform mediump vec2 inverse_transform_x; |
| 96 |
uniform mediump vec2 inverse_transform_y; |
| 97 |
uniform mediump vec2 inverse_transform_offset; |
| 98 |
#endif |
| 99 |
|
| 100 |
vec2 signNotZero(vec2 v) { |
| 101 |
return mix(vec2(-1.0), vec2(1.0), greaterThanEqual(v.xy, vec2(0.0))); |
| 102 |
} |
| 103 |
|
| 104 |
vec3 oct_to_vec3(vec2 oct) { |
| 105 |
oct = oct * 2.0 - 1.0; |
| 106 |
vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y)); |
| 107 |
if (v.z < 0.0) { |
| 108 |
v.xy = (1.0 - abs(v.yx)) * signNotZero(v.xy); |
| 109 |
} |
| 110 |
return normalize(v); |
| 111 |
} |
| 112 |
|
| 113 |
vec2 vec3_to_oct(vec3 e) { |
| 114 |
e /= abs(e.x) + abs(e.y) + abs(e.z); |
| 115 |
vec2 oct = e.z >= 0.0f ? e.xy : (vec2(1.0f) - abs(e.yx)) * signNotZero(e.xy); |
| 116 |
return oct * 0.5f + 0.5f; |
| 117 |
} |
| 118 |
|
| 119 |
vec4 oct_to_tang(vec2 oct_sign_encoded) { |
| 120 |
// Binormal sign encoded in y component |
| 121 |
vec2 oct = vec2(oct_sign_encoded.x, abs(oct_sign_encoded.y) * 2.0 - 1.0); |
| 122 |
return vec4(oct_to_vec3(oct), sign(oct_sign_encoded.y)); |
| 123 |
} |
| 124 |
|
| 125 |
vec2 tang_to_oct(vec4 base) { |
| 126 |
vec2 oct = vec3_to_oct(base.xyz); |
| 127 |
// Encode binormal sign in y component |
| 128 |
oct.y = oct.y * 0.5f + 0.5f; |
| 129 |
oct.y = base.w >= 0.0f ? oct.y : 1.0 - oct.y; |
| 130 |
return oct; |
| 131 |
} |
| 132 |
|
| 133 |
// Our original input for normals and tangents is 2 16-bit floats. |
| 134 |
// Transform Feedback has to write out 32-bits per channel. |
| 135 |
// Octahedral compression requires normalized vectors, but we need to store |
| 136 |
// non-normalized vectors until the very end. |
| 137 |
// Therefore, we will compress our normals into 16 bits using signed-normalized |
| 138 |
// fixed point precision. This works well, because we know that each normal |
| 139 |
// is no larger than |1| so we can normalize by dividing by the number of blend |
| 140 |
// shapes. |
| 141 |
uvec2 vec4_to_vec2(vec4 p_vec) { |
| 142 |
return uvec2(packSnorm2x16(p_vec.xy), packSnorm2x16(p_vec.zw)); |
| 143 |
} |
| 144 |
|
| 145 |
vec4 vec2_to_vec4(uvec2 p_vec) { |
| 146 |
return vec4(unpackSnorm2x16(p_vec.x), unpackSnorm2x16(p_vec.y)); |
| 147 |
} |
| 148 |
|
| 149 |
void main() { |
| 150 |
#ifdef MODE_2D |
| 151 |
out_vertex = in_vertex; |
| 152 |
|
| 153 |
#ifdef USE_BLEND_SHAPES |
| 154 |
#ifdef MODE_BLEND_PASS |
| 155 |
out_vertex = in_vertex + blend_vertex * blend_weight; |
| 156 |
#else |
| 157 |
out_vertex = in_vertex * blend_weight; |
| 158 |
#endif |
| 159 |
#ifdef FINAL_PASS |
| 160 |
out_vertex = normalize(out_vertex); |
| 161 |
#endif |
| 162 |
#endif // USE_BLEND_SHAPES |
| 163 |
|
| 164 |
#ifdef USE_SKELETON |
| 165 |
|
| 166 |
#define TEX(m) texelFetch(skeleton_texture, ivec2(m % 256u, m / 256u), 0) |
| 167 |
#define GET_BONE_MATRIX(a, b, w) mat2x4(TEX(a), TEX(b)) * w |
| 168 |
|
| 169 |
uvec4 bones = in_bone_attrib * uvec4(2u); |
| 170 |
uvec4 bones_a = bones + uvec4(1u); |
| 171 |
|
| 172 |
highp mat2x4 m = GET_BONE_MATRIX(bones.x, bones_a.x, in_weight_attrib.x); |
| 173 |
m += GET_BONE_MATRIX(bones.y, bones_a.y, in_weight_attrib.y); |
| 174 |
m += GET_BONE_MATRIX(bones.z, bones_a.z, in_weight_attrib.z); |
| 175 |
m += GET_BONE_MATRIX(bones.w, bones_a.w, in_weight_attrib.w); |
| 176 |
|
| 177 |
mat4 skeleton_matrix = mat4(vec4(skeleton_transform_x, 0.0, 0.0), vec4(skeleton_transform_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(skeleton_transform_offset, 0.0, 1.0)); |
| 178 |
mat4 inverse_matrix = mat4(vec4(inverse_transform_x, 0.0, 0.0), vec4(inverse_transform_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(inverse_transform_offset, 0.0, 1.0)); |
| 179 |
mat4 bone_matrix = mat4(m[0], m[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0)); |
| 180 |
|
| 181 |
bone_matrix = skeleton_matrix * transpose(bone_matrix) * inverse_matrix; |
| 182 |
|
| 183 |
out_vertex = (bone_matrix * vec4(out_vertex, 0.0, 1.0)).xy; |
| 184 |
#endif // USE_SKELETON |
| 185 |
|
| 186 |
#else // MODE_2D |
| 187 |
|
| 188 |
#ifdef USE_BLEND_SHAPES |
| 189 |
#ifdef MODE_BLEND_PASS |
| 190 |
out_vertex = in_vertex + blend_vertex * blend_weight; |
| 191 |
|
| 192 |
#ifdef USE_NORMAL |
| 193 |
vec3 normal = vec2_to_vec4(in_normal).xyz * blend_shape_count; |
| 194 |
vec3 normal_blend = oct_to_vec3(blend_normal) * blend_weight; |
| 195 |
#ifdef FINAL_PASS |
| 196 |
out_normal = vec3_to_oct(normalize(normal + normal_blend)); |
| 197 |
#else |
| 198 |
out_normal = vec4_to_vec2(vec4(normal + normal_blend, 0.0) / blend_shape_count); |
| 199 |
#endif |
| 200 |
#endif // USE_NORMAL |
| 201 |
|
| 202 |
#ifdef USE_TANGENT |
| 203 |
vec4 tangent = vec2_to_vec4(in_tangent) * blend_shape_count; |
| 204 |
vec4 tangent_blend = oct_to_tang(blend_tangent) * blend_weight; |
| 205 |
#ifdef FINAL_PASS |
| 206 |
out_tangent = tang_to_oct(vec4(normalize(tangent.xyz + tangent_blend.xyz), tangent.w)); |
| 207 |
#else |
| 208 |
out_tangent = vec4_to_vec2(vec4((tangent.xyz + tangent_blend.xyz) / blend_shape_count, tangent.w)); |
| 209 |
#endif |
| 210 |
#endif // USE_TANGENT |
| 211 |
|
| 212 |
#else // MODE_BLEND_PASS |
| 213 |
out_vertex = in_vertex * blend_weight; |
| 214 |
|
| 215 |
#ifdef USE_NORMAL |
| 216 |
vec3 normal = oct_to_vec3(in_normal); |
| 217 |
out_normal = vec4_to_vec2(vec4(normal * blend_weight / blend_shape_count, 0.0)); |
| 218 |
#endif |
| 219 |
#ifdef USE_TANGENT |
| 220 |
vec4 tangent = oct_to_tang(in_tangent); |
| 221 |
out_tangent = vec4_to_vec2(vec4(tangent.rgb * blend_weight / blend_shape_count, tangent.w)); |
| 222 |
#endif |
| 223 |
#endif // MODE_BLEND_PASS |
| 224 |
#else // USE_BLEND_SHAPES |
| 225 |
|
| 226 |
// Make attributes available to the skeleton shader if not written by blend shapes. |
| 227 |
out_vertex = in_vertex; |
| 228 |
#ifdef USE_NORMAL |
| 229 |
out_normal = in_normal; |
| 230 |
#endif |
| 231 |
#ifdef USE_TANGENT |
| 232 |
out_tangent = in_tangent; |
| 233 |
#endif |
| 234 |
#endif // USE_BLEND_SHAPES |
| 235 |
|
| 236 |
#ifdef USE_SKELETON |
| 237 |
|
| 238 |
#define TEX(m) texelFetch(skeleton_texture, ivec2(m % 256u, m / 256u), 0) |
| 239 |
#define GET_BONE_MATRIX(a, b, c, w) mat4(TEX(a), TEX(b), TEX(c), vec4(0.0, 0.0, 0.0, 1.0)) * w |
| 240 |
|
| 241 |
uvec4 bones = in_bone_attrib * uvec4(3); |
| 242 |
uvec4 bones_a = bones + uvec4(1); |
| 243 |
uvec4 bones_b = bones + uvec4(2); |
| 244 |
|
| 245 |
highp mat4 m; |
| 246 |
m = GET_BONE_MATRIX(bones.x, bones_a.x, bones_b.x, in_weight_attrib.x); |
| 247 |
m += GET_BONE_MATRIX(bones.y, bones_a.y, bones_b.y, in_weight_attrib.y); |
| 248 |
m += GET_BONE_MATRIX(bones.z, bones_a.z, bones_b.z, in_weight_attrib.z); |
| 249 |
m += GET_BONE_MATRIX(bones.w, bones_a.w, bones_b.w, in_weight_attrib.w); |
| 250 |
|
| 251 |
#ifdef USE_EIGHT_WEIGHTS |
| 252 |
bones = in_bone_attrib2 * uvec4(3); |
| 253 |
bones_a = bones + uvec4(1); |
| 254 |
bones_b = bones + uvec4(2); |
| 255 |
|
| 256 |
m += GET_BONE_MATRIX(bones.x, bones_a.x, bones_b.x, in_weight_attrib2.x); |
| 257 |
m += GET_BONE_MATRIX(bones.y, bones_a.y, bones_b.y, in_weight_attrib2.y); |
| 258 |
m += GET_BONE_MATRIX(bones.z, bones_a.z, bones_b.z, in_weight_attrib2.z); |
| 259 |
m += GET_BONE_MATRIX(bones.w, bones_a.w, bones_b.w, in_weight_attrib2.w); |
| 260 |
#endif |
| 261 |
|
| 262 |
// Reverse order because its transposed. |
| 263 |
out_vertex = (vec4(out_vertex, 1.0) * m).xyz; |
| 264 |
#ifdef USE_NORMAL |
| 265 |
vec3 vertex_normal = oct_to_vec3(out_normal); |
| 266 |
out_normal = vec3_to_oct(normalize((vec4(vertex_normal, 0.0) * m).xyz)); |
| 267 |
#endif // USE_NORMAL |
| 268 |
#ifdef USE_TANGENT |
| 269 |
vec4 vertex_tangent = oct_to_tang(out_tangent); |
| 270 |
out_tangent = tang_to_oct(vec4(normalize((vec4(vertex_tangent.xyz, 0.0) * m).xyz), vertex_tangent.w)); |
| 271 |
#endif // USE_TANGENT |
| 272 |
#endif // USE_SKELETON |
| 273 |
#endif // MODE_2D |
| 274 |
} |
| 275 |
|
| 276 |
/* clang-format off */ |
| 277 |
#[fragment] |
| 278 |
|
| 279 |
void main() { |
| 280 |
|
| 281 |
} |
| 282 |
/* clang-format on */ |