Ejemplo n.º 1
0
        private Geometry(RenderWareStream.Geometry geom, Mesh mesh, TextureDictionary[] textureDictionaries)
        {
            Mesh = mesh;

            if (geom.Skinning != null) {
                Mesh.boneWeights = Types.Convert(geom.Skinning.VertexBoneIndices, geom.Skinning.VertexBoneWeights);
                SkinToBoneMatrices = Types.Convert(geom.Skinning.SkinToBoneMatrices);
            }

            _geom = geom;
            _textureDictionaries = textureDictionaries;
            _materials = new Dictionary<MaterialFlags, UnityEngine.Material[]>();
        }
Ejemplo n.º 2
0
        private Geometry(RenderWareStream.Geometry geom, Mesh mesh, TextureDictionary[] textureDictionaries)
        {
            Mesh = mesh;

            if (geom.Skinning != null)
            {
                Mesh.boneWeights   = Types.Convert(geom.Skinning.VertexBoneIndices, geom.Skinning.VertexBoneWeights);
                SkinToBoneMatrices = Types.Convert(geom.Skinning.SkinToBoneMatrices);
            }

            _geom = geom;
            _textureDictionaries = textureDictionaries;
            _materials           = new Dictionary <MaterialFlags, UnityEngine.Material[]>();
        }
Ejemplo n.º 3
0
        private static Mesh Convert(RenderWareStream.Geometry src)
        {
            var mesh = new Mesh();

            // ReSharper disable ConvertClosureToMethodGroup
            var meshVertices = src.Vertices;//Utilities.F.ConvertArray( src.Vertices, x => Types.Convert(x));

            mesh.vertices = meshVertices;

            if (src.Normals != null)
            {
                mesh.normals = src.Normals;
            }

            if (src.Colours != null)
            {
                mesh.colors32 = src.Colours;
            }

            if (src.TexCoords != null && src.TexCoords.Length > 0)
            {
                mesh.uv = src.TexCoords[0];
            }
            // ReSharper restore ConvertClosureToMethodGroup

            if (src.Normals == null)
            {
                mesh.normals = CalculateNormals(src, meshVertices);
            }

            mesh.subMeshCount = src.MaterialSplits.Length;

            var isTriangleStrip = (src.Flags & GeometryFlag.TriangleStrips) == GeometryFlag.TriangleStrips;

            var subMesh = 0;

            foreach (var split in src.MaterialSplits)
            {
                var indices = isTriangleStrip
                    ? FromTriangleStrip(split.FaceIndices)
                    : split.FaceIndices;
                mesh.SetIndices(indices, MeshTopology.Triangles, subMesh++);
            }

            mesh.RecalculateBounds();

            return(mesh);
        }
Ejemplo n.º 4
0
        private static UnityEngine.Vector3[] CalculateNormals(RenderWareStream.Geometry src, UnityEngine.Vector3[] verts)
        {
            var norms = new UnityEngine.Vector3[src.VertexCount];

            for (var i = 0; i < src.FaceCount; ++i)
            {
                var face = src.Faces[i];

                var a = verts[face.Vertex0];
                var b = verts[face.Vertex1];
                var c = verts[face.Vertex2];

                var v = b - a;
                var w = c - b;

                var norm = new UnityEngine.Vector3(
                    v.y * w.z - v.z * w.y,
                    v.z * w.x - v.x * w.z,
                    v.x * w.y - v.y * w.x).normalized;

                norms[face.Vertex0] -= norm;
                norms[face.Vertex1] -= norm;
                norms[face.Vertex2] -= norm;
            }

            for (var i = 0; i < src.VertexCount; ++i)
            {
                if (norms[i].sqrMagnitude <= 0f)
                {
                    norms[i] = UnityEngine.Vector3.up;
                }
                else
                {
                    norms[i] = norms[i].normalized;
                }
            }

            return(norms);
        }