Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Keep unused generated normals warning-free and avoid setting getter in test (#517, #521)

Nguyen Xuan An9594edc275f4
diff --git a/src/gltf.js b/src/gltf.js
index bbe2c046..8b0f9260 100644
--- a/src/gltf.js
+++ b/src/gltf.js
@@ -858,6 +858,11 @@ function convertGeometry(source, mirrored, primitive, secondUvSet, onIgnoredVert
const repairedNormals = [];
for (let offset = 0; offset < normals.length; offset += 3) {
const length = Math.hypot(...normals.subarray(offset, offset + 3));
+ if (length < 1e-8 && !normal) {
+ // A vertex that no triangle with area touches has no generated normal; it draws nothing, so any unit vector will do.
+ normals[offset + 1] = 1;
+ continue;
+ }
if (length < 1e-8) {
repairedNormals.push(offset);
continue;
@@ -865,8 +870,8 @@ function convertGeometry(source, mirrored, primitive, secondUvSet, onIgnoredVert
for (let axis = 0; axis < 3; axis++) normals[offset + axis] /= length;
}
if (repairedNormals.length) {
- // A vertex normal of zero length cannot light anything: use the face normal of the triangles at that vertex, or up.
- const faceNormals = normal ? computeVertexNormals(positions, indices) : normals;
+ // An authored normal of zero length cannot light anything: use the face normal of the triangles at that vertex, or up.
+ const faceNormals = computeVertexNormals(positions, indices);
for (const offset of repairedNormals) {
const length = Math.hypot(...faceNormals.subarray(offset, offset + 3));
for (let axis = 0; axis < 3; axis++) normals[offset + axis] = length > 1e-8 ? faceNormals[offset + axis] / length : axis === 1 ? 1 : 0;
diff --git a/tests/gltf-tolerance.test.js b/tests/gltf-tolerance.test.js
index 080d66a7..292ac5e9 100644
--- a/tests/gltf-tolerance.test.js
+++ b/tests/gltf-tolerance.test.js
@@ -72,7 +72,7 @@ test('morph targets without a base NORMAL load: normals are generated and the mo
assert.match(asset.warnings[0], /^Repaired primitive 0 of mesh "Tri": it has morph targets but no base NORMAL/);
});
-test('a primitive without NORMAL repairs vertices on zero-area or opposing faces with a warning', async () => {
+test('a primitive without NORMAL falls back to a touching face normal or up when generated normals cancel out', async () => {
const floats = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0, 5, 5, 5]);
const indices = new Uint16Array([0, 1, 2, 0, 2, 1, 3, 3, 3, 0, 1, 2]);
const bytes = new Uint8Array(floats.byteLength + indices.byteLength);
@@ -97,5 +97,5 @@ test('a primitive without NORMAL repairs vertices on zero-area or opposing faces
const asset = await parseGltf(JSON.stringify(document));
assert.equal(asset.parts.length, 1);
assert.deepEqual(Array.from(asset.parts[0].geometry.normals), [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0]);
- assert.deepEqual(asset.warnings, ['Repaired primitive 0 of mesh "Tile": 1 vertex normal(s) had zero length and were rebuilt from the triangles.']);
+ assert.deepEqual(asset.warnings, []);
});
diff --git a/tests/renderer.test.js b/tests/renderer.test.js
index 76284db8..cac714cb 100644
--- a/tests/renderer.test.js
+++ b/tests/renderer.test.js
@@ -569,7 +569,7 @@ test('resize keeps the projection centered at 699 px and 1440 px', () => {
renderer.context = { getParameter: () => 8192 };
renderer.projection = new Float32Array(16);
renderer.pixelRatio = 1;
- renderer.samples = 1;
+ renderer.multisampleSamples = 1;
renderer.memoryBudgetMiB = 64;
renderer.geometryBytes = 0;
renderer.textureBytes = 0;