Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

63.3 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  glTF material/image/node import: what the products say about each other.
 *
 * The pixel gates prove the chain end to end, but only for the files they draw.
 * These cover the shapes those are not: an image in a GLB chunk, an image
 * already on disk, a node given as a matrix, a file with no node tree at all.
 */
import { describe, it, expect } from 'vitest';
import { importGltfMeshes } from '../src/assets/gltfImport';
import {
  assembleModelPrefab, materialProducts,
  type ImportedMesh, type PrefabAssembly, type ProductRefs,
} from '../src/assets/modelImport';
import { BlendMode, CullMode, MESH_MAX_BONES, MeshChannel } from 'esengine';
import { plainTriangle, meshoptTriangle, dracoTriangle } from './fixtures/gltfFixtures.mjs';

const PNG_1PX = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==';

/** POSITION x3 (VEC3) + TEXCOORD_0 x3 (VEC2) + 3 indices, as one data-uri buffer. */
function geometryBuffer(): { uri: string; views: unknown[]; accessors: unknown[] } {
  const positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]);
  const uvs = new Float32Array([0, 0, 1, 0, 0, 1]);
  const indices = new Uint16Array([0, 1, 2]);
  const bytes = Buffer.concat([
    Buffer.from(positions.buffer), Buffer.from(uvs.buffer), Buffer.from(indices.buffer),
  ]);
  return {
    uri: `data:application/octet-stream;base64,${bytes.toString('base64')}`,
    views: [
      { buffer: 0, byteOffset: 0, byteLength: 36 },
      { buffer: 0, byteOffset: 36, byteLength: 24 },
      { buffer: 0, byteOffset: 60, byteLength: 6 },
    ],
    accessors: [
      { bufferView: 0, componentType: 5126, count: 3, type: 'VEC3' },
      { bufferView: 1, componentType: 5126, count: 3, type: 'VEC2' },
      { bufferView: 2, componentType: 5123, count: 3, type: 'SCALAR' },
    ],
  };
}

function gltf(extra: Record<string, unknown>, primitives?: unknown[]): Uint8Array {
  const geo = geometryBuffer();
  const doc = {
    asset: { version: '2.0' },
    buffers: [{ byteLength: 66, uri: geo.uri }],
    bufferViews: geo.views,
    accessors: geo.accessors,
    meshes: [{
      name: 'Tri',
      primitives: primitives ?? [{
        attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, material: 0, mode: 4,
      }],
    }],
    ...extra,
  };
  return new TextEncoder().encode(JSON.stringify(doc));
}

/** The material every case below varies from: one baseColor image, one factor. */
function withInlineImage(): Record<string, unknown> {
  return {
    images: [{ mimeType: 'image/png', uri: `data:image/png;base64,${PNG_1PX}` }],
    textures: [{ source: 0 }],
    materials: [{
      name: 'Base',
      pbrMetallicRoughness: {
        baseColorFactor: [0.25, 0.5, 0.75, 1], baseColorTexture: { index: 0 },
        metallicFactor: 0,
      },
    }],
  };
}

/**
 * A glTF whose triangle carries TWO UV sets, the second offset from the first.
 * Identical sets would pass a reader that kept only one, so the fixture makes the
 * difference visible.
 */
function gltfWithSecondUv(): Uint8Array {
  const positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]);
  const uv0 = new Float32Array([0, 0, 1, 0, 0, 1]);
  const uv1 = new Float32Array([0.5, 0.25, 0.75, 0.25, 0.5, 0.5]);
  const indices = new Uint16Array([0, 1, 2]);
  const bytes = Buffer.concat([
    Buffer.from(positions.buffer), Buffer.from(uv0.buffer),
    Buffer.from(uv1.buffer), Buffer.from(indices.buffer),
  ]);
  const doc = {
    asset: { version: '2.0' },
    buffers: [{ byteLength: 90, uri: `data:application/octet-stream;base64,${bytes.toString('base64')}` }],
    bufferViews: [
      { buffer: 0, byteOffset: 0, byteLength: 36 },
      { buffer: 0, byteOffset: 36, byteLength: 24 },
      { buffer: 0, byteOffset: 60, byteLength: 24 },
      { buffer: 0, byteOffset: 84, byteLength: 6 },
    ],
    accessors: [
      { bufferView: 0, componentType: 5126, count: 3, type: 'VEC3' },
      { bufferView: 1, componentType: 5126, count: 3, type: 'VEC2' },
      { bufferView: 2, componentType: 5126, count: 3, type: 'VEC2' },
      { bufferView: 3, componentType: 5123, count: 3, type: 'SCALAR' },
    ],
    meshes: [{ name: 'Tri', primitives: [{
      attributes: { POSITION: 0, TEXCOORD_0: 1, TEXCOORD_1: 2 }, indices: 3, mode: 4,
    }] }],
    nodes: [{ mesh: 0 }],
    scenes: [{ nodes: [0] }],
    scene: 0,
  };
  return new TextEncoder().encode(JSON.stringify(doc));
}

/** One channel's values, vertex by vertex. */
function uvsOf(mesh: ImportedMesh, semantic: number): number[] {
  const channel = mesh.data.channels.find(c => c.semantic === semantic);
  if (!channel) return [];
  const view = new DataView(mesh.data.vertices.buffer, mesh.data.vertices.byteOffset);
  const out: number[] = [];
  for (let i = 0; i < mesh.vertexCount; i++) {
    const at = i * mesh.data.vertexStride + channel.offset;
    out.push(view.getFloat32(at, true), view.getFloat32(at + 4, true));
  }
  return out;
}

function texCoords(mesh: ImportedMesh): number[] {
  const channel = mesh.data.channels.find(c => c.semantic === MeshChannel.TexCoord0)!;
  const view = new DataView(mesh.data.vertices.buffer, mesh.data.vertices.byteOffset);
  const out: number[] = [];
  for (let i = 0; i < mesh.vertexCount; i++) {
    const at = i * mesh.data.vertexStride + channel.offset;
    out.push(view.getFloat32(at, true), view.getFloat32(at + 4, true));
  }
  return out;
}

describe('glTF material import', () => {
  it('extracts an inline image and names it in the material', async () => {
    const { meshes, textures, warnings } = await importGltfMeshes(gltf(withInlineImage()), 'model');
    expect(warnings).toEqual([]);
    expect(textures).toHaveLength(1);
    expect(textures[0]!.name).toBe('model_0.png');
    expect(textures[0]!.bytes.length).toBeGreaterThan(0);
    expect(meshes[0]!.material).toMatchObject({
      baseColor: [0.25, 0.5, 0.75, 1],
      baseColorTexture: { file: 'model_0.png', external: false },
    });
  });

  it('references an image already on disk instead of copying it', async () => {
    const doc = withInlineImage();
    doc.images = [{ uri: 'textures/diffuse%20map.png' }];
    const { meshes, textures } = await importGltfMeshes(gltf(doc), 'model');
    expect(textures).toEqual([]);
    expect(meshes[0]!.material?.baseColorTexture)
      .toEqual({ file: 'textures/diffuse map.png', external: true });
  });

  it('reads an image out of a GLB binary chunk', async () => {
    const png = Buffer.from(PNG_1PX, 'base64');
    const geo = geometryBuffer();
    const bin = Buffer.concat([Buffer.from(geo.uri.split(',')[1]!, 'base64'), png]);
    const json = Buffer.from(JSON.stringify({
      asset: { version: '2.0' },
      buffers: [{ byteLength: bin.length }],
      bufferViews: [
        ...(geo.views as { byteLength: number }[]),
        { buffer: 0, byteOffset: 66, byteLength: png.length },
      ],
      accessors: geo.accessors,
      images: [{ mimeType: 'image/png', bufferView: 3 }],
      textures: [{ source: 0 }],
      materials: [{ pbrMetallicRoughness: { baseColorTexture: { index: 0 } } }],
      meshes: [{ primitives: [{
        attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, material: 0, mode: 4,
      }] }],
    }));
    const pad = (b: Buffer, fill: number) =>
      Buffer.concat([b, Buffer.alloc((4 - (b.length % 4)) % 4, fill)]);
    const jsonChunk = pad(json, 0x20);
    const binChunk = pad(bin, 0);
    const header = Buffer.alloc(12);
    header.writeUInt32LE(0x46546c67, 0);
    header.writeUInt32LE(2, 4);
    header.writeUInt32LE(12 + 8 + jsonChunk.length + 8 + binChunk.length, 8);
    const chunk = (body: Buffer, kind: number) => {
      const head = Buffer.alloc(8);
      head.writeUInt32LE(body.length, 0);
      head.writeUInt32LE(kind, 4);
      return Buffer.concat([head, body]);
    };
    const glb = Buffer.concat([
      header, chunk(jsonChunk, 0x4e4f534a), chunk(binChunk, 0x004e4942),
    ]);

    const { meshes, textures } = await importGltfMeshes(new Uint8Array(glb), 'robot');
    expect(textures).toHaveLength(1);
    expect(textures[0]!.name).toBe('robot_0.png');
    expect(Buffer.from(textures[0]!.bytes).equals(png)).toBe(true);
    expect(meshes[0]!.material?.baseColorTexture?.file).toBe('robot_0.png');
  });

  it('reports the PBR channels it cannot draw rather than dropping them', async () => {
    const doc = withInlineImage();
    doc.materials = [{
      name: 'Metal',
      pbrMetallicRoughness: { metallicFactor: 1, roughnessFactor: 0.3 },
      normalTexture: { index: 0, scale: 0.4 }, emissiveFactor: [1, 0, 0],
      alphaMode: 'MASK', alphaCutoff: 0.4, doubleSided: false,
      extensions: { EXT_unimplemented_surface: {} },
    }];
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    const line = warnings.join('\n');
    expect(line).not.toContain('normal-map scale');
    expect(line).toContain('EXT_unimplemented_surface');
    // The channels that DO reach a shader are products, not warnings — metal and
    // roughness among them now that the lighting model has a view direction.
    expect(line).not.toContain('metallic-roughness');
    expect(line).not.toContain('emissive');
    expect(line).not.toContain('alpha cutoff');
    expect(materialProducts(meshes, 'model')[0]!.data.properties)
      .toMatchObject({ u_metallic: 1, u_roughness: 0.3, u_normalScale: 0.4 });
  });

  it.each([0, 0.5, 2, -1])('preserves normal scale %s through the material product', async scale => {
    const doc = withInlineImage();
    (doc.materials as Record<string, unknown>[])[0]!.normalTexture = { index: 0, scale };
    const result = await importGltfMeshes(gltf(doc), 'normal');
    expect(result.meshes[0]!.material!.normalScale).toBe(scale);
    expect(materialProducts(result.meshes, 'normal')[0]!.data.properties.u_normalScale).toBe(scale);
    expect(result.warnings.join('\n')).not.toContain('normal-map scale');
  });

  it('reports an invalid normal scale and keeps the default', async () => {
    const doc = withInlineImage();
    (doc.materials as Record<string, unknown>[])[0]!.normalTexture = { index: 0, scale: 'wrong' };
    const result = await importGltfMeshes(gltf(doc), 'normal');
    expect(result.meshes[0]!.material!.normalScale).toBeUndefined();
    expect(result.warnings.join('\n')).toContain('invalid normal-map scale');
  });

  it('prefers specular/glossiness to the metallic fallback and preserves its diffuse alpha and packed map', async () => {
    const doc = withInlineImage();
    doc.images = [{ uri: 'diffuse.png' }, { uri: 'specular.png' }, { uri: 'unused-fallback.png' }];
    doc.textures = [{ source: 0 }, { source: 1 }, { source: 2 }];
    doc.materials = [{
      name: 'Skin', alphaMode: 'MASK', alphaCutoff: 0.3,
      pbrMetallicRoughness: { baseColorFactor: [1, 0, 0, 1], baseColorTexture: { index: 2 }, metallicFactor: 1, roughnessFactor: 1 },
      extensions: { KHR_materials_pbrSpecularGlossiness: {
        diffuseFactor: [0.2, 0.4, 0.6, 0.8], diffuseTexture: { index: 0 },
        specularFactor: [0.08, 0.16, 0.24], glossinessFactor: 0.7, specularGlossinessTexture: { index: 1 },
      } },
    }];
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'legacy');
    const material = meshes[0]!.material!;
    expect(material).toMatchObject({ baseColor: [0.2, 0.4, 0.6, 0.8], baseColorTexture: { file: 'diffuse.png' }, specularGlossiness: { specular: [0.08, 0.16, 0.24], glossiness: 0.7 }, specularGlossinessTexture: { file: 'specular.png' } });
    expect(material.metallic).toBeUndefined();
    expect(material.roughness).toBeUndefined();
    const product = materialProducts(meshes, 'legacy', { characters: true })[0]!;
    expect(product.data).toMatchObject({ shader: 'builtin:model', switches: { ES_SPECULAR_GLOSSINESS: true }, properties: { u_specularColor: { r: 0.08, g: 0.16, b: 0.24, a: 1 }, u_glossiness: 0.7, u_alphaCutoff: 0.3 } });
    expect(product.images.map(i => i.file)).toEqual(['specular.png']);
    expect(warnings.join('\n')).not.toContain('not imported');
  });

  it('uses the specular/glossiness defaults even when the metallic fallback says something else', async () => {
    const doc = withInlineImage();
    (doc.materials as Record<string, unknown>[])[0]!.extensions = { KHR_materials_pbrSpecularGlossiness: {} };
    const { meshes } = await importGltfMeshes(gltf(doc), 'legacy');
    expect(meshes[0]!.material).toMatchObject({ baseColor: [1, 1, 1, 1], specularGlossiness: { specular: [1, 1, 1], glossiness: 1 } });
    expect(meshes[0]!.material!.baseColorTexture).toBeUndefined();
    expect(materialProducts(meshes, 'legacy')).toHaveLength(1);
  });

  it('is opaque and single-sided unless the source says otherwise', async () => {
    // Both are the glTF's own defaults, so a model arrives occluding itself the
    // way it was authored rather than as a stack of blended planes.
    const plain = (await importGltfMeshes(gltf(withInlineImage()), 'model')).meshes[0]!;
    expect(plain.material).toMatchObject({ opaque: true, cullBackfaces: true });

    const doc = withInlineImage();
    (doc.materials as Record<string, unknown>[])[0]!.alphaMode = 'BLEND';
    (doc.materials as Record<string, unknown>[])[0]!.doubleSided = true;
    const blended = (await importGltfMeshes(gltf(doc), 'model')).meshes[0]!;
    expect(blended.material).toMatchObject({ opaque: false, cullBackfaces: false });
  });

  it('carries a normal map, extracting its image like any other', async () => {
    const doc = withInlineImage();
    doc.images = [
      { mimeType: 'image/png', uri: `data:image/png;base64,${PNG_1PX}` },
      { uri: 'bump.png' },
    ];
    doc.textures = [{ source: 0 }, { source: 1 }];
    (doc.materials as { normalTexture?: unknown }[])[0]!.normalTexture = { index: 1 };
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(warnings).toEqual([]);
    expect(meshes[0]!.material?.normalTexture)
      .toEqual({ file: 'bump.png', external: true });
  });

  it('says so when a Draco blob will not decode, rather than reading zeroes', async () => {
    // The accessors of a Draco primitive point at nothing by design, so a blob
    // that fails to decode has to be reported: read as ordinary accessors, the
    // very same file is a mesh of vertices on the origin.
    const doc = {
      ...withInlineImage(),
      accessors: [
        { componentType: 5126, count: 3, type: 'VEC3' },
        { componentType: 5126, count: 3, type: 'VEC2' },
        { componentType: 5123, count: 3, type: 'SCALAR' },
      ],
    };
    const { meshes, warnings } = await importGltfMeshes(gltf(doc, [{
      attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, material: 0, mode: 4,
      extensions: { KHR_draco_mesh_compression: { bufferView: 0, attributes: {} } },
    }]), 'model');
    expect(meshes).toEqual([]);
    expect(warnings.join('\n')).toContain('Draco decode failed');
  });

  it('skips a primitive whose POSITION carries no data', async () => {
    const doc = {
      ...withInlineImage(),
      accessors: [
        { componentType: 5126, count: 3, type: 'VEC3' },
        { bufferView: 1, componentType: 5126, count: 3, type: 'VEC2' },
        { bufferView: 2, componentType: 5123, count: 3, type: 'SCALAR' },
      ],
    };
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(meshes).toEqual([]);
    expect(warnings.join('\n')).toContain('POSITION has no data');
  });

  it('applies a sparse accessor, which is the geometry it actually has', async () => {
    // Replaces vertex 1's position; the base view is the ordinary one.
    const target = Buffer.alloc(2);
    target.writeUInt16LE(1, 0);
    const value = Buffer.from(new Float32Array([9, 9, 9]).buffer);
    const extra = Buffer.concat([target, value]);
    const geo = geometryBuffer();
    const base = Buffer.from(geo.uri.split(',')[1]!, 'base64');
    const bytes = Buffer.concat([base, extra]);
    const doc = {
      ...withInlineImage(),
      buffers: [{ byteLength: bytes.length, uri: `data:application/octet-stream;base64,${bytes.toString('base64')}` }],
      bufferViews: [
        ...(geo.views as unknown[]),
        { buffer: 0, byteOffset: 66, byteLength: 2 },
        { buffer: 0, byteOffset: 68, byteLength: 12 },
      ],
      accessors: [
        {
          bufferView: 0, componentType: 5126, count: 3, type: 'VEC3',
          sparse: {
            count: 1,
            indices: { bufferView: 3, componentType: 5123 },
            values: { bufferView: 4 },
          },
        },
        { bufferView: 1, componentType: 5126, count: 3, type: 'VEC2' },
        { bufferView: 2, componentType: 5123, count: 3, type: 'SCALAR' },
      ],
    };
    const { meshes } = await importGltfMeshes(gltf(doc), 'model');
    expect(meshes[0]!.data.aabbMax).toEqual([9, 9, 9]);
  });

  it("carries a sampler's filter and wrap as import settings", async () => {
    const doc = withInlineImage();
    doc.samplers = [{ magFilter: 9728, wrapS: 33071, wrapT: 33071 }];
    (doc.textures as { sampler?: number }[])[0]!.sampler = 0;
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(warnings).toEqual([]);
    expect(meshes[0]!.material?.baseColorTexture?.settings)
      .toEqual({ filterMode: 'nearest', wrapMode: 'clamp' });
  });

  it('says which wrap it kept when the source addresses u and v differently', async () => {
    const doc = withInlineImage();
    doc.samplers = [{ wrapS: 33071, wrapT: 10497 }];
    (doc.textures as { sampler?: number }[])[0]!.sampler = 0;
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(warnings.join('\n')).toContain('wrapS and wrapT differ');
    expect(meshes[0]!.material?.baseColorTexture?.settings?.wrapMode).toBe('clamp');
  });

  it('reports deformation it cannot carry: joints nothing binds', async () => {
    const doc = {
      ...withInlineImage(),
      skins: [{}],
      nodes: [{ mesh: 0, skin: 0 }], scenes: [{ nodes: [0] }], scene: 0,
    };
    const { warnings } = await importGltfMeshes(gltf(doc, [{
      attributes: { POSITION: 0, TEXCOORD_0: 1, JOINTS_0: 1 }, indices: 2, material: 0, mode: 4,
    }]), 'model');
    expect(warnings.join('\n')).toContain('a skin naming joints');
  });

  it("carries a uv transform to the material, in the engine's flipped uv", async () => {
    const doc = withInlineImage();
    (doc.materials as { pbrMetallicRoughness: { baseColorTexture: Record<string, unknown> } }[])[0]!
      .pbrMetallicRoughness.baseColorTexture.extensions = { KHR_texture_transform: { scale: [2, 3], offset: [0.25, 0.5] } };
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(warnings).toEqual([]);
    // glTF reads (0.25 + 2u, 0.5 + 3v) of its own uv; with v' = 1 - v that is 1 - (0.5 + 3(1 - v')) = -2.5 + 3v'.
    expect(meshes[0]!.material!.uvTransform).toEqual({ tiling: [2, 3], offset: [0.25, -2.5], rotation: 0 });
    // The mesh's own uvs are left as authored: the material moves where they are read.
    expect(texCoords(meshes[0]!)).toEqual([0, 1, 1, 1, 0, 0]);
  });

  it('turns a rotated transform the other way, about the flipped origin', async () => {
    const doc = withInlineImage();
    (doc.materials as { pbrMetallicRoughness: { baseColorTexture: Record<string, unknown> } }[])[0]!
      .pbrMetallicRoughness.baseColorTexture.extensions = { KHR_texture_transform: { rotation: Math.PI / 2 } };
    const { meshes } = await importGltfMeshes(gltf(doc), 'model');
    const t = meshes[0]!.material!.uvTransform!;
    expect(t.rotation).toBeCloseTo(-Math.PI / 2);
    // glTF turns its uv (x, 1 - y) to (1 - y, -x); flipped back, the engine reads (1 - y, 1 + x).
    const at = (x: number, y: number): [number, number] => {
      const c = Math.cos(t.rotation), s = Math.sin(t.rotation);
      return [c * x + s * y + t.offset[0], -s * x + c * y + t.offset[1]];
    };
    for (const [x, y] of [[0, 0], [0.3, 0.7], [1, 0.2]] as const) {
      const [ex, ey] = at(x, y);
      expect(ex).toBeCloseTo(1 - y);
      expect(ey).toBeCloseTo(1 + x);
    }
  });

  it('reads every map through the first one when their transforms disagree, and says so', async () => {
    const doc = withInlineImage();
    const material = (doc.materials as Record<string, any>[])[0]!;
    material.pbrMetallicRoughness.baseColorTexture.extensions = { KHR_texture_transform: { scale: [4, 4] } };
    material.normalTexture = { index: 0 };
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(meshes[0]!.material!.uvTransform?.tiling).toEqual([4, 4]);
    expect(warnings.join('\n')).toContain('different KHR_texture_transform');
  });

  it.each([{}, { texCoord: 0 }, { offset: [0, 0], scale: [1, 1], rotation: 0 }])
    ('accepts an identity UV transform from a model exporter: %j', async transform => {
      const doc = withInlineImage();
      (doc.materials as { pbrMetallicRoughness: { baseColorTexture: Record<string, unknown> } }[])[0]!
        .pbrMetallicRoughness.baseColorTexture.extensions = { KHR_texture_transform: transform };
      const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
      expect(warnings).toEqual([]);
      expect(texCoords(meshes[0]!)).toEqual([0, 1, 1, 1, 0, 0]);
    });

  it.each([0, 1])('reports the effective UV set after an extension override to %s', async texCoord => {
    const doc = withInlineImage();
    const texture = (doc.materials as { pbrMetallicRoughness: { baseColorTexture: Record<string, unknown> } }[])[0]!
      .pbrMetallicRoughness.baseColorTexture;
    texture.texCoord = 1;
    texture.extensions = { KHR_texture_transform: { texCoord } };
    const { warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(warnings).toEqual(texCoord === 0 ? [] : [
      'material "Base": samples TEXCOORD_1; materials read the first set, so it will sample UV0',
    ]);
  });

  it("flips V, because glTF's uv origin is the image's top-left", async () => {
    const { meshes } = await importGltfMeshes(gltf(withInlineImage()), 'model');
    expect(texCoords(meshes[0]!)).toEqual([0, 1, 1, 1, 0, 0]);
  });
});

describe('glTF material products', () => {
  const shaded = (extra: Record<string, unknown>): Record<string, unknown> => {
    const doc = withInlineImage();
    doc.materials = [{ name: 'Shaded', ...extra }];
    return doc;
  };
  const productsOf = async (doc: Record<string, unknown>, refs: ProductRefs = {}) => {
    const { meshes } = await importGltfMeshes(gltf(doc), 'model');
    return { meshes, products: materialProducts(meshes, 'model', refs) };
  };

  it('writes nothing for a material a MeshRenderer already carries whole', async () => {
    // baseColor is the component's own texture x colour, so a material here
    // would be a second file saying what the first one says.
    const { products } = await productsOf(withInlineImage());
    expect(products).toEqual([]);
  });

  it('carries the channels a component has no room for', async () => {
    const { products } = await productsOf(shaded({
      emissiveFactor: [1, 0.5, 0], emissiveTexture: { index: 0 },
      occlusionTexture: { index: 0, strength: 0.25 },
      alphaMode: 'MASK', alphaCutoff: 0.4,
    }), { prefix: 'assets/models/' });
    expect(products).toHaveLength(1);
    expect(products[0]!.name).toBe('model_m0');
    expect(products[0]!.data.shader).toBe('builtin:model');
    // A material with no pbrMetallicRoughness block is glTF's default: fully
    // metallic, fully rough. Written out, since the engine's own default is not.
    expect(products[0]!.data.properties).toEqual({
      u_emissive: { r: 1, g: 0.5, b: 0, a: 1 },
      u_emissiveMap: 'model_0.png',
      u_occlusionMap: 'model_0.png',
      u_occlusionStrength: 0.25,
      u_alphaCutoff: 0.4,
      u_metallic: 1,
      u_roughness: 1,
    });
  });

  it('writes a tiled texture set as the Model shader\'s Tiling and Offset, which alone needs a material', async () => {
    const { products } = await productsOf(shaded({
      pbrMetallicRoughness: { baseColorTexture: { index: 0, extensions: { KHR_texture_transform: { scale: [0.5, 0.5] } } } },
    }));
    expect(products).toHaveLength(1);
    expect(products[0]!.data.properties).toMatchObject({ u_uvTiling: { x: 0.5, y: 0.5 }, u_uvOffset: { x: 0, y: 0.5 } });
    expect(products[0]!.data.properties).not.toHaveProperty('u_uvRotation');
  });

  it('takes the normal map WITH it, off the component', async () => {
    // A material shader samples its own units; a map left on the component
    // would be one nothing reads.
    const { meshes, products } = await productsOf(
      shaded({ normalTexture: { index: 0 }, emissiveFactor: [1, 1, 1] }),
      { prefix: 'assets/models/' });
    expect(products[0]!.data.properties.u_normalMap).toBe('model_0.png');
    const prefab = assembleModelPrefab('model', meshes, { refs: { prefix: 'assets/models/' } });
    const meshRenderer = prefab.entities[1]!.components[1]!.data;
    expect(meshRenderer.material).toBe('assets/models/model_m0.esmaterial');
    expect(meshRenderer.normalMap).toBeUndefined();
  });

  it('preserves HDR emission through imported material products', async () => {
    const doc = shaded({
      emissiveFactor: [1, 0.5, 0.125], emissiveTexture: { index: 0 },
      extensions: { KHR_materials_emissive_strength: { emissiveStrength: 24 } },
    });
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(warnings).toEqual([]);
    expect(meshes[0]!.material?.emissive).toEqual([24, 12, 3]);
    const products = materialProducts(meshes, 'model');
    expect(products[0]!.data.properties.u_emissive).toEqual({ r: 24, g: 12, b: 3, a: 1 });
    expect(products[0]!.data.properties.u_emissiveMap).toBe('model_0.png');
  });

  it.each([0, 0.5, 1])('preserves an emission multiplier of %s', async strength => {
    const { products } = await productsOf(shaded({
      emissiveFactor: [1, 0.5, 0], emissiveTexture: { index: 0 },
      extensions: { KHR_materials_emissive_strength: { emissiveStrength: strength } },
    }));
    expect(products[0]!.data.properties.u_emissive).toEqual(strength === 0
      ? undefined : { r: strength, g: strength * 0.5, b: 0, a: 1 });
  });

  it('defaults an omitted emissive strength to one on a specular/glossiness material', async () => {
    const doc = shaded({
      emissiveFactor: [1, 0.5, 0],
      extensions: { KHR_materials_emissive_strength: {}, KHR_materials_pbrSpecularGlossiness: {} },
    });
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(meshes[0]!.material?.emissive).toEqual([1, 0.5, 0]);
    expect(meshes[0]!.material?.specularGlossiness).toEqual({ specular: [1, 1, 1], glossiness: 1 });
    expect(warnings).toEqual([]);
  });

  it('carries the shading layers its extensions state, as the Model shader parameters', async () => {
    const { meshes, warnings } = await importGltfMeshes(gltf(shaded({
      extensions: {
        KHR_materials_clearcoat: { clearcoatFactor: 0.8, clearcoatRoughnessFactor: 0.1 },
        KHR_materials_sheen: { sheenColorFactor: [0.9, 0.8, 0.7], sheenRoughnessFactor: 0.4 },
        KHR_materials_anisotropy: { anisotropyStrength: 0.6, anisotropyRotation: 1.5 },
        KHR_materials_ior: { ior: 1.8 },
        KHR_materials_specular: { specularFactor: 0.5 },
        KHR_materials_iridescence: { iridescenceFactor: 1, iridescenceIor: 1.4, iridescenceThicknessMinimum: 200, iridescenceThicknessMaximum: 400 },
        KHR_materials_diffuse_transmission: { diffuseTransmissionFactor: 0.7, diffuseTransmissionColorFactor: [1, 0.5, 0.25] },
      },
    })), 'model');
    const products = materialProducts(meshes, 'model');
    expect(warnings).toEqual([]);
    expect(meshes[0]!.material?.opaque).toBe(true);
    expect(products[0]!.data.properties).toMatchObject({
      u_clearcoat: 0.8, u_clearcoatRoughness: 0.1,
      u_sheenColor: { r: 0.9, g: 0.8, b: 0.7, a: 1 }, u_sheenRoughness: 0.4,
      u_anisotropy: 0.6, u_anisotropyRotation: 1.5, u_ior: 1.8, u_specular: 0.5,
      u_iridescence: 1, u_iridescenceIor: 1.4, u_iridescenceThickness: 300,
      u_translucency: 0.7, u_translucencyColor: { r: 1, g: 0.5, b: 0.25, a: 1 },
    });
  });

  it('draws a transmissive surface blended, and tints it by the volume attenuation', async () => {
    const { products } = await productsOf(shaded({
      extensions: {
        KHR_materials_transmission: { transmissionFactor: 0.9 },
        KHR_materials_volume: { thicknessFactor: 0.3, attenuationColor: [0.5, 0.8, 1] },
      },
    }));
    expect(products[0]!.data).toMatchObject({ blendMode: BlendMode.Normal, depthWrite: false });
    expect(products[0]!.data.properties).toMatchObject({
      u_transmission: 0.9, u_thickness: 0.3, u_attenuationColor: { r: 0.5, g: 0.8, b: 1, a: 1 },
    });
  });

  it('writes nothing for a layer that is at its default, and says its maps are not imported', async () => {
    const { meshes, warnings } = await importGltfMeshes(gltf(shaded({
      extensions: {
        KHR_materials_clearcoat: { clearcoatFactor: 0 },
        KHR_materials_ior: { ior: 1.5 },
        KHR_materials_sheen: { sheenColorFactor: [0, 0, 0], sheenColorTexture: { index: 0 } },
      },
    })), 'model');
    expect(meshes[0]!.material?.clearcoat).toBeUndefined();
    expect(meshes[0]!.material?.ior).toBeUndefined();
    expect(meshes[0]!.material?.sheenColor).toBeUndefined();
    expect(warnings).toEqual(['material "Shaded": the textures of KHR_materials_sheen are not imported (their factors are)']);
  });

  it.each([-1, '24', null])('reports an invalid emission strength %s', async strength => {
    const doc = shaded({
      emissiveFactor: [1, 0.5, 0],
      extensions: { KHR_materials_emissive_strength: { emissiveStrength: strength } },
    });
    const { meshes, warnings } = await importGltfMeshes(gltf(doc), 'model');
    expect(meshes[0]!.material?.emissive).toEqual([1, 0.5, 0]);
    expect(warnings).toContain('material "Shaded": invalid emissiveStrength; using 1');
  });

  it('says the render state the draw would otherwise have said itself', async () => {
    // A material REPLACES the draw's blend/depth/cull, so a model told to
    // occlude itself must keep saying so through the material.
    const opaque = await productsOf(shaded({ emissiveFactor: [1, 1, 1] }));
    expect(opaque.products[0]!.data).toMatchObject({
      blendMode: BlendMode.None, depthTest: true, depthWrite: true, cull: CullMode.Back,
    });
    const blended = await productsOf(shaded({
      emissiveFactor: [1, 1, 1], alphaMode: 'BLEND', doubleSided: true,
    }));
    expect(blended.products[0]!.data).toMatchObject({
      blendMode: BlendMode.Normal, depthTest: true, depthWrite: false, cull: CullMode.None,
    });
  });

  it('writes one product for a material several primitives share', async () => {
    const doc = shaded({ emissiveFactor: [0, 0, 1] });
    const { meshes } = await importGltfMeshes(gltf(doc, [
      { attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, material: 0, mode: 4 },
      { attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, material: 0, mode: 4 },
    ]), 'model');
    const refs = { prefix: 'assets/models/' };
    expect(materialProducts(meshes, 'model', refs)).toHaveLength(1);
    const prefab = assembleModelPrefab('model', meshes, { refs });
    const refsUsed = prefab.entities.flatMap(e => e.components
      .filter(c => c.type === 'MeshRenderer').map(c => c.data.material));
    expect(refsUsed).toEqual(['assets/models/model_m0.esmaterial',
                              'assets/models/model_m0.esmaterial']);
  });

  it('spells an image already in the project as a logical path', async () => {
    // A material resolves a relative ref against its own directory, and this one
    // is not beside it.
    const doc = shaded({ emissiveTexture: { index: 0 } });
    doc.images = [{ uri: 'shared/glow.png' }];
    const inAssets = await productsOf(doc, {
      prefix: 'assets/models/', external: (uri) => `assets/${uri}`,
    });
    expect(inAssets.products[0]!.data.properties.u_emissiveMap).toBe('assets/shared/glow.png');
    const elsewhere = await productsOf(doc, {
      prefix: 'models/', external: (uri) => `art/${uri}`,
    });
    expect(elsewhere.products[0]!.data.properties.u_emissiveMap).toBe('/art/shared/glow.png');
  });
});

describe('glTF prefab assembly', () => {
  const assemble = async (bytes: Uint8Array, options: Partial<PrefabAssembly> = {}) => {
    const { meshes, nodes } = await importGltfMeshes(bytes, 'model');
    return assembleModelPrefab('model', meshes, { nodes, ...options });
  };
  const oneNode = { nodes: [{ mesh: 0 }], scenes: [{ nodes: [0] }], scene: 0 };

  it('draws a single-primitive node from the node itself', async () => {
    const prefab = await assemble(gltf({ ...withInlineImage(), ...oneNode }),
                            { refs: { prefix: 'assets/models/' } });
    expect(prefab.entities).toHaveLength(1);
    expect(prefab.rootEntityId).toBe('n0');
    expect(prefab.entities[0]!.components[1]).toEqual({
      type: 'MeshRenderer',
      data: {
        mesh: 'assets/models/model.esmesh',
        texture: 'assets/models/model_0.png',
        color: { r: 0.25, g: 0.5, b: 0.75, a: 1 },
        opaque: true,
        cullBackfaces: true,
        enabled: true,
      },
    });
  });

  it('points the root at a clip, stopped', async () => {
    const prefab = await assemble(gltf({ ...withInlineImage(), ...oneNode }),
                                  { timeline: 'assets/models/model_Spin.estimeline' });
    // Stopped: the products say what the model HAS. Playing it is the scene's.
    expect(prefab.entities[0]!.components.at(-1)).toEqual({
      type: 'TimelinePlayer',
      data: { timeline: 'assets/models/model_Spin.estimeline', playing: false },
    });
  });

  it('hangs a node’s further primitives off it', async () => {
    const two = [
      { attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, material: 0, mode: 4 },
      { attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, mode: 4 },
    ];
    const prefab = await assemble(gltf({ ...withInlineImage(), ...oneNode }, two));
    expect(prefab.entities.map(e => e.prefabEntityId)).toEqual(['n0', 'n0_p0', 'n0_p1']);
    expect(prefab.entities[0]!.children).toEqual(['n0_p0', 'n0_p1']);
    expect(prefab.entities[1]!.parent).toBe('n0');
    // The second primitive names no material: no texture, no tint, engine defaults.
    expect(prefab.entities[2]!.components[1]!.data).toEqual({
      mesh: 'model_0_1.esmesh', enabled: true,
    });  // no material named: engine defaults, blended and two-sided
  });

  it('places each node where the source puts it, parents included', async () => {
    const doc = {
      ...withInlineImage(),
      nodes: [
        { name: 'Model', translation: [40, 0, 0], children: [1, 2] },
        { name: 'Left', mesh: 0, translation: [-120, 60, 0] },
        { name: 'Right', mesh: 0, scale: [0.5, 0.5, 1] },
      ],
      scenes: [{ nodes: [0] }], scene: 0,
    };
    const prefab = await assemble(gltf(doc));
    expect(prefab.entities.map(e => e.name)).toEqual(['Model', 'Left', 'Right']);
    expect(prefab.entities[0]!.components[0]!.data)
      .toEqual({ position: { x: 40, y: 0, z: 0 } });
    expect(prefab.entities[1]!.components[0]!.data)
      .toEqual({ position: { x: -120, y: 60, z: 0 } });
    expect(prefab.entities[2]!.components[0]!.data)
      .toEqual({ scale: { x: 0.5, y: 0.5, z: 1 } });
    // One mesh drawn by two nodes is one product referenced twice.
    expect(prefab.entities[1]!.components[1]!.data.mesh)
      .toBe(prefab.entities[2]!.components[1]!.data.mesh);
  });

  it('decomposes a node given as a matrix', async () => {
    const doc = {
      ...withInlineImage(),
      // 90° about Z, scaled 2, moved to (10, 20, 30) — column-major.
      nodes: [{ mesh: 0, matrix: [0, 2, 0, 0, -2, 0, 0, 0, 0, 0, 2, 0, 10, 20, 30, 1] }],
      scenes: [{ nodes: [0] }], scene: 0,
    };
    const data = (await assemble(gltf(doc))).entities[0]!.components[0]!.data as {
      position: { x: number }; rotation: { z: number; w: number }; scale: { x: number };
    };
    expect(data.position).toEqual({ x: 10, y: 20, z: 30 });
    expect(data.scale.x).toBeCloseTo(2);
    expect(data.rotation.z).toBeCloseTo(Math.SQRT1_2);
    expect(data.rotation.w).toBeCloseTo(Math.SQRT1_2);
  });

  it('leaves out a normal map the geometry has no normals for', async () => {
    const doc = withInlineImage();
    doc.images = [
      { mimeType: 'image/png', uri: `data:image/png;base64,${PNG_1PX}` },
      { uri: 'bump.png' },
    ];
    doc.textures = [{ source: 0 }, { source: 1 }];
    (doc.materials as { normalTexture?: unknown }[])[0]!.normalTexture = { index: 1 };
    // The fixture's primitive declares no NORMAL, so the engine draws it unlit —
    // a normal map there would be a ref to something nothing reads.
    const prefab = await assemble(gltf({ ...doc, ...oneNode }));
    expect(prefab.entities[0]!.components[1]!.data.normalMap).toBeUndefined();
  });

  it('scales the root, since a glTF is in metres', async () => {
    const prefab = await assemble(gltf({ ...withInlineImage(), ...oneNode }), { scale: 32 });
    expect(prefab.entities[0]!.components[0]!.data)
      .toEqual({ scale: { x: 32, y: 32, z: 32 } });
  });

  it('lays the meshes under a holder when the file has no nodes', async () => {
    const prefab = await assemble(gltf(withInlineImage()));
    expect(prefab.entities.map(e => e.prefabEntityId)).toEqual(['root', 'm0']);
    expect(prefab.entities[1]!.parent).toBe('root');
  });

  it('spells an external image through the project resolver', async () => {
    const doc = withInlineImage();
    doc.images = [{ uri: '../textures/skin.png' }];
    const prefab = await assemble(gltf({ ...doc, ...oneNode }), {
      refs: {
        prefix: 'assets/models/',
        external: uri => `assets/${uri.replace('../', '')}`,
      },
    });
    expect(prefab.entities[0]!.components[1]!.data.texture).toBe('assets/textures/skin.png');
  });
});

/**
 * A glTF whose animation sampler data is appended to the geometry buffer.
 * Accessors 0-2 are the triangle; 3 is the sampler input, 4 its output.
 */
function animatedGltf(opts: {
  times: number[]; values: number[]; components: number;
  path?: string; interpolation?: string;
  nodes?: unknown[]; targetNode?: number; skins?: unknown[];
}): Uint8Array {
  const geo = geometryBuffer();
  const geoBytes = Buffer.from(geo.uri.split(',')[1]!, 'base64');
  const pad = Buffer.alloc((4 - (geoBytes.length % 4)) % 4);
  const times = Buffer.from(new Float32Array(opts.times).buffer);
  const values = Buffer.from(new Float32Array(opts.values).buffer);
  const bytes = Buffer.concat([geoBytes, pad, times, values]);
  const timesAt = geoBytes.length + pad.length;
  const type = ['SCALAR', '', 'VEC2', 'VEC3', 'VEC4'][opts.components]!;

  const doc = {
    asset: { version: '2.0' },
    buffers: [{ byteLength: bytes.length, uri: `data:application/octet-stream;base64,${bytes.toString('base64')}` }],
    bufferViews: [
      ...(geo.views as Record<string, unknown>[]),
      { buffer: 0, byteOffset: timesAt, byteLength: times.length },
      { buffer: 0, byteOffset: timesAt + times.length, byteLength: values.length },
    ],
    accessors: [
      ...(geo.accessors as Record<string, unknown>[]),
      { bufferView: 3, componentType: 5126, count: opts.times.length, type: 'SCALAR' },
      { bufferView: 4, componentType: 5126, count: opts.values.length / opts.components, type },
    ],
    meshes: [{ name: 'Tri', primitives: [{ attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, mode: 4 }] }],
    nodes: opts.nodes ?? [{ name: 'Root', children: [1] }, { name: 'Spinner', mesh: 0 }],
    scenes: [{ nodes: [0] }],
    scene: 0,
    ...(opts.skins ? { skins: opts.skins } : {}),
    animations: [{
      name: 'Spin',
      channels: [{ sampler: 0, target: { node: opts.targetNode ?? 1, path: opts.path ?? 'rotation' } }],
      samplers: [{ input: 3, output: 4, interpolation: opts.interpolation ?? 'LINEAR' }],
    }],
  };
  return new TextEncoder().encode(JSON.stringify(doc));
}

describe('glTF animation import', () => {
  const HALF = Math.SQRT1_2;

  it('turns a rotation channel into four component channels on the target node', async () => {
    const result = await importGltfMeshes(animatedGltf({
      times: [0, 1], values: [0, 0, 0, 1, 0, HALF, 0, HALF], components: 4,
    }), 'robot');

    expect(result.animations).toHaveLength(1);
    const doc = result.animations[0]!.document as any;
    expect(result.animations[0]!.name).toBe('robot_Spin');
    expect(doc.duration).toBe(1);
    expect(doc.tracks).toHaveLength(1);
    // The lone root IS the prefab root, so its child is addressed by its name.
    expect(doc.tracks[0].childPath).toBe('Spinner');
    expect(doc.tracks[0].component).toBe('Transform');
    expect(doc.tracks[0].channels.map((c: any) => c.property))
      .toEqual(['rotation.x', 'rotation.y', 'rotation.z', 'rotation.w']);
    const y = doc.tracks[0].channels[1].keyframes;
    expect(y[0]).toMatchObject({ time: 0, value: 0, interpolation: 'linear' });
    expect(y[1].value).toBeCloseTo(HALF, 6);
  });

  it('carries a CUBICSPLINE tangent into the keyframe it belongs to', async () => {
    // Per key: [inTangent, value, outTangent] — one scalar-per-component triple.
    const result = await importGltfMeshes(animatedGltf({
      times: [0, 1], components: 3, path: 'translation', interpolation: 'CUBICSPLINE',
      values: [
        0, 0, 0, /* v0 */ 0, 0, 0, /* out */ 2, 0, 0,
        3, 0, 0, /* v1 */ 10, 0, 0, /* out */ 0, 0, 0,
      ],
    }), 'robot');

    const x = (result.animations[0]!.document as any).tracks[0].channels[0].keyframes;
    expect(x[0]).toMatchObject({ value: 0, inTangent: 0, outTangent: 2, interpolation: 'hermite' });
    expect(x[1]).toMatchObject({ value: 10, inTangent: 3, outTangent: 0 });
  });

  it('flips a quaternion keyframe that would interpolate the long way round', async () => {
    // 170° about Y, then the SAME rotation written negated: component-wise that
    // is a 190° turn backwards unless the sign is aligned.
    const s = Math.sin((170 * Math.PI) / 180 / 2);
    const c = Math.cos((170 * Math.PI) / 180 / 2);
    const result = await importGltfMeshes(animatedGltf({
      times: [0, 1], components: 4, values: [0, s, 0, c, -0, -s, -0, -c],
    }), 'robot');

    const ch = (result.animations[0]!.document as any).tracks[0].channels;
    const yEnd = ch[1].keyframes[1].value;
    const wEnd = ch[3].keyframes[1].value;
    expect(yEnd).toBeCloseTo(s, 6);
    expect(wEnd).toBeCloseTo(c, 6);
  });

  it('gives two siblings of the same name distinct paths', async () => {
    const result = await importGltfMeshes(animatedGltf({
      times: [0], components: 3, path: 'translation', values: [1, 2, 3], targetNode: 2,
      nodes: [
        { name: 'Root', children: [1, 2] },
        { name: 'Arm', mesh: 0 },
        { name: 'Arm' },
      ],
    }), 'robot');

    const paths = result.nodes[0]!.children.map(n => n.name);
    expect(paths).toEqual(['Arm', 'Arm_2']);
    expect((result.animations[0]!.document as any).tracks[0].childPath).toBe('Arm_2');
  });

  it('says a channel aimed at a skinned mesh node moves nothing', async () => {
    const result = await importGltfMeshes(animatedGltf({
      times: [0, 1], components: 4, values: [0, 0, 0, 1, 0, HALF, 0, HALF],
      skins: [{ joints: [1] }],
      nodes: [{ name: 'Root', children: [1] }, { name: 'Spinner', mesh: 0, skin: 0 }],
    }), 'robot');

    expect(result.animations).toHaveLength(1);
    expect(result.warnings.some(w => /moves nothing/.test(w))).toBe(true);
  });

  it('reports a weights channel aimed at geometry with no shapes', async () => {
    const result = await importGltfMeshes(animatedGltf({
      times: [0, 1], components: 1, path: 'weights', values: [0, 1],
    }), 'robot');

    // Not dropped in silence, and not a track either: the channel is about a
    // shape the mesh it draws does not have.
    expect(result.animations).toHaveLength(0);
    expect(result.warnings.some(w => /draws nothing with morph targets/.test(w))).toBe(true);
  });
});

/**
 * The triangle bound to two joints. Accessors 3/4 are JOINTS_0 (u16) and
 * WEIGHTS_0; 5 is the skin's inverse bind matrices unless the caller drops it.
 */
function skinnedGltf(opts: {
  noBindMatrices?: boolean; skinOnNode?: boolean; weights?: boolean; extraJoints?: number;
} = {}): Uint8Array {
  const geo = geometryBuffer();
  const geoBytes = Buffer.from(geo.uri.split(',')[1]!, 'base64');
  const pad = Buffer.alloc((4 - (geoBytes.length % 4)) % 4);
  const joints = Buffer.from(new Uint16Array([0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0]).buffer);
  const weights = Buffer.from(new Float32Array([
    0.75, 0.25, 0, 0, 1, 0, 0, 0, 0.5, 0.5, 0, 0,
  ]).buffer);
  // Joint 1's bind pose sits 30 along x; joint 0's is identity.
  const bind = new Float32Array(32);
  for (let j = 0; j < 2; j++) for (let c = 0; c < 4; c++) bind[j * 16 + c * 5] = 1;
  bind[16 + 12] = 30;
  const parts = [geoBytes, pad, joints, weights, Buffer.from(bind.buffer)];
  const bytes = Buffer.concat(parts);
  let at = geoBytes.length + pad.length;
  const views: Record<string, unknown>[] = [];
  for (const p of [joints, weights, Buffer.from(bind.buffer)]) {
    views.push({ buffer: 0, byteOffset: at, byteLength: p.length });
    at += p.length;
  }

  const extra = Array.from({ length: opts.extraJoints ?? 0 });
  const doc: Record<string, unknown> = {
    asset: { version: '2.0' },
    buffers: [{ byteLength: bytes.length, uri: `data:application/octet-stream;base64,${bytes.toString('base64')}` }],
    bufferViews: [...(geo.views as Record<string, unknown>[]), ...views],
    accessors: [
      ...(geo.accessors as Record<string, unknown>[]),
      { bufferView: 3, componentType: 5123, count: 3, type: 'VEC4' },
      { bufferView: 4, componentType: 5126, count: 3, type: 'VEC4' },
      { bufferView: 5, componentType: 5126, count: 2, type: 'MAT4' },
    ],
    meshes: [{
      name: 'Tri',
      primitives: [{
        attributes: {
          POSITION: 0, TEXCOORD_0: 1, JOINTS_0: 3,
          ...(opts.weights === false ? {} : { WEIGHTS_0: 4 }),
        },
        indices: 2, mode: 4,
      }],
    }],
    nodes: [
      { name: 'Rig', children: [1, 2, 3, ...extra.map((_, i) => 4 + i)] },
      { name: 'Hip' },
      { name: 'Knee', translation: [30, 0, 0] },
      { name: 'Body', mesh: 0, ...(opts.skinOnNode === false ? {} : { skin: 0 }) },
      ...extra.map((_, i) => ({ name: `Spare${i}` })),
    ],
    scenes: [{ nodes: [0] }],
    scene: 0,
    skins: [{
      joints: [1, 2, ...extra.map((_, i) => 4 + i)],
      // A longer joint list than the accessor has matrices for is its own
      // (already covered) warning, so drop the accessor when the list grows.
      ...(opts.noBindMatrices || extra.length > 0 ? {} : { inverseBindMatrices: 5 }),
    }],
  };
  return new TextEncoder().encode(JSON.stringify(doc));
}

describe('glTF skinning import', () => {
  it('carries the joint channel, the weights and the bind pose', async () => {
    const { meshes, warnings } = await importGltfMeshes(skinnedGltf(), 'rig');
    const mesh = meshes[0]!;

    const joints = mesh.data.channels.find((c) => c.semantic === MeshChannel.Joints);
    const weights = mesh.data.channels.find((c) => c.semantic === MeshChannel.Weights);
    expect(joints).toMatchObject({ components: 4, type: 2 /* UInt16 */ });
    expect(weights).toMatchObject({ components: 4, type: 0 /* Float32 */ });

    const dv = new DataView(mesh.data.vertices.buffer, mesh.data.vertices.byteOffset);
    expect(dv.getUint16(joints!.offset + 2, true)).toBe(1);
    expect(dv.getFloat32(weights!.offset, true)).toBeCloseTo(0.75, 6);

    // The bind pose comes from the SKIN, and the joints are its node list.
    expect(mesh.data.inverseBindMatrices).toHaveLength(32);
    expect(mesh.data.inverseBindMatrices![16 + 12]).toBe(30);
    expect(mesh.skinJoints).toEqual([1, 2]);
    expect(warnings.filter((w) => /skin/i.test(w))).toEqual([]);
  });

  it('treats a missing bind accessor as an identity bind pose', async () => {
    const { meshes } = await importGltfMeshes(skinnedGltf({ noBindMatrices: true }), 'rig');
    const bind = meshes[0]!.data.inverseBindMatrices!;
    expect(bind[0]).toBe(1);
    expect(bind[16 + 12]).toBe(0);
  });

  it('imports static, and says so, when nothing binds the joints', async () => {
    for (const opts of [{ skinOnNode: false }, { weights: false }]) {
      const { meshes, warnings } = await importGltfMeshes(skinnedGltf(opts), 'rig');
      expect(meshes[0]!.data.channels.some((c) => c.semantic === MeshChannel.Joints)).toBe(false);
      expect(meshes[0]!.skinJoints).toBeUndefined();
      expect(warnings.some((w) => /JOINTS_0 without/.test(w))).toBe(true);
    }
  });

  it('imports static, and says the budget, when a skin binds more joints than a draw can pose', async () => {
    // The joint indices in the vertices index the SKIN's list, so a list longer
    // than the pose block means indices no uploaded matrix answers to. Drawn
    // wrong, the vertices go somewhere arbitrary; this is the file saying so.
    const over = MESH_MAX_BONES + 1;
    const { meshes, warnings } = await importGltfMeshes(
      skinnedGltf({ extraJoints: over - 2 }), 'rig');
    expect(meshes[0]!.data.channels.some((c) => c.semantic === MeshChannel.Joints)).toBe(false);
    expect(meshes[0]!.data.inverseBindMatrices).toBeUndefined();
    expect(meshes[0]!.skinJoints).toBeUndefined();
    expect(warnings.some((w) => w.includes(`binds ${over} joints`)
      && w.includes(`${MESH_MAX_BONES}`) && /imported static/.test(w))).toBe(true);
  });

  it('poses a skin that fills the budget exactly', async () => {
    // The boundary itself, so the check cannot be an off-by-one that quietly
    // costs a rig its last bone.
    const { meshes, warnings } = await importGltfMeshes(
      skinnedGltf({ extraJoints: MESH_MAX_BONES - 2 }), 'rig');
    expect(meshes[0]!.skinJoints).toHaveLength(MESH_MAX_BONES);
    expect(warnings.some((w) => /imported static/.test(w))).toBe(false);
  });

  it('names the joint entities the prefab gives those nodes', async () => {
    const { meshes, nodes } = await importGltfMeshes(skinnedGltf(), 'rig');
    const prefab = assembleModelPrefab('rig', meshes, { nodes });
    const body = prefab.entities.find((e) => e.name === 'Body')!;
    const skin = body.components.find((c) => c.type === 'MeshSkin');
    expect(skin?.data).toEqual({ joints: ['n1', 'n2'] });
    // Those ids are entities the same prefab carries — a joint list pointing
    // outside it would resolve to nothing at instantiation.
    const ids = prefab.entities.map((e) => e.prefabEntityId);
    expect(ids).toEqual(expect.arrayContaining(['n1', 'n2']));
  });
});

describe('meshopt-compressed geometry', () => {
  it('imports to the very same mesh the uncompressed file does', async () => {
    // The claim is not what the codec does — it is that compression is invisible
    // above the bufferView, which is the whole reason it can be decoded there.
    const plain = (await importGltfMeshes(plainTriangle(), 'model')).meshes[0]!;
    const packed = (await importGltfMeshes(await meshoptTriangle(), 'model')).meshes[0]!;
    expect(packed.vertexCount).toBe(plain.vertexCount);
    expect(packed.triangleCount).toBe(plain.triangleCount);
    expect([...packed.data.vertices]).toEqual([...plain.data.vertices]);
    expect([...packed.data.indices]).toEqual([...plain.data.indices]);
    expect(packed.data.channels).toEqual(plain.data.channels);
  });

  it('says nothing about it — a fallback buffer is not a missing one', async () => {
    // It declares a length and no uri by design; reporting it as unreadable would
    // send a user looking for a file that is not supposed to exist.
    const { warnings } = await importGltfMeshes(await meshoptTriangle(), 'model');
    expect(warnings).toEqual([]);
  });
});

describe('Draco-compressed geometry', () => {
  /** Every vertex as one "x,y,z/u,v" string, sorted — Draco reorders vertices to
   *  compress them, so what survives is the set of them, not their order. */
  const vertexSet = (mesh: ImportedMesh): string[] => {
    const view = new DataView(mesh.data.vertices.buffer, mesh.data.vertices.byteOffset);
    const at = (semantic: number): number =>
      mesh.data.channels.find(c => c.semantic === semantic)!.offset;
    const out: string[] = [];
    for (let i = 0; i < mesh.vertexCount; i++) {
      const base = i * mesh.data.vertexStride;
      const read = (offset: number, n: number): string => Array.from({ length: n },
        (_, c) => view.getFloat32(base + offset + c * 4, true).toFixed(3)).join(',');
      out.push(`${read(at(MeshChannel.Position), 3)}/${read(at(MeshChannel.TexCoord0), 2)}`);
    }
    return out.sort();
  };

  it('imports to the same triangle the uncompressed file does', async () => {
    const plain = (await importGltfMeshes(plainTriangle(), 'model')).meshes[0]!;
    const packed = (await importGltfMeshes(await dracoTriangle(), 'model')).meshes[0]!;
    expect(packed.vertexCount).toBe(plain.vertexCount);
    expect(packed.triangleCount).toBe(plain.triangleCount);
    expect(vertexSet(packed)).toEqual(vertexSet(plain));
  });

  it('reads it without complaint, accessors and all', async () => {
    // A Draco accessor has no bufferView by spec. Read as an ordinary one it is
    // "no data" — the check that catches a genuinely empty POSITION.
    const { warnings, meshes } = await importGltfMeshes(await dracoTriangle(), 'model');
    expect(warnings).toEqual([]);
    expect(meshes).toHaveLength(1);
  });
});

describe('a second UV set', () => {
  it('reaches the mesh, distinct from the first', async () => {
    const result = await importGltfMeshes(gltfWithSecondUv(), 'twoUv');
    const mesh = result.meshes[0]!;
    const uv0 = uvsOf(mesh, MeshChannel.TexCoord0);
    const uv1 = uvsOf(mesh, MeshChannel.TexCoord1);
    expect(uv1).toHaveLength(6);
    expect(uv1).not.toEqual(uv0);
    // Both sets are V-flipped at this boundary, as UV0 always has been: two sets
    // disagreeing about which way is up would be worse than one.
    expect(uv1).toEqual([0.5, 0.75, 0.75, 0.75, 0.5, 0.5]);
  });

  it('is absent from a mesh whose source has one set', async () => {
    const result = await importGltfMeshes(gltf({}), 'oneUv');
    const mesh = result.meshes[0]!;
    // Not "zeroes in a channel nobody wrote": a file states what it has, so a
    // mesh without a second set stays a mesh without one.
    expect(mesh.data.channels.some(c => c.semantic === MeshChannel.TexCoord1)).toBe(false);
    expect(uvsOf(mesh, MeshChannel.TexCoord1)).toEqual([]);
  });
});

/**
 * A triangle with two shapes to blend towards: the first moves one vertex, the
 * second moves another AND turns its normal. The two differ on purpose — a
 * reader that mixed the targets up would still find deltas, just the wrong ones.
 */
function morphGltf(opts: { names?: string[]; badCount?: boolean } = {}): Uint8Array {
  const positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]);
  const normals = new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1]);
  const uv = new Float32Array([0, 0, 1, 0, 0, 1]);
  const indices = new Uint16Array([0, 1, 2]);
  const shape0 = new Float32Array([0, 0, 0, 0, 2, 0, 0, 0, 0]);
  const shape1 = new Float32Array([0, 0, 0, 0, 0, 0, 3, 0, 0]);
  const shape1Normals = new Float32Array([0, 0, 0, 0, 0, 0, 0, 1, -1]);
  const parts = [positions, normals, uv, indices, shape0, shape1, shape1Normals];
  const bytes = Buffer.concat(parts.map((a) => Buffer.from(a.buffer)));
  const offsets: number[] = [];
  let at = 0;
  for (const part of parts) { offsets.push(at); at += part.byteLength; }

  const doc = {
    asset: { version: '2.0' },
    buffers: [{ byteLength: bytes.length, uri: `data:application/octet-stream;base64,${bytes.toString('base64')}` }],
    bufferViews: parts.map((part, i) => ({
      buffer: 0, byteOffset: offsets[i], byteLength: part.byteLength,
    })),
    accessors: [
      { bufferView: 0, componentType: 5126, count: 3, type: 'VEC3' },
      { bufferView: 1, componentType: 5126, count: 3, type: 'VEC3' },
      { bufferView: 2, componentType: 5126, count: 3, type: 'VEC2' },
      { bufferView: 3, componentType: 5123, count: 3, type: 'SCALAR' },
      { bufferView: 4, componentType: 5126, count: opts.badCount ? 2 : 3, type: 'VEC3' },
      { bufferView: 5, componentType: 5126, count: 3, type: 'VEC3' },
      { bufferView: 6, componentType: 5126, count: 3, type: 'VEC3' },
    ],
    meshes: [{
      name: 'Face',
      ...(opts.names ? { extras: { targetNames: opts.names } } : {}),
      primitives: [{
        attributes: { POSITION: 0, NORMAL: 1, TEXCOORD_0: 2 }, indices: 3, mode: 4,
        targets: [{ POSITION: 4 }, { POSITION: 5, NORMAL: 6 }],
      }],
    }],
    nodes: [{ mesh: 0 }], scenes: [{ nodes: [0] }], scene: 0,
  };
  return new TextEncoder().encode(JSON.stringify(doc));
}

describe('glTF morph targets', () => {
  it('carries the deltas each target moves its vertices by', async () => {
    const { meshes, warnings } = await importGltfMeshes(
      morphGltf({ names: ['Smile', 'Blink'] }), 'face');

    const morph = meshes[0]!.data.morph!;
    expect(morph.names).toEqual(['Smile', 'Blink']);
    expect(warnings.join('\n')).not.toMatch(/morph target/);

    // Six floats per vertex — the geometry has normals and the second target
    // bends them, so every target is stored with room for one.
    expect(morph.hasNormals).toBe(true);
    expect(morph.deltas).toHaveLength(2 * 3 * 6);
    // Target 0 lifts vertex 1 by 2 along y and touches nothing else.
    expect(morph.deltas[1 * 6 + 1]).toBe(2);
    expect([...morph.deltas.subarray(0, 18)].filter((d) => d !== 0)).toHaveLength(1);
    // Target 1 moves vertex 2 along x and turns its normal.
    expect(morph.deltas[18 + 2 * 6]).toBe(3);
    expect(morph.deltas[18 + 2 * 6 + 4]).toBe(1);
    expect(morph.deltas[18 + 2 * 6 + 5]).toBe(-1);
  });

  it('names a target the file left unnamed by its position', async () => {
    const { meshes } = await importGltfMeshes(morphGltf(), 'face');
    // Positional because a weight is: an unnamed target still has to be
    // addressable, and calling it nothing would renumber the ones behind it.
    expect(meshes[0]!.data.morph?.names).toEqual(['target 0', 'target 1']);
  });

  it('keeps a target whose deltas do not cover every vertex, saying it moves nothing', async () => {
    const { meshes, warnings } = await importGltfMeshes(
      morphGltf({ names: ['Smile', 'Blink'], badCount: true }), 'face');

    const morph = meshes[0]!.data.morph!;
    expect(warnings.join('\n')).toContain('"Smile" carries 2 POSITION deltas for 3 vertices');
    // Kept, not dropped: weights address targets by position, so removing one
    // would re-aim every weight behind it at the wrong shape.
    expect(morph.names).toEqual(['Smile', 'Blink']);
    expect([...morph.deltas.subarray(0, 18)]).toEqual(new Array(18).fill(0));
    expect(morph.deltas[18 + 2 * 6]).toBe(3);
  });

  it('turns a weights channel into a track per shape, on the entity that holds them', async () => {
    const doc = JSON.parse(new TextDecoder().decode(morphGltf({ names: ['Smile', 'Blink'] })));
    // Two keyframes moving the SECOND target only: a reader that mixed the two up
    // would still find a track, driving the wrong shape.
    const raw = Buffer.from(doc.buffers[0].uri.split(',')[1], 'base64');
    const extra = Buffer.from(new Float32Array([0, 1, /* weights */ 0, 0, 0, 1]).buffer);
    doc.buffers[0].uri = 'data:application/octet-stream;base64,'
      + Buffer.concat([raw, extra]).toString('base64');
    doc.buffers[0].byteLength = raw.length + extra.length;
    doc.bufferViews.push({ buffer: 0, byteOffset: raw.length, byteLength: 8 });
    doc.bufferViews.push({ buffer: 0, byteOffset: raw.length + 8, byteLength: 16 });
    doc.accessors.push({ bufferView: doc.bufferViews.length - 2, componentType: 5126, count: 2, type: 'SCALAR' });
    doc.accessors.push({ bufferView: doc.bufferViews.length - 1, componentType: 5126, count: 4, type: 'SCALAR' });
    doc.animations = [{
      name: 'Express',
      samplers: [{ input: doc.accessors.length - 2, output: doc.accessors.length - 1 }],
      channels: [{ sampler: 0, target: { node: 0, path: 'weights' } }],
    }];

    const { animations, warnings } = await importGltfMeshes(
      new TextEncoder().encode(JSON.stringify(doc)), 'face');

    expect(warnings.join('\n')).not.toMatch(/moves nothing/);
    const tracks = (animations[0]!.document as { tracks: Record<string, unknown>[] }).tracks;
    expect(tracks).toHaveLength(1);
    expect(tracks[0]!.component).toBe('MeshMorph');
    // One channel per target, addressed the way a weight is — by position.
    const channels = tracks[0]!.channels as { property: string; keyframes: { value: number }[] }[];
    expect(channels.map((c) => c.property)).toEqual(['weights.0', 'weights.1']);
    expect(channels[0]!.keyframes.map((k) => k.value)).toEqual([0, 0]);
    expect(channels[1]!.keyframes.map((k) => k.value)).toEqual([0, 1]);
  });

  it('leaves geometry with no targets without a morph section', async () => {
    const { meshes } = await importGltfMeshes(gltf(withInlineImage()), 'model');
    expect(meshes[0]!.data.morph).toBeUndefined();
  });
});

describe('lights and cameras', () => {
  it('KHR_lights_punctual and cameras land on their nodes, and on the prefab', async () => {
    const doc = {
      nodes: [
        { name: 'Sun', extensions: { KHR_lights_punctual: { light: 0 } } },
        { name: 'Lamp', translation: [0, 2, 0], extensions: { KHR_lights_punctual: { light: 1 } } },
        { name: 'Eye', camera: 0 },
        { name: 'Plan', camera: 1 },
      ],
      scenes: [{ nodes: [0, 1, 2, 3] }],
      extensionsUsed: ['KHR_lights_punctual'],
      extensions: { KHR_lights_punctual: { lights: [
        { type: 'directional', color: [1, 0.9, 0.8], intensity: 3 },
        { type: 'spot', intensity: 20, range: 12, spot: { innerConeAngle: Math.PI / 8, outerConeAngle: Math.PI / 4 } },
      ] } },
      cameras: [
        { type: 'perspective', perspective: { yfov: Math.PI / 3, znear: 0.05, zfar: 200, aspectRatio: 1.5 } },
        { type: 'orthographic', orthographic: { xmag: 4, ymag: 3, znear: 0.1, zfar: 50 } },
      ],
    };
    const { meshes, nodes } = await importGltfMeshes(gltf(doc), 'model');
    const byName = (n: string) => nodes.find((x) => x.name === n)!;
    expect(byName('Sun').light).toEqual({ type: 'directional', color: [1, 0.9, 0.8], intensity: 3 });
    const lamp = byName('Lamp').light!;
    expect(lamp.range).toBe(12);
    // Half angles in radians become full angles in degrees.
    expect(lamp.innerAngle).toBeCloseTo(45);
    expect(lamp.outerAngle).toBeCloseTo(90);
    expect(byName('Eye').camera).toMatchObject({ projection: 'perspective', near: 0.05, far: 200, aspect: 1.5 });
    expect(byName('Eye').camera!.yfov).toBeCloseTo(60);
    expect(byName('Plan').camera).toEqual({ projection: 'orthographic', halfHeight: 3, near: 0.1, far: 50 });

    const prefab = assembleModelPrefab('model', meshes, { nodes });
    const comp = (name: string, type: string) =>
      prefab.entities.find((e) => e.name === name)!.components.find((c) => c.type === type)?.data;
    expect(comp('Sun', 'Light')).toEqual({ type: 1, color: { r: 1, g: 0.9, b: 0.8, a: 1 }, intensity: 3 });
    // A punctual lamp is in candela and falls off as 1/d²: an inverse-square Light at candela over pi, its range in metres.
    expect(comp('Lamp', 'Light')).toMatchObject({ type: 3, radius: 12, inverseSquare: true });
    expect((comp('Lamp', 'Light') as { intensity: number }).intensity).toBeCloseTo(20 / Math.PI);
    // Imported into a centimetre project, the range is scaled with the model; the falloff measures in metres anyway.
    const scaled = assembleModelPrefab('model', meshes, { nodes, scale: 100 });
    expect(scaled.entities.find((e) => e.name === 'Lamp')!.components.find((c) => c.type === 'Light')!.data)
        .toMatchObject({ radius: 1200, inverseSquare: true });
    // A model's camera never takes over the scene it is dropped into.
    expect(comp('Eye', 'Camera')).toMatchObject({ projectionType: 0, nearPlane: 0.05, farPlane: 200, isActive: false });
    expect(comp('Plan', 'Camera')).toMatchObject({ projectionType: 1, orthoSize: 3 });
  });
});