Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions api/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ export const flockTransform = {
// Final updates.
mesh.refreshBoundingInfo();
mesh.computeWorldMatrix(true);
refreshSizeAwareMaterials(mesh);
flock.updatePhysics(mesh);
});
},
Expand Down Expand Up @@ -859,3 +860,42 @@ export const flockTransform = {
return { x, y, z };
},
};

function refreshSizeAwareMaterials(mesh) {
const parts = [mesh, ...mesh.getChildMeshes(false)];

for (const part of parts) {
if (!part.material) continue;

const material = ensureMaterialUniqueForResize(part);
if (!material) continue;

// Recompute size-aware properties so the material matches the resized geometry.
part.computeWorldMatrix(true);
const extend = part.getBoundingInfo().boundingBox.extendSizeWorld;

if (material instanceof flock.GradientMaterial) {
material.scale = extend.y > 0 ? 1 / extend.y : 1;
}
}
}

function ensureMaterialUniqueForResize(part) {
const mat = part.material;
if (!mat) return null;

// If we've already made this material unique for this mesh, reuse it.
if (mat.metadata?.uniqueForResize) return mat;

if (typeof mat.clone === "function") {
const cloneName = `${mat.name || "material"}__${part.name}__resize`;
const cloned = mat.clone(cloneName);
cloned.metadata = { ...(cloned.metadata || {}), uniqueForResize: true };
part.material = cloned;
return cloned;
}

// Fall back to tagging the existing material if cloning is unavailable.
mat.metadata = { ...(mat.metadata || {}), uniqueForResize: true };
return mat;
}