From 1a9edc400f4ad0d217200b7694bf5383cce6543d Mon Sep 17 00:00:00 2001 From: Ruben Weituschewitz Date: Wed, 1 Apr 2020 18:33:28 +0200 Subject: [PATCH] Recalculate normals on import --- Assets/Scripts/Services/ImportFolder.cs | 2 ++ Assets/Scripts/Util/Stl/StlImporter.cs | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Services/ImportFolder.cs b/Assets/Scripts/Services/ImportFolder.cs index b157707..e759c80 100644 --- a/Assets/Scripts/Services/ImportFolder.cs +++ b/Assets/Scripts/Services/ImportFolder.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; @@ -16,6 +17,7 @@ namespace StlVault.Services { + [DebuggerDisplay("{" + nameof(DisplayName) + "}")] internal sealed class ImportFolder : FileSourceBase, IImportFolder { private readonly Dictionary _knownFiles = new Dictionary(); diff --git a/Assets/Scripts/Util/Stl/StlImporter.cs b/Assets/Scripts/Util/Stl/StlImporter.cs index 534350b..e175441 100644 --- a/Assets/Scripts/Util/Stl/StlImporter.cs +++ b/Assets/Scripts/Util/Stl/StlImporter.cs @@ -117,11 +117,13 @@ void WriteFacet(int currentFacet) ref var face = ref facets[currentFacet]; // Invert indices because .stl files are right-handed - vertices[index0] = new Vector3(-face.vert_1.y, face.vert_1.z, face.vert_1.x); - vertices[index1] = new Vector3(-face.vert_2.y, face.vert_2.z, face.vert_2.x); - vertices[index2] = new Vector3(-face.vert_3.y, face.vert_3.z, face.vert_3.x); - var normal = new Vector3(-face.normal.y, face.normal.z, face.normal.x); - + var a = new Vector3(-face.vert_1.y, face.vert_1.z, face.vert_1.x); + var b = new Vector3(-face.vert_2.y, face.vert_2.z, face.vert_2.x); + var c = new Vector3(-face.vert_3.y, face.vert_3.z, face.vert_3.x); + vertices[index0] = a; vertices[index1] = b; vertices[index2] = c; + + // Recompute normal vector + var normal = Vector3.Cross(a - b, c - a).normalized; normals[index0] = normal; normals[index1] = normal; normals[index2] = normal;