Exemple #1
0
        //IOpsCommand
        public void Run(OpsContext context, OpsStatement statement)
        {
            AddVDataArguments arguments = statement.Arguments as AddVDataArguments;

            OpsConsole.WriteLine(String.Format("Adding vertex element {0} of type {1}", arguments.FriendlyName, arguments.type));

            foreach (OpsModel model in statement.GetContent(context))
            {
                foreach (OpsMeshContainer meshContainer in model.GetMeshEnumerator())
                {
                    VertexElement[] OldVEs = meshContainer.MeshData.Mesh.Declaration.Clone() as VertexElement[];

                    int iExists;
                    if (false == MeshDeclarationHelper.FindElement(OldVEs, arguments.usage, arguments.usageIdx, out iExists))
                    {
                        int             addedIdx;
                        VertexElement[] VEs = MeshDeclarationHelper.AddElement(OldVEs, arguments.type, arguments.usage, arguments.usageIdx, out addedIdx);

                        Mesh newMesh = meshContainer.MeshData.Mesh.Clone(meshContainer.MeshData.Mesh.Options.Value, VEs, meshContainer.MeshData.Mesh.Device);
                        meshContainer.ReplaceMesh(newMesh);
                    }
                    else
                    {
                        OpsConsole.WriteLine(String.Format("Found existing vertex element {0}{1} on {2}", arguments.usage, arguments.usageIdx, meshContainer.FriendlyName(model)));
                    }
                }
            }
        }
Exemple #2
0
        //IOpsCommand
        public void Run(OpsContext context, OpsStatement statement)
        {
            OpsConsole.WriteLine("Cleaning models");

            foreach (OpsModel model in statement.GetContent(context))
            {
                foreach (OpsMeshContainer meshContainer in model.GetMeshEnumerator())
                {
                    string errorsAndWarnings;
                    meshContainer.MeshData.Mesh.Validate(meshContainer.GetAdjacencyStream(), out errorsAndWarnings);
                    if (errorsAndWarnings == null || errorsAndWarnings.Length == 0)
                    {
                        continue;
                    }
                    else
                    {
                        OpsConsole.WriteLine("Cleaning '{0}'", meshContainer.FriendlyName(model));
                        OpsConsole.WriteLine(errorsAndWarnings);
                    }

                    VertexElement[] OldVEs        = null;
                    int             offsetOfIndex = -1;
                    Mesh            dirtyMesh     = meshContainer.MeshData.Mesh;
                    if (meshContainer.SkinInformation != null)
                    {
                        int psizeUsageIdx = -1;
                        int remapIdx      = -1;
                        OldVEs = meshContainer.MeshData.Mesh.Declaration.Clone() as VertexElement[];

                        if (!MeshDeclarationHelper.FindValidUsageIndex(OldVEs, DeclarationUsage.PointSize, out psizeUsageIdx))
                        {
                            throw new OpsException("Could not add remapping-indexing vertex element to declaration");
                        }

                        VertexElement[] VEs = MeshDeclarationHelper.AddElement(OldVEs, DeclarationType.Float1, DeclarationUsage.PointSize, psizeUsageIdx, out remapIdx);

                        dirtyMesh = meshContainer.MeshData.Mesh.Clone(meshContainer.MeshData.Mesh.Options.Value, VEs, meshContainer.MeshData.Mesh.Device);

                        offsetOfIndex = VEs[remapIdx].Offset;
                        VertexBuffer   vb = dirtyMesh.VertexBuffer;
                        GraphicsStream gs = vb.Lock(0, 0, LockFlags.None);
                        for (int iVertex = 0; iVertex < dirtyMesh.NumberVertices; iVertex++)
                        {
                            gs.Seek(dirtyMesh.NumberBytesPerVertex * iVertex + offsetOfIndex, SeekOrigin.Begin);
                            gs.Write(iVertex);
                        }
                        vb.Unlock();
                        gs = null;
                        vb = null;
                    }

                    int[] adjOut;
                    Mesh  cleanedMesh = Mesh.Clean(CleanType.BackFacing | CleanType.BowTies, dirtyMesh, meshContainer.GetAdjacency(), out adjOut, out errorsAndWarnings);

                    if (errorsAndWarnings != null && errorsAndWarnings.Length != 0)
                    {
                        OpsConsole.WriteLine("Remaining Errors and Warnings:");
                        OpsConsole.WriteLine(errorsAndWarnings);
                    }

                    if (meshContainer.SkinInformation != null)
                    {
                        int[] vertexRemap = new int[cleanedMesh.NumberVertices];

                        VertexBuffer   vb = cleanedMesh.VertexBuffer;
                        GraphicsStream gs = vb.Lock(0, 0, LockFlags.None);
                        for (int iVertex = 0; iVertex < cleanedMesh.NumberVertices; iVertex++)
                        {
                            gs.Seek(cleanedMesh.NumberBytesPerVertex * iVertex + offsetOfIndex, SeekOrigin.Begin);
                            vertexRemap[iVertex] = (int)gs.Read(typeof(int));
                        }
                        vb.Unlock();

                        meshContainer.RemapSkin(vertexRemap);

                        cleanedMesh = cleanedMesh.Clone(cleanedMesh.Options.Value, OldVEs, cleanedMesh.Device);
                    }

                    meshContainer.SetAdjacency(adjOut);


                    meshContainer.ReplaceMesh(cleanedMesh);
                }
            }
        }
Exemple #3
0
        //IOpsCommand
        public void Run(OpsContext context, OpsStatement statement)
        {
            CloneVDataArguments arguments = statement.Arguments as CloneVDataArguments;

            OpsConsole.WriteLine(String.Format("Cloning vertex element {0} as {1}", arguments.FriendlySrcName, arguments.FriendlyDstName));

            foreach (OpsModel model in statement.GetContent(context))
            {
                foreach (OpsMeshContainer meshContainer in model.GetMeshEnumerator())
                {
                    int srcIdx;
                    int dstIdx;

                    VertexElement[] OldVEs = meshContainer.MeshData.Mesh.Declaration.Clone() as VertexElement[];


                    int srcUsageIdx = arguments.srcUsageIdx;
                    if (arguments.hasSrcUsageIdx == false)
                    {
                        for (int i = 0; i < OldVEs.Length; i++)
                        {
                            if (OldVEs[i].DeclarationUsage == arguments.srcUsage)
                            {
                                srcUsageIdx = OldVEs[i].UsageIndex;
                                break;
                            }
                        }
                    }

                    if (srcUsageIdx < 0)
                    {
                        continue;
                    }

                    if (!MeshDeclarationHelper.FindElement(OldVEs, arguments.srcUsage, srcUsageIdx, out srcIdx))
                    {
                        throw new OpsException("Could not find source vertex element");
                    }

                    int dstUsageIdxPerMesh = arguments.dstUsageIdx;
                    if (!arguments.hasDstUsageIdx)
                    {
                        if (MeshDeclarationHelper.FindValidUsageIndex(OldVEs, arguments.dstUsage, out dstUsageIdxPerMesh, out dstIdx))
                        {
                            throw new OpsException("Could not find open usage index for destination vertex element");
                        }
                    }

                    int             copyIdx;
                    VertexElement[] VEs = MeshDeclarationHelper.AddElement(OldVEs, OldVEs[srcIdx].DeclarationType, arguments.dstUsage, dstUsageIdxPerMesh, out copyIdx);

                    if (!MeshDeclarationHelper.FindElement(VEs, arguments.srcUsage, srcUsageIdx, out srcIdx))
                    {
                        throw new OpsException("Could not find source vertex element");
                    }

                    if (!MeshDeclarationHelper.FindElement(VEs, arguments.dstUsage, dstUsageIdxPerMesh, out dstIdx))
                    {
                        throw new OpsException("Could not find destination vertex element");
                    }



                    Mesh newMesh = meshContainer.MeshData.Mesh.Clone(meshContainer.MeshData.Mesh.Options.Value, VEs, meshContainer.MeshData.Mesh.Device);

                    int    sizeInBytes = MeshDeclarationHelper.GetTypeSize(VEs[srcIdx].DeclarationType);
                    int    srcOffset   = VEs[srcIdx].Offset;
                    int    dstOffset   = VEs[dstIdx].Offset;
                    byte[] copyBuffer  = new byte[sizeInBytes];

                    VertexBuffer   vb = newMesh.VertexBuffer;
                    GraphicsStream gs = vb.Lock(0, 0, LockFlags.None);
                    for (int iVertex = 0; iVertex < newMesh.NumberVertices; iVertex++)
                    {
                        gs.Seek(newMesh.NumberBytesPerVertex * iVertex + srcOffset, SeekOrigin.Begin);
                        gs.Read(copyBuffer, 0, sizeInBytes);

                        gs.Seek(newMesh.NumberBytesPerVertex * iVertex + dstOffset, SeekOrigin.Begin);
                        gs.Write(copyBuffer, 0, sizeInBytes);
                    }
                    vb.Unlock();
                    gs = null;
                    vb = null;

                    meshContainer.ReplaceMesh(newMesh);
                }
            }
        }