donmccurdy / glTF-Transform

glTF 2.0 SDK for JavaScript and TypeScript, on Web and Node.js.
https://gltf-transform.dev
MIT License
1.42k stars 150 forks source link

Parse texture from Material #1552

Closed zrshannes closed 2 weeks ago

zrshannes commented 2 weeks ago

I have a material with texture, texCoord, and intensity. i wrote myself a extension for gltf-transform so "lightmap" Material will be read and writeable.

    {
      "doubleSided": true,
      "extensions": {
        "lightmap": {
          "intensity": 3.14,
          "index": 0,
          "texCoord": {
            "type": "Fixed",
            "value": "LM"
          }
        }
      },
      "name": "Material",
      "pbrMetallicRoughness": {
        "metallicFactor": 0,
        "roughnessFactor": 0.5
      }
    },

write:

        this.document.getRoot().listMaterials().forEach((material) => {
            if (material.getExtension(LIGHTMAP_EXTENSION_NAME)) {
                const materialIndex = context.materialIndexMap.get(material);
                const materialDef = jsonDoc.json.materials[materialIndex];
                materialDef.extensions = materialDef.extensions || {};

                const extensionDataLightmap = this.extensionDataLightmaps.get(materialIndex);
                if (extensionDataLightmap) {
                    console.log("\nWRITE Lightmap: \n", extensionDataLightmap, "\nto Scene Index", materialIndex);
                    materialDef.extensions[LIGHTMAP_EXTENSION_NAME] = extensionDataLightmap;
                    isLightmapUsed = true;
                }
            }
        });

read:

        const materialDefs = context.jsonDoc.json.materials || [];
        materialDefs.forEach((materialDef, index) => {
            if (materialDef.extensions && materialDef.extensions[LIGHTMAP_EXTENSION_NAME]) {
                this.extensionDataLightmaps.set(index, materialDef.extensions[LIGHTMAP_EXTENSION_NAME]);
                context.materials[index].setExtension(LIGHTMAP_EXTENSION_NAME, this.createLightmap());
            }
        });

when i process a glb the processed glb is missing the texture, buffers etc. material is copied and image is also present and converted. what am i missing to handle the given material correctly?