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.

9.8 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  A model's DDS images arrive as PNG, and maps whose pixels contradict their slot are called out.
 */
import { describe, it, expect } from 'vitest';
import { PNG } from 'pngjs';
import { prepareModelImages } from '../src/assets/modelImages';
import { encodeRgbaPng } from '../src/assets/png';
import { importedImageSettings, type ImportedMaterial, type ModelImportResult } from '../src/assets/modelImport';

/** An 8x8 uncompressed BGRA DDS, every texel `bgra`. */
function dds(bgra: number[]): Uint8Array {
    const head = new Uint8Array(128);
    const v = new DataView(head.buffer);
    v.setUint32(0, 0x20534444, true);
    v.setUint32(4, 124, true);
    v.setUint32(12, 8, true);
    v.setUint32(16, 8, true);
    v.setUint32(80, 0x41, true);
    v.setUint32(88, 32, true);
    v.setUint32(92, 0x00ff0000, true);
    return new Uint8Array([...head, ...new Array(64).fill(bgra).flat()]);
}

function png(pixel: (i: number) => number[]): Uint8Array {
    const rgba = new Uint8Array(8 * 8 * 4);
    for (let i = 0; i < 64; i++) rgba.set(pixel(i), i * 4);
    return encodeRgbaPng(8, 8, rgba);
}

function model(material: Partial<ImportedMaterial>, externalFiles: string[] = []): ModelImportResult {
    const mat = { index: 0, name: 'm', baseColor: [1, 1, 1, 1], opaque: true, cullBackfaces: true, ...material } as ImportedMaterial;
    return {
        meshes: [{ material: mat } as ModelImportResult['meshes'][number]],
        textures: [], externalFiles, nodes: [], animations: [], warnings: [],
    };
}

describe('preparing a model\'s images', () => {
    it('retains specular RGB and linear gloss alpha while scheduling one explicit shader decode', () => {
        const packed = png(i => [40 + i, 100, 180, 70 + i]);
        const result = model({
            specularGlossiness: { specular: [1, 1, 1], glossiness: 1 },
            specularGlossinessTexture: { file: 'packed.png', external: true, settings: { sRGB: true, wrapMode: 'clamp' } },
        }, ['packed.png']);
        prepareModelImages(result, () => packed);
        expect(result.meshes[0]!.material!.specularGlossinessTexture).toEqual({ file: 'packed.png', external: true, settings: { sRGB: false, wrapMode: 'clamp' } });
        expect(importedImageSettings(result.meshes).get('packed.png')?.settings).toMatchObject({ sRGB: false, wrapMode: 'clamp' });
        expect(result.textures).toEqual([]);
        expect(result.externalFiles).toEqual(['packed.png']);
    });

    it('turns an external DDS into an embedded PNG, keeping its sampler settings', () => {
        const files: Record<string, Uint8Array> = { 'Textures/Wall.dds': dds([30, 20, 10, 255]) };
        const result = model({ baseColorTexture: { file: 'Textures/Wall.dds', external: true, settings: { wrapMode: 'clamp' } } },
            ['scene.bin', 'Textures/Wall.dds']);
        prepareModelImages(result, uri => files[uri] ?? null);

        expect(result.meshes[0]!.material!.baseColorTexture).toEqual({ file: 'Wall.png', external: false, settings: { wrapMode: 'clamp' } });
        expect(result.externalFiles).toEqual(['scene.bin']);
        const decoded = PNG.sync.read(Buffer.from(result.textures[0]!.bytes));
        expect([...decoded.data.subarray(0, 4)]).toEqual([10, 20, 30, 255]);
    });

    it('packs separate metalness, roughness and occlusion maps the way glTF does', () => {
        const files: Record<string, Uint8Array> = {
            'metal.png': png(() => [200, 0, 0, 255]),
            'rough.png': png(() => [60, 0, 0, 255]),
            'ao.png': png(() => [150, 0, 0, 255]),
        };
        const result = model({
            metallic: 0.3, roughness: 0.5,
            separateMaps: {
                metalness: { file: 'metal.png', external: true }, roughness: { file: 'rough.png', external: true },
                occlusion: { file: 'ao.png', external: true },
            },
        }, ['metal.png', 'rough.png', 'ao.png']);
        prepareModelImages(result, uri => files[uri] ?? null);
        const material = result.meshes[0]!.material!;
        expect(material.separateMaps).toBeUndefined();
        expect(material.metallicRoughnessTexture).toEqual({ file: 'm_orm.png', external: false, settings: { sRGB: false } });
        expect(material.occlusionTexture?.file).toBe('m_orm.png');
        // The factors step aside, since the maps now say it.
        expect(material.metallic).toBe(1);
        expect(material.roughness).toBe(1);
        const packed = PNG.sync.read(Buffer.from(result.textures.find(t => t.name === 'm_orm.png')!.bytes));
        expect([...packed.data.subarray(0, 4)]).toEqual([150, 60, 200, 255]);
    });

    it('inverts a glossiness map, and leaves a channel with no image at full so the factor speaks', () => {
        const files: Record<string, Uint8Array> = { 'gloss.png': png(() => [230, 0, 0, 255]) };
        const result = model({ metallic: 0.3, roughness: 0.5, separateMaps: { roughness: { file: 'gloss.png', external: true }, roughnessIsGlossiness: true } },
            ['gloss.png']);
        prepareModelImages(result, uri => files[uri] ?? null);
        const material = result.meshes[0]!.material!;
        expect(material.metallic).toBe(0.3);
        expect(material.roughness).toBe(1);
        const packed = PNG.sync.read(Buffer.from(result.textures[0]!.bytes));
        expect([...packed.data.subarray(0, 4)]).toEqual([255, 25, 255, 255]);
    });

    it('says so when a map cannot be read, and keeps the constants', () => {
        const files: Record<string, Uint8Array> = { 'metal.png': png(() => [200, 0, 0, 255]), 'rough.jpg': new Uint8Array([1, 2, 3]) };
        const result = model({ metallic: 0.3, roughness: 0.5, separateMaps: { metalness: { file: 'metal.png', external: true }, roughness: { file: 'rough.jpg', external: true } } },
            ['metal.png', 'rough.jpg']);
        prepareModelImages(result, uri => files[uri] ?? null);
        const material = result.meshes[0]!.material!;
        expect(material.metallicRoughnessTexture).toBeUndefined();
        expect(result.warnings.join('\n')).toMatch(/could not be packed/);
    });

    it('drops an occlusion map that is zero everywhere, and says so', () => {
        const result = model({ occlusionTexture: { file: 'orm.png', external: true }, occlusionStrength: 1 });
        prepareModelImages(result, () => png(i => [0, 100 + i, 0, 255]));
        expect(result.meshes[0]!.material!.occlusionTexture).toBeUndefined();
        expect(result.meshes[0]!.material!.occlusionStrength).toBeUndefined();
        expect(result.warnings.join('\n')).toMatch(/occlusion map orm\.png is zero in every texel/);
    });

    it('keeps an occlusion map with any light in it', () => {
        const result = model({ occlusionTexture: { file: 'orm.png', external: true } });
        prepareModelImages(result, () => png(i => [i === 5 ? 1 : 0, 0, 0, 255]));
        expect(result.meshes[0]!.material!.occlusionTexture?.file).toBe('orm.png');
    });

    it('notices a map that is one colour, and still imports it', () => {
        const result = model({ metallicRoughnessTexture: { file: 'Spec.dds', external: true } });
        prepareModelImages(result, () => dds([0, 188, 0, 255]));
        expect(result.meshes[0]!.material!.metallicRoughnessTexture?.file).toBe('Spec.png');
        expect(result.warnings).toEqual(['Spec.dds is one colour (0, 188, 0, 255) across 8x8 — a material constant would say the same']);
    });

    it('reads an FBX base colour\'s alpha as a cutout, a see-through surface, or nothing', () => {
        const run = (alpha: (i: number) => number, alphaFromBaseColor = true) => {
            const result = model({ baseColorTexture: { file: 'leaf.png', external: true } });
            prepareModelImages(result, () => png(i => [90, 120, 40, alpha(i)]), { alphaFromBaseColor });
            const m = result.meshes[0]!.material!;
            return { cutoff: m.alphaCutoff, opaque: m.opaque, mipCoverage: m.baseColorTexture?.settings?.mipCoverage };
        };
        // A cutout's mips are told the cutoff, so distance does not thin it.
        expect(run(i => (i % 4 === 0 ? 0 : 255))).toEqual({ cutoff: 0.5, opaque: true, mipCoverage: 0.5 });
        expect(run(() => 128)).toEqual({ cutoff: undefined, opaque: false, mipCoverage: undefined });
        expect(run(() => 255)).toEqual({ cutoff: undefined, opaque: true, mipCoverage: undefined });
        // A glTF says its alpha mode itself; an OPAQUE one ignores the channel.
        expect(run(i => (i % 4 === 0 ? 0 : 255), false)).toEqual({ cutoff: undefined, opaque: true, mipCoverage: undefined });
    });

    it('marks the maps that hold numbers as not sRGB, and leaves colour alone', () => {
        const result = model({
            baseColorTexture: { file: 'base.jpg', external: true },
            emissiveTexture: { file: 'glow.jpg', external: true },
            normalTexture: { file: 'n.jpg', external: true, settings: { wrapMode: 'clamp' } },
            metallicRoughnessTexture: { file: 'mr.jpg', external: true },
            occlusionTexture: { file: 'ao.jpg', external: true },
        });
        prepareModelImages(result, () => null);
        const m = result.meshes[0]!.material!;
        expect([m.baseColorTexture?.settings, m.emissiveTexture?.settings]).toEqual([undefined, undefined]);
        expect(m.normalTexture?.settings).toEqual({ wrapMode: 'clamp', sRGB: false });
        expect([m.metallicRoughnessTexture?.settings, m.occlusionTexture?.settings])
            .toEqual([{ sRGB: false }, { sRGB: false }]);
    });

    it('skips a DDS it cannot read rather than referencing a file nothing loads', () => {
        const result = model({ normalTexture: { file: 'gone.dds', external: true } }, ['gone.dds']);
        prepareModelImages(result, () => null);
        expect(result.meshes[0]!.material!.normalTexture).toBeUndefined();
        expect(result.warnings.join('\n')).toMatch(/gone\.dds could not be read/);
    });
});