예제 #1
0
        private MeshDesc BuildMeshDesc(string name, Material material, List <Vertex> vertices, List <Face> faces)
        {
            MeshDesc meshDesc = new MeshDesc();

            meshDesc.name     = name;
            meshDesc.vertices = new MeshDesc.Vertex[vertices.Count];
            for (int i = 0; i < vertices.Count; i++)
            {
                MeshDesc.Vertex v = new MeshDesc.Vertex();
                v.position           = vertices[i].coord;
                v.normal             = vertices[i].normal;
                v.texCoords          = new Vector2[] { vertices[i].texCoord };
                v.color              = vertices[i].color;
                v.boneIndicesGlobal  = new short[] { 0, 0, 0, 0 };
                v.boneIndicesLocal   = new short[] { 0, 0, 0, 0 };
                v.boneWeights        = new float[] { 1, 0, 0, 0 };
                meshDesc.vertices[i] = v;
            }
            meshDesc.indices = new ushort[faces.Count * 3];
            int j = 0;

            for (int i = 0; i < faces.Count; i++)
            {
                meshDesc.indices[j + 0] = (ushort)faces[i].vertexIndices[0];
                meshDesc.indices[j + 1] = (ushort)faces[i].vertexIndices[1];
                meshDesc.indices[j + 2] = (ushort)faces[i].vertexIndices[2];
                j += 3;
            }
            meshDesc.boneIndexMap = new short[] { 0 };
            meshDesc.uvLayerCount = 1;
            CalculateTangents(meshDesc);

            MeshDesc.Texture textureDiffuse = new MeshDesc.Texture();
            textureDiffuse.filename     = material.textureDiffuse;
            textureDiffuse.uvLayerIndex = 0;

            MeshDesc.Texture textureLightmap = null;

            MeshDesc.Texture textureNormal = null;
            if (material.textureNormal != null)
            {
                textureNormal              = new MeshDesc.Texture();
                textureNormal.filename     = material.textureNormal;
                textureNormal.uvLayerIndex = 0;
            }

            MeshDesc.Texture textureSpecular = null;
            if (material.textureSpecular != null)
            {
                textureSpecular              = new MeshDesc.Texture();
                textureSpecular.filename     = material.textureSpecular;
                textureSpecular.uvLayerIndex = 0;
            }

            meshDesc.textures     = new MeshDesc.Texture[] { textureDiffuse, textureLightmap, textureNormal, textureSpecular };
            meshDesc.renderParams = new object[] { material.specular };
            meshDesc.isShadeless  = material.ambient > 0.99f;
            return(meshDesc);
        }
예제 #2
0
        private MeshDesc LoadMesh(BinaryReader file, bool hasArmature)
        {
            MeshDesc mesh = new MeshDesc();

            mesh.name = file.ReadString();
            int uvLayerCount = file.ReadInt32();

            mesh.uvLayerCount = uvLayerCount;
            int textureCount = file.ReadInt32();

            mesh.textures = new MeshDesc.Texture[textureCount];
            for (int textureID = 0; textureID < textureCount; textureID++)
            {
                MeshDesc.Texture texture = new MeshDesc.Texture();
                texture.filename         = file.ReadString();
                texture.uvLayerIndex     = file.ReadInt32();
                mesh.textures[textureID] = texture;
            }
            Dictionary <short, short> boneIndexDict = new Dictionary <short, short>();
            List <short> boneIndexMap = new List <short>();
            int          vertexCount  = file.ReadInt32();

            mesh.vertices = new MeshDesc.Vertex[vertexCount];
            for (int vertexID = 0; vertexID < vertexCount; vertexID++)
            {
                MeshDesc.Vertex vertex    = new MeshDesc.Vertex();
                float           positionX = file.ReadSingle();
                float           positionY = file.ReadSingle();
                float           positionZ = file.ReadSingle();
                vertex.position = new Vector3(positionX, positionY, positionZ);
                float normalX = file.ReadSingle();
                float normalY = file.ReadSingle();
                float normalZ = file.ReadSingle();
                vertex.normal = new Vector3(normalX, normalY, normalZ);
                float colorR = file.ReadByte() / 255.0f;
                float colorG = file.ReadByte() / 255.0f;
                float colorB = file.ReadByte() / 255.0f;
                float colorA = file.ReadByte() / 255.0f;
                vertex.color     = new Vector4(colorR, colorG, colorB, 1.0f);
                vertex.texCoords = new Vector2[uvLayerCount];
                for (int uvLayerID = 0; uvLayerID < uvLayerCount; uvLayerID++)
                {
                    float texCoordX = file.ReadSingle();
                    float texCoordY = file.ReadSingle();
                    vertex.texCoords[uvLayerID] = new Vector2(texCoordX, texCoordY);
                }
                vertex.tangents = new Vector4[uvLayerCount];
                for (int uvLayerID = 0; uvLayerID < uvLayerCount; uvLayerID++)
                {
                    float tangentX = file.ReadSingle();
                    float tangentY = file.ReadSingle();
                    float tangentZ = file.ReadSingle();
                    float tangentW = file.ReadSingle();
                    vertex.tangents[uvLayerID] = new Vector4(tangentX, tangentY, tangentZ, tangentW);
                }
                if (hasArmature)
                {
                    vertex.boneIndicesGlobal = new short[4];
                    vertex.boneIndicesLocal  = new short[4];
                    for (int i = 0; i < 4; i++)
                    {
                        vertex.boneIndicesGlobal[i] = file.ReadInt16();
                    }
                    vertex.boneWeights = new float[4];
                    float weightSum = 0;
                    for (int i = 0; i < 4; i++)
                    {
                        float weight = file.ReadSingle();
                        vertex.boneWeights[i] = weight;
                        weightSum            += weight;
                    }
                    if (weightSum == 0)
                    {
                        vertex.boneWeights[0] = 1.0f;
                    }
                    else
                    {
                        if (weightSum != 1.0f)
                        {
                            for (int i = 0; i < 4; i++)
                            {
                                vertex.boneWeights[i] /= weightSum;
                            }
                        }
                    }
                    short indexDefault = -1;
                    for (int i = 0; i < 4; i++)
                    {
                        if (vertex.boneWeights[i] > 0)
                        {
                            indexDefault = vertex.boneIndicesGlobal[i];
                            break;
                        }
                    }
                    for (int i = 0; i < 4; i++)
                    {
                        short indexGlobal = vertex.boneIndicesGlobal[i];
                        if (vertex.boneWeights[i] == 0)
                        {
                            indexGlobal = indexDefault;
                        }
                        short indexLocal;
                        if (!boneIndexDict.TryGetValue(indexGlobal, out indexLocal))
                        {
                            indexLocal = (short)boneIndexMap.Count;
                            boneIndexDict[indexGlobal] = indexLocal;
                            boneIndexMap.Add(indexGlobal);
                        }
                        vertex.boneIndicesLocal[i] = indexLocal;
                    }
                }
                mesh.vertices[vertexID] = vertex;
            }
            mesh.boneIndexMap = boneIndexMap.ToArray();
            int faceCount = file.ReadInt32();

            mesh.indices = new ushort[faceCount * 3];
            int index = 0;

            for (int faceID = 0; faceID < faceCount; faceID++)
            {
                mesh.indices[index + 0] = (ushort)file.ReadInt32();
                mesh.indices[index + 1] = (ushort)file.ReadInt32();
                mesh.indices[index + 2] = (ushort)file.ReadInt32();
                index += 3;
            }
            return(mesh);
        }