Created
May 29, 2019 14:56
-
-
Save simonwittber/0297a2a8127efddd818d624ce14bd6a8 to your computer and use it in GitHub Desktop.
Extract a submesh from a mesh into a new mesh instance, for Unity3D.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Mesh Extract(Mesh m, int meshIndex) | |
{ | |
var vertices = m.vertices; | |
var normals = m.normals; | |
var newVerts = new List<Vector3>(); | |
var newNorms = new List<Vector3>(); | |
var newTris = new List<int>(); | |
var triangles = m.GetTriangles(meshIndex); | |
for (var i = 0; i < triangles.Length; i += 3) | |
{ | |
var A = triangles[i + 0]; | |
var B = triangles[i + 1]; | |
var C = triangles[i + 2]; | |
newVerts.Add(vertices[A]); | |
newVerts.Add(vertices[B]); | |
newVerts.Add(vertices[C]); | |
newNorms.Add(normals[A]); | |
newNorms.Add(normals[B]); | |
newNorms.Add(normals[C]); | |
newTris.Add(newTris.Count); | |
newTris.Add(newTris.Count); | |
newTris.Add(newTris.Count); | |
} | |
var mesh = new Mesh(); | |
mesh.indexFormat = newVerts.Count > 65536 ? IndexFormat.UInt32 : IndexFormat.UInt16; | |
mesh.SetVertices(newVerts); | |
mesh.SetNormals(newNorms); | |
mesh.SetTriangles(newTris, 0, true); | |
return mesh; | |
} |
public static Mesh Extract(Mesh m, int meshIndex)
{
var vertices = m.vertices;
var normals = m.normals;
var uvs = m.uv;
var newVerts = new List<Vector3>();
var newNorms = new List<Vector3>();
var newUVs = new List<Vector2>();
var newTris = new List<int>();
var triangles = m.GetTriangles(meshIndex);
for (var i = 0; i < triangles.Length; i += 3)
{
var A = triangles[i + 0];
var B = triangles[i + 1];
var C = triangles[i + 2];
newVerts.Add(vertices[A]);
newVerts.Add(vertices[B]);
newVerts.Add(vertices[C]);
newNorms.Add(normals[A]);
newNorms.Add(normals[B]);
newNorms.Add(normals[C]);
newUVs.Add(uvs[A]);
newUVs.Add(uvs[B]);
newUVs.Add(uvs[C]);
newTris.Add(newTris.Count);
newTris.Add(newTris.Count);
newTris.Add(newTris.Count);
}
var mesh = new Mesh();
mesh.indexFormat = newVerts.Count > 65536 ? IndexFormat.UInt32 : IndexFormat.UInt16;
mesh.SetVertices(newVerts);
mesh.SetNormals(newNorms);
mesh.SetUVs(0, newUVs);
mesh.SetTriangles(newTris, 0, true);
return mesh;
}
thx for this,
upper copy paste is modified to also include UVs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
explains how to use this script? I couldn't make it work