pbrBRDFConfiguration.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { SerializationHelper, serialize, expandToProperty } from "../../Misc/decorators";
  2. import { Scene } from '../../scene';
  3. /**
  4. * @hidden
  5. */
  6. export interface IMaterialBRDFDefines {
  7. BRDF_V_HEIGHT_CORRELATED: boolean;
  8. MS_BRDF_ENERGY_CONSERVATION: boolean;
  9. SPHERICAL_HARMONICS: boolean;
  10. /** @hidden */
  11. _areMiscDirty: boolean;
  12. }
  13. /**
  14. * Define the code related to the BRDF parameters of the pbr material.
  15. */
  16. export class PBRBRDFConfiguration {
  17. /**
  18. * Default value used for the energy conservation.
  19. * This should only be changed to adapt to the type of texture in scene.environmentBRDFTexture.
  20. */
  21. public static DEFAULT_USE_ENERGY_CONSERVATION = true;
  22. /**
  23. * Default value used for the Smith Visibility Height Correlated mode.
  24. * This should only be changed to adapt to the type of texture in scene.environmentBRDFTexture.
  25. */
  26. public static DEFAULT_USE_SMITH_VISIBILITY_HEIGHT_CORRELATED = true;
  27. /**
  28. * Default value used for the IBL diffuse part.
  29. * This can help switching back to the polynomials mode globally which is a tiny bit
  30. * less GPU intensive at the drawback of a lower quality.
  31. */
  32. public static DEFAULT_USE_SPHERICAL_HARMONICS = true;
  33. @serialize()
  34. private _useEnergyConservation = PBRBRDFConfiguration.DEFAULT_USE_ENERGY_CONSERVATION;
  35. /**
  36. * Defines if the material uses energy conservation.
  37. */
  38. @expandToProperty("_markAllSubMeshesAsMiscDirty")
  39. public useEnergyConservation = PBRBRDFConfiguration.DEFAULT_USE_ENERGY_CONSERVATION;
  40. @serialize()
  41. private _useSmithVisibilityHeightCorrelated = PBRBRDFConfiguration.DEFAULT_USE_SMITH_VISIBILITY_HEIGHT_CORRELATED;
  42. /**
  43. * LEGACY Mode set to false
  44. * Defines if the material uses height smith correlated visibility term.
  45. * If you intent to not use our default BRDF, you need to load a separate BRDF Texture for the PBR
  46. * You can either load https://assets.babylonjs.com/environments/uncorrelatedBRDF.png
  47. * or https://assets.babylonjs.com/environments/uncorrelatedBRDF.dds to have more precision
  48. * Not relying on height correlated will also disable energy conservation.
  49. */
  50. @expandToProperty("_markAllSubMeshesAsMiscDirty")
  51. public useSmithVisibilityHeightCorrelated = PBRBRDFConfiguration.DEFAULT_USE_SMITH_VISIBILITY_HEIGHT_CORRELATED;
  52. @serialize()
  53. private _useSphericalHarmonics = PBRBRDFConfiguration.DEFAULT_USE_SPHERICAL_HARMONICS;
  54. /**
  55. * LEGACY Mode set to false
  56. * Defines if the material uses spherical harmonics vs spherical polynomials for the
  57. * diffuse part of the IBL.
  58. * The harmonics despite a tiny bigger cost has been proven to provide closer results
  59. * to the ground truth.
  60. */
  61. @expandToProperty("_markAllSubMeshesAsMiscDirty")
  62. public useSphericalHarmonics = PBRBRDFConfiguration.DEFAULT_USE_SPHERICAL_HARMONICS;
  63. /** @hidden */
  64. private _internalMarkAllSubMeshesAsMiscDirty: () => void;
  65. /** @hidden */
  66. public _markAllSubMeshesAsMiscDirty(): void {
  67. this._internalMarkAllSubMeshesAsMiscDirty();
  68. }
  69. /**
  70. * Instantiate a new istance of clear coat configuration.
  71. * @param markAllSubMeshesAsMiscDirty Callback to flag the material to dirty
  72. */
  73. constructor(markAllSubMeshesAsMiscDirty: () => void) {
  74. this._internalMarkAllSubMeshesAsMiscDirty = markAllSubMeshesAsMiscDirty;
  75. }
  76. /**
  77. * Checks to see if a texture is used in the material.
  78. * @param defines the list of "defines" to update.
  79. */
  80. public prepareDefines(defines: IMaterialBRDFDefines): void {
  81. defines.BRDF_V_HEIGHT_CORRELATED = this._useSmithVisibilityHeightCorrelated;
  82. defines.MS_BRDF_ENERGY_CONSERVATION = this._useEnergyConservation && this._useSmithVisibilityHeightCorrelated;
  83. defines.SPHERICAL_HARMONICS = this._useSphericalHarmonics;
  84. }
  85. /**
  86. * Get the current class name of the texture useful for serialization or dynamic coding.
  87. * @returns "PBRClearCoatConfiguration"
  88. */
  89. public getClassName(): string {
  90. return "PBRBRDFConfiguration";
  91. }
  92. /**
  93. * Makes a duplicate of the current configuration into another one.
  94. * @param brdfConfiguration define the config where to copy the info
  95. */
  96. public copyTo(brdfConfiguration: PBRBRDFConfiguration): void {
  97. SerializationHelper.Clone(() => brdfConfiguration, this);
  98. }
  99. /**
  100. * Serializes this BRDF configuration.
  101. * @returns - An object with the serialized config.
  102. */
  103. public serialize(): any {
  104. return SerializationHelper.Serialize(this);
  105. }
  106. /**
  107. * Parses a anisotropy Configuration from a serialized object.
  108. * @param source - Serialized object.
  109. * @param scene Defines the scene we are parsing for
  110. * @param rootUrl Defines the rootUrl to load from
  111. */
  112. public parse(source: any, scene: Scene, rootUrl: string): void {
  113. SerializationHelper.Parse(() => this, source, scene, rootUrl);
  114. }
  115. }