import { Material } from "./Material.js";
import {} from "./RenderDevice.js";
import { TextureBinding } from "./TextureBinding.js";
export class MaterialInstance {
    baseMaterial;
    overrides = new Map();
    dirty = true;
    baseRevision;
    constructor(baseMaterial) {
        this.baseMaterial = baseMaterial;
        this.baseRevision = baseMaterial.getRevision();
    }
    setOverride(name, value) {
        this.overrides.set(name, cloneUniformValue(value));
        this.dirty = true;
    }
    clearOverride(name) {
        if (this.overrides.delete(name)) {
            this.dirty = true;
        }
    }
    getParameter(name) {
        const override = this.overrides.get(name);
        if (override !== undefined) {
            return cloneUniformValue(override);
        }
        return this.baseMaterial.getParameter(name);
    }
    getParameters() {
        const parameters = new Map(this.baseMaterial.getParameters());
        for (const [key, value] of this.overrides) {
            parameters.set(key, cloneUniformValue(value));
        }
        return parameters;
    }
    isDirty() {
        return this.dirty || this.baseRevision !== this.baseMaterial.getRevision();
    }
    markClean() {
        this.dirty = false;
        this.baseRevision = this.baseMaterial.getRevision();
    }
}
function cloneUniformValue(value) {
    if (typeof value === "number") {
        return value;
    }
    if (value instanceof TextureBinding) {
        return value;
    }
    return ArrayBuffer.isView(value) ? new value.constructor(value) : [...value];
}
//# sourceMappingURL=MaterialInstance.js.map