import { AssetTransformer } from "../../AssetTransformer.js";
import { buffer_geometry_ensure_tangents } from "../../../graphics/geometry/MikkT/buffer_geometry_ensure_tangents.js";

/**
 * Ensures that all geometries have proper tangents
 */
export class GLTFAssetTransformMikkTSpaceTangents extends AssetTransformer {
    /**
     *
     * @param {Asset} source
     * @param {AssetDescription} asset_description
     * @return {Promise<Asset>}
     */
    async transform(source, asset_description) {
        const root = source.create();

        root.traverse(o => {
            if (o.isMesh && o.geometry !== null) {
                buffer_geometry_ensure_tangents(o.geometry);
            }
        });

        return source;
    }

    static INSTANCE = new GLTFAssetTransformMikkTSpaceTangents();
}
