public List <Mesh> BuildMesh(List <Mesh> meshesToUse = null) { var meshes = new List <Mesh>(); int curTriangle = 0; int curAvailableTriangle = triangle.Count; int curAvailableVertex = kMaxVertex; int curAvailableIndex = kMaxIndex; int curLine = 0; int curAvailableLine = line.Count; int curMesh = 0; while (true) { int numTriangle = ProcessShapes(ref curAvailableTriangle, ref curAvailableVertex, ref curAvailableIndex, MeshData.kVertexPerTriangle, MeshData.kIndexPerTriangle); int numLine = ProcessShapes(ref curAvailableLine, ref curAvailableVertex, ref curAvailableIndex, MeshData.kVertexPerLine, MeshData.kIndexPerLine); if (numTriangle > 0 || numLine > 0) { MeshData md = new MeshData(numLine, numTriangle); if (numTriangle > 0) { md.Add(triangle, curTriangle, curTriangle + numTriangle); curTriangle += numTriangle; } if (numLine > 0) { md.Add(line, curLine, curLine + numLine); curLine += numLine; } meshes.Add(md.CreateMesh( meshesToUse != null && meshesToUse.Count > curMesh ? meshesToUse[curMesh] : null )); curAvailableVertex = kMaxVertex; curAvailableIndex = kMaxIndex; ++curMesh; } else { break; } } if (meshesToUse != null) { for (int i = curMesh; i != meshesToUse.Count; ++i) { UnityEngine.Object.DestroyImmediate(meshesToUse[i]); meshesToUse[i] = null; } } return(meshes); }
/// <summary> /// Parse read bytes from a STL binary file. /// </summary> /// <param name="bytes">Read bytes.</param> /// <param name="normalSmoothAngleThreshold">Angle where normal angles below this angle is /// interpreted as a smooth surface.</param> /// <returns>Array of meshes parsed from <paramref name="bytes"/>.</returns> public static Mesh[] ReadBinary(byte[] bytes, float normalSmoothAngleThreshold) { // Specification from https://en.wikipedia.org/wiki/STL_(file_format). // ------------------------------------------------------------------- // UINT8[80] – Header // UINT32 – Number of triangles // // foreach triangle // REAL32[3] – Normal vector // REAL32[3] – Vertex 1 // REAL32[3] – Vertex 2 // REAL32[3] – Vertex 3 // UINT16 – Attribute byte count // end // ------------------------------------------------------------------- var meshes = new List <Mesh>(); using (var memStream = new MemoryStream(bytes)) using (var binStream = new BinaryReader(memStream)) { binStream.ReadBytes(80); int numTriangles = (int)binStream.ReadUInt32(); var meshData = new MeshData(3 * numTriangles, normalSmoothAngleThreshold); var vertexBuffer = new Vector3[3]; for (int globalTriangleNumber = 0; globalTriangleNumber < numTriangles; ++globalTriangleNumber) { var normal = binStream.ReadVector3().ToLeftHanded(); for (int triangleVertexIndex = 0; triangleVertexIndex < 3; ++triangleVertexIndex) { vertexBuffer[triangleVertexIndex] = binStream.ReadVector3().ToLeftHanded(); } meshData.Add(normal, vertexBuffer); // TODO STL: Parse color if bit index 15 is set (or not in some implementations) ("hack"). binStream.ReadUInt16(); if (meshData.Vertices.Count >= ushort.MaxValue - 3) { meshes.Add((Mesh)meshData); } } if (meshData.IsValid) { meshes.Add((Mesh)meshData); } } return(meshes.ToArray()); }