Exemple #1
0
        public IGeometricObjectElementCollide Clone(GeometricObjectCollide mesh)
        {
            GeometricObjectElementCollideTriangles sm = (GeometricObjectElementCollideTriangles)MemberwiseClone();

            sm.geo = mesh;
            sm.Reset();
            return(sm);
        }
        public static GeometricObjectCollide Read(Reader reader, Pointer offset, CollideType type = CollideType.None)
        {
            MapLoader l = MapLoader.Loader;
            //l.print("CollideMesh " + offset);
            GeometricObjectCollide m = new GeometricObjectCollide(offset, type);

            //l.print("Mesh obj: " + offset);
            if (Settings.s.engineVersion == Settings.EngineVersion.R3 || Settings.s.game == Settings.Game.R2Revolution)
            {
                m.num_vertices = reader.ReadUInt16();
                m.num_elements = reader.ReadUInt16();
                if (Settings.s.engineVersion == Settings.EngineVersion.R3 && Settings.s.game != Settings.Game.LargoWinch)
                {
                    reader.ReadUInt32();
                }
            }
            if (Settings.s.engineVersion <= Settings.EngineVersion.Montreal)
            {
                m.num_vertices = (ushort)reader.ReadUInt32();
            }
            m.off_vertices = Pointer.Read(reader);
            if (Settings.s.engineVersion < Settings.EngineVersion.R3 && Settings.s.game != Settings.Game.R2Revolution)
            {
                m.off_normals = Pointer.Read(reader);
                Pointer.Read(reader);
                reader.ReadInt32();
            }
            if (Settings.s.engineVersion <= Settings.EngineVersion.Montreal)
            {
                m.num_elements = (ushort)reader.ReadUInt32();
            }
            m.off_element_types = Pointer.Read(reader);
            m.off_elements      = Pointer.Read(reader);
            if (Settings.s.game != Settings.Game.R2Revolution && Settings.s.game != Settings.Game.LargoWinch)
            {
                Pointer.Read(reader);
                if (Settings.s.engineVersion == Settings.EngineVersion.R2)
                {
                    reader.ReadInt32();
                    reader.ReadInt32();
                    reader.ReadInt32();
                    reader.ReadInt32();
                    m.num_vertices = reader.ReadUInt16();
                    m.num_elements = reader.ReadUInt16();
                }
                if (Settings.s.engineVersion <= Settings.EngineVersion.Montreal)
                {
                    reader.ReadInt32();
                    reader.ReadInt32();
                }
            }
            reader.ReadInt32();
            reader.ReadSingle();
            reader.ReadSingle();
            reader.ReadSingle();
            reader.ReadSingle();
            if (Settings.s.engineVersion < Settings.EngineVersion.R3)
            {
                reader.ReadUInt32();
            }

            // Vertices
            Pointer off_current = Pointer.Goto(ref reader, m.off_vertices);

            m.vertices = new Vector3[m.num_vertices];
            for (int i = 0; i < m.num_vertices; i++)
            {
                float x = reader.ReadSingle();
                float z = reader.ReadSingle();
                float y = reader.ReadSingle();
                m.vertices[i] = new Vector3(x, y, z);
            }

            // Normals
            if (m.off_normals != null)
            {
                off_current = Pointer.Goto(ref reader, m.off_normals);
                m.normals   = new Vector3[m.num_vertices];
                for (int i = 0; i < m.num_vertices; i++)
                {
                    float x = reader.ReadSingle();
                    float z = reader.ReadSingle();
                    float y = reader.ReadSingle();
                    m.normals[i] = new Vector3(x, y, z);
                }
            }
            // Read subblock types & initialize arrays
            Pointer.Goto(ref reader, m.off_element_types);
            m.element_types = new ushort[m.num_elements];
            m.elements      = new IGeometricObjectElementCollide[m.num_elements];
            for (uint i = 0; i < m.num_elements; i++)
            {
                m.element_types[i] = reader.ReadUInt16();
            }
            m.gao       = new GameObject("Collide Set " + (type != CollideType.None ? type + " " : "") + "@ " + offset);
            m.gao.tag   = "Collide";
            m.gao.layer = LayerMask.NameToLayer("Collide");
            for (uint i = 0; i < m.num_elements; i++)
            {
                Pointer.Goto(ref reader, m.off_elements + (i * 4));
                Pointer block_offset = Pointer.Read(reader);
                Pointer.Goto(ref reader, block_offset);
                switch (m.element_types[i])
                {
                /*1 = indexedtriangles
                 * 2 = facemap
                 * 3 = sprite
                 * 4 = TMesh
                 * 5 = points
                 * 6 = lines
                 * 7 = spheres
                 * 8 = alignedboxes
                 * 9 = cones
                 * 13 = deformationsetinfo*/
                case 1:     // Collide submesh
                    m.elements[i] = GeometricObjectElementCollideTriangles.Read(reader, block_offset, m);
                    //material_i++;
                    break;

                case 7:
                    m.elements[i] = GeometricObjectElementCollideSpheres.Read(reader, block_offset, m);
                    break;

                case 8:
                    m.elements[i] = GeometricObjectElementCollideAlignedBoxes.Read(reader, block_offset, m);
                    break;

                default:
                    m.elements[i] = null;
                    l.print("Unknown collide geometric element type " + m.element_types[i] + " at offset " + block_offset + " (Object: " + offset + ")");
                    break;
                }
            }

            for (uint i = 0; i < m.num_elements; i++)
            {
                if (m.elements[i] != null)
                {
                    GameObject child = m.elements[i].Gao;
                    child.transform.SetParent(m.gao.transform);
                    child.transform.localPosition = Vector3.zero;

                    /*if (m.subblocks[i] is CollideMeshElement) {
                     *  GameObject child = ((CollideMeshElement)m.subblocks[i]).Gao;
                     *  child.transform.SetParent(m.gao.transform);
                     *  child.transform.localPosition = Vector3.zero;
                     * } else if (m.subblocks[i] is CollideSpheresElement) {
                     *  GameObject child = ((CollideSpheresElement)m.subblocks[i]).Gao;
                     *  child.transform.SetParent(m.gao.transform);
                     *  child.transform.localPosition = Vector3.zero;
                     * } else if (m.subblocks[i] is CollideAlignedBoxesElement) {
                     *  GameObject child = ((CollideAlignedBoxesElement)m.subblocks[i]).Gao;
                     *  child.transform.SetParent(m.gao.transform);
                     *  child.transform.localPosition = Vector3.zero;
                     * }*/
                }
            }
            m.SetVisualsActive(false); // Invisible by default
            //m.gao.SetActive(false); // Invisible by default
            return(m);
        }
Exemple #3
0
        public static GeometricObjectElementCollideTriangles Read(Reader reader, Pointer offset, GeometricObjectCollide geo)
        {
            MapLoader l = MapLoader.Loader;
            GeometricObjectElementCollideTriangles sm = new GeometricObjectElementCollideTriangles(offset, geo);

            sm.off_material = Pointer.Read(reader);
            if (Settings.s.game == Settings.Game.R2Revolution || Settings.s.game == Settings.Game.LargoWinch)
            {
                sm.num_triangles = reader.ReadUInt16();
                reader.ReadUInt16();
                sm.off_triangles = Pointer.Read(reader);
                if (Settings.s.game == Settings.Game.LargoWinch)
                {
                    sm.off_normals = Pointer.Read(reader);
                    sm.off_unk     = Pointer.Read(reader);
                }
            }
            else
            {
                if (Settings.s.engineVersion < Settings.EngineVersion.R3)
                {
                    sm.num_triangles = reader.ReadUInt16();
                    sm.num_mapping   = reader.ReadUInt16();
                    sm.off_triangles = Pointer.Read(reader);
                    sm.off_mapping   = Pointer.Read(reader);
                    sm.off_normals   = Pointer.Read(reader);
                    sm.off_uvs       = Pointer.Read(reader);
                    if (Settings.s.engineVersion == Settings.EngineVersion.Montreal)
                    {
                        reader.ReadUInt32();
                    }
                    if (Settings.s.game != Settings.Game.TTSE)
                    {
                        Pointer.Read(reader);                        // table of num_unk vertex indices (vertices, because max = num_vertices - 1)
                        reader.ReadUInt16();                         // num_unk
                        sm.ind_parallelBox = reader.ReadInt16();
                    }
                }
                else
                {
                    sm.off_triangles   = Pointer.Read(reader);
                    sm.off_normals     = Pointer.Read(reader);
                    sm.num_triangles   = reader.ReadUInt16();
                    sm.ind_parallelBox = reader.ReadInt16();
                    reader.ReadUInt32();
                    if (Settings.s.game != Settings.Game.Dinosaur)
                    {
                        sm.off_mapping = Pointer.Read(reader);
                        sm.off_unk     = Pointer.Read(reader);                     // num_mapping_entries * 3 floats
                        sm.off_unk2    = Pointer.Read(reader);                     // num_mapping_entries * 1 float
                        sm.num_mapping = reader.ReadUInt16();
                        reader.ReadUInt16();
                    }
                }
            }
            if (!geo.isBoundingVolume)
            {
                if (sm.off_material != null)
                {
                    sm.gameMaterial = GameMaterial.FromOffsetOrRead(sm.off_material, reader);
                }
            }
            else
            {
                // Sector superobject
            }
            Pointer.Goto(ref reader, sm.off_triangles);
            sm.triangles = new int[sm.num_triangles * 3];
            for (int j = 0; j < sm.num_triangles; j++)
            {
                sm.triangles[(j * 3) + 0] = reader.ReadInt16();
                sm.triangles[(j * 3) + 1] = reader.ReadInt16();
                sm.triangles[(j * 3) + 2] = reader.ReadInt16();
            }
            Pointer.DoAt(ref reader, sm.off_normals, () => {
                sm.normals = new Vector3[sm.num_triangles];
                for (int j = 0; j < sm.num_triangles; j++)
                {
                    float x       = reader.ReadSingle();
                    float z       = reader.ReadSingle();
                    float y       = reader.ReadSingle();
                    sm.normals[j] = new Vector3(x, y, z);
                }
            });

            if (sm.num_mapping > 0 && sm.off_mapping != null)
            {
                Pointer.Goto(ref reader, sm.off_mapping);
                sm.mapping = new int[sm.num_triangles * 3];
                for (int i = 0; i < sm.num_triangles; i++)
                {
                    sm.mapping[(i * 3) + 0] = reader.ReadInt16();
                    sm.mapping[(i * 3) + 1] = reader.ReadInt16();
                    sm.mapping[(i * 3) + 2] = reader.ReadInt16();
                }
                if (sm.off_uvs != null)
                {
                    Pointer.Goto(ref reader, sm.off_uvs);
                    sm.uvs = new Vector2[sm.num_mapping];
                    for (int i = 0; i < sm.num_mapping; i++)
                    {
                        sm.uvs[i] = new Vector2(reader.ReadSingle(), reader.ReadSingle());
                    }
                }
            }

            /*R3Pointer.Goto(ref reader, sm.off_mapping);
             * sm.mapping = new int[sm.num_triangles * 3];
             * for (int j = 0; j < sm.num_triangles; j++) {
             *  sm.mapping[(j * 3) + 0] = reader.ReadInt16();
             *  sm.mapping[(j * 3) + 1] = reader.ReadInt16();
             *  sm.mapping[(j * 3) + 2] = reader.ReadInt16();
             * }
             * R3Pointer.Goto(ref reader, sm.off_unk);
             * sm.normals = new Vector3[sm.num_mapping_entries];
             * for (int j = 0; j < sm.num_mapping_entries; j++) {
             *  float x = reader.ReadSingle();
             *  float z = reader.ReadSingle();
             *  float y = reader.ReadSingle();
             *  sm.normals[j] = new Vector3(x, y, z);
             * }*/
            return(sm);
        }
        public static GeometricObjectCollide Read(Reader reader, Pointer offset, CollideType type = CollideType.None, bool isBoundingVolume = false)
        {
            MapLoader l = MapLoader.Loader;
            //l.print("CollideMesh " + offset);
            GeometricObjectCollide m = new GeometricObjectCollide(offset, type);

            m.isBoundingVolume = isBoundingVolume;
            if (Settings.s.engineVersion == Settings.EngineVersion.R3 || Settings.s.game == Settings.Game.R2Revolution)
            {
                m.num_vertices = reader.ReadUInt16();
                m.num_elements = reader.ReadUInt16();
                if (Settings.s.engineVersion == Settings.EngineVersion.R3 && Settings.s.game != Settings.Game.LargoWinch)
                {
                    reader.ReadUInt32();
                }
            }
            if (Settings.s.engineVersion <= Settings.EngineVersion.Montreal)
            {
                m.num_vertices = (ushort)reader.ReadUInt32();
            }
            m.off_vertices = Pointer.Read(reader);
            if (Settings.s.engineVersion < Settings.EngineVersion.R3 && Settings.s.game != Settings.Game.R2Revolution)
            {
                m.off_normals = Pointer.Read(reader);
                Pointer.Read(reader);
                reader.ReadInt32();
            }
            if (Settings.s.engineVersion <= Settings.EngineVersion.Montreal)
            {
                m.num_elements = (ushort)reader.ReadUInt32();
            }
            m.off_element_types = Pointer.Read(reader);
            m.off_elements      = Pointer.Read(reader);
            if (Settings.s.game != Settings.Game.R2Revolution && Settings.s.game != Settings.Game.LargoWinch)
            {
                Pointer.Read(reader);
                if (Settings.s.engineVersion < Settings.EngineVersion.R3)
                {
                    if (Settings.s.engineVersion == Settings.EngineVersion.R2)
                    {
                        reader.ReadInt32();
                        reader.ReadInt32();
                        reader.ReadInt32();
                        reader.ReadInt32();
                        m.num_vertices = reader.ReadUInt16();
                        m.num_elements = reader.ReadUInt16();
                    }
                    if (Settings.s.engineVersion <= Settings.EngineVersion.Montreal)
                    {
                        reader.ReadInt32();
                        reader.ReadInt32();
                    }
                    reader.ReadUInt16();
                    m.num_parallelBoxes = reader.ReadUInt16();
                }
                else
                {
                    //l.print((Pointer.Current(reader).FileOffset - offset.FileOffset));
                    m.off_parallelBoxes = Pointer.Read(reader);
                }
            }
            else
            {
                reader.ReadUInt32();
            }
            if (Settings.s.engineVersion != Settings.EngineVersion.Montreal)
            {
                m.sphereRadius = reader.ReadSingle(); // bounding volume radius
                float sphereX = reader.ReadSingle();  // x
                float sphereZ = reader.ReadSingle();  // z
                float sphereY = reader.ReadSingle();  // y
                m.sphereCenter = new Vector3(sphereX, sphereY, sphereZ);

                if (Settings.s.engineVersion == Settings.EngineVersion.R2)
                {
                    reader.ReadUInt32();
                }
            }

            // Vertices
            Pointer off_current = Pointer.Goto(ref reader, m.off_vertices);

            m.vertices = new Vector3[m.num_vertices];
            for (int i = 0; i < m.num_vertices; i++)
            {
                float x = reader.ReadSingle();
                float z = reader.ReadSingle();
                float y = reader.ReadSingle();
                m.vertices[i] = new Vector3(x, y, z);
            }

            // Normals
            if (m.off_normals != null)
            {
                off_current = Pointer.Goto(ref reader, m.off_normals);
                m.normals   = new Vector3[m.num_vertices];
                for (int i = 0; i < m.num_vertices; i++)
                {
                    float x = reader.ReadSingle();
                    float z = reader.ReadSingle();
                    float y = reader.ReadSingle();
                    m.normals[i] = new Vector3(x, y, z);
                }
            }
            // Read subblock types & initialize arrays
            Pointer.Goto(ref reader, m.off_element_types);
            m.element_types = new ushort[m.num_elements];
            m.elements      = new IGeometricObjectElementCollide[m.num_elements];
            for (uint i = 0; i < m.num_elements; i++)
            {
                m.element_types[i] = reader.ReadUInt16();
            }

            for (uint i = 0; i < m.num_elements; i++)
            {
                Pointer.Goto(ref reader, m.off_elements + (i * 4));
                Pointer block_offset = Pointer.Read(reader);
                Pointer.Goto(ref reader, block_offset);
                switch (m.element_types[i])
                {
                /*1 = indexedtriangles
                 * 2 = facemap
                 * 3 = sprite
                 * 4 = TMesh
                 * 5 = points
                 * 6 = lines
                 * 7 = spheres
                 * 8 = alignedboxes
                 * 9 = cones
                 * 13 = deformationsetinfo*/
                case 1:     // Collide submesh
                    m.elements[i] = GeometricObjectElementCollideTriangles.Read(reader, block_offset, m);
                    //material_i++;
                    break;

                case 7:
                    m.elements[i] = GeometricObjectElementCollideSpheres.Read(reader, block_offset, m);
                    break;

                case 8:
                    m.elements[i] = GeometricObjectElementCollideAlignedBoxes.Read(reader, block_offset, m);
                    break;

                default:
                    m.elements[i] = null;
                    l.print("Unknown collide geometric element type " + m.element_types[i] + " at offset " + block_offset + " (Object: " + offset + ")");
                    break;
                }
            }
            return(m);
        }