mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
feat: faster models
This commit is contained in:
1 parent
0f29145ab0
commit
e05f02099d
8 files changed
+77
-34
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import { toUniqueId } from '@blitzkit/core';
|
||||
import { NodeIO } from '@gltf-transform/core';
|
||||
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
|
||||
import draco3d from 'draco3dgltf';
|
||||
import { readdir } from 'fs/promises';
|
||||
import { extractModel } from '../core/blitz/extractModel';
|
||||
import { readXMLDVPL } from '../core/blitz/readXMLDVPL';
|
||||
@@ -13,7 +15,12 @@ import { TankParameters } from './tankIcons';
|
||||
export async function tankModels() {
|
||||
console.log('Building tank models...');
|
||||
|
||||
const nodeIO = new NodeIO();
|
||||
const nodeIO = new NodeIO()
|
||||
.registerExtensions(ALL_EXTENSIONS)
|
||||
.registerDependencies({
|
||||
'draco3d.decoder': await draco3d.createDecoderModule(),
|
||||
'draco3d.encoder': await draco3d.createEncoderModule(),
|
||||
});
|
||||
const nations = await readdir(`${DATA}/XML/item_defs/vehicles`).then(
|
||||
(nations) => nations.filter((nation) => nation !== 'common'),
|
||||
);
|
||||
@@ -32,6 +39,7 @@ export async function tankModels() {
|
||||
if (tankKey.includes('tutorial_bot')) return;
|
||||
|
||||
const id = toUniqueId(nation, tank.id);
|
||||
|
||||
const parameters = await readYAMLDVPL<TankParameters>(
|
||||
`${DATA}/3d/Tanks/Parameters/${nation}/${tankKey}.yaml.dvpl`,
|
||||
);
|
||||
|
||||
@@ -8,9 +8,6 @@ export const vertexAttributeGLTFName: Partial<Record<VertexAttribute, string>> =
|
||||
[VertexAttribute.TEXCOORD1]: 'TEXCOORD_0',
|
||||
[VertexAttribute.TEXCOORD2]: 'TEXCOORD_0',
|
||||
[VertexAttribute.TEXCOORD3]: 'TEXCOORD_0',
|
||||
[VertexAttribute.TANGENT]: 'TANGENT',
|
||||
[VertexAttribute.JOINTINDEX]: 'JOINT_0',
|
||||
[VertexAttribute.JOINTWEIGHT]: 'WEIGHT_0',
|
||||
};
|
||||
export const vertexAttributeGltfVectorSizes = {
|
||||
...vertexAttributeVectorSizes,
|
||||
|
||||
@@ -6,6 +6,8 @@ import {
|
||||
VertexAttribute,
|
||||
} from '@blitzkit/core';
|
||||
import { Document, Material, Node, Scene } from '@gltf-transform/core';
|
||||
import { KHRDracoMeshCompression } from '@gltf-transform/extensions';
|
||||
import { dedup, prune } from '@gltf-transform/functions';
|
||||
import { times } from 'lodash-es';
|
||||
import { dirname } from 'path';
|
||||
import { Matrix4, Quaternion, Vector3, Vector4Tuple } from 'three';
|
||||
@@ -223,7 +225,9 @@ export async function extractModel(data: string, path: string) {
|
||||
const primitive = document
|
||||
.createPrimitive()
|
||||
.setIndices(indicesAccessor)
|
||||
.setMaterial(material);
|
||||
.setMaterial(material)
|
||||
.setName(batchKey);
|
||||
|
||||
const attributes = new Map<VertexAttribute, number[][]>();
|
||||
|
||||
polygonGroup.vertices.forEach((vertex) => {
|
||||
@@ -236,25 +240,23 @@ export async function extractModel(data: string, path: string) {
|
||||
});
|
||||
});
|
||||
|
||||
if (attributes.has(VertexAttribute.TANGENT)) {
|
||||
attributes.set(
|
||||
VertexAttribute.TANGENT,
|
||||
attributes
|
||||
.get(VertexAttribute.TANGENT)!
|
||||
.map((tangent) => [...tangent, 1]),
|
||||
);
|
||||
}
|
||||
|
||||
attributes.forEach((value, attribute) => {
|
||||
const name = vertexAttributeGLTFName[attribute];
|
||||
if (!name || primitive.getAttribute(name)) return;
|
||||
const vertexSize = vertexAttributeGltfVectorSizes[attribute];
|
||||
|
||||
if (
|
||||
name === undefined ||
|
||||
primitive.getAttribute(name) !== null
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const vertexSize = vertexAttributeGltfVectorSizes[attribute];
|
||||
const attributeAccessor = document
|
||||
.createAccessor(name)
|
||||
.setType(vertexSize === 1 ? 'SCALAR' : `VEC${vertexSize}`)
|
||||
.setArray(new Float32Array(value.flat()))
|
||||
.setBuffer(buffer);
|
||||
|
||||
primitive.setAttribute(name, attributeAccessor);
|
||||
});
|
||||
|
||||
@@ -288,5 +290,19 @@ export async function extractModel(data: string, path: string) {
|
||||
|
||||
parseHierarchies(sc2['#hierarchy'], scene);
|
||||
|
||||
scene.addChild(document.createNode('test'));
|
||||
|
||||
await document.transform(prune({ keepAttributes: true }), dedup());
|
||||
|
||||
document
|
||||
.createExtension(KHRDracoMeshCompression)
|
||||
.setRequired(true)
|
||||
.setEncoderOptions({
|
||||
method: KHRDracoMeshCompression.EncoderMethod.EDGEBREAKER,
|
||||
encodeSpeed: 0,
|
||||
decodeSpeed: 0,
|
||||
quantizationVolume: 'scene',
|
||||
});
|
||||
|
||||
return document;
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
import { DdsReadStream, PvrReadStream } from '@blitzkit/core';
|
||||
import { existsSync } from 'fs';
|
||||
import sharp from 'sharp';
|
||||
import sharp, { JpegOptions } from 'sharp';
|
||||
import { readDVPLFile } from '../readDVPLFile';
|
||||
import { TextureMutation } from './constants';
|
||||
|
||||
const jpegOptions: JpegOptions = {
|
||||
quality: 20,
|
||||
mozjpeg: true,
|
||||
};
|
||||
|
||||
export async function readTexture(path: string, mutation?: TextureMutation) {
|
||||
const ddsTexturePath = path.replace('.tex', '.dx11.dds.dvpl');
|
||||
const isDds = existsSync(ddsTexturePath);
|
||||
@@ -35,7 +40,7 @@ export async function readTexture(path: string, mutation?: TextureMutation) {
|
||||
raw.data[index + 3] = 255;
|
||||
}
|
||||
|
||||
return await sharp(raw.data, { raw }).jpeg().toBuffer();
|
||||
return await sharp(raw.data, { raw }).jpeg(jpegOptions).toBuffer();
|
||||
}
|
||||
|
||||
case TextureMutation.RoughnessMetallicness: {
|
||||
@@ -44,16 +49,16 @@ export async function readTexture(path: string, mutation?: TextureMutation) {
|
||||
});
|
||||
const metallicness = sharp(raw.data, { raw })
|
||||
.extractChannel('green')
|
||||
.jpeg()
|
||||
.jpeg(jpegOptions)
|
||||
.toBuffer();
|
||||
const roughness = sharp(raw.data, { raw })
|
||||
.extractChannel('alpha')
|
||||
.jpeg()
|
||||
.jpeg(jpegOptions)
|
||||
.toBuffer();
|
||||
|
||||
return await newImage
|
||||
.joinChannel(await Promise.all([roughness, metallicness]))
|
||||
.jpeg()
|
||||
.jpeg(jpegOptions)
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
@@ -64,15 +69,18 @@ export async function readTexture(path: string, mutation?: TextureMutation) {
|
||||
*/
|
||||
return await sharp(raw.data, { raw })
|
||||
.extractChannel('alpha')
|
||||
.jpeg()
|
||||
.jpeg(jpegOptions)
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
case TextureMutation.Albedo: {
|
||||
return await sharp(raw.data, { raw }).removeAlpha().jpeg().toBuffer();
|
||||
return await sharp(raw.data, { raw })
|
||||
.removeAlpha()
|
||||
.jpeg(jpegOptions)
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
default:
|
||||
return await sharp(raw.data, { raw }).jpeg().toBuffer();
|
||||
return await sharp(raw.data, { raw }).jpeg(jpegOptions).toBuffer();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user