| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// Uniforms access |
| 3 |
//------------------------------------------------------------------------------ |
| 4 |
|
| 5 |
mat4 getLightFromWorldMatrix() { |
| 6 |
return frameUniforms.lightFromWorldMatrix[0]; |
| 7 |
} |
| 8 |
|
| 9 |
#if defined(HAS_SHADOWING) |
| 10 |
mat4 getSpotLightFromWorldMatrix(uint index) { |
| 11 |
return shadowUniforms.spotLightFromWorldMatrix[index]; |
| 12 |
} |
| 13 |
#endif |
| 14 |
|
| 15 |
/** @public-api */ |
| 16 |
mat4 getWorldFromModelMatrix() { |
| 17 |
return objectUniforms.worldFromModelMatrix; |
| 18 |
} |
| 19 |
|
| 20 |
/** @public-api */ |
| 21 |
mat3 getWorldFromModelNormalMatrix() { |
| 22 |
return objectUniforms.worldFromModelNormalMatrix; |
| 23 |
} |
| 24 |
|
| 25 |
//------------------------------------------------------------------------------ |
| 26 |
// Attributes access |
| 27 |
//------------------------------------------------------------------------------ |
| 28 |
|
| 29 |
#if defined(HAS_SKINNING_OR_MORPHING) |
| 30 |
vec3 mulBoneNormal(vec3 n, uint i) { |
| 31 |
vec4 q = bonesUniforms.bones[i + 0u]; |
| 32 |
vec3 is = bonesUniforms.bones[i + 3u].xyz; |
| 33 |
|
| 34 |
// apply the inverse of the non-uniform scales |
| 35 |
n *= is; |
| 36 |
// apply the rigid transform (valid only for unit quaternions) |
| 37 |
n += 2.0 * cross(q.xyz, cross(q.xyz, n) + q.w * n); |
| 38 |
|
| 39 |
return n; |
| 40 |
} |
| 41 |
|
| 42 |
vec3 mulBoneVertex(vec3 v, uint i) { |
| 43 |
vec4 q = bonesUniforms.bones[i + 0u]; |
| 44 |
vec3 t = bonesUniforms.bones[i + 1u].xyz; |
| 45 |
vec3 s = bonesUniforms.bones[i + 2u].xyz; |
| 46 |
|
| 47 |
// apply the non-uniform scales |
| 48 |
v *= s; |
| 49 |
// apply the rigid transform (valid only for unit quaternions) |
| 50 |
v += 2.0 * cross(q.xyz, cross(q.xyz, v) + q.w * v); |
| 51 |
// apply the translation |
| 52 |
v += t; |
| 53 |
|
| 54 |
return v; |
| 55 |
} |
| 56 |
|
| 57 |
void skinNormal(inout vec3 n, const uvec4 ids, const vec4 weights) { |
| 58 |
n = mulBoneNormal(n, ids.x * 4u) * weights.x |
| 59 |
+ mulBoneNormal(n, ids.y * 4u) * weights.y |
| 60 |
+ mulBoneNormal(n, ids.z * 4u) * weights.z |
| 61 |
+ mulBoneNormal(n, ids.w * 4u) * weights.w; |
| 62 |
} |
| 63 |
|
| 64 |
void skinPosition(inout vec3 p, const uvec4 ids, const vec4 weights) { |
| 65 |
p = mulBoneVertex(p, ids.x * 4u) * weights.x |
| 66 |
+ mulBoneVertex(p, ids.y * 4u) * weights.y |
| 67 |
+ mulBoneVertex(p, ids.z * 4u) * weights.z |
| 68 |
+ mulBoneVertex(p, ids.w * 4u) * weights.w; |
| 69 |
} |
| 70 |
#endif |
| 71 |
|
| 72 |
/** @public-api */ |
| 73 |
vec4 getPosition() { |
| 74 |
vec4 pos = mesh_position; |
| 75 |
|
| 76 |
#if defined(HAS_SKINNING_OR_MORPHING) |
| 77 |
|
| 78 |
if (objectUniforms.morphingEnabled == 1) { |
| 79 |
pos += objectUniforms.morphWeights.x * mesh_custom0; |
| 80 |
pos += objectUniforms.morphWeights.y * mesh_custom1; |
| 81 |
pos += objectUniforms.morphWeights.z * mesh_custom2; |
| 82 |
pos += objectUniforms.morphWeights.w * mesh_custom3; |
| 83 |
} |
| 84 |
|
| 85 |
if (objectUniforms.skinningEnabled == 1) { |
| 86 |
skinPosition(pos.xyz, mesh_bone_indices, mesh_bone_weights); |
| 87 |
} |
| 88 |
|
| 89 |
#endif |
| 90 |
|
| 91 |
return pos; |
| 92 |
} |
| 93 |
|
| 94 |
#if defined(HAS_ATTRIBUTE_CUSTOM0) |
| 95 |
vec4 getCustom0() { return mesh_custom0; } |
| 96 |
#endif |
| 97 |
#if defined(HAS_ATTRIBUTE_CUSTOM1) |
| 98 |
vec4 getCustom1() { return mesh_custom1; } |
| 99 |
#endif |
| 100 |
#if defined(HAS_ATTRIBUTE_CUSTOM2) |
| 101 |
vec4 getCustom2() { return mesh_custom2; } |
| 102 |
#endif |
| 103 |
#if defined(HAS_ATTRIBUTE_CUSTOM3) |
| 104 |
vec4 getCustom3() { return mesh_custom3; } |
| 105 |
#endif |
| 106 |
#if defined(HAS_ATTRIBUTE_CUSTOM4) |
| 107 |
vec4 getCustom4() { return mesh_custom4; } |
| 108 |
#endif |
| 109 |
#if defined(HAS_ATTRIBUTE_CUSTOM5) |
| 110 |
vec4 getCustom5() { return mesh_custom5; } |
| 111 |
#endif |
| 112 |
#if defined(HAS_ATTRIBUTE_CUSTOM6) |
| 113 |
vec4 getCustom6() { return mesh_custom6; } |
| 114 |
#endif |
| 115 |
#if defined(HAS_ATTRIBUTE_CUSTOM7) |
| 116 |
vec4 getCustom7() { return mesh_custom7; } |
| 117 |
#endif |
| 118 |
|
| 119 |
/** @public-api */ |
| 120 |
int getVertexIndex() { |
| 121 |
#if defined(TARGET_METAL_ENVIRONMENT) || defined(TARGET_VULKAN_ENVIRONMENT) |
| 122 |
return gl_VertexIndex; |
| 123 |
#else |
| 124 |
return gl_VertexID; |
| 125 |
#endif |
| 126 |
} |
| 127 |
|
| 128 |
//------------------------------------------------------------------------------ |
| 129 |
// Helpers |
| 130 |
//------------------------------------------------------------------------------ |
| 131 |
|
| 132 |
/** |
| 133 |
* Computes and returns the position in world space of the current vertex. |
| 134 |
* The world position computation depends on the current vertex domain. This |
| 135 |
* function optionally applies vertex skinning if needed. |
| 136 |
* |
| 137 |
* NOTE: the "transform" and "position" temporaries are necessary to work around |
| 138 |
* an issue with Adreno drivers (b/110851741). |
| 139 |
*/ |
| 140 |
vec4 computeWorldPosition() { |
| 141 |
#if defined(VERTEX_DOMAIN_OBJECT) |
| 142 |
mat4 transform = getWorldFromModelMatrix(); |
| 143 |
vec3 position = getPosition().xyz; |
| 144 |
return mulMat4x4Float3(transform, position); |
| 145 |
#elif defined(VERTEX_DOMAIN_WORLD) |
| 146 |
return vec4(getPosition().xyz, 1.0); |
| 147 |
#elif defined(VERTEX_DOMAIN_VIEW) |
| 148 |
mat4 transform = getWorldFromViewMatrix(); |
| 149 |
vec3 position = getPosition().xyz; |
| 150 |
return mulMat4x4Float3(transform, position); |
| 151 |
#else |
| 152 |
mat4 transform = getWorldFromClipMatrix(); |
| 153 |
vec4 position = transform * getPosition(); |
| 154 |
if (abs(position.w) < MEDIUMP_FLT_MIN) { |
| 155 |
position.w = position.w < 0.0 ? -MEDIUMP_FLT_MIN : MEDIUMP_FLT_MIN; |
| 156 |
} |
| 157 |
return position * (1.0 / position.w); |
| 158 |
#endif |
| 159 |
} |