feat: enhance cubic mapping with smooth normals and blending weights for improved texture transitions

This commit is contained in:
CNCKitchen
2026-03-19 13:36:21 +01:00
parent 32eddcad37
commit 32cc538bfb
4 changed files with 152 additions and 32 deletions
+20 -3
View File
@@ -262,6 +262,7 @@ function toIndexed(geometry, nonIndexedWeights = null) {
const positions = [];
const normals = [];
const normalSums = [];
const weights = nonIndexedWeights ? [] : null;
const indices = [];
const vertMap = new Map();
@@ -281,15 +282,31 @@ function toIndexed(geometry, nonIndexedWeights = null) {
idx = positions.length / 3;
positions.push(px, py, pz);
normals.push(nx_, ny_, nz_);
normalSums.push(nx_, ny_, nz_);
if (weights) weights.push(nonIndexedWeights[i]);
vertMap.set(key, idx);
} else if (weights && nonIndexedWeights[i] > weights[idx]) {
// MAX: if any incident original face was excluded, the shared vertex is excluded
weights[idx] = nonIndexedWeights[i];
} else {
normalSums[idx * 3] += nx_;
normalSums[idx * 3 + 1] += ny_;
normalSums[idx * 3 + 2] += nz_;
if (weights && nonIndexedWeights[i] > weights[idx]) {
// MAX: if any incident original face was excluded, the shared vertex is excluded
weights[idx] = nonIndexedWeights[i];
}
}
indices.push(idx);
}
for (let i = 0; i < positions.length / 3; i++) {
const nx = normalSums[i * 3];
const ny = normalSums[i * 3 + 1];
const nz = normalSums[i * 3 + 2];
const len = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;
normals[i * 3] = nx / len;
normals[i * 3 + 1] = ny / len;
normals[i * 3 + 2] = nz / len;
}
return { positions, normals, weights, indices };
}