protected override void OnEnabledInSimulation() { //create a mesh mesh = CreateComponent <Component_Mesh>(enabled: false); mesh.Name = "Mesh 1"; //generate vertices. use StandardVertex to make it easier StandardVertex.StaticOneTexCoord[] vertices = new StandardVertex.StaticOneTexCoord[4]; var v = new StandardVertex.StaticOneTexCoord(); v.Position = new Vector3F(-0.4f, -0.4f, 0f); v.Normal = new Vector3F(0, 0, 1); v.Tangent = new Vector4F(1, 0, 0, 0); v.Color = new ColorValue(1, 1, 1); //v.TexCoord0 = new Vector2F(-1, -1); vertices[0] = v; v.Position = new Vector3F(0.4f, -0.4f, 0); vertices[1] = v; v.Position = new Vector3F(0.4f, 0.4f, 0); vertices[2] = v; v.Position = new Vector3F(-0.4f, 0.4f, 0); vertices[3] = v; //generate indices var indices = new int[] { 0, 1, 2, 2, 3, 0 }; //create geometry of the mesh geometry = mesh.CreateComponent <Component_MeshGeometry>(); geometry.VertexStructure = StandardVertex.MakeStructure(StandardVertex.Components.StaticOneTexCoord, true, out int vertexSize); geometry.Vertices = ConvertVertices(vertices); geometry.Indices = indices; //mesh has been created, now we can enable it mesh.Enabled = true; meshInSpace = CreateComponent <Component_MeshInSpace>(enabled: false); meshInSpace.Transform = new Transform(new Vector3(1, 1, 1)); //make reference to the mesh. 'Root' reference - global path from scene root. meshInSpace.Mesh = ReferenceUtility.MakeRootReference(mesh); meshInSpace.Color = new ColorValue(1, 0, 0); meshInSpace.Enabled = true; }
//??? Для объединенного MeshInSpace transform выбирать как у первого MeshInSpace или лучше считать средний transform? //??? MeshInSpace.ReplaceMeterial,ReplaceMeterialSelectively ? отбрасывать или выбирать первый. Как с geometry? static Component_MeshInSpace MergeMeshInSpaces(Component_MeshInSpace[] meshInSpaces, DocumentInstance document, UndoMultiAction undo) { if (meshInSpaces.Length < 2) { return(null); } var newTransform = meshInSpaces[0].Transform.Value; var newMatrixInverse = newTransform.ToMatrix4().GetInverse(); var newRotationInverse = newTransform.Rotation.GetInverse(); Component_Mesh.StructureClass newStructure = null; var newGeometries = new List <Component_MeshGeometry>(); for (int i = 0; i < meshInSpaces.Length; i++) { var m = meshInSpaces[i].Mesh.Value; if (m == null) { continue; } var oldTransform = meshInSpaces[i].Transform.Value; var transformMatrix = newMatrixInverse * oldTransform.ToMatrix4(); var rotation = newRotationInverse * oldTransform.Rotation; var geometries = m.GetComponents <Component_MeshGeometry>(); newStructure = Component_Mesh.StructureClass.Concat(newStructure, m.ExtractStructure().Structure, newGeometries.Count); foreach (var g in geometries) { if (g is Component_MeshGeometry_Procedural meshGeometryProcedural) { VertexElement[] vertexStructure = null; byte[] vertices = null; int[] indices = null; Component_Material material = null; Component_Mesh.StructureClass structure = null; meshGeometryProcedural.GetProceduralGeneratedData(ref vertexStructure, ref vertices, ref indices, ref material, ref structure); var newMeshGeometry = new Component_MeshGeometry(); newMeshGeometry.Name = meshGeometryProcedural.Name; newMeshGeometry.VertexStructure = vertexStructure; newMeshGeometry.Vertices = vertices; newMeshGeometry.Indices = indices; newMeshGeometry.Material = meshGeometryProcedural.Material; //newMeshGeometry.Material = material; TransformVertices(newMeshGeometry.Vertices.Value, new MeshData.MeshGeometryFormat(newMeshGeometry.VertexStructure), transformMatrix, rotation); newGeometries.Add(newMeshGeometry); } else { //??? Проверять CloneSupport? var newMeshGeometry = (Component_MeshGeometry)g.Clone(); if (newMeshGeometry.Vertices.Value != null) { newMeshGeometry.Vertices = (byte[])newMeshGeometry.Vertices.Value.Clone(); TransformVertices(newMeshGeometry.Vertices.Value, new MeshData.MeshGeometryFormat(newMeshGeometry.VertexStructure), transformMatrix, rotation); } newGeometries.Add(newMeshGeometry); } } } //changes var parent = meshInSpaces[0].Parent; undo.AddAction(new UndoActionComponentCreateDelete(document, meshInSpaces, create: false)); Component_MeshInSpace newMeshInSpace = parent.CreateComponent <Component_MeshInSpace>(); bool wasEnabled = newMeshInSpace.Enabled; try { newMeshInSpace.Enabled = false; newMeshInSpace.Name = CommonFunctions.GetUniqueFriendlyName(newMeshInSpace); newMeshInSpace.Transform = newTransform; var newMesh = newMeshInSpace.CreateComponent <Component_Mesh>(); newMesh.Name = CommonFunctions.GetUniqueFriendlyName(newMesh); newMesh.Structure = newStructure; newMeshInSpace.Mesh = ReferenceUtility.MakeReference <Component_Mesh>(null, ReferenceUtility.CalculateRootReference(newMesh)); foreach (var g in newGeometries) { newMesh.AddComponent(g); CommonFunctions.EnsureNameIsUnique(g); } } finally { newMeshInSpace.Enabled = wasEnabled; } undo.AddAction(new UndoActionComponentCreateDelete(document, new[] { newMeshInSpace }, create: true)); return(newMeshInSpace); }