@luma.gl/shadertools 9.3.0-alpha.2 → 9.3.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/dist/dist.dev.js +2114 -198
  2. package/dist/dist.min.js +315 -184
  3. package/dist/index.cjs +463 -199
  4. package/dist/index.cjs.map +4 -4
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/lib/preprocessor/preprocessor.d.ts.map +1 -1
  10. package/dist/lib/preprocessor/preprocessor.js +33 -7
  11. package/dist/lib/preprocessor/preprocessor.js.map +1 -1
  12. package/dist/lib/shader-assembler.d.ts.map +1 -1
  13. package/dist/lib/shader-assembler.js +8 -1
  14. package/dist/lib/shader-assembler.js.map +1 -1
  15. package/dist/lib/shader-module/shader-module.d.ts +1 -1
  16. package/dist/lib/shader-module/shader-module.d.ts.map +1 -1
  17. package/dist/lib/utils/assert.d.ts.map +1 -1
  18. package/dist/lib/utils/assert.js +3 -1
  19. package/dist/lib/utils/assert.js.map +1 -1
  20. package/dist/modules/engine/skin/skin.d.ts +29 -0
  21. package/dist/modules/engine/skin/skin.d.ts.map +1 -0
  22. package/dist/modules/engine/skin/skin.js +88 -0
  23. package/dist/modules/engine/skin/skin.js.map +1 -0
  24. package/dist/modules/lighting/lights/lighting-glsl.d.ts +1 -1
  25. package/dist/modules/lighting/lights/lighting-glsl.d.ts.map +1 -1
  26. package/dist/modules/lighting/lights/lighting-glsl.js +20 -2
  27. package/dist/modules/lighting/lights/lighting-glsl.js.map +1 -1
  28. package/dist/modules/lighting/lights/lighting-wgsl.d.ts +1 -1
  29. package/dist/modules/lighting/lights/lighting-wgsl.d.ts.map +1 -1
  30. package/dist/modules/lighting/lights/lighting-wgsl.js +63 -13
  31. package/dist/modules/lighting/lights/lighting-wgsl.js.map +1 -1
  32. package/dist/modules/lighting/lights/lighting.d.ts +27 -3
  33. package/dist/modules/lighting/lights/lighting.d.ts.map +1 -1
  34. package/dist/modules/lighting/lights/lighting.js +32 -6
  35. package/dist/modules/lighting/lights/lighting.js.map +1 -1
  36. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.d.ts +1 -1
  37. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.d.ts.map +1 -1
  38. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.js +106 -79
  39. package/dist/modules/lighting/pbr-material/pbr-material-wgsl.js.map +1 -1
  40. package/dist/modules/lighting/pbr-material/pbr-material.d.ts +20 -4
  41. package/dist/modules/lighting/pbr-material/pbr-material.d.ts.map +1 -1
  42. package/dist/modules/lighting/pbr-material/pbr-projection.d.ts.map +1 -1
  43. package/dist/modules/lighting/pbr-material/pbr-projection.js +12 -1
  44. package/dist/modules/lighting/pbr-material/pbr-projection.js.map +1 -1
  45. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts +1 -40
  46. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts.map +1 -1
  47. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.js +40 -76
  48. package/dist/modules/lighting/phong-material/phong-shaders-wgsl.js.map +1 -1
  49. package/dist/modules/math/random/random.d.ts +1 -1
  50. package/dist/modules/math/random/random.d.ts.map +1 -1
  51. package/dist/modules/math/random/random.js +2 -3
  52. package/dist/modules/math/random/random.js.map +1 -1
  53. package/package.json +2 -2
  54. package/src/index.ts +1 -0
  55. package/src/lib/preprocessor/preprocessor.ts +40 -7
  56. package/src/lib/shader-assembler.ts +8 -1
  57. package/src/lib/shader-module/shader-module.ts +1 -1
  58. package/src/lib/utils/assert.ts +3 -1
  59. package/src/modules/engine/skin/skin.ts +116 -0
  60. package/src/modules/lighting/lights/lighting-glsl.ts +20 -2
  61. package/src/modules/lighting/lights/lighting-wgsl.ts +63 -13
  62. package/src/modules/lighting/lights/lighting.ts +45 -9
  63. package/src/modules/lighting/pbr-material/pbr-material-wgsl.ts +106 -79
  64. package/src/modules/lighting/pbr-material/pbr-projection.ts +13 -1
  65. package/src/modules/lighting/phong-material/phong-shaders-wgsl.ts +40 -77
  66. package/src/modules/math/random/random.ts +2 -3
@@ -97,6 +97,14 @@ export declare const pbrMaterial: {
97
97
  readonly lightPosition2: "vec3<f32>";
98
98
  readonly lightDirection2: "vec3<f32>";
99
99
  readonly lightAttenuation2: "vec3<f32>";
100
+ readonly lightColor3: "vec3<f32>";
101
+ readonly lightPosition3: "vec3<f32>";
102
+ readonly lightDirection3: "vec3<f32>";
103
+ readonly lightAttenuation3: "vec3<f32>";
104
+ readonly lightColor4: "vec3<f32>";
105
+ readonly lightPosition4: "vec3<f32>";
106
+ readonly lightDirection4: "vec3<f32>";
107
+ readonly lightAttenuation4: "vec3<f32>";
100
108
  };
101
109
  readonly defaultUniforms: {
102
110
  readonly enabled: 1;
@@ -116,13 +124,21 @@ export declare const pbrMaterial: {
116
124
  readonly lightPosition2: readonly [1, 1, 2];
117
125
  readonly lightDirection2: readonly [1, 1, 1];
118
126
  readonly lightAttenuation2: readonly [1, 0, 0];
127
+ readonly lightColor3: readonly [1, 1, 1];
128
+ readonly lightPosition3: readonly [1, 1, 2];
129
+ readonly lightDirection3: readonly [1, 1, 1];
130
+ readonly lightAttenuation3: readonly [1, 0, 0];
131
+ readonly lightColor4: readonly [1, 1, 1];
132
+ readonly lightPosition4: readonly [1, 1, 2];
133
+ readonly lightDirection4: readonly [1, 1, 1];
134
+ readonly lightAttenuation4: readonly [1, 0, 0];
119
135
  };
120
- readonly source: "// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))\nstruct AmbientLight {\n color: vec3<f32>,\n};\n\nstruct PointLight {\n color: vec3<f32>,\n position: vec3<f32>,\n attenuation: vec3<f32>, // 2nd order x:Constant-y:Linear-z:Exponential\n};\n\nstruct DirectionalLight {\n color: vec3<f32>,\n direction: vec3<f32>,\n};\n\nstruct lightingUniforms {\n enabled: i32,\n pointLightCount: i32,\n directionalLightCount: i32,\n\n ambientColor: vec3<f32>,\n\n // TODO - support multiple lights by uncommenting arrays below\n lightType: i32,\n lightColor: vec3<f32>,\n lightDirection: vec3<f32>,\n lightPosition: vec3<f32>,\n lightAttenuation: vec3<f32>,\n\n // AmbientLight ambientLight;\n // PointLight pointLight[MAX_LIGHTS];\n // DirectionalLight directionalLight[MAX_LIGHTS];\n};\n\n// Binding 0:1 is reserved for lighting (Note: could go into separate bind group as it is stable across draw calls)\n@binding(1) @group(0) var<uniform> lighting : lightingUniforms;\n\nfn lighting_getPointLight(index: i32) -> PointLight {\n return PointLight(lighting.lightColor, lighting.lightPosition, lighting.lightAttenuation);\n}\n\nfn lighting_getDirectionalLight(index: i32) -> DirectionalLight {\n return DirectionalLight(lighting.lightColor, lighting.lightDirection);\n} \n\nfn getPointLightAttenuation(pointLight: PointLight, distance: f32) -> f32 {\n return pointLight.attenuation.x\n + pointLight.attenuation.y * distance\n + pointLight.attenuation.z * distance * distance;\n}\n";
121
- readonly vs: "precision highp int;\n\n// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))\nstruct AmbientLight {\n vec3 color;\n};\n\nstruct PointLight {\n vec3 color;\n vec3 position;\n vec3 attenuation; // 2nd order x:Constant-y:Linear-z:Exponential\n};\n\nstruct DirectionalLight {\n vec3 color;\n vec3 direction;\n};\n\nuniform lightingUniforms {\n int enabled;\n int lightType;\n\n int directionalLightCount;\n int pointLightCount;\n\n vec3 ambientColor;\n\n vec3 lightColor0;\n vec3 lightPosition0;\n vec3 lightDirection0;\n vec3 lightAttenuation0;\n\n vec3 lightColor1;\n vec3 lightPosition1;\n vec3 lightDirection1;\n vec3 lightAttenuation1;\n\n vec3 lightColor2;\n vec3 lightPosition2;\n vec3 lightDirection2;\n vec3 lightAttenuation2;\n} lighting;\n\nPointLight lighting_getPointLight(int index) {\n switch (index) {\n case 0:\n return PointLight(lighting.lightColor0, lighting.lightPosition0, lighting.lightAttenuation0);\n case 1:\n return PointLight(lighting.lightColor1, lighting.lightPosition1, lighting.lightAttenuation1);\n case 2:\n default: \n return PointLight(lighting.lightColor2, lighting.lightPosition2, lighting.lightAttenuation2);\n }\n}\n\nDirectionalLight lighting_getDirectionalLight(int index) {\n switch (index) {\n case 0:\n return DirectionalLight(lighting.lightColor0, lighting.lightDirection0);\n case 1:\n return DirectionalLight(lighting.lightColor1, lighting.lightDirection1);\n case 2:\n default: \n return DirectionalLight(lighting.lightColor2, lighting.lightDirection2);\n }\n} \n\nfloat getPointLightAttenuation(PointLight pointLight, float distance) {\n return pointLight.attenuation.x\n + pointLight.attenuation.y * distance\n + pointLight.attenuation.z * distance * distance;\n}\n\n// #endif\n";
122
- readonly fs: "precision highp int;\n\n// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))\nstruct AmbientLight {\n vec3 color;\n};\n\nstruct PointLight {\n vec3 color;\n vec3 position;\n vec3 attenuation; // 2nd order x:Constant-y:Linear-z:Exponential\n};\n\nstruct DirectionalLight {\n vec3 color;\n vec3 direction;\n};\n\nuniform lightingUniforms {\n int enabled;\n int lightType;\n\n int directionalLightCount;\n int pointLightCount;\n\n vec3 ambientColor;\n\n vec3 lightColor0;\n vec3 lightPosition0;\n vec3 lightDirection0;\n vec3 lightAttenuation0;\n\n vec3 lightColor1;\n vec3 lightPosition1;\n vec3 lightDirection1;\n vec3 lightAttenuation1;\n\n vec3 lightColor2;\n vec3 lightPosition2;\n vec3 lightDirection2;\n vec3 lightAttenuation2;\n} lighting;\n\nPointLight lighting_getPointLight(int index) {\n switch (index) {\n case 0:\n return PointLight(lighting.lightColor0, lighting.lightPosition0, lighting.lightAttenuation0);\n case 1:\n return PointLight(lighting.lightColor1, lighting.lightPosition1, lighting.lightAttenuation1);\n case 2:\n default: \n return PointLight(lighting.lightColor2, lighting.lightPosition2, lighting.lightAttenuation2);\n }\n}\n\nDirectionalLight lighting_getDirectionalLight(int index) {\n switch (index) {\n case 0:\n return DirectionalLight(lighting.lightColor0, lighting.lightDirection0);\n case 1:\n return DirectionalLight(lighting.lightColor1, lighting.lightDirection1);\n case 2:\n default: \n return DirectionalLight(lighting.lightColor2, lighting.lightDirection2);\n }\n} \n\nfloat getPointLightAttenuation(PointLight pointLight, float distance) {\n return pointLight.attenuation.x\n + pointLight.attenuation.y * distance\n + pointLight.attenuation.z * distance * distance;\n}\n\n// #endif\n";
136
+ readonly source: "// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))\nconst MAX_LIGHTS: i32 = 5;\n\nstruct AmbientLight {\n color: vec3<f32>,\n};\n\nstruct PointLight {\n color: vec3<f32>,\n position: vec3<f32>,\n attenuation: vec3<f32>, // 2nd order x:Constant-y:Linear-z:Exponential\n};\n\nstruct DirectionalLight {\n color: vec3<f32>,\n direction: vec3<f32>,\n};\n\nstruct lightingUniforms {\n enabled: i32,\n lightType: i32,\n\n directionalLightCount: i32,\n pointLightCount: i32,\n\n ambientColor: vec3<f32>,\n\n lightColor0: vec3<f32>,\n lightPosition0: vec3<f32>,\n lightDirection0: vec3<f32>,\n lightAttenuation0: vec3<f32>,\n\n lightColor1: vec3<f32>,\n lightPosition1: vec3<f32>,\n lightDirection1: vec3<f32>,\n lightAttenuation1: vec3<f32>,\n\n lightColor2: vec3<f32>,\n lightPosition2: vec3<f32>,\n lightDirection2: vec3<f32>,\n lightAttenuation2: vec3<f32>,\n\n lightColor3: vec3<f32>,\n lightPosition3: vec3<f32>,\n lightDirection3: vec3<f32>,\n lightAttenuation3: vec3<f32>,\n\n lightColor4: vec3<f32>,\n lightPosition4: vec3<f32>,\n lightDirection4: vec3<f32>,\n lightAttenuation4: vec3<f32>,\n};\n\n// Binding 0:1 is reserved for lighting (Note: could go into separate bind group as it is stable across draw calls)\n@binding(1) @group(0) var<uniform> lighting : lightingUniforms;\n\nfn lighting_getPointLight(index: i32) -> PointLight {\n switch (index) {\n case 0: {\n return PointLight(lighting.lightColor0, lighting.lightPosition0, lighting.lightAttenuation0);\n }\n case 1: {\n return PointLight(lighting.lightColor1, lighting.lightPosition1, lighting.lightAttenuation1);\n }\n case 2: {\n return PointLight(lighting.lightColor2, lighting.lightPosition2, lighting.lightAttenuation2);\n }\n case 3: {\n return PointLight(lighting.lightColor3, lighting.lightPosition3, lighting.lightAttenuation3);\n }\n case 4, default: {\n return PointLight(lighting.lightColor4, lighting.lightPosition4, lighting.lightAttenuation4);\n }\n }\n}\n\nfn lighting_getDirectionalLight(index: i32) -> DirectionalLight {\n switch (index) {\n case 0: {\n return DirectionalLight(lighting.lightColor0, lighting.lightDirection0);\n }\n case 1: {\n return DirectionalLight(lighting.lightColor1, lighting.lightDirection1);\n }\n case 2: {\n return DirectionalLight(lighting.lightColor2, lighting.lightDirection2);\n }\n case 3: {\n return DirectionalLight(lighting.lightColor3, lighting.lightDirection3);\n }\n case 4, default: {\n return DirectionalLight(lighting.lightColor4, lighting.lightDirection4);\n }\n }\n} \n\nfn getPointLightAttenuation(pointLight: PointLight, distance: f32) -> f32 {\n return pointLight.attenuation.x\n + pointLight.attenuation.y * distance\n + pointLight.attenuation.z * distance * distance;\n}\n";
137
+ readonly vs: "precision highp int;\n\n// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))\nstruct AmbientLight {\n vec3 color;\n};\n\nstruct PointLight {\n vec3 color;\n vec3 position;\n vec3 attenuation; // 2nd order x:Constant-y:Linear-z:Exponential\n};\n\nstruct DirectionalLight {\n vec3 color;\n vec3 direction;\n};\n\nuniform lightingUniforms {\n int enabled;\n int lightType;\n\n int directionalLightCount;\n int pointLightCount;\n\n vec3 ambientColor;\n\n vec3 lightColor0;\n vec3 lightPosition0;\n vec3 lightDirection0;\n vec3 lightAttenuation0;\n\n vec3 lightColor1;\n vec3 lightPosition1;\n vec3 lightDirection1;\n vec3 lightAttenuation1;\n\n vec3 lightColor2;\n vec3 lightPosition2;\n vec3 lightDirection2;\n vec3 lightAttenuation2;\n\n vec3 lightColor3;\n vec3 lightPosition3;\n vec3 lightDirection3;\n vec3 lightAttenuation3;\n\n vec3 lightColor4;\n vec3 lightPosition4;\n vec3 lightDirection4;\n vec3 lightAttenuation4;\n} lighting;\n\nPointLight lighting_getPointLight(int index) {\n switch (index) {\n case 0:\n return PointLight(lighting.lightColor0, lighting.lightPosition0, lighting.lightAttenuation0);\n case 1:\n return PointLight(lighting.lightColor1, lighting.lightPosition1, lighting.lightAttenuation1);\n case 2:\n return PointLight(lighting.lightColor2, lighting.lightPosition2, lighting.lightAttenuation2);\n case 3:\n return PointLight(lighting.lightColor3, lighting.lightPosition3, lighting.lightAttenuation3);\n case 4:\n default: \n return PointLight(lighting.lightColor4, lighting.lightPosition4, lighting.lightAttenuation4);\n }\n}\n\nDirectionalLight lighting_getDirectionalLight(int index) {\n switch (index) {\n case 0:\n return DirectionalLight(lighting.lightColor0, lighting.lightDirection0);\n case 1:\n return DirectionalLight(lighting.lightColor1, lighting.lightDirection1);\n case 2:\n return DirectionalLight(lighting.lightColor2, lighting.lightDirection2);\n case 3:\n return DirectionalLight(lighting.lightColor3, lighting.lightDirection3);\n case 4:\n default: \n return DirectionalLight(lighting.lightColor4, lighting.lightDirection4);\n }\n} \n\nfloat getPointLightAttenuation(PointLight pointLight, float distance) {\n return pointLight.attenuation.x\n + pointLight.attenuation.y * distance\n + pointLight.attenuation.z * distance * distance;\n}\n\n// #endif\n";
138
+ readonly fs: "precision highp int;\n\n// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))\nstruct AmbientLight {\n vec3 color;\n};\n\nstruct PointLight {\n vec3 color;\n vec3 position;\n vec3 attenuation; // 2nd order x:Constant-y:Linear-z:Exponential\n};\n\nstruct DirectionalLight {\n vec3 color;\n vec3 direction;\n};\n\nuniform lightingUniforms {\n int enabled;\n int lightType;\n\n int directionalLightCount;\n int pointLightCount;\n\n vec3 ambientColor;\n\n vec3 lightColor0;\n vec3 lightPosition0;\n vec3 lightDirection0;\n vec3 lightAttenuation0;\n\n vec3 lightColor1;\n vec3 lightPosition1;\n vec3 lightDirection1;\n vec3 lightAttenuation1;\n\n vec3 lightColor2;\n vec3 lightPosition2;\n vec3 lightDirection2;\n vec3 lightAttenuation2;\n\n vec3 lightColor3;\n vec3 lightPosition3;\n vec3 lightDirection3;\n vec3 lightAttenuation3;\n\n vec3 lightColor4;\n vec3 lightPosition4;\n vec3 lightDirection4;\n vec3 lightAttenuation4;\n} lighting;\n\nPointLight lighting_getPointLight(int index) {\n switch (index) {\n case 0:\n return PointLight(lighting.lightColor0, lighting.lightPosition0, lighting.lightAttenuation0);\n case 1:\n return PointLight(lighting.lightColor1, lighting.lightPosition1, lighting.lightAttenuation1);\n case 2:\n return PointLight(lighting.lightColor2, lighting.lightPosition2, lighting.lightAttenuation2);\n case 3:\n return PointLight(lighting.lightColor3, lighting.lightPosition3, lighting.lightAttenuation3);\n case 4:\n default: \n return PointLight(lighting.lightColor4, lighting.lightPosition4, lighting.lightAttenuation4);\n }\n}\n\nDirectionalLight lighting_getDirectionalLight(int index) {\n switch (index) {\n case 0:\n return DirectionalLight(lighting.lightColor0, lighting.lightDirection0);\n case 1:\n return DirectionalLight(lighting.lightColor1, lighting.lightDirection1);\n case 2:\n return DirectionalLight(lighting.lightColor2, lighting.lightDirection2);\n case 3:\n return DirectionalLight(lighting.lightColor3, lighting.lightDirection3);\n case 4:\n default: \n return DirectionalLight(lighting.lightColor4, lighting.lightDirection4);\n }\n} \n\nfloat getPointLightAttenuation(PointLight pointLight, float distance) {\n return pointLight.attenuation.x\n + pointLight.attenuation.y * distance\n + pointLight.attenuation.z * distance * distance;\n}\n\n// #endif\n";
123
139
  readonly getUniforms: (props?: import("../lights/lighting").LightingProps, prevUniforms?: Partial<import("../lights/lighting").LightingUniforms>) => import("../lights/lighting").LightingUniforms;
124
140
  }, ShaderModule<import("./pbr-projection").PBRProjectionProps>];
125
- readonly source: "struct PBRFragmentInputs {\n pbr_vPosition: vec3f,\n pbr_vUV: vec2f,\n pbr_vTBN: mat3f,\n pbr_vNormal: vec3f\n};\n\nvar fragmentInputs: PBRFragmentInputs;\n\nfn pbr_setPositionNormalTangentUV(position: vec4f, normal: vec4f, tangent: vec4f, uv: vec2f)\n{\n var pos: vec4f = pbrProjection.modelMatrix * position;\n fragmentInputs.pbr_vPosition = vec3(pos.xyz) / pos.w;\n\n#ifdef HAS_NORMALS\n#ifdef HAS_TANGENTS\n let normalW: vec3f = normalize(vec3(pbrProjection.normalMatrix * vec4(normal.xyz, 0.0)));\n let tangentW: vec3f = normalize(vec3(pbrProjection.modelMatrix * vec4(tangent.xyz, 0.0)));\n let bitangentW: vec3f = cross(normalW, tangentW) * tangent.w;\n fragmentInputs.pbr_vTBN = mat3(tangentW, bitangentW, normalW);\n#else // HAS_TANGENTS != 1\n fragmentInputs.pbr_vNormal = normalize(vec3(pbrProjection.modelMatrix * vec4(normal.xyz, 0.0)));\n#endif\n#endif\n\n#ifdef HAS_UV\n fragmentInputs.pbr_vUV = uv;\n#else\n fragmentInputs.pbr_vUV = vec2(0.,0.);\n#endif\n}\n\nstruct pbrMaterialUniforms {\n // Material is unlit\n unlit: uint32,\n\n // Base color map\n baseColorMapEnabled: uint32,\n baseColorFactor: vec4f,\n\n normalMapEnabled : uint32,\n normalScale: f32, // #ifdef HAS_NORMALMAP\n\n emissiveMapEnabled: uint32,\n emissiveFactor: vec3f, // #ifdef HAS_EMISSIVEMAP\n\n metallicRoughnessValues: vec2f,\n metallicRoughnessMapEnabled: uint32,\n\n occlusionMapEnabled: i32,\n occlusionStrength: f32, // #ifdef HAS_OCCLUSIONMAP\n \n alphaCutoffEnabled: i32,\n alphaCutoff: f32, // #ifdef ALPHA_CUTOFF\n \n // IBL\n IBLenabled: i32,\n scaleIBLAmbient: vec2f, // #ifdef USE_IBL\n \n // debugging flags used for shader output of intermediate PBR variables\n // #ifdef PBR_DEBUG\n scaleDiffBaseMR: vec4f,\n scaleFGDSpec: vec4f\n // #endif\n}\n\n@binding(2) @group(0) var<uniform> pbrMaterial : pbrMaterialUniforms;\n\n// Samplers\n#ifdef HAS_BASECOLORMAP\nuniform sampler2D pbr_baseColorSampler;\n#endif\n#ifdef HAS_NORMALMAP\nuniform sampler2D pbr_normalSampler;\n#endif\n#ifdef HAS_EMISSIVEMAP\nuniform sampler2D pbr_emissiveSampler;\n#endif\n#ifdef HAS_METALROUGHNESSMAP\nuniform sampler2D pbr_metallicRoughnessSampler;\n#endif\n#ifdef HAS_OCCLUSIONMAP\nuniform sampler2D pbr_occlusionSampler;\n#endif\n#ifdef USE_IBL\nuniform samplerCube pbr_diffuseEnvSampler;\nuniform samplerCube pbr_specularEnvSampler;\nuniform sampler2D pbr_brdfLUT;\n#endif\n\n// Encapsulate the various inputs used by the various functions in the shading equation\n// We store values in this struct to simplify the integration of alternative implementations\n// of the shading terms, outlined in the Readme.MD Appendix.\nstruct PBRInfo {\n NdotL: f32, // cos angle between normal and light direction\n NdotV: f32, // cos angle between normal and view direction\n NdotH: f32, // cos angle between normal and half vector\n LdotH: f32, // cos angle between light direction and half vector\n VdotH: f32, // cos angle between view direction and half vector\n perceptualRoughness: f32, // roughness value, as authored by the model creator (input to shader)\n metalness: f32, // metallic value at the surface\n reflectance0: vec3f, // full reflectance color (normal incidence angle)\n reflectance90: vec3f, // reflectance color at grazing angle\n alphaRoughness: f32, // roughness mapped to a more linear change in the roughness (proposed by [2])\n diffuseColor: vec3f, // color contribution from diffuse lighting\n specularColor: vec3f, // color contribution from specular lighting\n n: vec3f, // normal at surface point\n v: vec3f, // vector from surface point to camera\n};\n\nconst M_PI = 3.141592653589793;\nconst c_MinRoughness = 0.04;\n\nfn SRGBtoLINEAR(srgbIn: vec4f ) -> vec4f\n{\n#ifdef MANUAL_SRGB\n#ifdef SRGB_FAST_APPROXIMATION\n var linOut: vec3f = pow(srgbIn.xyz,vec3(2.2));\n#else // SRGB_FAST_APPROXIMATION\n var bLess: vec3f = step(vec3(0.04045),srgbIn.xyz);\n var linOut: vec3f = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );\n#endif //SRGB_FAST_APPROXIMATION\n return vec4f(linOut,srgbIn.w);;\n#else //MANUAL_SRGB\n return srgbIn;\n#endif //MANUAL_SRGB\n}\n\n// Find the normal for this fragment, pulling either from a predefined normal map\n// or from the interpolated mesh normal and tangent attributes.\nfn getNormal() -> vec3f\n{\n // Retrieve the tangent space matrix\n#ifndef HAS_TANGENTS\n var pos_dx: vec3f = dFdx(fragmentInputs.pbr_vPosition);\n var pos_dy: vec3f = dFdy(fragmentInputs.pbr_vPosition);\n var tex_dx: vec3f = dFdx(vec3(fragmentInputs.pbr_vUV, 0.0));\n var tex_dy: vec3f = dFdy(vec3(fragmentInputs.pbr_vUV, 0.0));\n var t: vec3f = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);\n\n#ifdef HAS_NORMALS\n var ng: vec3f = normalize(fragmentInputs.pbr_vNormal);\n#else\n var ng: vec3f = cross(pos_dx, pos_dy);\n#endif\n\n t = normalize(t - ng * dot(ng, t));\n var b: vec3f = normalize(cross(ng, t));\n var tbn: mat3f = mat3f(t, b, ng);\n#else // HAS_TANGENTS\n var tbn: mat3f = fragmentInputs.pbr_vTBN;\n#endif\n\n#ifdef HAS_NORMALMAP\n vec3 n = texture(pbr_normalSampler, fragmentInputs.pbr_vUV).rgb;\n n = normalize(tbn * ((2.0 * n - 1.0) * vec3(pbrMaterial.normalScale, pbrMaterial.normalScale, 1.0)));\n#else\n // The tbn matrix is linearly interpolated, so we need to re-normalize\n vec3 n = normalize(tbn[2].xyz);\n#endif\n\n return n;\n}\n\n// Calculation of the lighting contribution from an optional Image Based Light source.\n// Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].\n// See our README.md on Environment Maps [3] for additional discussion.\n#ifdef USE_IBL\nfn getIBLContribution(PBRInfo pbrInfo, vec3 n, vec3 reflection) -> vec3f\n{\n float mipCount = 9.0; // resolution of 512x512\n float lod = (pbrInfo.perceptualRoughness * mipCount);\n // retrieve a scale and bias to F0. See [1], Figure 3\n vec3 brdf = SRGBtoLINEAR(texture(pbr_brdfLUT,\n vec2(pbrInfo.NdotV, 1.0 - pbrInfo.perceptualRoughness))).rgb;\n vec3 diffuseLight = SRGBtoLINEAR(texture(pbr_diffuseEnvSampler, n)).rgb;\n\n#ifdef USE_TEX_LOD\n vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection, lod)).rgb;\n#else\n vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection)).rgb;\n#endif\n\n vec3 diffuse = diffuseLight * pbrInfo.diffuseColor;\n vec3 specular = specularLight * (pbrInfo.specularColor * brdf.x + brdf.y);\n\n // For presentation, this allows us to disable IBL terms\n diffuse *= pbrMaterial.scaleIBLAmbient.x;\n specular *= pbrMaterial.scaleIBLAmbient.y;\n\n return diffuse + specular;\n}\n#endif\n\n// Basic Lambertian diffuse\n// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog\n// See also [1], Equation 1\nfn diffuse(pbrInfo: PBRInfo) -> vec3<f32> {\n return pbrInfo.diffuseColor / M_PI;\n}\n\n// The following equation models the Fresnel reflectance term of the spec equation (aka F())\n// Implementation of fresnel from [4], Equation 15\nfn specularReflection(pbrInfo: PBRInfo) -> vec3<f32> {\n return pbrInfo.reflectance0 +\n (pbrInfo.reflectance90 - pbrInfo.reflectance0) *\n pow(clamp(1.0 - pbrInfo.VdotH, 0.0, 1.0), 5.0);\n}\n\n// This calculates the specular geometric attenuation (aka G()),\n// where rougher material will reflect less light back to the viewer.\n// This implementation is based on [1] Equation 4, and we adopt their modifications to\n// alphaRoughness as input as originally proposed in [2].\nfn geometricOcclusion(pbrInfo: PBRInfo) -> f32 {\n let NdotL: f32 = pbrInfo.NdotL;\n let NdotV: f32 = pbrInfo.NdotV;\n let r: f32 = pbrInfo.alphaRoughness;\n\n let attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));\n let attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));\n return attenuationL * attenuationV;\n}\n\n// The following equation(s) model the distribution of microfacet normals across\n// the area being drawn (aka D())\n// Implementation from \"Average Irregularity Representation of a Roughened Surface\n// for Ray Reflection\" by T. S. Trowbridge, and K. P. Reitz\n// Follows the distribution function recommended in the SIGGRAPH 2013 course notes\n// from EPIC Games [1], Equation 3.\nfn microfacetDistribution(pbrInfo: PBRInfo) -> f32 {\n let roughnessSq = pbrInfo.alphaRoughness * pbrInfo.alphaRoughness;\n let f = (pbrInfo.NdotH * roughnessSq - pbrInfo.NdotH) * pbrInfo.NdotH + 1.0;\n return roughnessSq / (PI * f * f);\n}\n\nfn PBRInfo_setAmbientLight(pbrInfo: ptr<function, PBRInfo>) {\n (*pbrInfo).NdotL = 1.0;\n (*pbrInfo).NdotH = 0.0;\n (*pbrInfo).LdotH = 0.0;\n (*pbrInfo).VdotH = 1.0;\n}\n\nfn PBRInfo_setDirectionalLight(pbrInfo: ptr<function, PBRInfo>, lightDirection: vec3<f32>) {\n let n = (*pbrInfo).n;\n let v = (*pbrInfo).v;\n let l = normalize(lightDirection); // Vector from surface point to light\n let h = normalize(l + v); // Half vector between both l and v\n\n (*pbrInfo).NdotL = clamp(dot(n, l), 0.001, 1.0);\n (*pbrInfo).NdotH = clamp(dot(n, h), 0.0, 1.0);\n (*pbrInfo).LdotH = clamp(dot(l, h), 0.0, 1.0);\n (*pbrInfo).VdotH = clamp(dot(v, h), 0.0, 1.0);\n}\n\nfn PBRInfo_setPointLight(pbrInfo: ptr<function, PBRInfo>, pointLight: PointLight) {\n let light_direction = normalize(pointLight.position - fragmentInputs.pbr_vPosition);\n PBRInfo_setDirectionalLight(pbrInfo, light_direction);\n}\n\nfn calculateFinalColor(pbrInfo: PBRInfo, lightColor: vec3<f32>) -> vec3<f32> {\n // Calculate the shading terms for the microfacet specular shading model\n let F = specularReflection(pbrInfo);\n let G = geometricOcclusion(pbrInfo);\n let D = microfacetDistribution(pbrInfo);\n\n // Calculation of analytical lighting contribution\n let diffuseContrib = (1.0 - F) * diffuse(pbrInfo);\n let specContrib = F * G * D / (4.0 * pbrInfo.NdotL * pbrInfo.NdotV);\n // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)\n return pbrInfo.NdotL * lightColor * (diffuseContrib + specContrib);\n}\n\nfn pbr_filterColor(colorUnused: vec4<f32>) -> vec4<f32> {\n // The albedo may be defined from a base texture or a flat color\n var baseColor: vec4<f32>;\n #ifdef HAS_BASECOLORMAP\n baseColor = SRGBtoLINEAR(textureSample(pbr_baseColorSampler, pbr_baseColorSampler, fragmentInputs.pbr_vUV)) * pbrMaterial.baseColorFactor;\n #else\n baseColor = pbrMaterial.baseColorFactor;\n #endif\n\n #ifdef ALPHA_CUTOFF\n if (baseColor.a < pbrMaterial.alphaCutoff) {\n discard;\n }\n #endif\n\n var color = vec3<f32>(0.0, 0.0, 0.0);\n\n if (pbrMaterial.unlit) {\n color = baseColor.rgb;\n } else {\n // Metallic and Roughness material properties are packed together\n // In glTF, these factors can be specified by fixed scalar values\n // or from a metallic-roughness map\n var perceptualRoughness = pbrMaterial.metallicRoughnessValues.y;\n var metallic = pbrMaterial.metallicRoughnessValues.x;\n #ifdef HAS_METALROUGHNESSMAP\n // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.\n // This layout intentionally reserves the 'r' channel for (optional) occlusion map data\n let mrSample = textureSample(pbr_metallicRoughnessSampler, pbr_metallicRoughnessSampler, fragmentInputs.pbr_vUV);\n perceptualRoughness = mrSample.g * perceptualRoughness;\n metallic = mrSample.b * metallic;\n #endif\n perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);\n metallic = clamp(metallic, 0.0, 1.0);\n // Roughness is authored as perceptual roughness; as is convention,\n // convert to material roughness by squaring the perceptual roughness [2].\n let alphaRoughness = perceptualRoughness * perceptualRoughness;\n\n let f0 = vec3<f32>(0.04);\n var diffuseColor = baseColor.rgb * (vec3<f32>(1.0) - f0);\n diffuseColor *= 1.0 - metallic;\n let specularColor = mix(f0, baseColor.rgb, metallic);\n\n // Compute reflectance.\n let reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\n\n // For typical incident reflectance range (between 4% to 100%) set the grazing\n // reflectance to 100% for typical fresnel effect.\n // For very low reflectance range on highly diffuse objects (below 4%),\n // incrementally reduce grazing reflectance to 0%.\n let reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);\n let specularEnvironmentR0 = specularColor;\n let specularEnvironmentR90 = vec3<f32>(1.0, 1.0, 1.0) * reflectance90;\n\n let n = getNormal(); // normal at surface point\n let v = normalize(pbrProjection.camera - fragmentInputs.pbr_vPosition); // Vector from surface point to camera\n\n let NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);\n let reflection = -normalize(reflect(v, n));\n\n var pbrInfo = PBRInfo(\n 0.0, // NdotL\n NdotV,\n 0.0, // NdotH\n 0.0, // LdotH\n 0.0, // VdotH\n perceptualRoughness,\n metallic,\n specularEnvironmentR0,\n specularEnvironmentR90,\n alphaRoughness,\n diffuseColor,\n specularColor,\n n,\n v\n );\n\n #ifdef USE_LIGHTS\n // Apply ambient light\n PBRInfo_setAmbientLight(&pbrInfo);\n color += calculateFinalColor(pbrInfo, lighting.ambientColor);\n\n // Apply directional light\n for (var i = 0; i < lighting.directionalLightCount; i++) {\n if (i < lighting.directionalLightCount) {\n PBRInfo_setDirectionalLight(&pbrInfo, lighting_getDirectionalLight(i).direction);\n color += calculateFinalColor(pbrInfo, lighting_getDirectionalLight(i).color);\n }\n }\n\n // Apply point light\n for (var i = 0; i < lighting.pointLightCount; i++) {\n if (i < lighting.pointLightCount) {\n PBRInfo_setPointLight(&pbrInfo, lighting_getPointLight(i));\n let attenuation = getPointLightAttenuation(\n lighting_getPointLight(i),\n distance(lighting_getPointLight(i).position, fragmentInputs.pbr_vPosition)\n );\n color += calculateFinalColor(pbrInfo, lighting_getPointLight(i).color / attenuation);\n }\n }\n #endif\n\n // Calculate lighting contribution from image based lighting source (IBL)\n #ifdef USE_IBL\n if (pbrMaterial.IBLenabled) {\n color += getIBLContribution(pbrInfo, n, reflection);\n }\n #endif\n\n // Apply optional PBR terms for additional (optional) shading\n #ifdef HAS_OCCLUSIONMAP\n if (pbrMaterial.occlusionMapEnabled) {\n let ao = textureSample(pbr_occlusionSampler, pbr_occlusionSampler, fragmentInputs.pbr_vUV).r;\n color = mix(color, color * ao, pbrMaterial.occlusionStrength);\n }\n #endif\n\n #ifdef HAS_EMISSIVEMAP\n if (pbrMaterial.emissiveMapEnabled) {\n let emissive = SRGBtoLINEAR(textureSample(pbr_emissiveSampler, pbr_emissiveSampler, fragmentInputs.pbr_vUV)).rgb * pbrMaterial.emissiveFactor;\n color += emissive;\n }\n #endif\n\n // This section uses mix to override final color for reference app visualization\n // of various parameters in the lighting equation.\n #ifdef PBR_DEBUG\n // TODO: Figure out how to debug multiple lights\n\n // color = mix(color, F, pbr_scaleFGDSpec.x);\n // color = mix(color, vec3(G), pbr_scaleFGDSpec.y);\n // color = mix(color, vec3(D), pbr_scaleFGDSpec.z);\n // color = mix(color, specContrib, pbr_scaleFGDSpec.w);\n\n // color = mix(color, diffuseContrib, pbr_scaleDiffBaseMR.x);\n color = mix(color, baseColor.rgb, pbrMaterial.scaleDiffBaseMR.y);\n color = mix(color, vec3<f32>(metallic), pbrMaterial.scaleDiffBaseMR.z);\n color = mix(color, vec3<f32>(perceptualRoughness), pbrMaterial.scaleDiffBaseMR.w);\n #endif\n }\n\n return vec4<f32>(pow(color, vec3<f32>(1.0 / 2.2)), baseColor.a);\n}\n";
141
+ readonly source: "struct PBRFragmentInputs {\n pbr_vPosition: vec3f,\n pbr_vUV: vec2f,\n pbr_vTBN: mat3x3f,\n pbr_vNormal: vec3f\n};\n\nvar<private> fragmentInputs: PBRFragmentInputs;\n\nfn pbr_setPositionNormalTangentUV(position: vec4f, normal: vec4f, tangent: vec4f, uv: vec2f)\n{\n var pos: vec4f = pbrProjection.modelMatrix * position;\n fragmentInputs.pbr_vPosition = pos.xyz / pos.w;\n fragmentInputs.pbr_vNormal = vec3f(0.0, 0.0, 1.0);\n fragmentInputs.pbr_vTBN = mat3x3f(\n vec3f(1.0, 0.0, 0.0),\n vec3f(0.0, 1.0, 0.0),\n vec3f(0.0, 0.0, 1.0)\n );\n fragmentInputs.pbr_vUV = vec2f(0.0, 0.0);\n\n#ifdef HAS_NORMALS\n let normalW: vec3f = normalize((pbrProjection.normalMatrix * vec4f(normal.xyz, 0.0)).xyz);\n fragmentInputs.pbr_vNormal = normalW;\n#ifdef HAS_TANGENTS\n let tangentW: vec3f = normalize((pbrProjection.modelMatrix * vec4f(tangent.xyz, 0.0)).xyz);\n let bitangentW: vec3f = cross(normalW, tangentW) * tangent.w;\n fragmentInputs.pbr_vTBN = mat3x3f(tangentW, bitangentW, normalW);\n#endif\n#endif\n\n#ifdef HAS_UV\n fragmentInputs.pbr_vUV = uv;\n#endif\n}\n\nstruct pbrMaterialUniforms {\n // Material is unlit\n unlit: u32,\n\n // Base color map\n baseColorMapEnabled: u32,\n baseColorFactor: vec4f,\n\n normalMapEnabled : u32,\n normalScale: f32, // #ifdef HAS_NORMALMAP\n\n emissiveMapEnabled: u32,\n emissiveFactor: vec3f, // #ifdef HAS_EMISSIVEMAP\n\n metallicRoughnessValues: vec2f,\n metallicRoughnessMapEnabled: u32,\n\n occlusionMapEnabled: i32,\n occlusionStrength: f32, // #ifdef HAS_OCCLUSIONMAP\n \n alphaCutoffEnabled: i32,\n alphaCutoff: f32, // #ifdef ALPHA_CUTOFF\n \n // IBL\n IBLenabled: i32,\n scaleIBLAmbient: vec2f, // #ifdef USE_IBL\n \n // debugging flags used for shader output of intermediate PBR variables\n // #ifdef PBR_DEBUG\n scaleDiffBaseMR: vec4f,\n scaleFGDSpec: vec4f,\n // #endif\n}\n\n@binding(2) @group(0) var<uniform> pbrMaterial : pbrMaterialUniforms;\n\n// Samplers\n#ifdef HAS_BASECOLORMAP\n@binding(3) @group(0) var pbr_baseColorSampler: texture_2d<f32>;\n@binding(4) @group(0) var pbr_baseColorSamplerSampler: sampler;\n#endif\n#ifdef HAS_NORMALMAP\n@binding(5) @group(0) var pbr_normalSampler: texture_2d<f32>;\n@binding(6) @group(0) var pbr_normalSamplerSampler: sampler;\n#endif\n#ifdef HAS_EMISSIVEMAP\n@binding(7) @group(0) var pbr_emissiveSampler: texture_2d<f32>;\n@binding(8) @group(0) var pbr_emissiveSamplerSampler: sampler;\n#endif\n#ifdef HAS_METALROUGHNESSMAP\n@binding(9) @group(0) var pbr_metallicRoughnessSampler: texture_2d<f32>;\n@binding(10) @group(0) var pbr_metallicRoughnessSamplerSampler: sampler;\n#endif\n#ifdef HAS_OCCLUSIONMAP\n@binding(11) @group(0) var pbr_occlusionSampler: texture_2d<f32>;\n@binding(12) @group(0) var pbr_occlusionSamplerSampler: sampler;\n#endif\n#ifdef USE_IBL\n@binding(13) @group(0) var pbr_diffuseEnvSampler: texture_cube<f32>;\n@binding(14) @group(0) var pbr_diffuseEnvSamplerSampler: sampler;\n@binding(15) @group(0) var pbr_specularEnvSampler: texture_cube<f32>;\n@binding(16) @group(0) var pbr_specularEnvSamplerSampler: sampler;\n@binding(17) @group(0) var pbr_BrdfLUT: texture_2d<f32>;\n@binding(18) @group(0) var pbr_BrdfLUTSampler: sampler;\n#endif\n\n// Encapsulate the various inputs used by the various functions in the shading equation\n// We store values in this struct to simplify the integration of alternative implementations\n// of the shading terms, outlined in the Readme.MD Appendix.\nstruct PBRInfo {\n NdotL: f32, // cos angle between normal and light direction\n NdotV: f32, // cos angle between normal and view direction\n NdotH: f32, // cos angle between normal and half vector\n LdotH: f32, // cos angle between light direction and half vector\n VdotH: f32, // cos angle between view direction and half vector\n perceptualRoughness: f32, // roughness value, as authored by the model creator (input to shader)\n metalness: f32, // metallic value at the surface\n reflectance0: vec3f, // full reflectance color (normal incidence angle)\n reflectance90: vec3f, // reflectance color at grazing angle\n alphaRoughness: f32, // roughness mapped to a more linear change in the roughness (proposed by [2])\n diffuseColor: vec3f, // color contribution from diffuse lighting\n specularColor: vec3f, // color contribution from specular lighting\n n: vec3f, // normal at surface point\n v: vec3f, // vector from surface point to camera\n};\n\nconst M_PI = 3.141592653589793;\nconst c_MinRoughness = 0.04;\n\nfn SRGBtoLINEAR(srgbIn: vec4f ) -> vec4f\n{\n var linOut: vec3f = srgbIn.xyz;\n#ifdef MANUAL_SRGB\n let bLess: vec3f = step(vec3f(0.04045), srgbIn.xyz);\n linOut = mix(\n srgbIn.xyz / vec3f(12.92),\n pow((srgbIn.xyz + vec3f(0.055)) / vec3f(1.055), vec3f(2.4)),\n bLess\n );\n#ifdef SRGB_FAST_APPROXIMATION\n linOut = pow(srgbIn.xyz, vec3f(2.2));\n#endif\n#endif\n return vec4f(linOut, srgbIn.w);\n}\n\n// Find the normal for this fragment, pulling either from a predefined normal map\n// or from the interpolated mesh normal and tangent attributes.\nfn getNormal() -> vec3f\n{\n // Retrieve the tangent space matrix\n let pos_dx: vec3f = dpdx(fragmentInputs.pbr_vPosition);\n let pos_dy: vec3f = dpdy(fragmentInputs.pbr_vPosition);\n let tex_dx: vec3f = dpdx(vec3f(fragmentInputs.pbr_vUV, 0.0));\n let tex_dy: vec3f = dpdy(vec3f(fragmentInputs.pbr_vUV, 0.0));\n var t: vec3f = (tex_dy.y * pos_dx - tex_dx.y * pos_dy) / (tex_dx.x * tex_dy.y - tex_dy.x * tex_dx.y);\n\n var ng: vec3f = cross(pos_dx, pos_dy);\n#ifdef HAS_NORMALS\n ng = normalize(fragmentInputs.pbr_vNormal);\n#endif\n t = normalize(t - ng * dot(ng, t));\n var b: vec3f = normalize(cross(ng, t));\n var tbn: mat3x3f = mat3x3f(t, b, ng);\n#ifdef HAS_TANGENTS\n tbn = fragmentInputs.pbr_vTBN;\n#endif\n\n // The tbn matrix is linearly interpolated, so we need to re-normalize\n var n: vec3f = normalize(tbn[2].xyz);\n#ifdef HAS_NORMALMAP\n n = textureSample(pbr_normalSampler, pbr_normalSamplerSampler, fragmentInputs.pbr_vUV).rgb;\n n = normalize(tbn * ((2.0 * n - 1.0) * vec3f(pbrMaterial.normalScale, pbrMaterial.normalScale, 1.0)));\n#endif\n\n return n;\n}\n\n// Calculation of the lighting contribution from an optional Image Based Light source.\n// Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].\n// See our README.md on Environment Maps [3] for additional discussion.\n#ifdef USE_IBL\nfn getIBLContribution(pbrInfo: PBRInfo, n: vec3f, reflection: vec3f) -> vec3f\n{\n let mipCount: f32 = 9.0; // resolution of 512x512\n let lod: f32 = pbrInfo.perceptualRoughness * mipCount;\n // retrieve a scale and bias to F0. See [1], Figure 3\n let brdf = SRGBtoLINEAR(\n textureSample(\n pbr_BrdfLUT,\n pbr_BrdfLUTSampler,\n vec2f(pbrInfo.NdotV, 1.0 - pbrInfo.perceptualRoughness)\n )\n ).rgb;\n let diffuseLight =\n SRGBtoLINEAR(textureSample(pbr_diffuseEnvSampler, pbr_diffuseEnvSamplerSampler, n)).rgb;\n let specularLightDefault =\n SRGBtoLINEAR(textureSample(pbr_specularEnvSampler, pbr_specularEnvSamplerSampler, reflection)).rgb;\n var specularLight = specularLightDefault;\n#ifdef USE_TEX_LOD\n specularLight = SRGBtoLINEAR(\n textureSampleLevel(\n pbr_specularEnvSampler,\n pbr_specularEnvSamplerSampler,\n reflection,\n lod\n )\n ).rgb;\n#endif\n\n let diffuse = diffuseLight * pbrInfo.diffuseColor * pbrMaterial.scaleIBLAmbient.x;\n let specular =\n specularLight * (pbrInfo.specularColor * brdf.x + brdf.y) * pbrMaterial.scaleIBLAmbient.y;\n\n return diffuse + specular;\n}\n#endif\n\n// Basic Lambertian diffuse\n// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog\n// See also [1], Equation 1\nfn diffuse(pbrInfo: PBRInfo) -> vec3<f32> {\n return pbrInfo.diffuseColor / M_PI;\n}\n\n// The following equation models the Fresnel reflectance term of the spec equation (aka F())\n// Implementation of fresnel from [4], Equation 15\nfn specularReflection(pbrInfo: PBRInfo) -> vec3<f32> {\n return pbrInfo.reflectance0 +\n (pbrInfo.reflectance90 - pbrInfo.reflectance0) *\n pow(clamp(1.0 - pbrInfo.VdotH, 0.0, 1.0), 5.0);\n}\n\n// This calculates the specular geometric attenuation (aka G()),\n// where rougher material will reflect less light back to the viewer.\n// This implementation is based on [1] Equation 4, and we adopt their modifications to\n// alphaRoughness as input as originally proposed in [2].\nfn geometricOcclusion(pbrInfo: PBRInfo) -> f32 {\n let NdotL: f32 = pbrInfo.NdotL;\n let NdotV: f32 = pbrInfo.NdotV;\n let r: f32 = pbrInfo.alphaRoughness;\n\n let attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));\n let attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));\n return attenuationL * attenuationV;\n}\n\n// The following equation(s) model the distribution of microfacet normals across\n// the area being drawn (aka D())\n// Implementation from \"Average Irregularity Representation of a Roughened Surface\n// for Ray Reflection\" by T. S. Trowbridge, and K. P. Reitz\n// Follows the distribution function recommended in the SIGGRAPH 2013 course notes\n// from EPIC Games [1], Equation 3.\nfn microfacetDistribution(pbrInfo: PBRInfo) -> f32 {\n let roughnessSq = pbrInfo.alphaRoughness * pbrInfo.alphaRoughness;\n let f = (pbrInfo.NdotH * roughnessSq - pbrInfo.NdotH) * pbrInfo.NdotH + 1.0;\n return roughnessSq / (M_PI * f * f);\n}\n\nfn PBRInfo_setAmbientLight(pbrInfo: ptr<function, PBRInfo>) {\n (*pbrInfo).NdotL = 1.0;\n (*pbrInfo).NdotH = 0.0;\n (*pbrInfo).LdotH = 0.0;\n (*pbrInfo).VdotH = 1.0;\n}\n\nfn PBRInfo_setDirectionalLight(pbrInfo: ptr<function, PBRInfo>, lightDirection: vec3<f32>) {\n let n = (*pbrInfo).n;\n let v = (*pbrInfo).v;\n let l = normalize(lightDirection); // Vector from surface point to light\n let h = normalize(l + v); // Half vector between both l and v\n\n (*pbrInfo).NdotL = clamp(dot(n, l), 0.001, 1.0);\n (*pbrInfo).NdotH = clamp(dot(n, h), 0.0, 1.0);\n (*pbrInfo).LdotH = clamp(dot(l, h), 0.0, 1.0);\n (*pbrInfo).VdotH = clamp(dot(v, h), 0.0, 1.0);\n}\n\nfn PBRInfo_setPointLight(pbrInfo: ptr<function, PBRInfo>, pointLight: PointLight) {\n let light_direction = normalize(pointLight.position - fragmentInputs.pbr_vPosition);\n PBRInfo_setDirectionalLight(pbrInfo, light_direction);\n}\n\nfn calculateFinalColor(pbrInfo: PBRInfo, lightColor: vec3<f32>) -> vec3<f32> {\n // Calculate the shading terms for the microfacet specular shading model\n let F = specularReflection(pbrInfo);\n let G = geometricOcclusion(pbrInfo);\n let D = microfacetDistribution(pbrInfo);\n\n // Calculation of analytical lighting contribution\n let diffuseContrib = (1.0 - F) * diffuse(pbrInfo);\n let specContrib = F * G * D / (4.0 * pbrInfo.NdotL * pbrInfo.NdotV);\n // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)\n return pbrInfo.NdotL * lightColor * (diffuseContrib + specContrib);\n}\n\nfn pbr_filterColor(colorUnused: vec4<f32>) -> vec4<f32> {\n // The albedo may be defined from a base texture or a flat color\n var baseColor: vec4<f32> = pbrMaterial.baseColorFactor;\n #ifdef HAS_BASECOLORMAP\n baseColor = SRGBtoLINEAR(\n textureSample(pbr_baseColorSampler, pbr_baseColorSamplerSampler, fragmentInputs.pbr_vUV)\n ) * pbrMaterial.baseColorFactor;\n #endif\n\n #ifdef ALPHA_CUTOFF\n if (baseColor.a < pbrMaterial.alphaCutoff) {\n discard;\n }\n #endif\n\n var color = vec3<f32>(0.0, 0.0, 0.0);\n\n if (pbrMaterial.unlit != 0u) {\n color = baseColor.rgb;\n } else {\n // Metallic and Roughness material properties are packed together\n // In glTF, these factors can be specified by fixed scalar values\n // or from a metallic-roughness map\n var perceptualRoughness = pbrMaterial.metallicRoughnessValues.y;\n var metallic = pbrMaterial.metallicRoughnessValues.x;\n #ifdef HAS_METALROUGHNESSMAP\n // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.\n // This layout intentionally reserves the 'r' channel for (optional) occlusion map data\n let mrSample = textureSample(\n pbr_metallicRoughnessSampler,\n pbr_metallicRoughnessSamplerSampler,\n fragmentInputs.pbr_vUV\n );\n perceptualRoughness = mrSample.g * perceptualRoughness;\n metallic = mrSample.b * metallic;\n #endif\n perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);\n metallic = clamp(metallic, 0.0, 1.0);\n // Roughness is authored as perceptual roughness; as is convention,\n // convert to material roughness by squaring the perceptual roughness [2].\n let alphaRoughness = perceptualRoughness * perceptualRoughness;\n\n let f0 = vec3<f32>(0.04);\n var diffuseColor = baseColor.rgb * (vec3<f32>(1.0) - f0);\n diffuseColor *= 1.0 - metallic;\n let specularColor = mix(f0, baseColor.rgb, metallic);\n\n // Compute reflectance.\n let reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\n\n // For typical incident reflectance range (between 4% to 100%) set the grazing\n // reflectance to 100% for typical fresnel effect.\n // For very low reflectance range on highly diffuse objects (below 4%),\n // incrementally reduce grazing reflectance to 0%.\n let reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);\n let specularEnvironmentR0 = specularColor;\n let specularEnvironmentR90 = vec3<f32>(1.0, 1.0, 1.0) * reflectance90;\n\n let n = getNormal(); // normal at surface point\n let v = normalize(pbrProjection.camera - fragmentInputs.pbr_vPosition); // Vector from surface point to camera\n\n let NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);\n let reflection = -normalize(reflect(v, n));\n\n var pbrInfo = PBRInfo(\n 0.0, // NdotL\n NdotV,\n 0.0, // NdotH\n 0.0, // LdotH\n 0.0, // VdotH\n perceptualRoughness,\n metallic,\n specularEnvironmentR0,\n specularEnvironmentR90,\n alphaRoughness,\n diffuseColor,\n specularColor,\n n,\n v\n );\n\n #ifdef USE_LIGHTS\n // Apply ambient light\n PBRInfo_setAmbientLight(&pbrInfo);\n color += calculateFinalColor(pbrInfo, lighting.ambientColor);\n\n // Apply directional light\n for (var i = 0; i < lighting.directionalLightCount; i++) {\n if (i < lighting.directionalLightCount) {\n PBRInfo_setDirectionalLight(&pbrInfo, lighting_getDirectionalLight(i).direction);\n color += calculateFinalColor(pbrInfo, lighting_getDirectionalLight(i).color);\n }\n }\n\n // Apply point light\n for (var i = 0; i < lighting.pointLightCount; i++) {\n if (i < lighting.pointLightCount) {\n PBRInfo_setPointLight(&pbrInfo, lighting_getPointLight(i));\n let attenuation = getPointLightAttenuation(\n lighting_getPointLight(i),\n distance(lighting_getPointLight(i).position, fragmentInputs.pbr_vPosition)\n );\n color += calculateFinalColor(pbrInfo, lighting_getPointLight(i).color / attenuation);\n }\n }\n #endif\n\n // Calculate lighting contribution from image based lighting source (IBL)\n #ifdef USE_IBL\n if (pbrMaterial.IBLenabled != 0) {\n color += getIBLContribution(pbrInfo, n, reflection);\n }\n #endif\n\n // Apply optional PBR terms for additional (optional) shading\n #ifdef HAS_OCCLUSIONMAP\n if (pbrMaterial.occlusionMapEnabled != 0) {\n let ao =\n textureSample(pbr_occlusionSampler, pbr_occlusionSamplerSampler, fragmentInputs.pbr_vUV).r;\n color = mix(color, color * ao, pbrMaterial.occlusionStrength);\n }\n #endif\n\n #ifdef HAS_EMISSIVEMAP\n if (pbrMaterial.emissiveMapEnabled != 0u) {\n let emissive = SRGBtoLINEAR(\n textureSample(pbr_emissiveSampler, pbr_emissiveSamplerSampler, fragmentInputs.pbr_vUV)\n ).rgb * pbrMaterial.emissiveFactor;\n color += emissive;\n }\n #endif\n\n // This section uses mix to override final color for reference app visualization\n // of various parameters in the lighting equation.\n #ifdef PBR_DEBUG\n // TODO: Figure out how to debug multiple lights\n\n // color = mix(color, F, pbr_scaleFGDSpec.x);\n // color = mix(color, vec3(G), pbr_scaleFGDSpec.y);\n // color = mix(color, vec3(D), pbr_scaleFGDSpec.z);\n // color = mix(color, specContrib, pbr_scaleFGDSpec.w);\n\n // color = mix(color, diffuseContrib, pbr_scaleDiffBaseMR.x);\n color = mix(color, baseColor.rgb, pbrMaterial.scaleDiffBaseMR.y);\n color = mix(color, vec3<f32>(metallic), pbrMaterial.scaleDiffBaseMR.z);\n color = mix(color, vec3<f32>(perceptualRoughness), pbrMaterial.scaleDiffBaseMR.w);\n #endif\n }\n\n return vec4<f32>(pow(color, vec3<f32>(1.0 / 2.2)), baseColor.a);\n}\n";
126
142
  readonly vs: "out vec3 pbr_vPosition;\nout vec2 pbr_vUV;\n\n#ifdef HAS_NORMALS\n# ifdef HAS_TANGENTS\nout mat3 pbr_vTBN;\n# else\nout vec3 pbr_vNormal;\n# endif\n#endif\n\nvoid pbr_setPositionNormalTangentUV(vec4 position, vec4 normal, vec4 tangent, vec2 uv)\n{\n vec4 pos = pbrProjection.modelMatrix * position;\n pbr_vPosition = vec3(pos.xyz) / pos.w;\n\n#ifdef HAS_NORMALS\n#ifdef HAS_TANGENTS\n vec3 normalW = normalize(vec3(pbrProjection.normalMatrix * vec4(normal.xyz, 0.0)));\n vec3 tangentW = normalize(vec3(pbrProjection.modelMatrix * vec4(tangent.xyz, 0.0)));\n vec3 bitangentW = cross(normalW, tangentW) * tangent.w;\n pbr_vTBN = mat3(tangentW, bitangentW, normalW);\n#else // HAS_TANGENTS != 1\n pbr_vNormal = normalize(vec3(pbrProjection.modelMatrix * vec4(normal.xyz, 0.0)));\n#endif\n#endif\n\n#ifdef HAS_UV\n pbr_vUV = uv;\n#else\n pbr_vUV = vec2(0.,0.);\n#endif\n}\n";
127
143
  readonly fs: "precision highp float;\n\nuniform pbrMaterialUniforms {\n // Material is unlit\n bool unlit;\n\n // Base color map\n bool baseColorMapEnabled;\n vec4 baseColorFactor;\n\n bool normalMapEnabled; \n float normalScale; // #ifdef HAS_NORMALMAP\n\n bool emissiveMapEnabled;\n vec3 emissiveFactor; // #ifdef HAS_EMISSIVEMAP\n\n vec2 metallicRoughnessValues;\n bool metallicRoughnessMapEnabled;\n\n bool occlusionMapEnabled;\n float occlusionStrength; // #ifdef HAS_OCCLUSIONMAP\n \n bool alphaCutoffEnabled;\n float alphaCutoff; // #ifdef ALPHA_CUTOFF\n\n vec3 specularColorFactor;\n float specularIntensityFactor;\n bool specularColorMapEnabled;\n bool specularIntensityMapEnabled;\n\n float ior;\n\n float transmissionFactor;\n bool transmissionMapEnabled;\n\n float thicknessFactor;\n float attenuationDistance;\n vec3 attenuationColor;\n\n float clearcoatFactor;\n float clearcoatRoughnessFactor;\n bool clearcoatMapEnabled;\n\n vec3 sheenColorFactor;\n float sheenRoughnessFactor;\n bool sheenColorMapEnabled;\n\n float iridescenceFactor;\n float iridescenceIor;\n vec2 iridescenceThicknessRange;\n bool iridescenceMapEnabled;\n\n float anisotropyStrength;\n float anisotropyRotation;\n vec2 anisotropyDirection;\n bool anisotropyMapEnabled;\n\n float emissiveStrength;\n \n // IBL\n bool IBLenabled;\n vec2 scaleIBLAmbient; // #ifdef USE_IBL\n \n // debugging flags used for shader output of intermediate PBR variables\n // #ifdef PBR_DEBUG\n vec4 scaleDiffBaseMR;\n vec4 scaleFGDSpec;\n // #endif\n} pbrMaterial;\n\n// Samplers\n#ifdef HAS_BASECOLORMAP\nuniform sampler2D pbr_baseColorSampler;\n#endif\n#ifdef HAS_NORMALMAP\nuniform sampler2D pbr_normalSampler;\n#endif\n#ifdef HAS_EMISSIVEMAP\nuniform sampler2D pbr_emissiveSampler;\n#endif\n#ifdef HAS_METALROUGHNESSMAP\nuniform sampler2D pbr_metallicRoughnessSampler;\n#endif\n#ifdef HAS_OCCLUSIONMAP\nuniform sampler2D pbr_occlusionSampler;\n#endif\n#ifdef HAS_SPECULARCOLORMAP\nuniform sampler2D pbr_specularColorSampler;\n#endif\n#ifdef HAS_SPECULARINTENSITYMAP\nuniform sampler2D pbr_specularIntensitySampler;\n#endif\n#ifdef HAS_TRANSMISSIONMAP\nuniform sampler2D pbr_transmissionSampler;\n#endif\n#ifdef HAS_CLEARCOATMAP\nuniform sampler2D pbr_clearcoatSampler;\nuniform sampler2D pbr_clearcoatRoughnessSampler;\n#endif\n#ifdef HAS_SHEENCOLORMAP\nuniform sampler2D pbr_sheenColorSampler;\nuniform sampler2D pbr_sheenRoughnessSampler;\n#endif\n#ifdef HAS_IRIDESCENCEMAP\nuniform sampler2D pbr_iridescenceSampler;\n#endif\n#ifdef HAS_ANISOTROPYMAP\nuniform sampler2D pbr_anisotropySampler;\n#endif\n#ifdef USE_IBL\nuniform samplerCube pbr_diffuseEnvSampler;\nuniform samplerCube pbr_specularEnvSampler;\nuniform sampler2D pbr_brdfLUT;\n#endif\n\n// Inputs from vertex shader\n\nin vec3 pbr_vPosition;\nin vec2 pbr_vUV;\n\n#ifdef HAS_NORMALS\n#ifdef HAS_TANGENTS\nin mat3 pbr_vTBN;\n#else\nin vec3 pbr_vNormal;\n#endif\n#endif\n\n// Encapsulate the various inputs used by the various functions in the shading equation\n// We store values in this struct to simplify the integration of alternative implementations\n// of the shading terms, outlined in the Readme.MD Appendix.\nstruct PBRInfo {\n float NdotL; // cos angle between normal and light direction\n float NdotV; // cos angle between normal and view direction\n float NdotH; // cos angle between normal and half vector\n float LdotH; // cos angle between light direction and half vector\n float VdotH; // cos angle between view direction and half vector\n float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)\n float metalness; // metallic value at the surface\n vec3 reflectance0; // full reflectance color (normal incidence angle)\n vec3 reflectance90; // reflectance color at grazing angle\n float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])\n vec3 diffuseColor; // color contribution from diffuse lighting\n vec3 specularColor; // color contribution from specular lighting\n vec3 n; // normal at surface point\n vec3 v; // vector from surface point to camera\n};\n\nconst float M_PI = 3.141592653589793;\nconst float c_MinRoughness = 0.04;\n\nvec4 SRGBtoLINEAR(vec4 srgbIn)\n{\n#ifdef MANUAL_SRGB\n#ifdef SRGB_FAST_APPROXIMATION\n vec3 linOut = pow(srgbIn.xyz,vec3(2.2));\n#else // SRGB_FAST_APPROXIMATION\n vec3 bLess = step(vec3(0.04045),srgbIn.xyz);\n vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );\n#endif //SRGB_FAST_APPROXIMATION\n return vec4(linOut,srgbIn.w);;\n#else //MANUAL_SRGB\n return srgbIn;\n#endif //MANUAL_SRGB\n}\n\n// Find the normal for this fragment, pulling either from a predefined normal map\n// or from the interpolated mesh normal and tangent attributes.\nvec3 getNormal()\n{\n // Retrieve the tangent space matrix\n#ifndef HAS_TANGENTS\n vec3 pos_dx = dFdx(pbr_vPosition);\n vec3 pos_dy = dFdy(pbr_vPosition);\n vec3 tex_dx = dFdx(vec3(pbr_vUV, 0.0));\n vec3 tex_dy = dFdy(vec3(pbr_vUV, 0.0));\n vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);\n\n#ifdef HAS_NORMALS\n vec3 ng = normalize(pbr_vNormal);\n#else\n vec3 ng = cross(pos_dx, pos_dy);\n#endif\n\n t = normalize(t - ng * dot(ng, t));\n vec3 b = normalize(cross(ng, t));\n mat3 tbn = mat3(t, b, ng);\n#else // HAS_TANGENTS\n mat3 tbn = pbr_vTBN;\n#endif\n\n#ifdef HAS_NORMALMAP\n vec3 n = texture(pbr_normalSampler, pbr_vUV).rgb;\n n = normalize(tbn * ((2.0 * n - 1.0) * vec3(pbrMaterial.normalScale, pbrMaterial.normalScale, 1.0)));\n#else\n // The tbn matrix is linearly interpolated, so we need to re-normalize\n vec3 n = normalize(tbn[2].xyz);\n#endif\n\n return n;\n}\n\n// Calculation of the lighting contribution from an optional Image Based Light source.\n// Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].\n// See our README.md on Environment Maps [3] for additional discussion.\n#ifdef USE_IBL\nvec3 getIBLContribution(PBRInfo pbrInfo, vec3 n, vec3 reflection)\n{\n float mipCount = 9.0; // resolution of 512x512\n float lod = (pbrInfo.perceptualRoughness * mipCount);\n // retrieve a scale and bias to F0. See [1], Figure 3\n vec3 brdf = SRGBtoLINEAR(texture(pbr_brdfLUT,\n vec2(pbrInfo.NdotV, 1.0 - pbrInfo.perceptualRoughness))).rgb;\n vec3 diffuseLight = SRGBtoLINEAR(texture(pbr_diffuseEnvSampler, n)).rgb;\n\n#ifdef USE_TEX_LOD\n vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection, lod)).rgb;\n#else\n vec3 specularLight = SRGBtoLINEAR(texture(pbr_specularEnvSampler, reflection)).rgb;\n#endif\n\n vec3 diffuse = diffuseLight * pbrInfo.diffuseColor;\n vec3 specular = specularLight * (pbrInfo.specularColor * brdf.x + brdf.y);\n\n // For presentation, this allows us to disable IBL terms\n diffuse *= pbrMaterial.scaleIBLAmbient.x;\n specular *= pbrMaterial.scaleIBLAmbient.y;\n\n return diffuse + specular;\n}\n#endif\n\n// Basic Lambertian diffuse\n// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog\n// See also [1], Equation 1\nvec3 diffuse(PBRInfo pbrInfo)\n{\n return pbrInfo.diffuseColor / M_PI;\n}\n\n// The following equation models the Fresnel reflectance term of the spec equation (aka F())\n// Implementation of fresnel from [4], Equation 15\nvec3 specularReflection(PBRInfo pbrInfo)\n{\n return pbrInfo.reflectance0 +\n (pbrInfo.reflectance90 - pbrInfo.reflectance0) *\n pow(clamp(1.0 - pbrInfo.VdotH, 0.0, 1.0), 5.0);\n}\n\n// This calculates the specular geometric attenuation (aka G()),\n// where rougher material will reflect less light back to the viewer.\n// This implementation is based on [1] Equation 4, and we adopt their modifications to\n// alphaRoughness as input as originally proposed in [2].\nfloat geometricOcclusion(PBRInfo pbrInfo)\n{\n float NdotL = pbrInfo.NdotL;\n float NdotV = pbrInfo.NdotV;\n float r = pbrInfo.alphaRoughness;\n\n float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));\n float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));\n return attenuationL * attenuationV;\n}\n\n// The following equation(s) model the distribution of microfacet normals across\n// the area being drawn (aka D())\n// Implementation from \"Average Irregularity Representation of a Roughened Surface\n// for Ray Reflection\" by T. S. Trowbridge, and K. P. Reitz\n// Follows the distribution function recommended in the SIGGRAPH 2013 course notes\n// from EPIC Games [1], Equation 3.\nfloat microfacetDistribution(PBRInfo pbrInfo)\n{\n float roughnessSq = pbrInfo.alphaRoughness * pbrInfo.alphaRoughness;\n float f = (pbrInfo.NdotH * roughnessSq - pbrInfo.NdotH) * pbrInfo.NdotH + 1.0;\n return roughnessSq / (M_PI * f * f);\n}\n\nvoid PBRInfo_setAmbientLight(inout PBRInfo pbrInfo) {\n pbrInfo.NdotL = 1.0;\n pbrInfo.NdotH = 0.0;\n pbrInfo.LdotH = 0.0;\n pbrInfo.VdotH = 1.0;\n}\n\nvoid PBRInfo_setDirectionalLight(inout PBRInfo pbrInfo, vec3 lightDirection) {\n vec3 n = pbrInfo.n;\n vec3 v = pbrInfo.v;\n vec3 l = normalize(lightDirection); // Vector from surface point to light\n vec3 h = normalize(l+v); // Half vector between both l and v\n\n pbrInfo.NdotL = clamp(dot(n, l), 0.001, 1.0);\n pbrInfo.NdotH = clamp(dot(n, h), 0.0, 1.0);\n pbrInfo.LdotH = clamp(dot(l, h), 0.0, 1.0);\n pbrInfo.VdotH = clamp(dot(v, h), 0.0, 1.0);\n}\n\nvoid PBRInfo_setPointLight(inout PBRInfo pbrInfo, PointLight pointLight) {\n vec3 light_direction = normalize(pointLight.position - pbr_vPosition);\n PBRInfo_setDirectionalLight(pbrInfo, light_direction);\n}\n\nvec3 calculateFinalColor(PBRInfo pbrInfo, vec3 lightColor) {\n // Calculate the shading terms for the microfacet specular shading model\n vec3 F = specularReflection(pbrInfo);\n float G = geometricOcclusion(pbrInfo);\n float D = microfacetDistribution(pbrInfo);\n\n // Calculation of analytical lighting contribution\n vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInfo);\n vec3 specContrib = F * G * D / (4.0 * pbrInfo.NdotL * pbrInfo.NdotV);\n // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)\n return pbrInfo.NdotL * lightColor * (diffuseContrib + specContrib);\n}\n\nvec4 pbr_filterColor(vec4 colorUnused)\n{\n // The albedo may be defined from a base texture or a flat color\n#ifdef HAS_BASECOLORMAP\n vec4 baseColor = SRGBtoLINEAR(texture(pbr_baseColorSampler, pbr_vUV)) * pbrMaterial.baseColorFactor;\n#else\n vec4 baseColor = pbrMaterial.baseColorFactor;\n#endif\n\n#ifdef ALPHA_CUTOFF\n if (baseColor.a < pbrMaterial.alphaCutoff) {\n discard;\n }\n#endif\n\n vec3 color = vec3(0, 0, 0);\n\n if(pbrMaterial.unlit){\n color.rgb = baseColor.rgb;\n }\n else{\n // Metallic and Roughness material properties are packed together\n // In glTF, these factors can be specified by fixed scalar values\n // or from a metallic-roughness map\n float perceptualRoughness = pbrMaterial.metallicRoughnessValues.y;\n float metallic = pbrMaterial.metallicRoughnessValues.x;\n#ifdef HAS_METALROUGHNESSMAP\n // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.\n // This layout intentionally reserves the 'r' channel for (optional) occlusion map data\n vec4 mrSample = texture(pbr_metallicRoughnessSampler, pbr_vUV);\n perceptualRoughness = mrSample.g * perceptualRoughness;\n metallic = mrSample.b * metallic;\n#endif\n perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);\n metallic = clamp(metallic, 0.0, 1.0);\n // Roughness is authored as perceptual roughness; as is convention,\n // convert to material roughness by squaring the perceptual roughness [2].\n float alphaRoughness = perceptualRoughness * perceptualRoughness;\n\n vec3 f0 = vec3(0.04);\n vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);\n diffuseColor *= 1.0 - metallic;\n vec3 specularColor = mix(f0, baseColor.rgb, metallic);\n\n // Compute reflectance.\n float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\n\n // For typical incident reflectance range (between 4% to 100%) set the grazing\n // reflectance to 100% for typical fresnel effect.\n // For very low reflectance range on highly diffuse objects (below 4%),\n // incrementally reduce grazing reflecance to 0%.\n float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);\n vec3 specularEnvironmentR0 = specularColor.rgb;\n vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;\n\n vec3 n = getNormal(); // normal at surface point\n vec3 v = normalize(pbrProjection.camera - pbr_vPosition); // Vector from surface point to camera\n\n float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);\n vec3 reflection = -normalize(reflect(v, n));\n\n PBRInfo pbrInfo = PBRInfo(\n 0.0, // NdotL\n NdotV,\n 0.0, // NdotH\n 0.0, // LdotH\n 0.0, // VdotH\n perceptualRoughness,\n metallic,\n specularEnvironmentR0,\n specularEnvironmentR90,\n alphaRoughness,\n diffuseColor,\n specularColor,\n n,\n v\n );\n\n\n#ifdef USE_LIGHTS\n // Apply ambient light\n PBRInfo_setAmbientLight(pbrInfo);\n color += calculateFinalColor(pbrInfo, lighting.ambientColor);\n\n // Apply directional light\n for(int i = 0; i < lighting.directionalLightCount; i++) {\n if (i < lighting.directionalLightCount) {\n PBRInfo_setDirectionalLight(pbrInfo, lighting_getDirectionalLight(i).direction);\n color += calculateFinalColor(pbrInfo, lighting_getDirectionalLight(i).color);\n }\n }\n\n // Apply point light\n for(int i = 0; i < lighting.pointLightCount; i++) {\n if (i < lighting.pointLightCount) {\n PBRInfo_setPointLight(pbrInfo, lighting_getPointLight(i));\n float attenuation = getPointLightAttenuation(lighting_getPointLight(i), distance(lighting_getPointLight(i).position, pbr_vPosition));\n color += calculateFinalColor(pbrInfo, lighting_getPointLight(i).color / attenuation);\n }\n }\n#endif\n\n // Calculate lighting contribution from image based lighting source (IBL)\n#ifdef USE_IBL\n if (pbrMaterial.IBLenabled) {\n color += getIBLContribution(pbrInfo, n, reflection);\n }\n#endif\n\n // Apply optional PBR terms for additional (optional) shading\n#ifdef HAS_OCCLUSIONMAP\n if (pbrMaterial.occlusionMapEnabled) {\n float ao = texture(pbr_occlusionSampler, pbr_vUV).r;\n color = mix(color, color * ao, pbrMaterial.occlusionStrength);\n }\n#endif\n\n#ifdef HAS_EMISSIVEMAP\n if (pbrMaterial.emissiveMapEnabled) {\n vec3 emissive = SRGBtoLINEAR(texture(pbr_emissiveSampler, pbr_vUV)).rgb * pbrMaterial.emissiveFactor;\n color += emissive;\n }\n#endif\n\n // This section uses mix to override final color for reference app visualization\n // of various parameters in the lighting equation.\n#ifdef PBR_DEBUG\n // TODO: Figure out how to debug multiple lights\n\n // color = mix(color, F, pbr_scaleFGDSpec.x);\n // color = mix(color, vec3(G), pbr_scaleFGDSpec.y);\n // color = mix(color, vec3(D), pbr_scaleFGDSpec.z);\n // color = mix(color, specContrib, pbr_scaleFGDSpec.w);\n\n // color = mix(color, diffuseContrib, pbr_scaleDiffBaseMR.x);\n color = mix(color, baseColor.rgb, pbrMaterial.scaleDiffBaseMR.y);\n color = mix(color, vec3(metallic), pbrMaterial.scaleDiffBaseMR.z);\n color = mix(color, vec3(perceptualRoughness), pbrMaterial.scaleDiffBaseMR.w);\n#endif\n\n }\n\n return vec4(pow(color,vec3(1.0/2.2)), baseColor.a);\n}\n";
128
144
  readonly defines: {
@@ -1 +1 @@
1
- {"version":3,"file":"pbr-material.d.ts","sourceRoot":"","sources":["../../../../src/modules/lighting/pbr-material/pb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,EAAE,YAAY,CAAC,kBAAkB,CAY1D,CAAC"}
1
+ {"version":3,"file":"pbr-projection.d.ts","sourceRoot":"","sources":["../../../../src/modules/lighting/pbr-material/pbr-projection.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAC,MAAM,eAAe,CAAC;AAE/D,OAAO,EAAC,YAAY,EAAC,oDAAiD;AAsBtE,MAAM,MAAM,kBAAkB,GAAG;IAC/B,yBAAyB,EAAE,aAAa,CAAC;IACzC,WAAW,EAAE,aAAa,CAAC;IAC3B,YAAY,EAAE,aAAa,CAAC;IAC5B,MAAM,EAAE,YAAY,CAAC;CACtB,CAAC;AAEF,eAAO,MAAM,aAAa,EAAE,YAAY,CAAC,kBAAkB,CAa1D,CAAC"}
@@ -9,8 +9,19 @@ uniform pbrProjectionUniforms {
9
9
  vec3 camera;
10
10
  } pbrProjection;
11
11
  `;
12
+ const wgslUniformBlock = /* wgsl */ `\
13
+ struct pbrProjectionUniforms {
14
+ modelViewProjectionMatrix: mat4x4<f32>,
15
+ modelMatrix: mat4x4<f32>,
16
+ normalMatrix: mat4x4<f32>,
17
+ camera: vec3<f32>
18
+ };
19
+
20
+ @binding(0) @group(0) var<uniform> pbrProjection: pbrProjectionUniforms;
21
+ `;
12
22
  export const pbrProjection = {
13
23
  name: 'pbrProjection',
24
+ source: wgslUniformBlock,
14
25
  vs: uniformBlock,
15
26
  fs: uniformBlock,
16
27
  // TODO why is this needed?
@@ -19,7 +30,7 @@ export const pbrProjection = {
19
30
  modelViewProjectionMatrix: 'mat4x4<f32>',
20
31
  modelMatrix: 'mat4x4<f32>',
21
32
  normalMatrix: 'mat4x4<f32>',
22
- camera: 'vec3<i32>'
33
+ camera: 'vec3<f32>'
23
34
  }
24
35
  };
25
36
  //# sourceMappingURL=pbr-projection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"pbr-projection.js","sourceRoot":"","sources":["../../../../src/modules/lighting/pbr-material/pbr-projection.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,+BAA+B;AAC/B,oCAAoC;AAMpC,MAAM,YAAY,GAAG,UAAU,CAAC;;;;;;;CAO/B,CAAC;AASF,MAAM,CAAC,MAAM,aAAa,GAAqC;IAC7D,IAAI,EAAE,eAAe;IACrB,EAAE,EAAE,YAAY;IAChB,EAAE,EAAE,YAAY;IAChB,2BAA2B;IAC3B,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK;IAC3B,YAAY,EAAE;QACZ,yBAAyB,EAAE,aAAa;QACxC,WAAW,EAAE,aAAa;QAC1B,YAAY,EAAE,aAAa;QAC3B,MAAM,EAAE,WAAW;KACpB;CACF,CAAC"}
1
+ {"version":3,"file":"pbr-projection.js","sourceRoot":"","sources":["../../../../src/modules/lighting/pbr-material/pbr-projection.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,+BAA+B;AAC/B,oCAAoC;AAMpC,MAAM,YAAY,GAAG,UAAU,CAAC;;;;;;;CAO/B,CAAC;AAEF,MAAM,gBAAgB,GAAG,UAAU,CAAC;;;;;;;;;CASnC,CAAC;AASF,MAAM,CAAC,MAAM,aAAa,GAAqC;IAC7D,IAAI,EAAE,eAAe;IACrB,MAAM,EAAE,gBAAgB;IACxB,EAAE,EAAE,YAAY;IAChB,EAAE,EAAE,YAAY;IAChB,2BAA2B;IAC3B,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK;IAC3B,YAAY,EAAE;QACZ,yBAAyB,EAAE,aAAa;QACxC,WAAW,EAAE,aAAa;QAC1B,YAAY,EAAE,aAAa;QAC3B,MAAM,EAAE,WAAW;KACpB;CACF,CAAC"}
@@ -1,41 +1,2 @@
1
- export declare const PHONG_WGSL = "struct phongMaterialUniforms {\n ambient: f32,\n diffuse: f32,\n shininess: f32,\n specularColor: vec3<f32>,\n};\n\n@binding(2) @group(0) var<uniform> phongMaterial : phongMaterialUniforms;\n\nfn lighting_getLightColor(surfaceColor: vec3<f32>, light_direction: vec3<f32>, view_direction: vec3<f32>, normal_worldspace: vec3<f32>, color: vec3<f32>) -> vec3<f32> {\n let halfway_direction: vec3<f32> = normalize(light_direction + view_direction);\n var lambertian: f32 = dot(light_direction, normal_worldspace);\n var specular: f32 = 0.0;\n if (lambertian > 0.0) {\n let specular_angle = max(dot(normal_worldspace, halfway_direction), 0.0);\n specular = pow(specular_angle, phongMaterial.shininess);\n }\n lambertian = max(lambertian, 0.0);\n return (lambertian * phongMaterial.diffuse * surfaceColor + specular * phongMaterial.specularColor) * color;\n}\n\nfn lighting_getLightColor2(surfaceColor: vec3<f32>, cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32> {\n var lightColor: vec3<f32> = surfaceColor;\n\n if (lighting.enabled == 0) {\n return lightColor;\n }\n\n let view_direction: vec3<f32> = normalize(cameraPosition - position_worldspace);\n lightColor = phongMaterial.ambient * surfaceColor * lighting.ambientColor;\n\n if (lighting.lightType == 0) {\n let pointLight: PointLight = lighting_getPointLight(0);\n let light_position_worldspace: vec3<f32> = pointLight.position;\n let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);\n lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);\n } else if (lighting.lightType == 1) {\n var directionalLight: DirectionalLight = lighting_getDirectionalLight(0);\n lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);\n }\n \n return lightColor;\n /*\n for (int i = 0; i < MAX_LIGHTS; i++) {\n if (i >= lighting.pointLightCount) {\n break;\n }\n PointLight pointLight = lighting.pointLight[i];\n vec3 light_position_worldspace = pointLight.position;\n vec3 light_direction = normalize(light_position_worldspace - position_worldspace);\n lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);\n }\n\n for (int i = 0; i < MAX_LIGHTS; i++) {\n if (i >= lighting.directionalLightCount) {\n break;\n }\n DirectionalLight directionalLight = lighting.directionalLight[i];\n lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);\n }\n */\n}\n\nfn lighting_getSpecularLightColor(cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32>{\n var lightColor = vec3<f32>(0, 0, 0);\n let surfaceColor = vec3<f32>(0, 0, 0);\n\n if (lighting.enabled == 0) {\n let view_direction = normalize(cameraPosition - position_worldspace);\n\n switch (lighting.lightType) {\n case 0, default: {\n let pointLight: PointLight = lighting_getPointLight(0);\n let light_position_worldspace: vec3<f32> = pointLight.position;\n let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);\n lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);\n }\n case 1: {\n let directionalLight: DirectionalLight = lighting_getDirectionalLight(0);\n lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);\n }\n }\n }\n return lightColor;\n}\n";
2
- /**
3
- for (int i = 0; i < MAX_LIGHTS; i++) {
4
- if (i >= lighting.pointLightCount) {
5
- break;
6
- }
7
- PointLight pointLight = lighting_getPointLight(i);
8
- vec3 light_position_worldspace = pointLight.position;
9
- vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
10
- lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
11
- }
12
-
13
- for (int i = 0; i < MAX_LIGHTS; i++) {
14
- if (i >= lighting.directionalLightCount) {
15
- break;
16
- }
17
- PointLight pointLight = lighting_getDirectionalLight(i);
18
- lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
19
- }
20
- }
21
- /**
22
- for (int i = 0; i < MAX_LIGHTS; i++) {
23
- if (i >= lighting.pointLightCount) {
24
- break;
25
- }
26
- PointLight pointLight = lighting_getPointLight(i);
27
- vec3 light_position_worldspace = pointLight.position;
28
- vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
29
- lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
30
- }
31
-
32
- for (int i = 0; i < MAX_LIGHTS; i++) {
33
- if (i >= lighting.directionalLightCount) {
34
- break;
35
- }
36
- PointLight pointLight = lighting_getDirectionalLight(i);
37
- lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
38
- }
39
- }
40
- */
1
+ export declare const PHONG_WGSL = "struct phongMaterialUniforms {\n ambient: f32,\n diffuse: f32,\n shininess: f32,\n specularColor: vec3<f32>,\n};\n\n@binding(2) @group(0) var<uniform> phongMaterial : phongMaterialUniforms;\n\nfn lighting_getLightColor(surfaceColor: vec3<f32>, light_direction: vec3<f32>, view_direction: vec3<f32>, normal_worldspace: vec3<f32>, color: vec3<f32>) -> vec3<f32> {\n let halfway_direction: vec3<f32> = normalize(light_direction + view_direction);\n var lambertian: f32 = dot(light_direction, normal_worldspace);\n var specular: f32 = 0.0;\n if (lambertian > 0.0) {\n let specular_angle = max(dot(normal_worldspace, halfway_direction), 0.0);\n specular = pow(specular_angle, phongMaterial.shininess);\n }\n lambertian = max(lambertian, 0.0);\n return (lambertian * phongMaterial.diffuse * surfaceColor + specular * phongMaterial.specularColor) * color;\n}\n\nfn lighting_getLightColor2(surfaceColor: vec3<f32>, cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32> {\n var lightColor: vec3<f32> = surfaceColor;\n\n if (lighting.enabled == 0) {\n return lightColor;\n }\n\n let view_direction: vec3<f32> = normalize(cameraPosition - position_worldspace);\n lightColor = phongMaterial.ambient * surfaceColor * lighting.ambientColor;\n\n for (var i: i32 = 0; i < lighting.pointLightCount; i++) {\n let pointLight: PointLight = lighting_getPointLight(i);\n let light_position_worldspace: vec3<f32> = pointLight.position;\n let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);\n let light_attenuation = getPointLightAttenuation(\n pointLight,\n distance(light_position_worldspace, position_worldspace)\n );\n lightColor += lighting_getLightColor(\n surfaceColor,\n light_direction,\n view_direction,\n normal_worldspace,\n pointLight.color / light_attenuation\n );\n }\n\n let totalLights = min(MAX_LIGHTS, lighting.pointLightCount + lighting.directionalLightCount);\n for (var i: i32 = lighting.pointLightCount; i < totalLights; i++) {\n let directionalLight: DirectionalLight = lighting_getDirectionalLight(i);\n lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);\n } \n \n return lightColor;\n}\n\nfn lighting_getSpecularLightColor(cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32>{\n var lightColor = vec3<f32>(0, 0, 0);\n let surfaceColor = vec3<f32>(0, 0, 0);\n\n if (lighting.enabled != 0) {\n let view_direction = normalize(cameraPosition - position_worldspace);\n\n for (var i: i32 = 0; i < lighting.pointLightCount; i++) {\n let pointLight: PointLight = lighting_getPointLight(i);\n let light_position_worldspace: vec3<f32> = pointLight.position;\n let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);\n let light_attenuation = getPointLightAttenuation(\n pointLight,\n distance(light_position_worldspace, position_worldspace)\n );\n lightColor += lighting_getLightColor(\n surfaceColor,\n light_direction,\n view_direction,\n normal_worldspace,\n pointLight.color / light_attenuation\n );\n }\n\n let totalLights = min(MAX_LIGHTS, lighting.pointLightCount + lighting.directionalLightCount);\n for (var i: i32 = lighting.pointLightCount; i < totalLights; i++) {\n let directionalLight: DirectionalLight = lighting_getDirectionalLight(i);\n lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);\n }\n }\n return lightColor;\n}\n";
41
2
  //# sourceMappingURL=phong-shaders-wgsl.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"phong-shaders-wgsl.d.ts","sourceRoot":"","sources":["../../../../src/modules/lighting/phong-material/phong-shaders-wgsl.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,UAAU,ywHAsFtB,CAAC;AAGF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAsCI"}
1
+ {"version":3,"file":"phong-shaders-wgsl.d.ts","sourceRoot":"","sources":["../../../../src/modules/lighting/phong-material/phong-shaders-wgsl.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,UAAU,4tHA0FtB,CAAC"}
@@ -33,99 +33,63 @@ fn lighting_getLightColor2(surfaceColor: vec3<f32>, cameraPosition: vec3<f32>, p
33
33
  let view_direction: vec3<f32> = normalize(cameraPosition - position_worldspace);
34
34
  lightColor = phongMaterial.ambient * surfaceColor * lighting.ambientColor;
35
35
 
36
- if (lighting.lightType == 0) {
37
- let pointLight: PointLight = lighting_getPointLight(0);
36
+ for (var i: i32 = 0; i < lighting.pointLightCount; i++) {
37
+ let pointLight: PointLight = lighting_getPointLight(i);
38
38
  let light_position_worldspace: vec3<f32> = pointLight.position;
39
39
  let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
40
- lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
41
- } else if (lighting.lightType == 1) {
42
- var directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
43
- lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
44
- }
45
-
46
- return lightColor;
47
- /*
48
- for (int i = 0; i < MAX_LIGHTS; i++) {
49
- if (i >= lighting.pointLightCount) {
50
- break;
51
- }
52
- PointLight pointLight = lighting.pointLight[i];
53
- vec3 light_position_worldspace = pointLight.position;
54
- vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
55
- lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
40
+ let light_attenuation = getPointLightAttenuation(
41
+ pointLight,
42
+ distance(light_position_worldspace, position_worldspace)
43
+ );
44
+ lightColor += lighting_getLightColor(
45
+ surfaceColor,
46
+ light_direction,
47
+ view_direction,
48
+ normal_worldspace,
49
+ pointLight.color / light_attenuation
50
+ );
56
51
  }
57
52
 
58
- for (int i = 0; i < MAX_LIGHTS; i++) {
59
- if (i >= lighting.directionalLightCount) {
60
- break;
61
- }
62
- DirectionalLight directionalLight = lighting.directionalLight[i];
53
+ let totalLights = min(MAX_LIGHTS, lighting.pointLightCount + lighting.directionalLightCount);
54
+ for (var i: i32 = lighting.pointLightCount; i < totalLights; i++) {
55
+ let directionalLight: DirectionalLight = lighting_getDirectionalLight(i);
63
56
  lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
64
- }
65
- */
57
+ }
58
+
59
+ return lightColor;
66
60
  }
67
61
 
68
62
  fn lighting_getSpecularLightColor(cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32>{
69
63
  var lightColor = vec3<f32>(0, 0, 0);
70
64
  let surfaceColor = vec3<f32>(0, 0, 0);
71
65
 
72
- if (lighting.enabled == 0) {
66
+ if (lighting.enabled != 0) {
73
67
  let view_direction = normalize(cameraPosition - position_worldspace);
74
68
 
75
- switch (lighting.lightType) {
76
- case 0, default: {
77
- let pointLight: PointLight = lighting_getPointLight(0);
78
- let light_position_worldspace: vec3<f32> = pointLight.position;
79
- let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
80
- lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
81
- }
82
- case 1: {
83
- let directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
69
+ for (var i: i32 = 0; i < lighting.pointLightCount; i++) {
70
+ let pointLight: PointLight = lighting_getPointLight(i);
71
+ let light_position_worldspace: vec3<f32> = pointLight.position;
72
+ let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
73
+ let light_attenuation = getPointLightAttenuation(
74
+ pointLight,
75
+ distance(light_position_worldspace, position_worldspace)
76
+ );
77
+ lightColor += lighting_getLightColor(
78
+ surfaceColor,
79
+ light_direction,
80
+ view_direction,
81
+ normal_worldspace,
82
+ pointLight.color / light_attenuation
83
+ );
84
+ }
85
+
86
+ let totalLights = min(MAX_LIGHTS, lighting.pointLightCount + lighting.directionalLightCount);
87
+ for (var i: i32 = lighting.pointLightCount; i < totalLights; i++) {
88
+ let directionalLight: DirectionalLight = lighting_getDirectionalLight(i);
84
89
  lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
85
- }
86
90
  }
87
91
  }
88
92
  return lightColor;
89
93
  }
90
94
  `;
91
- // TODO - handle multiple lights
92
- /**
93
- for (int i = 0; i < MAX_LIGHTS; i++) {
94
- if (i >= lighting.pointLightCount) {
95
- break;
96
- }
97
- PointLight pointLight = lighting_getPointLight(i);
98
- vec3 light_position_worldspace = pointLight.position;
99
- vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
100
- lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
101
- }
102
-
103
- for (int i = 0; i < MAX_LIGHTS; i++) {
104
- if (i >= lighting.directionalLightCount) {
105
- break;
106
- }
107
- PointLight pointLight = lighting_getDirectionalLight(i);
108
- lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
109
- }
110
- }
111
- /**
112
- for (int i = 0; i < MAX_LIGHTS; i++) {
113
- if (i >= lighting.pointLightCount) {
114
- break;
115
- }
116
- PointLight pointLight = lighting_getPointLight(i);
117
- vec3 light_position_worldspace = pointLight.position;
118
- vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
119
- lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
120
- }
121
-
122
- for (int i = 0; i < MAX_LIGHTS; i++) {
123
- if (i >= lighting.directionalLightCount) {
124
- break;
125
- }
126
- PointLight pointLight = lighting_getDirectionalLight(i);
127
- lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
128
- }
129
- }
130
- */
131
95
  //# sourceMappingURL=phong-shaders-wgsl.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"phong-shaders-wgsl.js","sourceRoot":"","sources":["../../../../src/modules/lighting/phong-material/phong-shaders-wgsl.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,+BAA+B;AAC/B,oCAAoC;AAEpC,MAAM,CAAC,MAAM,UAAU,GAAG,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsFpC,CAAC;AAEF,gCAAgC;AAChC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAsCI"}
1
+ {"version":3,"file":"phong-shaders-wgsl.js","sourceRoot":"","sources":["../../../../src/modules/lighting/phong-material/phong-shaders-wgsl.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,+BAA+B;AAC/B,oCAAoC;AAEpC,MAAM,CAAC,MAAM,UAAU,GAAG,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0FpC,CAAC"}
@@ -1,7 +1,7 @@
1
1
  /** Quick random generator for fragment shaders */
2
2
  export declare const random: {
3
3
  readonly name: "random";
4
- readonly source: "fn random(scale: vec3f, seed: float) -> f32 {\n /* use the fragment position for a different seed per-pixel */\n return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);\n}\n";
4
+ readonly source: "fn random(scale: vec3f, seed: f32) -> f32 {\n return fract(sin(dot(scale + vec3f(seed), vec3f(12.9898, 78.233, 151.7182))) * 43758.5453 + seed);\n}\n";
5
5
  readonly fs: "float random(vec3 scale, float seed) {\n /* use the fragment position for a different seed per-pixel */\n return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);\n}\n";
6
6
  };
7
7
  //# sourceMappingURL=random.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../../../../src/modules/math/random/random.ts"],"names":[],"mappings":"AAoBA,kDAAkD;AAClD,eAAO,MAAM,MAAM;;;;CAIsB,CAAC"}
1
+ {"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../../../../src/modules/math/random/random.ts"],"names":[],"mappings":"AAmBA,kDAAkD;AAClD,eAAO,MAAM,MAAM;;;;CAIsB,CAAC"}
@@ -2,9 +2,8 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
  const source = /* wgsl */ `\
5
- fn random(scale: vec3f, seed: float) -> f32 {
6
- /* use the fragment position for a different seed per-pixel */
7
- return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);
5
+ fn random(scale: vec3f, seed: f32) -> f32 {
6
+ return fract(sin(dot(scale + vec3f(seed), vec3f(12.9898, 78.233, 151.7182))) * 43758.5453 + seed);
8
7
  }
9
8
  `;
10
9
  const fs = /* glsl */ `\
@@ -1 +1 @@
1
- {"version":3,"file":"random.js","sourceRoot":"","sources":["../../../../src/modules/math/random/random.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,+BAA+B;AAC/B,oCAAoC;AAIpC,MAAM,MAAM,GAAG,UAAU,CAAC;;;;;CAKzB,CAAC;AAEF,MAAM,EAAE,GAAG,UAAU,CAAC;;;;;CAKrB,CAAC;AAEF,kDAAkD;AAClD,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,IAAI,EAAE,QAAQ;IACd,MAAM;IACN,EAAE;CACqC,CAAC"}
1
+ {"version":3,"file":"random.js","sourceRoot":"","sources":["../../../../src/modules/math/random/random.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,+BAA+B;AAC/B,oCAAoC;AAIpC,MAAM,MAAM,GAAG,UAAU,CAAC;;;;CAIzB,CAAC;AAEF,MAAM,EAAE,GAAG,UAAU,CAAC;;;;;CAKrB,CAAC;AAEF,kDAAkD;AAClD,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,IAAI,EAAE,QAAQ;IACd,MAAM;IACN,EAAE;CACqC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luma.gl/shadertools",
3
- "version": "9.3.0-alpha.2",
3
+ "version": "9.3.0-alpha.6",
4
4
  "description": "Shader module system for luma.gl",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -52,5 +52,5 @@
52
52
  "@math.gl/core": "^4.1.0",
53
53
  "@math.gl/types": "^4.1.0"
54
54
  },
55
- "gitHead": "7fedf8d8902f58490a4ffca9a873daee3c732f24"
55
+ "gitHead": "59fda5480c4d0bb3d64545d4621175221f2b6c7c"
56
56
  }
package/src/index.ts CHANGED
@@ -70,6 +70,7 @@ export {fp64, fp64arithmetic} from './modules/math/fp64/fp64';
70
70
  // export {projection} from './modules/engine/project/project';
71
71
  export type {PickingProps, PickingUniforms} from './modules/engine/picking/picking';
72
72
  export {picking} from './modules/engine/picking/picking';
73
+ export {skin} from './modules/engine/skin/skin';
73
74
 
74
75
  // lighting
75
76
  export {
@@ -3,7 +3,11 @@
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
5
  const IFDEF_REGEXP = /^\s*\#\s*ifdef\s*([a-zA-Z_]+)\s*$/;
6
+ const IFNDEF_REGEXP = /^\s*\#\s*ifndef\s*([a-zA-Z_]+)\s*(?:\/\/.*)?$/;
7
+ const ELSE_REGEXP = /^\s*\#\s*else\s*(?:\/\/.*)?$/;
6
8
  const ENDIF_REGEXP = /^\s*\#\s*endif\s*$/;
9
+ const IFDEF_WITH_COMMENT_REGEXP = /^\s*\#\s*ifdef\s*([a-zA-Z_]+)\s*(?:\/\/.*)?$/;
10
+ const ENDIF_WITH_COMMENT_REGEXP = /^\s*\#\s*endif\s*(?:\/\/.*)?$/;
7
11
 
8
12
  export type PreprocessorOptions = {
9
13
  defines?: Record<string, boolean>;
@@ -13,19 +17,48 @@ export function preprocess(source: string, options?: PreprocessorOptions): strin
13
17
  const lines = source.split('\n');
14
18
  const output: string[] = [];
15
19
 
20
+ const conditionalStack: Array<{
21
+ parentActive: boolean;
22
+ branchTaken: boolean;
23
+ active: boolean;
24
+ }> = [];
16
25
  let conditional = true;
17
- let currentDefine: string | null = null;
26
+
18
27
  for (const line of lines) {
19
- const matchIf = line.match(IFDEF_REGEXP);
20
- const matchEnd = line.match(ENDIF_REGEXP);
21
- if (matchIf) {
22
- currentDefine = matchIf[1];
23
- conditional = Boolean(options?.defines?.[currentDefine]);
28
+ const matchIf = line.match(IFDEF_WITH_COMMENT_REGEXP) || line.match(IFDEF_REGEXP);
29
+ const matchIfNot = line.match(IFNDEF_REGEXP);
30
+ const matchElse = line.match(ELSE_REGEXP);
31
+ const matchEnd = line.match(ENDIF_WITH_COMMENT_REGEXP) || line.match(ENDIF_REGEXP);
32
+
33
+ if (matchIf || matchIfNot) {
34
+ const defineName = (matchIf || matchIfNot)?.[1];
35
+ const defineValue: boolean = Boolean(options?.defines?.[defineName!]);
36
+ const branchTaken: boolean = matchIf ? defineValue : !defineValue;
37
+ const active: boolean = conditional && branchTaken;
38
+ conditionalStack.push({parentActive: conditional, branchTaken, active});
39
+ conditional = active;
40
+ } else if (matchElse) {
41
+ const currentConditional = conditionalStack[conditionalStack.length - 1];
42
+ if (!currentConditional) {
43
+ throw new Error('Encountered #else without matching #ifdef or #ifndef');
44
+ }
45
+ currentConditional.active =
46
+ currentConditional.parentActive && !currentConditional.branchTaken;
47
+ currentConditional.branchTaken = true;
48
+ conditional = currentConditional.active;
24
49
  } else if (matchEnd) {
25
- conditional = true;
50
+ conditionalStack.pop();
51
+ conditional = conditionalStack.length
52
+ ? conditionalStack[conditionalStack.length - 1].active
53
+ : true;
26
54
  } else if (conditional) {
27
55
  output.push(line);
28
56
  }
29
57
  }
58
+
59
+ if (conditionalStack.length > 0) {
60
+ throw new Error('Unterminated conditional block in shader source');
61
+ }
62
+
30
63
  return output.join('\n');
31
64
  }
@@ -87,9 +87,16 @@ export class ShaderAssembler {
87
87
  modules,
88
88
  hookFunctions
89
89
  });
90
+ const defines = {
91
+ ...modules.reduce<Record<string, boolean>>((accumulator, module) => {
92
+ Object.assign(accumulator, module.defines);
93
+ return accumulator;
94
+ }, {}),
95
+ ...props.defines
96
+ };
90
97
  // WGSL does not have built-in preprocessing support (just compile time constants)
91
98
  const preprocessedSource =
92
- props.platformInfo.shaderLanguage === 'wgsl' ? preprocess(source) : source;
99
+ props.platformInfo.shaderLanguage === 'wgsl' ? preprocess(source, {defines}) : source;
93
100
  return {source: preprocessedSource, getUniforms, modules};
94
101
  }
95
102