public void BakeIntoMesh(DocumentInstance document, UndoMultiAction undoMultiAction) { var mesh = Parent as Component_Mesh; if (mesh != null) { bool meshWasEnabled = mesh.Enabled; try { //disable mesh mesh.Enabled = false; //convert mesh geometries bool needUndoForNextActions = true; ConvertProceduralMeshGeometries(document, mesh, needUndoForNextActions ? undoMultiAction : null, ref needUndoForNextActions); var undoMultiAction2 = needUndoForNextActions ? undoMultiAction : null; OnBakeIntoMesh(document, undoMultiAction2); BakeIntoMeshEvent?.Invoke(this, document, undoMultiAction2); } finally { //enable mesh mesh.Enabled = meshWasEnabled; } } }
protected override void OnBakeIntoMesh(DocumentInstance document, UndoMultiAction undoMultiAction) { base.OnBakeIntoMesh(document, undoMultiAction); var mesh = (Component_Mesh)Parent; var geometries = mesh.GetComponents <Component_MeshGeometry>(); int aa = 0; foreach (var geometry in geometries) { var positions = geometry.VerticesExtractChannel <Vector3F>(VertexElementSemantic.Position); if (positions != null) { aa = aa + 1; if (DataN == 0 || DataN == aa) { var vertexStructure = geometry.VertexStructure.Value; vertexStructure.GetInfo(out var vertexSize, out _); var oldValue = geometry.Vertices; var vertices = geometry.Vertices.Value; var vertexCount = vertices.Length / vertexSize; var newPositions = new Vector3F[positions.Length]; for (int n = 0; n < positions.Length; n++) { ProcessVertex(aa, ref positions[n], out newPositions[n]); } var newVertices = (byte[])vertices.Clone(); if (geometry.VerticesWriteChannel(VertexElementSemantic.Position, newPositions, newVertices)) { //update property geometry.Vertices = newVertices; //undo if (undoMultiAction != null) { var property = (Metadata.Property)geometry.MetadataGetMemberBySignature("property:Vertices"); var undoAction = new UndoActionPropertiesChange(new UndoActionPropertiesChange.Item(geometry, property, oldValue)); undoMultiAction.AddAction(undoAction); } } } } } }
///////////////////////////////////////// public virtual void UpdateAlignment(UndoMultiAction undoMultiAction) { var random = new Random(); var groupOfObjects = Parent as Component_GroupOfObjects; if (groupOfObjects != null) { //var surface = Surface.Value; //if( surface != null ) //{ var indexes = GetObjectsOfElement(); var scene = groupOfObjects.FindParent <Component_Scene>(); var newObjects = groupOfObjects.ObjectsGetData(indexes); for (int n = 0; n < indexes.Count; n++) { var index = indexes[n]; ref var obj = ref newObjects[n]; double positionZ = 0; obj.UniqueIdentifier = 0; if (AutoAlign && scene != null) { var r = Component_Scene_Utility.CalculateObjectPositionZ(scene, groupOfObjects, obj.Position.Z, obj.Position.ToVector2()); if (r.found) { obj.Position.Z = r.positionZ + positionZ; } } //obj.Rotation = rotation; //obj.Scale = scale; } //delete and undo to delete undoMultiAction.AddAction(new Component_GroupOfObjects_Editor.UndoActionCreateDelete(groupOfObjects, indexes.ToArray(), false, true)); //add new data var newIndexes = groupOfObjects.ObjectsAdd(newObjects); undoMultiAction.AddAction(new Component_GroupOfObjects_Editor.UndoActionCreateDelete(groupOfObjects, newIndexes, true, false)); //} }
static void ConvertProceduralMeshGeometries(DocumentInstance document, Component_Mesh mesh, UndoMultiAction undoMultiAction, ref bool needUndoForNextActions) { //needUndoForNextActions = true; var meshGeometries = mesh.GetComponents <Component_MeshGeometry>(); if (meshGeometries.Any(g => g is Component_MeshGeometry_Procedural)) { //!!!!? bool hasOrdinary = meshGeometries.Any(g => !(g is Component_MeshGeometry_Procedural)); if (!hasOrdinary) { needUndoForNextActions = false; //??? Если были и обычные geometry и procedural? Как правильно needUndoForNextActions? Пока так: undo не нужен только если все procedural } //!!!!right? !needUndoForNextActions if (undoMultiAction != null && !needUndoForNextActions) { //add structure update to undo var property = (Metadata.Property)mesh.MetadataGetMemberBySignature("property:" + nameof(Component_Mesh.Structure)); undoMultiAction.AddAction(new UndoActionPropertiesChange(new UndoActionPropertiesChange.Item(mesh, property, mesh.Structure?.Clone()))); } for (int i = 0; i < meshGeometries.Length; i++) { var meshGeometry = meshGeometries[i]; //convert to usual Component_MeshGeometry if (meshGeometry 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 insertIndex = meshGeometryProcedural.Parent.Components.IndexOf(meshGeometryProcedural); var meshGeometryNew = mesh.CreateComponent <Component_MeshGeometry>(insertIndex); meshGeometryNew.Name = meshGeometry.Name; meshGeometryNew.VertexStructure = vertexStructure; meshGeometryNew.Vertices = vertices; meshGeometryNew.Indices = indices; meshGeometryNew.Material = meshGeometryProcedural.Material; //concut structures. If the geometry is procedural it is not in a structure yet. mesh.Structure = Component_Mesh.StructureClass.Concat(mesh.Structure, structure, i); //delete old mesh geometry if (undoMultiAction != null) { undoMultiAction.AddAction(new UndoActionComponentCreateDelete(document, new Component[] { meshGeometry }, create: false)); } else { meshGeometry.Dispose(); } //add created geometry to undo if (undoMultiAction != null) { undoMultiAction.AddAction(new UndoActionComponentCreateDelete(document, new Component[] { meshGeometryNew }, create: true)); } } } } }
protected virtual void OnBakeIntoMesh(DocumentInstance document, UndoMultiAction undoMultiAction) { }
protected override void OnBakeIntoMesh(DocumentInstance document, UndoMultiAction undoMultiAction) { base.OnBakeIntoMesh(document, undoMultiAction); var mesh = (Component_Mesh)Parent; var geometries = mesh.GetComponents <Component_MeshGeometry>(); var bounds = Bounds.Cleared; { foreach (var geometry in geometries) { var positions = geometry.VerticesExtractChannel <Vector3F>(VertexElementSemantic.Position); if (positions != null) { foreach (var p in positions) { bounds.Add(p); } } } //bounds.Expand( ExtendBounds ); } var tiles = Tiles.Value; foreach (var geometry in geometries) { var positions = geometry.VerticesExtractChannel <Vector3F>(VertexElementSemantic.Position); var normals = geometry.VerticesExtractChannel <Vector3F>(VertexElementSemantic.Normal); if (positions != null && normals != null) { var vertexStructure = geometry.VertexStructure.Value; vertexStructure.GetInfo(out var vertexSize, out _); var oldValue = geometry.Vertices; var vertices = geometry.Vertices.Value; var vertexCount = vertices.Length / vertexSize; var newTexCoords = new Vector2F[vertexCount]; for (int n = 0; n < vertexCount; n++) { ProcessVertex(ref tiles, ref bounds, ref positions[n], ref normals[n], out newTexCoords[n]); } var newVertices = (byte[])vertices.Clone(); if (geometry.VerticesWriteChannel(VertexElementSemantic.TextureCoordinate0, newTexCoords, newVertices)) { //update property geometry.Vertices = newVertices; //undo if (undoMultiAction != null) { var property = (Metadata.Property)geometry.MetadataGetMemberBySignature("property:Vertices"); var undoAction = new UndoActionPropertiesChange(new UndoActionPropertiesChange.Item(geometry, property, oldValue)); undoMultiAction.AddAction(undoAction); } } } } }