pbrMetallicRoughnessMaterial.glsl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Compute parameters for physically based rendering using the
  3. * metallic/roughness workflow. All inputs are linear; sRGB texture values must
  4. * be decoded beforehand
  5. *
  6. * @name czm_pbrMetallicRoughnessMaterial
  7. * @glslFunction
  8. *
  9. * @param {vec3} baseColor For dielectrics, this is the base color. For metals, this is the f0 value (reflectance at normal incidence)
  10. * @param {float} metallic 0.0 indicates dielectric. 1.0 indicates metal. Values in between are allowed (e.g. to model rust or dirt);
  11. * @param {float} roughness A value between 0.0 and 1.0
  12. * @return {czm_pbrParameters} parameters to pass into {@link czm_pbrLighting}
  13. */
  14. czm_pbrParameters czm_pbrMetallicRoughnessMaterial(
  15. vec3 baseColor,
  16. float metallic,
  17. float roughness
  18. )
  19. {
  20. czm_pbrParameters results;
  21. // roughness is authored as perceptual roughness
  22. // square it to get material roughness
  23. roughness = clamp(roughness, 0.0, 1.0);
  24. results.roughness = roughness * roughness;
  25. // dielectrics use f0 = 0.04, metals use albedo as f0
  26. metallic = clamp(metallic, 0.0, 1.0);
  27. const vec3 REFLECTANCE_DIELECTRIC = vec3(0.04);
  28. vec3 f0 = mix(REFLECTANCE_DIELECTRIC, baseColor, metallic);
  29. results.f0 = f0;
  30. // diffuse only applies to dielectrics.
  31. results.diffuseColor = baseColor * (1.0 - f0) * (1.0 - metallic);
  32. return results;
  33. }