material.decalMapConfiguration.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { __decorate } from "../tslib.es6.js";
  2. import { serialize, expandToProperty } from "../Misc/decorators.js";
  3. import { MaterialDefines } from "./materialDefines.js";
  4. import { MaterialPluginBase } from "./materialPluginBase.js";
  5. import { MaterialFlags } from "./materialFlags.js";
  6. import { RegisterClass } from "../Misc/typeStore.js";
  7. import { BindTextureMatrix, PrepareDefinesForMergedUV } from "./materialHelper.functions.js";
  8. /**
  9. * @internal
  10. */
  11. export class DecalMapDefines extends MaterialDefines {
  12. constructor() {
  13. super(...arguments);
  14. this.DECAL = false;
  15. this.DECALDIRECTUV = 0;
  16. this.DECAL_SMOOTHALPHA = false;
  17. this.GAMMADECAL = false;
  18. }
  19. }
  20. /**
  21. * Plugin that implements the decal map component of a material
  22. * @since 5.49.1
  23. */
  24. export class DecalMapConfiguration extends MaterialPluginBase {
  25. /** @internal */
  26. _markAllSubMeshesAsTexturesDirty() {
  27. this._enable(this._isEnabled);
  28. this._internalMarkAllSubMeshesAsTexturesDirty();
  29. }
  30. /**
  31. * Creates a new DecalMapConfiguration
  32. * @param material The material to attach the decal map plugin to
  33. * @param addToPluginList If the plugin should be added to the material plugin list
  34. */
  35. constructor(material, addToPluginList = true) {
  36. super(material, "DecalMap", 150, new DecalMapDefines(), addToPluginList);
  37. this._isEnabled = false;
  38. /**
  39. * Enables or disables the decal map on this material
  40. */
  41. this.isEnabled = false;
  42. this._smoothAlpha = false;
  43. /**
  44. * Enables or disables the smooth alpha mode on this material. Default: false.
  45. * When enabled, the alpha value used to blend the decal map will be the squared value and will produce a smoother result.
  46. */
  47. this.smoothAlpha = false;
  48. this.registerForExtraEvents = true; // because we override the hardBindForSubMesh method
  49. this._internalMarkAllSubMeshesAsTexturesDirty = material._dirtyCallbacks[1];
  50. }
  51. isReadyForSubMesh(defines, scene, engine, subMesh) {
  52. const decalMap = subMesh.getMesh().decalMap;
  53. if (!this._isEnabled || !decalMap?.texture || !MaterialFlags.DecalMapEnabled || !scene.texturesEnabled) {
  54. return true;
  55. }
  56. return decalMap.isReady();
  57. }
  58. prepareDefines(defines, scene, mesh) {
  59. const decalMap = mesh.decalMap;
  60. if (!this._isEnabled || !decalMap?.texture || !MaterialFlags.DecalMapEnabled || !scene.texturesEnabled) {
  61. const isDirty = defines.DECAL;
  62. if (isDirty) {
  63. defines.markAsTexturesDirty();
  64. }
  65. defines.DECAL = false;
  66. }
  67. else {
  68. const isDirty = !defines.DECAL || defines.GAMMADECAL !== decalMap.texture.gammaSpace;
  69. if (isDirty) {
  70. defines.markAsTexturesDirty();
  71. }
  72. defines.DECAL = true;
  73. defines.GAMMADECAL = decalMap.texture.gammaSpace;
  74. defines.DECAL_SMOOTHALPHA = this._smoothAlpha;
  75. PrepareDefinesForMergedUV(decalMap.texture, defines, "DECAL");
  76. }
  77. }
  78. hardBindForSubMesh(uniformBuffer, scene, _engine, subMesh) {
  79. /**
  80. * Note that we override hardBindForSubMesh and not bindForSubMesh because the material can be shared by multiple meshes,
  81. * in which case mustRebind could return false even though the decal map is different for each mesh: that's because the decal map
  82. * is not part of the material but hosted by the decalMap of the mesh instead.
  83. */
  84. const decalMap = subMesh.getMesh().decalMap;
  85. if (!this._isEnabled || !decalMap?.texture || !MaterialFlags.DecalMapEnabled || !scene.texturesEnabled) {
  86. return;
  87. }
  88. const isFrozen = this._material.isFrozen;
  89. const texture = decalMap.texture;
  90. if (!uniformBuffer.useUbo || !isFrozen || !uniformBuffer.isSync) {
  91. uniformBuffer.updateFloat4("vDecalInfos", texture.coordinatesIndex, 0, 0, 0);
  92. BindTextureMatrix(texture, uniformBuffer, "decal");
  93. }
  94. uniformBuffer.setTexture("decalSampler", texture);
  95. }
  96. getClassName() {
  97. return "DecalMapConfiguration";
  98. }
  99. getSamplers(samplers) {
  100. samplers.push("decalSampler");
  101. }
  102. getUniforms() {
  103. return {
  104. ubo: [
  105. { name: "vDecalInfos", size: 4, type: "vec4" },
  106. { name: "decalMatrix", size: 16, type: "mat4" },
  107. ],
  108. };
  109. }
  110. }
  111. __decorate([
  112. serialize(),
  113. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  114. ], DecalMapConfiguration.prototype, "isEnabled", void 0);
  115. __decorate([
  116. serialize(),
  117. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  118. ], DecalMapConfiguration.prototype, "smoothAlpha", void 0);
  119. RegisterClass("BABYLON.DecalMapConfiguration", DecalMapConfiguration);
  120. //# sourceMappingURL=material.decalMapConfiguration.js.map