import { TextureAttachmentsByMaterialType } from "./TextureAttachmensByMaterialType.js";

/**
 *
 * @param {Material} material
 * @param {function(THREE.Texture, TextureAttachment)} callback
 * @param {*} [thisArg]
 */
export function traverseMaterialTextures(material, callback, thisArg) {

    //patch textures
    const materialType = material.type;

    /**
     *
     * @type {TextureAttachment[]}
     */
    const attachments = TextureAttachmentsByMaterialType[materialType];


    if (attachments !== undefined) {
        const attachment_count = attachments.length;

        for (let i = 0; i < attachment_count; i++) {
            const attachment = attachments[i];

            const texture = attachment.read(material);

            if (texture === null || texture === undefined) {
                continue;
            }

            callback.call(thisArg, texture, attachment);

        }
    }
}
