// Fewer shader programs, the same picture. three.js compiles a separate program for every mix // of material flags it meets, and the first frame pays for every one of them: a second or more // each on a machine that draws WebGL in software. Most of our flags only differ in ways that // don't change a pixel, so this pass lines the materials up on the same flags: // // - flat shading: the geometry gets its face normals baked in instead (what flatShading // computes per pixel), so flat and smooth materials share a program; // - vertex colors: a material without them gets a white color attribute on its geometry (white // times the material's color is the material's color), so it shares the vertex-colored one; // - instance colors: an instanced mesh without them gets white ones. // // It only touches what it can change without a visible difference: a material whose geometry // is also drawn by another material, or already carries colors it ignores, is left alone. import * as THREE from "three"; const done = new WeakSet(); function isStandard(m) { return m && (m.isMeshStandardMaterial || m.isMeshLambertMaterial || m.isMeshPhongMaterial); } function whiteColors(geometry) { const n = geometry.attributes.position.count; const ones = new Uint8Array(n * 3).fill(255); geometry.setAttribute("color", new THREE.BufferAttribute(ones, 3, true)); } /** toNonIndexed() that leaves per-instance attributes alone (three's expands them too). */ function deindex(g) { const index = g.index.array; const out = new THREE.BufferGeometry(); for (const [name, a] of Object.entries(g.attributes)) { if (a.isInstancedBufferAttribute) { out.setAttribute(name, a); continue; } const size = a.itemSize; const src = a.array; const dst = new src.constructor(index.length * size); for (let i = 0; i < index.length; i++) for (let k = 0; k < size; k++) dst[i * size + k] = src[index[i] * size + k]; out.setAttribute(name, new THREE.BufferAttribute(dst, size, a.normalized)); } for (const grp of g.groups) out.addGroup(grp.start, grp.count, grp.materialIndex); out.userData = g.userData; return out; } export function unify(root) { // who draws what: geometry → materials, material → meshes const geoMats = new Map(); const matMeshes = new Map(); const skip = new Set(); root.traverse((o) => { if (!o.isMesh || !o.material) return; if (Array.isArray(o.material)) { for (const m of o.material) skip.add(m); return; } const m = o.material; if (!geoMats.has(o.geometry)) geoMats.set(o.geometry, new Set()); geoMats.get(o.geometry).add(m); if (!matMeshes.has(m)) matMeshes.set(m, []); matMeshes.get(m).push(o); // instance colors: white where there are none if (o.isInstancedMesh && !o.instanceColor && isStandard(m) && !done.has(o)) { done.add(o); o.instanceColor = new THREE.InstancedBufferAttribute(new Float32Array(o.instanceMatrix.count * 3).fill(1), 3); } }); for (const [m, meshes] of matMeshes) { if (!isStandard(m) || skip.has(m) || done.has(m)) continue; // only geometry this material draws alone (and with a position attribute to size it by) const geos = [...new Set(meshes.map((o) => o.geometry))]; if (!geos.every((g) => g.attributes.position && geoMats.get(g).size === 1 && !Object.values(g.attributes).some((a) => a.isInterleavedBufferAttribute))) continue; done.add(m); if (m.flatShading) { for (const g of geos) { const flat = g.index ? deindex(g) : g; flat.deleteAttribute("normal"); flat.computeVertexNormals(); if (flat !== g) { for (const o of meshes) if (o.geometry === g) o.geometry = flat; g.dispose(); } } m.flatShading = false; m.needsUpdate = true; } if (!m.vertexColors) { const current = [...new Set(meshes.map((o) => o.geometry))]; if (current.some((g) => g.attributes.color)) continue; for (const g of current) whiteColors(g); m.vertexColors = true; m.needsUpdate = true; } } }