From 25485b518a111dc68c8d1ddb8544f44e7627632a Mon Sep 17 00:00:00 2001 From: Zack Brown Date: Sun, 11 Aug 2024 07:38:40 +0100 Subject: [PATCH] Do not automatically strip flat-shaded vertex normals (#127) --- Sources/Polygon.swift | 4 ++-- Tests/MeshExportTests.swift | 19 +++++++++++++------ Tests/MeshTests.swift | 7 ------- Tests/SceneKitTests.swift | 15 ++++----------- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/Sources/Polygon.swift b/Sources/Polygon.swift index 32f020d2..9325131c 100644 --- a/Sources/Polygon.swift +++ b/Sources/Polygon.swift @@ -161,9 +161,9 @@ public extension Polygon { vertices.contains(where: { $0.texcoord != .zero }) } - /// A Boolean value that indicates whether the polygon includes vertex normals that differ from the face normal. + /// A Boolean value that indicates whether the polygon includes vertex normals. var hasVertexNormals: Bool { - vertices.contains(where: { !$0.normal.isEqual(to: plane.normal) && $0.normal != .zero }) + vertices.contains(where: { $0.normal != .zero }) } /// A Boolean value that indicates whether the polygon includes vertex colors. diff --git a/Tests/MeshExportTests.swift b/Tests/MeshExportTests.swift index a86a08e1..19dcec31 100644 --- a/Tests/MeshExportTests.swift +++ b/Tests/MeshExportTests.swift @@ -169,12 +169,19 @@ class MeshExportTests: XCTestCase { vt 1 0 vt 0 0 - f 1/1 2/2 3/3 4/4 - f 5/1 6/2 7/3 8/4 - f 7/1 4/2 3/3 8/4 - f 5/1 2/2 1/3 6/4 - f 6/1 1/2 4/3 7/4 - f 2/1 5/2 8/3 3/4 + vn 1 0 0 + vn -1 0 0 + vn 0 1 0 + vn 0 -1 0 + vn 0 0 1 + vn 0 0 -1 + + f 1/1/1 2/2/1 3/3/1 4/4/1 + f 5/1/2 6/2/2 7/3/2 8/4/2 + f 7/1/3 4/2/3 3/3/3 8/4/3 + f 5/1/4 2/2/4 1/3/4 6/4/4 + f 6/1/5 1/2/5 4/3/5 7/4/5 + f 2/1/6 5/2/6 8/3/6 3/4/6 """) } diff --git a/Tests/MeshTests.swift b/Tests/MeshTests.swift index 1e9dcd7a..d6681d0d 100644 --- a/Tests/MeshTests.swift +++ b/Tests/MeshTests.swift @@ -335,11 +335,4 @@ class MeshTests: XCTestCase { let mesh = Mesh.cube().withoutTexcoords() XCTAssertFalse(mesh.hasTexcoords) } - - func testMeshWithoutVertexNormals() { - let cube = Mesh.cube() - XCTAssertFalse(cube.hasVertexNormals) - let sphere = Mesh.sphere().smoothingNormals(forAnglesGreaterThan: .zero) - XCTAssertFalse(sphere.hasVertexNormals) - } } diff --git a/Tests/SceneKitTests.swift b/Tests/SceneKitTests.swift index d5edbe82..5cd02b52 100644 --- a/Tests/SceneKitTests.swift +++ b/Tests/SceneKitTests.swift @@ -111,15 +111,15 @@ class SceneKitTests: XCTestCase { func testExportCube() { let cube = Mesh.cube() let geometry = SCNGeometry(polygons: cube) - XCTAssertEqual(geometry.sources.count, 2) - XCTAssertEqual(geometry.sources.first?.vectorCount, 20) + XCTAssertEqual(geometry.sources.count, 3) + XCTAssertEqual(geometry.sources.first?.vectorCount, 24) } func testExportCubeWithoutTexcoords() { let cube = Mesh.cube().withoutTexcoords() let geometry = SCNGeometry(polygons: cube) - XCTAssertEqual(geometry.sources.count, 1) - XCTAssertEqual(geometry.sources.first?.vectorCount, 8) + XCTAssertEqual(geometry.sources.count, 2) + XCTAssertEqual(geometry.sources.first?.vectorCount, 24) } func testExportSphere() { @@ -136,13 +136,6 @@ class SceneKitTests: XCTestCase { XCTAssertEqual(geometry.sources.first?.vectorCount, 151) } - func testExportSphereWithoutTexcoordsOrNormals() { - let sphere = Mesh.sphere().withoutTexcoords().smoothingNormals(forAnglesGreaterThan: .zero) - let geometry = SCNGeometry(polygons: sphere) - XCTAssertEqual(geometry.sources.count, 1) - XCTAssertEqual(geometry.sources.first?.vectorCount, 114) - } - func testExportMeshWithColors() throws { let mesh = Mesh.lathe(.curve([ .point(.unitY, color: .red),