示例#1
0
        protected override void OnEnabled()
        {
            base.OnEnabled();

            mesh = ComponentUtility.CreateComponent <Component_Mesh>(null, true, false);
            mesh.CreateComponent <Component_MeshGeometry_Box>();
            mesh.Enabled = true;

            processedCubemapNeedUpdate = true;
        }
示例#2
0
        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;
        }
        void UpdatePreviewMesh()
        {
            var mesh = ProjectSettings.Get.MaterialPreviewMesh.Value;

            previewMeshUsed = mesh;

            if (mesh == null)
            {
                if (defaultMesh == null)
                {
                    //sphere if null
                    defaultMesh = Scene.CreateComponent <Component_Mesh>(enabled: false);
                    var sphere = defaultMesh.CreateComponent <Component_MeshGeometry_Sphere>();
                    sphere.SegmentsHorizontal = 64;
                    sphere.SegmentsVertical   = 64;
                    defaultMesh.Enabled       = true;
                }
                mesh = defaultMesh;
            }

            var meshInSpace = (Component_MeshInSpace)Scene.GetComponent("Mesh In Space");

            meshInSpace.Mesh = mesh;
        }
示例#4
0
        //Component_MeshInSpace contains only one Component_Mesh.
        static void MergeGeometries(Component_Mesh mesh, DocumentInstance document, UndoMultiAction undo)
        {
            Component_MeshGeometry[] geometries = mesh.GetComponents <Component_MeshGeometry>();
            if (geometries == null || geometries.Length < 2)
            {
                return;
            }

            Reference <Component_Material> material = geometries[0].Material;
            //for( int i = 1; i < geometries.Length; i++ )
            //{
            //	if( !( material.Value == null && geometries[ i ].Material.Value == null || material.Equals( geometries[ i ].Material ) ) )
            //	{
            //		//??? Если разные Material какой вариант лучше: 1) Брать из первого geometry с вопросом в MessageBox; 2) Disable в меню для Action. 3) Соединять те, которые с одинаковым материалом.
            //		if( EditorMessageBox.ShowQuestion( "Mesh geometries have different materials. Merge them using a material from the first geometry?", MessageBoxButtons.OKCancel ) == DialogResult.Cancel )
            //		{
            //			return;
            //		}
            //	}
            //}

            var extracted = mesh.ExtractStructure();

            var newIndices         = new List <int>();
            var newVertices        = new List <byte>();
            var newVertexStructure = extracted.MeshGeometries[0].VertexStructure;
            var newVertexFormat    = new MeshData.MeshGeometryFormat(newVertexStructure);

            for (int geomIndex = 0; geomIndex < extracted.MeshGeometries.Length; geomIndex++)
            {
                var g = extracted.MeshGeometries[geomIndex];


                if (g.Vertices == null || g.Indices == null)
                {
                    continue;
                }
                int indexOffset = newVertices.Count / newVertexFormat.vertexSize;

                for (int i = 0; i < g.Indices.Length; i++)
                {
                    newIndices.Add(g.Indices[i] + indexOffset);
                }

                if (!CommonFunctions.IsSameVertexStructure(newVertexStructure, g.VertexStructure))
                {
                    g.Vertices = MeshData.ConvertToFormat(new MeshData.MeshGeometryFormat(g.VertexStructure), g.Vertices, newVertexFormat);
                }

                newVertices.AddRange(g.Vertices);

                foreach (var face in extracted.Structure.Faces)
                {
                    for (int i = 0; i < face.Triangles.Length; i++)
                    {
                        if (face.Triangles[i].RawGeometry == geomIndex)
                        {
                            face.Triangles[i].RawGeometry = 0;
                            face.Triangles[i].RawVertex  += indexOffset;
                        }
                    }
                }
            }

            // changes in the mesh

            if (undo != null)
            {
                //add structure update to undo
                var property = (Metadata.Property)mesh.MetadataGetMemberBySignature("property:" + nameof(Component_Mesh.Structure));
                undo.AddAction(new UndoActionPropertiesChange(new UndoActionPropertiesChange.Item(mesh, property, mesh.Structure?.Clone())));
            }

            bool meshWasEnabled = mesh.Enabled;

            mesh.Enabled = false;
            try
            {
                var newGeometry = mesh.CreateComponent <Component_MeshGeometry>();
                newGeometry.Material = material;

                newGeometry.Vertices        = newVertices.ToArray();
                newGeometry.Indices         = newIndices.ToArray();
                newGeometry.VertexStructure = newVertexStructure;

                //add created geometry to undo
                undo?.AddAction(new UndoActionComponentCreateDelete(document, new Component[] { newGeometry }, create: true));

                mesh.Structure = extracted.Structure;

                //delete old mesh geometry
                undo?.AddAction(new UndoActionComponentCreateDelete(document, geometries, create: false));

                newGeometry.Name = CommonFunctions.GetUniqueFriendlyName(newGeometry);
            }
            finally
            {
                mesh.Enabled = meshWasEnabled;
            }
        }
        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));
                        }
                    }
                }
            }
        }