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