struct GltfMaterial { int shadingModel; // 0: metallic-roughness, 1: specular-glossiness // PbrMetallicRoughness vec4 pbrBaseColorFactor; int pbrBaseColorTexture; float pbrMetallicFactor; float pbrRoughnessFactor; int pbrMetallicRoughnessTexture; // KHR_materials_pbrSpecularGlossiness vec4 khrDiffuseFactor; int khrDiffuseTexture; vec3 khrSpecularFactor; float khrGlossinessFactor; int khrSpecularGlossinessTexture; int emissiveTexture; vec3 emissiveFactor; int alphaMode; float alphaCutoff; bool doubleSided; int normalTexture; float normalTextureScale; int occlusionTexture; float occlusionTextureStrength; }; struct PrimMeshInfo { uint indexOffset; uint vertexOffset; int materialIndex; }; vec3 computeDiffuse(GltfMaterial mat, vec3 lightDir, vec3 normal) { // Lambertian float dotNL = max(dot(normal, lightDir), 0.0); return mat.pbrBaseColorFactor.xyz * dotNL; } vec3 computeSpecular(GltfMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) { // Compute specular only if not in shadow const float kPi = 3.14159265; const float kShininess = 60.0; // Specular const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); vec3 V = normalize(-viewDir); vec3 R = reflect(-lightDir, normal); float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); return vec3(specular); }