public Mesh MergeMeshes(IEnumerable<Mesh> meshes) { if (meshes == null) { throw new ArgumentNullException("meshes"); } if (!meshes.All(t => this.Meshes.Contains(t))) { throw new ArgumentOutOfRangeException("meshes"); } if (meshes.Count() == 0) { return null; } Func<Index, int, Index> indexAdd = (index, offset) => new Index( index.A >= 0 ? index.A + offset : -1, index.B >= 0 ? index.B + offset : -1, index.C >= 0 ? index.C + offset : -1, index.D >= 0 ? index.D + offset : -1); var merge = new Mesh(); int verticesIndex = 0; int textureCoordinatesIndex = 0; int vertexNormalsIndex = 0; foreach (var mesh in meshes) { this.Meshes.Remove(mesh); foreach (var v in mesh.Vertices) { merge.Vertices.Add(v); } foreach (var v in mesh.TextureCoordinates) { merge.TextureCoordinates.Add(v); } foreach (var v in mesh.VertexNormals) { merge.VertexNormals.Add(v); } foreach (var lod in mesh.Lods) { foreach (var face in lod.FaceGroups.SelectMany(t => t.Faces)) { face.VerticesIndex = indexAdd(face.VerticesIndex, verticesIndex); face.TextureCoordinatesIndex = indexAdd(face.TextureCoordinatesIndex, textureCoordinatesIndex); face.VertexNormalsIndex = indexAdd(face.VertexNormalsIndex, vertexNormalsIndex); } merge.Lods.Add(lod); } verticesIndex += mesh.Vertices.Count; textureCoordinatesIndex += mesh.TextureCoordinates.Count; vertexNormalsIndex += mesh.VertexNormals.Count; foreach (var hardpoint in mesh.Hardpoints) { merge.Hardpoints .Add(new Hardpoint { HardpointType = hardpoint.HardpointType, Position = hardpoint.Position }); } foreach (var engineGlow in mesh.EngineGlows) { merge.EngineGlows .Add(new EngineGlow { IsDisabled = engineGlow.IsDisabled, CoreColor = engineGlow.CoreColor, OuterColor = engineGlow.OuterColor, Format = engineGlow.Format, Position = engineGlow.Position, Look = engineGlow.Look, Up = engineGlow.Up, Right = engineGlow.Right }); } } merge.SortLods(); merge.ComputeHitzone(); merge.CompactBuffers(); this.Meshes.Add(merge); return merge; }
public Mesh MergeMeshes(IEnumerable <Mesh> meshes) { if (meshes == null) { throw new ArgumentNullException("meshes"); } if (!meshes.All(t => this.Meshes.Contains(t))) { throw new ArgumentOutOfRangeException("meshes"); } if (meshes.Count() == 0) { return(null); } Func <Index, int, Index> indexAdd = (index, offset) => new Index( index.A >= 0 ? index.A + offset : -1, index.B >= 0 ? index.B + offset : -1, index.C >= 0 ? index.C + offset : -1, index.D >= 0 ? index.D + offset : -1); var merge = new Mesh(); int verticesIndex = 0; int textureCoordinatesIndex = 0; int vertexNormalsIndex = 0; foreach (var mesh in meshes) { this.Meshes.Remove(mesh); foreach (var v in mesh.Vertices) { merge.Vertices.Add(v); } foreach (var v in mesh.TextureCoordinates) { merge.TextureCoordinates.Add(v); } foreach (var v in mesh.VertexNormals) { merge.VertexNormals.Add(v); } foreach (var lod in mesh.Lods) { foreach (var face in lod.FaceGroups.SelectMany(t => t.Faces)) { face.VerticesIndex = indexAdd(face.VerticesIndex, verticesIndex); face.TextureCoordinatesIndex = indexAdd(face.TextureCoordinatesIndex, textureCoordinatesIndex); face.VertexNormalsIndex = indexAdd(face.VertexNormalsIndex, vertexNormalsIndex); } merge.Lods.Add(lod); } verticesIndex += mesh.Vertices.Count; textureCoordinatesIndex += mesh.TextureCoordinates.Count; vertexNormalsIndex += mesh.VertexNormals.Count; foreach (var hardpoint in mesh.Hardpoints) { merge.Hardpoints .Add(new Hardpoint { HardpointType = hardpoint.HardpointType, Position = hardpoint.Position }); } foreach (var engineGlow in mesh.EngineGlows) { merge.EngineGlows .Add(new EngineGlow { IsDisabled = engineGlow.IsDisabled, CoreColor = engineGlow.CoreColor, OuterColor = engineGlow.OuterColor, Format = engineGlow.Format, Position = engineGlow.Position, Look = engineGlow.Look, Up = engineGlow.Up, Right = engineGlow.Right }); } } merge.SortLods(); merge.ComputeHitzone(); merge.CompactBuffers(); this.Meshes.Add(merge); return(merge); }
public void SplitMesh(Mesh mesh) { if (mesh == null) { throw new ArgumentNullException("mesh"); } if (!this.Meshes.Contains(mesh)) { throw new ArgumentOutOfRangeException("mesh"); } this.Meshes.Remove(mesh); foreach (var lod in mesh.Lods) { var lodMesh = new Mesh(); lodMesh.Descriptor.MeshType = mesh.Descriptor.MeshType; lodMesh.Descriptor.ExplosionType = mesh.Descriptor.ExplosionType; lodMesh.Descriptor.TargetId = mesh.Descriptor.TargetId; lodMesh.RotationScale.Pivot = mesh.RotationScale.Pivot; lodMesh.RotationScale.Look = mesh.RotationScale.Look; lodMesh.RotationScale.Up = mesh.RotationScale.Up; lodMesh.RotationScale.Right = mesh.RotationScale.Right; foreach (var v in mesh.Vertices) { lodMesh.Vertices.Add(v); } foreach (var v in mesh.TextureCoordinates) { lodMesh.TextureCoordinates.Add(v); } foreach (var v in mesh.VertexNormals) { lodMesh.VertexNormals.Add(v); } lodMesh.Lods.Add(lod); foreach (var hardpoint in mesh.Hardpoints) { lodMesh.Hardpoints .Add(new Hardpoint { HardpointType = hardpoint.HardpointType, Position = hardpoint.Position }); } foreach (var engineGlow in mesh.EngineGlows) { lodMesh.EngineGlows .Add(new EngineGlow { IsDisabled = engineGlow.IsDisabled, CoreColor = engineGlow.CoreColor, OuterColor = engineGlow.OuterColor, Format = engineGlow.Format, Position = engineGlow.Position, Look = engineGlow.Look, Up = engineGlow.Up, Right = engineGlow.Right }); } lodMesh.ComputeHitzone(); lodMesh.CompactBuffers(); this.Meshes.Add(lodMesh); } }