import { traverseMaterialTextures } from "../../../asset/loaders/material/traverseMaterialTextures.js";
import { UvContext } from "./UvContext.js";
import { max2 } from "../../../../core/math/max2.js";

export class MaterialDescriptor {
    constructor() {

        /**
         *
         * @type {THREE.Material|null}
         */
        this.source_material = null;

        /**
         *
         * @type {UvContext[]}
         */
        this.uvs = [];
    }

    build_uvs() {

        traverseMaterialTextures(this.source_material, (t, attachment) => {
            const uv_index = attachment.uv_index;

            let uv_context = this.uvs[uv_index];

            if (uv_context === undefined) {
                uv_context = new UvContext();
                uv_context.resolution.set(1, 1);

                this.uvs[uv_index] = uv_context;
            }

            const image = t.image;

            if (image !== undefined && image.width !== undefined) {
                uv_context.resolution.x = max2(uv_context.resolution.x, image.width);
                uv_context.resolution.y = max2(uv_context.resolution.y, image.height);
            }

        });

    }

}
