material.detailMapConfiguration.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { __decorate } from "../tslib.es6.js";
  2. import { Material } from "./material.js";
  3. import { serialize, expandToProperty, serializeAsTexture } from "../Misc/decorators.js";
  4. import { MaterialFlags } from "./materialFlags.js";
  5. import { MaterialDefines } from "./materialDefines.js";
  6. import { MaterialPluginBase } from "./materialPluginBase.js";
  7. import { BindTextureMatrix, PrepareDefinesForMergedUV } from "./materialHelper.functions.js";
  8. /**
  9. * @internal
  10. */
  11. export class MaterialDetailMapDefines extends MaterialDefines {
  12. constructor() {
  13. super(...arguments);
  14. this.DETAIL = false;
  15. this.DETAILDIRECTUV = 0;
  16. this.DETAIL_NORMALBLENDMETHOD = 0;
  17. }
  18. }
  19. /**
  20. * Plugin that implements the detail map component of a material
  21. *
  22. * Inspired from:
  23. * Unity: https://docs.unity3d.com/Packages/com.unity.render-pipelines.high-definition@9.0/manual/Mask-Map-and-Detail-Map.html and https://docs.unity3d.com/Manual/StandardShaderMaterialParameterDetail.html
  24. * Unreal: https://docs.unrealengine.com/en-US/Engine/Rendering/Materials/HowTo/DetailTexturing/index.html
  25. * Cryengine: https://docs.cryengine.com/display/SDKDOC2/Detail+Maps
  26. */
  27. export class DetailMapConfiguration extends MaterialPluginBase {
  28. /** @internal */
  29. _markAllSubMeshesAsTexturesDirty() {
  30. this._enable(this._isEnabled);
  31. this._internalMarkAllSubMeshesAsTexturesDirty();
  32. }
  33. constructor(material, addToPluginList = true) {
  34. super(material, "DetailMap", 140, new MaterialDetailMapDefines(), addToPluginList);
  35. this._texture = null;
  36. /**
  37. * Defines how strongly the detail diffuse/albedo channel is blended with the regular diffuse/albedo texture
  38. * Bigger values mean stronger blending
  39. */
  40. this.diffuseBlendLevel = 1;
  41. /**
  42. * Defines how strongly the detail roughness channel is blended with the regular roughness value
  43. * Bigger values mean stronger blending. Only used with PBR materials
  44. */
  45. this.roughnessBlendLevel = 1;
  46. /**
  47. * Defines how strong the bump effect from the detail map is
  48. * Bigger values mean stronger effect
  49. */
  50. this.bumpLevel = 1;
  51. this._normalBlendMethod = Material.MATERIAL_NORMALBLENDMETHOD_WHITEOUT;
  52. this._isEnabled = false;
  53. /**
  54. * Enable or disable the detail map on this material
  55. */
  56. this.isEnabled = false;
  57. this._internalMarkAllSubMeshesAsTexturesDirty = material._dirtyCallbacks[1];
  58. }
  59. isReadyForSubMesh(defines, scene, engine) {
  60. if (!this._isEnabled) {
  61. return true;
  62. }
  63. if (defines._areTexturesDirty && scene.texturesEnabled) {
  64. if (engine.getCaps().standardDerivatives && this._texture && MaterialFlags.DetailTextureEnabled) {
  65. // Detail texture cannot be not blocking.
  66. if (!this._texture.isReady()) {
  67. return false;
  68. }
  69. }
  70. }
  71. return true;
  72. }
  73. prepareDefines(defines, scene) {
  74. if (this._isEnabled) {
  75. defines.DETAIL_NORMALBLENDMETHOD = this._normalBlendMethod;
  76. const engine = scene.getEngine();
  77. if (defines._areTexturesDirty) {
  78. if (engine.getCaps().standardDerivatives && this._texture && MaterialFlags.DetailTextureEnabled && this._isEnabled) {
  79. PrepareDefinesForMergedUV(this._texture, defines, "DETAIL");
  80. defines.DETAIL_NORMALBLENDMETHOD = this._normalBlendMethod;
  81. }
  82. else {
  83. defines.DETAIL = false;
  84. }
  85. }
  86. }
  87. else {
  88. defines.DETAIL = false;
  89. }
  90. }
  91. bindForSubMesh(uniformBuffer, scene) {
  92. if (!this._isEnabled) {
  93. return;
  94. }
  95. const isFrozen = this._material.isFrozen;
  96. if (!uniformBuffer.useUbo || !isFrozen || !uniformBuffer.isSync) {
  97. if (this._texture && MaterialFlags.DetailTextureEnabled) {
  98. uniformBuffer.updateFloat4("vDetailInfos", this._texture.coordinatesIndex, this.diffuseBlendLevel, this.bumpLevel, this.roughnessBlendLevel);
  99. BindTextureMatrix(this._texture, uniformBuffer, "detail");
  100. }
  101. }
  102. // Textures
  103. if (scene.texturesEnabled) {
  104. if (this._texture && MaterialFlags.DetailTextureEnabled) {
  105. uniformBuffer.setTexture("detailSampler", this._texture);
  106. }
  107. }
  108. }
  109. hasTexture(texture) {
  110. if (this._texture === texture) {
  111. return true;
  112. }
  113. return false;
  114. }
  115. getActiveTextures(activeTextures) {
  116. if (this._texture) {
  117. activeTextures.push(this._texture);
  118. }
  119. }
  120. getAnimatables(animatables) {
  121. if (this._texture && this._texture.animations && this._texture.animations.length > 0) {
  122. animatables.push(this._texture);
  123. }
  124. }
  125. dispose(forceDisposeTextures) {
  126. if (forceDisposeTextures) {
  127. this._texture?.dispose();
  128. }
  129. }
  130. getClassName() {
  131. return "DetailMapConfiguration";
  132. }
  133. getSamplers(samplers) {
  134. samplers.push("detailSampler");
  135. }
  136. getUniforms() {
  137. return {
  138. ubo: [
  139. { name: "vDetailInfos", size: 4, type: "vec4" },
  140. { name: "detailMatrix", size: 16, type: "mat4" },
  141. ],
  142. };
  143. }
  144. }
  145. __decorate([
  146. serializeAsTexture("detailTexture"),
  147. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  148. ], DetailMapConfiguration.prototype, "texture", void 0);
  149. __decorate([
  150. serialize()
  151. ], DetailMapConfiguration.prototype, "diffuseBlendLevel", void 0);
  152. __decorate([
  153. serialize()
  154. ], DetailMapConfiguration.prototype, "roughnessBlendLevel", void 0);
  155. __decorate([
  156. serialize()
  157. ], DetailMapConfiguration.prototype, "bumpLevel", void 0);
  158. __decorate([
  159. serialize(),
  160. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  161. ], DetailMapConfiguration.prototype, "normalBlendMethod", void 0);
  162. __decorate([
  163. serialize(),
  164. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  165. ], DetailMapConfiguration.prototype, "isEnabled", void 0);
  166. //# sourceMappingURL=material.detailMapConfiguration.js.map