-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimized iridescence spheres #6
Conversation
Thanks for these optimizations! Both have several issues reported in the glTF Validator:
I wonder if the texcoords could be removed, and perhaps packing the asset into a GLB might solve the texture error? |
That texture error surprises me a bit. This is actually not caused by an error in the model. It is caused by the fact that the textures are contained in a When dragging-and-dropping the asset (i.e. the I opened KhronosGroup/glTF-Validator#208 for tracking this. The point about the For completeness, here's the modified code (with the somewhat hacky part to remove the import path from "path";
import fs from "fs";
import { NodeIO } from "@gltf-transform/core";
import { dedup, prune } from "@gltf-transform/functions";
import { KHRONOS_EXTENSIONS } from "@gltf-transform/extensions";
const baseDir = "C:/glTF-Sample-Assets/Models/";
function ensureDirectoryExists(fileName: string) {
const directory = path.dirname(fileName);
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
}
async function runOptimize(modelName: string) {
const inputDir = baseDir + modelName + "/glTF/";
const outputDir = baseDir + modelName + "/glTF-Optimized/";
const inputFileName = inputDir + modelName + ".gltf";
const outputFileName = outputDir + modelName + ".gltf";
const io = new NodeIO().registerExtensions(KHRONOS_EXTENSIONS);
const document = await io.read(inputFileName);
await document.transform(dedup());
await document.transform(prune());
const meshes = document.getRoot().listMeshes();
for (const mesh of meshes) {
const primitives = mesh.listPrimitives();
for (const primitive of primitives) {
if (primitive.getMaterial()?.getBaseColorTexture() === null) {
primitive.setAttribute("TEXCOORD_0", null);
}
}
}
const jsonDocument = await io.writeJSON(document);
ensureDirectoryExists(outputFileName);
fs.writeFileSync(outputFileName, JSON.stringify(jsonDocument.json, null, 2));
for (const uri of Object.keys(jsonDocument.resources)) {
const resource = jsonDocument.resources[uri];
const resourceFileName = path.join(outputDir, uri);
ensureDirectoryExists(resourceFileName);
fs.writeFileSync(resourceFileName, resource);
}
}
async function run() {
await runOptimize("IridescenceDielectricSpheres");
await runOptimize("IridescenceMetallicSpheres");
}
run(); |
The
IridescenceDielectricSpheres
andIridescenceMetallicSpheres
models contain 343 spheres that only differ in the material that is applied. The.bin
file of these assets had >10MB, because the geometry data for these spheres was stored 343 times.This PR replaces the asset with one where the
.bin
file contains the geometry data only once, and only has 41 KB (!). There is no visual difference between the assets, as far as I can tell.The assets have been created with the following snippet, using the
dedup()
function ofglTF-Transform
: