Skip to content

Commit

Permalink
Recalculate normals on import
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwe committed Apr 1, 2020
1 parent 116289b commit 1a9edc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Assets/Scripts/Services/ImportFolder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
Expand All @@ -16,6 +17,7 @@

namespace StlVault.Services
{
[DebuggerDisplay("{" + nameof(DisplayName) + "}")]
internal sealed class ImportFolder : FileSourceBase, IImportFolder
{
private readonly Dictionary<string, IFileInfo> _knownFiles = new Dictionary<string, IFileInfo>();
Expand Down
12 changes: 7 additions & 5 deletions Assets/Scripts/Util/Stl/StlImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1a9edc4

Please sign in to comment.