| 1 |
void main() { |
| 2 |
// Initialize the inputs to sensible default values, see material_inputs.vs |
| 3 |
MaterialVertexInputs material; |
| 4 |
initMaterialVertex(material); |
| 5 |
|
| 6 |
#if defined(HAS_ATTRIBUTE_TANGENTS) |
| 7 |
// If the material defines a value for the "normal" property, we need to output |
| 8 |
// the full orthonormal basis to apply normal mapping |
| 9 |
#if defined(MATERIAL_NEEDS_TBN) |
| 10 |
// Extract the normal and tangent in world space from the input quaternion |
| 11 |
// We encode the orthonormal basis as a quaternion to save space in the attributes |
| 12 |
toTangentFrame(mesh_tangents, material.worldNormal, vertex_worldTangent.xyz); |
| 13 |
|
| 14 |
#if defined(HAS_SKINNING_OR_MORPHING) |
| 15 |
if (objectUniforms.morphingEnabled == 1) { |
| 16 |
vec3 normal0, normal1, normal2, normal3; |
| 17 |
toTangentFrame(mesh_custom4, normal0); |
| 18 |
toTangentFrame(mesh_custom5, normal1); |
| 19 |
toTangentFrame(mesh_custom6, normal2); |
| 20 |
toTangentFrame(mesh_custom7, normal3); |
| 21 |
material.worldNormal += objectUniforms.morphWeights.x * normal0; |
| 22 |
material.worldNormal += objectUniforms.morphWeights.y * normal1; |
| 23 |
material.worldNormal += objectUniforms.morphWeights.z * normal2; |
| 24 |
material.worldNormal += objectUniforms.morphWeights.w * normal3; |
| 25 |
material.worldNormal = normalize(material.worldNormal); |
| 26 |
} |
| 27 |
|
| 28 |
if (objectUniforms.skinningEnabled == 1) { |
| 29 |
skinNormal(material.worldNormal, mesh_bone_indices, mesh_bone_weights); |
| 30 |
skinNormal(vertex_worldTangent.xyz, mesh_bone_indices, mesh_bone_weights); |
| 31 |
} |
| 32 |
#endif |
| 33 |
|
| 34 |
// We don't need to normalize here, even if there's a scale in the matrix |
| 35 |
// because we ensure the worldFromModelNormalMatrix pre-scales the normal such that |
| 36 |
// all its components are < 1.0. This prevents the bitangent to exceed the range of fp16 |
| 37 |
// in the fragment shader, where we renormalize after interpolation |
| 38 |
vertex_worldTangent.xyz = objectUniforms.worldFromModelNormalMatrix * vertex_worldTangent.xyz; |
| 39 |
vertex_worldTangent.w = mesh_tangents.w; |
| 40 |
material.worldNormal = objectUniforms.worldFromModelNormalMatrix * material.worldNormal; |
| 41 |
#else // MATERIAL_NEEDS_TBN |
| 42 |
// Without anisotropy or normal mapping we only need the normal vector |
| 43 |
toTangentFrame(mesh_tangents, material.worldNormal); |
| 44 |
|
| 45 |
#if defined(HAS_SKINNING_OR_MORPHING) |
| 46 |
if (objectUniforms.skinningEnabled == 1) { |
| 47 |
skinNormal(material.worldNormal, mesh_bone_indices, mesh_bone_weights); |
| 48 |
} |
| 49 |
#endif |
| 50 |
|
| 51 |
material.worldNormal = objectUniforms.worldFromModelNormalMatrix * material.worldNormal; |
| 52 |
|
| 53 |
#endif // MATERIAL_HAS_ANISOTROPY || MATERIAL_HAS_NORMAL || MATERIAL_HAS_CLEAR_COAT_NORMAL |
| 54 |
#endif // HAS_ATTRIBUTE_TANGENTS |
| 55 |
|
| 56 |
// Invoke user code |
| 57 |
materialVertex(material); |
| 58 |
|
| 59 |
// Handle built-in interpolated attributes |
| 60 |
#if defined(HAS_ATTRIBUTE_COLOR) |
| 61 |
vertex_color = material.color; |
| 62 |
#endif |
| 63 |
#if defined(HAS_ATTRIBUTE_UV0) |
| 64 |
vertex_uv01.xy = material.uv0; |
| 65 |
#endif |
| 66 |
#if defined(HAS_ATTRIBUTE_UV1) |
| 67 |
vertex_uv01.zw = material.uv1; |
| 68 |
#endif |
| 69 |
|
| 70 |
// Handle user-defined interpolated attributes |
| 71 |
#if defined(VARIABLE_CUSTOM0) |
| 72 |
VARIABLE_CUSTOM_AT0 = material.VARIABLE_CUSTOM0; |
| 73 |
#endif |
| 74 |
#if defined(VARIABLE_CUSTOM1) |
| 75 |
VARIABLE_CUSTOM_AT1 = material.VARIABLE_CUSTOM1; |
| 76 |
#endif |
| 77 |
#if defined(VARIABLE_CUSTOM2) |
| 78 |
VARIABLE_CUSTOM_AT2 = material.VARIABLE_CUSTOM2; |
| 79 |
#endif |
| 80 |
#if defined(VARIABLE_CUSTOM3) |
| 81 |
VARIABLE_CUSTOM_AT3 = material.VARIABLE_CUSTOM3; |
| 82 |
#endif |
| 83 |
|
| 84 |
// The world position can be changed by the user in materialVertex() |
| 85 |
vertex_worldPosition = material.worldPosition.xyz; |
| 86 |
#ifdef HAS_ATTRIBUTE_TANGENTS |
| 87 |
vertex_worldNormal = material.worldNormal; |
| 88 |
#endif |
| 89 |
|
| 90 |
#if defined(HAS_SHADOWING) && defined(HAS_DIRECTIONAL_LIGHTING) |
| 91 |
vertex_lightSpacePosition = computeLightSpacePosition(vertex_worldPosition, vertex_worldNormal, |
| 92 |
frameUniforms.lightDirection, frameUniforms.shadowBias.y, getLightFromWorldMatrix()); |
| 93 |
#endif |
| 94 |
|
| 95 |
#if defined(HAS_SHADOWING) && defined(HAS_DYNAMIC_LIGHTING) |
| 96 |
for (uint l = 0u; l < uint(MAX_SHADOW_CASTING_SPOTS); l++) { |
| 97 |
vec3 dir = shadowUniforms.directionShadowBias[l].xyz; |
| 98 |
float bias = shadowUniforms.directionShadowBias[l].w; |
| 99 |
vertex_spotLightSpacePosition[l] = computeLightSpacePosition(vertex_worldPosition, |
| 100 |
vertex_worldNormal, dir, bias, getSpotLightFromWorldMatrix(l)); |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
#if defined(VERTEX_DOMAIN_DEVICE) |
| 105 |
// The other vertex domains are handled in initMaterialVertex()->computeWorldPosition() |
| 106 |
gl_Position = getPosition(); |
| 107 |
#else |
| 108 |
gl_Position = getClipFromWorldMatrix() * getWorldPosition(material); |
| 109 |
#endif |
| 110 |
|
| 111 |
#if defined(MATERIAL_HAS_CLIP_SPACE_TRANSFORM) |
| 112 |
gl_Position = getClipSpaceTransform(material) * gl_Position; |
| 113 |
#endif |
| 114 |
|
| 115 |
// this must happen before we compensate for vulkan below |
| 116 |
vertex_position = gl_Position; |
| 117 |
|
| 118 |
#if defined(TARGET_VULKAN_ENVIRONMENT) |
| 119 |
// In Vulkan, clip space is Y-down. In OpenGL and Metal, clip space is Y-up. |
| 120 |
gl_Position.y = -gl_Position.y; |
| 121 |
#endif |
| 122 |
|
| 123 |
gl_Position.z = dot(gl_Position.zw, frameUniforms.clipControl.xy); |
| 124 |
} |