Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

STL-file and vertex normals

Discussion in 'Scripting' started by rsqw, Nov 23, 2012.

  1. rsqw

    rsqw

    Joined:
    Nov 23, 2012
    Posts:
    2
    Hi, all.

    I am new in Unity3d, and mayble I've missed something but ...

    I need to import and stl-file into unity3d, so I wrote this code:
    Code (csharp):
    1.  
    2. using System.IO;
    3. using UnityEngine;
    4. using System.Collections.Generic;
    5.  
    6. /// <summary>
    7. /// Class adds me an ability to import *.stl files
    8. /// </summary>
    9. // ReSharper disable CheckNamespace
    10. public class StlReader : MonoBehaviour {
    11. // ReSharper restore CheckNamespace
    12.     /// <summary>
    13.     /// The number of the maximum vertexes in model
    14.     /// </summary>
    15.     protected static int MaxVertexCount = 64500;
    16.  
    17.     /// <summary>
    18.     /// Contains the whole model triangles count
    19.     /// </summary>
    20.     protected uint TrianglesCount { get; set; }
    21.  
    22.     /// <summary>
    23.     /// List of all imported meshes
    24.     /// </summary>
    25.     protected List<Mesh> Meshes = new List<Mesh>();
    26.  
    27.     /// <summary>
    28.     /// Reads the file and creates meshes within 65 000 of triangles
    29.     /// </summary>
    30.     /// <param name="fileName">name of the file</param>
    31.     /// <returns>List of meshes</returns>
    32.     public List<Mesh> Read(string fileName) {
    33.         var bytes = System.IO.File.ReadAllBytes(fileName);
    34.         var reader = new BinaryReader(new MemoryStream(bytes));
    35.         reader.ReadBytes(80);
    36.  
    37.         var trianglesCount = reader.ReadUInt32();
    38.         TrianglesCount = trianglesCount;
    39.         Debug.Log(trianglesCount);
    40.  
    41.         var vertices = new List<Vector3>();
    42.         var triangles = new List<int>();
    43.         var triangle = 0;
    44.         var items = new Dictionary<string, Vector3>();
    45.        
    46.         for (var i = 0; i < trianglesCount; i++){
    47.             // the normal
    48.             reader.ReadSingle();
    49.             reader.ReadSingle();
    50.             reader.ReadSingle();
    51.  
    52.             var vertex1 = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
    53.             var vertex2 = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
    54.             var vertex3 = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
    55.            
    56.             vertices.Add(vertex1);
    57.             vertices.Add(vertex2);
    58.             vertices.Add(vertex3);
    59.            
    60.             triangles.Add(triangle++);
    61.             triangles.Add(triangle++);
    62.             triangles.Add(triangle++);
    63.  
    64.             reader.ReadUInt16(); // byte count
    65.  
    66.             if (vertices.Count <= MaxVertexCount - 1){
    67.                 continue;
    68.             }
    69.  
    70.             AddMesh(vertices, triangles);
    71.  
    72.             vertices.Clear();
    73.             triangles.Clear();
    74.             items.Clear();
    75.             triangle = 0;
    76.         }
    77.  
    78.         if (vertices.Count > 0) {
    79.             AddMesh(vertices, triangles);
    80.         }
    81.  
    82.         return Meshes;
    83.     }
    84.  
    85.     /// <summary>
    86.     /// Adds new mesh to the list
    87.     /// </summary>
    88.     /// <param name="vertices"></param>
    89.     /// <param name="triangles"></param>
    90.     protected void AddMesh(List<Vector3> vertices, List<int> triangles) {
    91.         var mesh = new Mesh {
    92.             vertices = vertices.ToArray(),
    93.             triangles = triangles.ToArray()
    94.         };
    95.        
    96.  
    97.         mesh.RecalculateNormals();
    98.         mesh.Optimize();
    99.         Meshes.Add(mesh);
    100.     }
    101. }
    102.  
    103.  
    all is quite simple except the quality of the model :(

    Take a look:
    http://www.freeimagehosting.net/8jwiu

    As you see, there is no smooth :((

    I read about the vertex normals, but I cannot make them work, because size of normals array should be the same as vertex array.

    Maybe you have some ideas, how I can make model smooth?

    Any help will be appreciated, thanks!
     
  2. gavi

    gavi

    Joined:
    Mar 30, 2013
    Posts:
    10
    Hello, are you still on this topic?
    Are you sure that the STL file was smooth to being with?
    The normals, in the STL file, relate to the triangle, so you are expected to to set the same normal to each vertex of the triangle, or you have merge some vertices.
    Have you progressed since on this?