Exemplo n.º 1
0
        public static StaticObject Create()
        {
            MDagPath         meshDagPath       = new MDagPath();
            MItSelectionList selectionIterator = MayaHelper.GetActiveSelectionListIterator(MFn.Type.kMesh);

            selectionIterator.getDagPath(meshDagPath);
            MFnMesh mesh = new MFnMesh(meshDagPath);

            selectionIterator.next();
            if (!selectionIterator.isDone)
            {
                MGlobal.displayError("StaticObject:Create - More than 1 mesh is selected");
                throw new Exception("StaticObject:Create - More than 1 mesh is selected");
            }

            if (!mesh.IsTriangulated())
            {
                MGlobal.displayError("StaticObject:Create - Mesh isn't triangulated");
                throw new Exception("StaticObject:Create - Mesh isn't triangulated");
            }
            if (mesh.ContainsHoles())
            {
                MGlobal.displayError("StaticObject:Create - Mesh Contains holes");
                throw new Exception("StaticObject:Create - Mesh Contains holes");
            }

            MayaMeshData             meshData    = mesh.GetMeshData();
            Dictionary <int, string> shaderNames = new Dictionary <int, string>();
            List <StaticObjectFace>  faces       = new List <StaticObjectFace>();

            //Build Shader Name map
            for (int i = 0; i < meshData.ShaderIndices.Count; i++)
            {
                int shaderIndex = meshData.ShaderIndices[i];

                if (!shaderNames.ContainsKey(shaderIndex))
                {
                    MPlug      shaderPlug = new MFnDependencyNode(meshData.Shaders[shaderIndex]).findPlug("surfaceShader");
                    MPlugArray plugArray  = new MPlugArray();

                    shaderPlug.connectedTo(plugArray, true, false);

                    MFnDependencyNode material = new MFnDependencyNode(shaderPlug[0].node);

                    shaderNames.Add(shaderIndex, material.name);
                }
            }

            //Construct faces
            int currentIndex = 0;

            for (int polygonIndex = 0; polygonIndex < mesh.numPolygons; polygonIndex++)
            {
                int    shaderIndex = meshData.ShaderIndices[polygonIndex];
                uint[] faceIndices =
                {
                    (uint)meshData.TriangleVertices[currentIndex],
                    (uint)meshData.TriangleVertices[currentIndex + 1],
                    (uint)meshData.TriangleVertices[currentIndex + 2]
                };
                Vector2[] uvs =
                {
                    new Vector2(meshData.UArray[currentIndex],         1 - meshData.VArray[currentIndex]),
                    new Vector2(meshData.UArray[currentIndex + 1], 1 - meshData.VArray[currentIndex + 1]),
                    new Vector2(meshData.UArray[currentIndex + 2], 1 - meshData.VArray[currentIndex + 2])
                };


                faces.Add(new StaticObjectFace(faceIndices, shaderNames[shaderIndex], uvs));
                currentIndex += 3;
            }

            return(new StaticObject(CreateSubmeshes(meshData.VertexArray.ToVector3List(), new List <ColorRGBA4B>(), faces)));
        }