How to use the @gltf-transform/core.GLTFUtil.addImage function in @gltf-transform/core

To help you get started, we’ve selected a few @gltf-transform/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github donmccurdy / glTF-Transform / packages / atlas / src / atlas.ts View on Github external
return Promise.all(pending).then(() => {
    const buffer = (canvas as any).toBuffer() as Buffer;
    const arrayBuffer = GLTFUtil.trimBuffer(buffer);

    GLTFUtil.addImage(container, 'atlas', arrayBuffer, atlasIsPNG ? 'image/png': 'image/jpeg');
    const atlasImageIndex = container.json.images.length - 1;

    // Reassign textures to atlas.
    const imageMap = new Map();
    images.forEach((i) => imageMap.set(i.index, i));
    container.json.materials.forEach((material) => {
      if (!material.pbrMetallicRoughness) return;
      if (!material.pbrMetallicRoughness.baseColorTexture) return;
      const baseColorTexture = material.pbrMetallicRoughness.baseColorTexture;
      const textureIndex = baseColorTexture.index;
      const textureDef = container.json.textures[textureIndex];
      if (!imageMap.has(textureDef.source)) return;
      const image = imageMap.get(textureDef.source);
      baseColorTexture['extensions'] = baseColorTexture['extensions'] || {};
      baseColorTexture['extensions'][KHR_TEXTURE_TRANSFORM] = {
        offset: [image.x / atlasWidth, image.y / atlasHeight],
github donmccurdy / glTF-Transform / packages / ao / src / ao.ts View on Github external
const primitives = [];
    const meshes = container.json.meshes || [];
    meshes.forEach((mesh) => {
        mesh.primitives.forEach((primitive) => {
            const position = container.getAccessorArray(primitive.attributes['POSITION']);
            const cells = primitive.indices !== undefined ? container.getAccessorArray(primitive.indices) : undefined;
            primitives.push({position, cells, def: primitive});
        })
    });

    if (primitives.length === 0) {
        logger.warn('No primitives found.');
        return;
    }

    GLTFUtil.addImage(container, 'occlusion', TEXTURE_DATA, TEXTURE_MIME_TYPE);
    container.json.textures.push({source: container.json.images.length - 1});
    const occlusionTextureIndex = container.json.textures.length - 1;

    let regl;
    if (options.gl) {
        const gl = options.gl(resolution, resolution);
        gl.getExtension('OES_texture_float');
        gl.getExtension('OES_element_index_uint');
        regl = REGL({gl, extensions: ['OES_texture_float', 'OES_element_index_uint']});
    }

    // TODO: Implement baking such that primitives affect other primitives, and respect
    // world transforms.
    primitives.forEach((primitive, index) => {
        logger.info(`Baking primitive ${index} / ${primitives.length}.`);