Ejemplo n.º 1
0
    List <GameObject> InstantiateStaticObjects(Parser.Tr2Room tr2room, int roomidx, Transform owner)
    {
        List <GameObject> objects = new List <GameObject>();

        for (int staticMeshCount = 0; staticMeshCount < tr2room.NumStaticMeshes; staticMeshCount++)
        {
            for (int k = 0; k < m_StaticPrefabs.Count; k++)
            {
                //room static meshes are instantce of Level mesh asset
                //choose only the meshe from whole level that belongs to this room
                if (tr2room.StaticMeshes[staticMeshCount].ObjectID == m_leveldata.StaticMeshes[k].ObjectID)
                {
                    Vector3 meshPos;
                    meshPos.x = tr2room.StaticMeshes[staticMeshCount].x;
                    meshPos.y = -tr2room.StaticMeshes[staticMeshCount].y;
                    meshPos.z = tr2room.StaticMeshes[staticMeshCount].z;

                    Vector3 rot;
                    rot.x = 0;
                    rot.y = ((tr2room.StaticMeshes[staticMeshCount].Rotation >> 14) & 0x03) * 90;
                    rot.z = 0;
                    int itemIdx = (int)m_leveldata.StaticMeshes[k].StartingMesh;

                    GameObject go = (GameObject)GameObject.Instantiate(m_StaticPrefabs[k].UnityObject, meshPos * Settings.SceneScaling, Quaternion.Euler(rot));
                    go.name             = "room" + roomidx + "__staticBase" + k + "__meshBase" + itemIdx;
                    go.transform.parent = owner;
                    AICondition.SetActive(go, true);
                    objects.Add(go);
                }
            }
        }
        return(objects);
    }
Ejemplo n.º 2
0
    public static Vector3[] GetWaterPortal(Parser.Tr2Room room)
    {
        Vector3[] portal_polygon = null;
        float     surface        = (-room.info.yTop * Settings.SceneScaling); //get scaled room surface point
        int       Flags          = room.Flags;

        if (Flags == 72 || (Flags & 1) == 1)
        {
            if (room.Portals != null)
            {
                for (int p = 0; p < room.Portals.Length; p++)
                {
                    Parser.Tr2RoomPortal port = room.Portals[p];
                    Parser.Tr2Vertex     n    = port.Normal;
                    if ((Vector3.Dot(new Vector3(n.y, n.y, n.z), Vector3.right) > 0.85f) && (port.Vertices != null))  // choose only horizontal portals
                    {
                        portal_polygon = new Vector3[port.Vertices.Length];
                        for (int i = 0; i < portal_polygon.Length; i++)
                        {
                            portal_polygon[i] = new Vector3(port.Vertices[i].x * Settings.SceneScaling, surface, port.Vertices[i].z * Settings.SceneScaling);
                        }

                        break;
                    }
                }
            }
        }

        return(portal_polygon);
    }
Ejemplo n.º 3
0
    public void InitRoom(Parser.Tr2Room room, List<GameObject> objects)
    {
        m_leveldata = Level.m_leveldata;
        m_Mesh = GetComponent<MeshFilter>().mesh;
        m_Transform = transform;
        m_StaticObjects = objects;
        m_Tr2Room = room;

        //These are not serialised by Editor. Move them to Start()
        //m_RoomVertices = m_Mesh.vertices;
        //m_SharedTriangles = MeshModifier.GetSharedTriangles(m_Mesh);
        //m_RoomAnalyzer = new SurfaceAnalyzer(m_Mesh);
        //m_RoomEdges = m_RoomAnalyzer.Analyze(m_RoomVertices, m_SharedTriangles,null);
    }
Ejemplo n.º 4
0
 RoomType DetectRoomType(Parser.Tr2Room room, Bounds b)
 {
     if ((room.Flags & 1) == 1) //water room below, bug: direct use of Flags causing error
     {
         //calculate water depth
         b = GetBound();
         if (b.size.y > 0.75)
         {
             return(RoomType.DeepWater);
         }
         else
         {
             return(RoomType.ShalloWater);
         }
     }
     return(RoomType.Land);
 }
Ejemplo n.º 5
0
    public void  InitRoom(Parser.Tr2Room room, List <GameObject> objects)
    {
#if UNITY_EDITOR
        m_Mesh = GetComponent <MeshFilter>().sharedMesh;
#else
        m_Mesh = GetComponent <MeshFilter>().mesh;
#endif

        m_Transform     = transform;
        m_StaticObjects = objects;
        m_Tr2Room       = room;

        //These are not serialised by Editor. Move them to Start()
        //m_RoomVertices = m_Mesh.vertices;
        //m_SharedTriangles = MeshModifier.GetSharedTriangles(m_Mesh);
        //m_RoomAnalyzer = new SurfaceAnalyzer(m_Mesh);
        //m_RoomEdges = m_RoomAnalyzer.Analyze(m_RoomVertices, m_SharedTriangles,null);

        /*
         * Update: Calculate center point of room, useful for vertex modultion
         * */

        float room_width = room.NumXsectors * 1024 * Settings.SceneScaling;
        float room_depth = room.NumZsectors * 1024 * Settings.SceneScaling;
        float bottom     = (-room.info.yBottom * Settings.SceneScaling);
        float surface    = (-room.info.yTop * Settings.SceneScaling);
        float x          = transform.position.x;
        float z          = transform.position.z;
        m_CenterPoint = new Vector3(x, bottom, z) + new Vector3(room_width, (surface - bottom) * 2, room_depth) * 0.5f;// - transform.position; //in world space

        m_RoomBound = new Bounds(m_CenterPoint, Vector3.zero);
        m_RoomBound.SetMinMax(new Vector3(x, bottom, z), new Vector3(x + room_width, surface, z + room_depth));

        m_RoomType = DetectRoomType(room, m_RoomBound);
        Flags      = m_Tr2Room.Flags;

        m_PortalPolygon = GetWaterPortal(room);
    }
Ejemplo n.º 6
0
    public static Mesh CreateRoomMesh(Parser.Tr2Room tr2room, Parser.Tr2Level leveldata, ref bool has_water)
    {
        Vector3[] sharedVertices = null;
        has_water = false;
        byte[] is_water_vertex = null;
        bool   has_water_face  = false;

        if (tr2room.RoomData.NumVertices > 0)
        {
            int LightMode   = tr2room.LightMode;
            int NumVertices = tr2room.RoomData.NumVertices; // optimized for field access
            Parser.Tr2VertexRoom[] Vertices = tr2room.RoomData.Vertices;
            sharedVertices  = new Vector3[NumVertices];
            is_water_vertex = new byte[sharedVertices.Length];
            for (int vertAttribCount = 0; vertAttribCount < NumVertices; vertAttribCount++)
            {
                float x = Vertices[vertAttribCount].Vertex.x;
                float y = Vertices[vertAttribCount].Vertex.y;
                float z = Vertices[vertAttribCount].Vertex.z;

                ////print("chk vtx x y z:" +x+ " " +y + " " +z);

                sharedVertices[vertAttribCount].x = x;
                sharedVertices[vertAttribCount].y = -y;
                sharedVertices[vertAttribCount].z = z;
                is_water_vertex[vertAttribCount]  = 10;
                if ((Vertices[vertAttribCount].Attributes & 0x8000L) == 0x8000L)
                {
                    is_water_vertex[vertAttribCount] = 1;
                    has_water = true;
                }
            }


            //warning: avariable lengh array in a structure can cause access violence


            //if(tr2room.RoomData.NumRectangles > 0)
            //{

            //selected_texObjectIdx = tr2room.RoomData.Rectangles[0].Texture;
            //selected_texObj =  leveldata.ObjectTextures[selected_texObjectIdx];
            //selected_texTileIdx = selected_texObj.Tile;
            int NumRectangles = tr2room.RoomData.NumRectangles; // optimized for field access
            int strideVertIdx = (NumRectangles * 4);
            int strideTriIdx  = (NumRectangles * 3 * 2);

            int numNonsharedVertices = strideVertIdx + (tr2room.RoomData.NumTriangles * 3);
            int numNonsharedTris     = strideTriIdx + (tr2room.RoomData.NumTriangles * 3);

            Vector3[] nonSharedVertices = new Vector3[numNonsharedVertices];
            Vector2[] nonSharedUVs      = new Vector2[numNonsharedVertices];
            Vector2[] nonSharedUV2s     = new Vector2[numNonsharedVertices];
            Color[]   nonSharedColor    = new Color[numNonsharedVertices];

            int[] nonSharedTris = new int[numNonsharedTris];

            //triangles = new int[num_rectangles * 3 * 2];
            Parser.Tr2Face4[] Rectangles = tr2room.RoomData.Rectangles; // optimized for field access
            for (int rectCount = 0; rectCount < NumRectangles; rectCount++)
            {
                int Idx0 = Rectangles[rectCount].Vertices0;
                int Idx1 = Rectangles[rectCount].Vertices1;
                int Idx2 = Rectangles[rectCount].Vertices2;
                int Idx3 = Rectangles[rectCount].Vertices3;

                ////print ("idx0 - Idx1 - Idx2 - Idx3:" + Idx0 + " " + Idx1 + " " + Idx2 +" " + Idx3);


                int vertOrUVIdx0 = rectCount * 4 + 0;
                int vertOrUVIdx1 = rectCount * 4 + 1;
                int vertOrUVIdx2 = rectCount * 4 + 2;
                int vertOrUVIdx3 = rectCount * 4 + 3;

                if (has_water)
                {
                    if (IsFaceInWater(is_water_vertex, Idx0, Idx1, Idx2, Idx3)) //if all vertices are in water
                    {
                        continue;
                    }
                }
                else
                {
                    if (IsFaceInWater(leveldata, Rectangles[rectCount].Texture))
                    {
                        has_water_face = true;
                        continue;
                    }
                }

                nonSharedVertices[vertOrUVIdx0] = sharedVertices[Idx0];
                nonSharedVertices[vertOrUVIdx1] = sharedVertices[Idx1];
                nonSharedVertices[vertOrUVIdx2] = sharedVertices[Idx2];
                nonSharedVertices[vertOrUVIdx3] = sharedVertices[Idx3];

                if (has_water)
                {
                    //Added vertex color for lighting effect
                    //Debug.Log("Light Atrrib0" + (Vertices[Idx0].Attributes & 0x1f));
                    //Debug.Log("Light Atrrib1" + (Vertices[Idx1].Attributes & 0x1f));
                    //Debug.Log("Light Atrrib2" + (Vertices[Idx2].Attributes & 0x1f));
                }

                //Added vertex color for lighting effect
                nonSharedColor[vertOrUVIdx0] = Color.white * (1 - Vertices[Idx0].Lighting2 * 1.220852154804053e-4f); // ((0.5f - ((float)(Vertices[Idx0].Attributes & 0x1f) / 32f)) + 0.5f);
                nonSharedColor[vertOrUVIdx1] = Color.white * (1 - Vertices[Idx1].Lighting2 * 1.220852154804053e-4f); // ((0.5f - ((float)(Vertices[Idx1].Attributes & 0x1f) / 32f)) + 0.5f);
                nonSharedColor[vertOrUVIdx2] = Color.white * (1 - Vertices[Idx2].Lighting2 * 1.220852154804053e-4f); // ((0.5f - ((float)(Vertices[Idx2].Attributes & 0x1f) / 32f)) + 0.5f);
                nonSharedColor[vertOrUVIdx3] = Color.white * (1 - Vertices[Idx3].Lighting2 * 1.220852154804053e-4f); // ((0.5f - ((float)(Vertices[Idx3].Attributes & 0x1f) / 32f)) + 0.5f);

                ushort texObjectIdx = Rectangles[rectCount].Texture;
                if (texObjectIdx >= leveldata.ObjectTextures.Length)
                {
                    continue;                                                  //fixed:  outof bound exception for Parser.Tr2Level.ObjectTextures
                }
                Parser.Tr2ObjectTexture texObj = leveldata.ObjectTextures[texObjectIdx];
                ushort texTileIdx = texObj.Tile;  //bind this textile in material?

                //if(texTileIdx != prevTexture)
                //{
                //newMatCount +=1;
                //prevTexture = texTileIdx;
                ////print("newMatCount:"+ newMatCount);
                //}

                SetFaceUVs(nonSharedUVs, vertOrUVIdx0, vertOrUVIdx1, vertOrUVIdx2, vertOrUVIdx3, texObj);

                ////print("uv[Idx0]"+ uv[Idx0].x + " " + uv[Idx0].y);
                ////print("uv[Idx1]"+ uv[Idx1].x + " " + uv[Idx1].y);


                //ushort opacity = texObj.TransparencyFlags;  //isItOpacq

                nonSharedTris[rectCount * 6 + 0] = vertOrUVIdx0;
                nonSharedTris[rectCount * 6 + 1] = vertOrUVIdx1;
                nonSharedTris[rectCount * 6 + 2] = vertOrUVIdx2;

                nonSharedTris[rectCount * 6 + 3] = vertOrUVIdx2;
                nonSharedTris[rectCount * 6 + 4] = vertOrUVIdx3;
                nonSharedTris[rectCount * 6 + 5] = vertOrUVIdx0;
            }

            Parser.Tr2Face3[] Triangles = tr2room.RoomData.Triangles; // optimized for field access
            int NumTriangles            = tr2room.RoomData.NumTriangles;
            for (int triCount = 0; triCount < NumTriangles; triCount++)
            {
                ////print("tr2room.RoomData.NumTriangles"+ tr2room.RoomData.NumTriangles);

                int Idx0 = Triangles[triCount].Vertices0;
                int Idx1 = Triangles[triCount].Vertices1;
                int Idx2 = Triangles[triCount].Vertices2;

                ////print ("idx0 - Idx1 - Idx2:" + Idx0 + " " + Idx1 + " " + Idx2);
                //[][][][]+[][][]
                int vertOrUVIdx0 = triCount * 3 + 0;
                int vertOrUVIdx1 = triCount * 3 + 1;
                int vertOrUVIdx2 = triCount * 3 + 2;

                if (has_water)
                {
                    if (IsFaceInWater(is_water_vertex, Idx0, Idx1, Idx2)) //if all vertices are in water
                    {
                        continue;
                    }
                }
                else
                {
                    if (IsFaceInWater(leveldata, Triangles[triCount].Texture))
                    {
                        has_water_face = true;
                        continue;
                    }
                }

                nonSharedVertices[strideVertIdx + vertOrUVIdx0] = sharedVertices[Idx0];
                nonSharedVertices[strideVertIdx + vertOrUVIdx1] = sharedVertices[Idx1];
                nonSharedVertices[strideVertIdx + vertOrUVIdx2] = sharedVertices[Idx2];

                if (has_water)
                {
                    //Added vertex color for lighting effect
                    //Debug.Log("Light Atrrib0" + (Vertices[Idx0].Attributes & 0x1f));
                    //Debug.Log("Light Atrrib1" + (Vertices[Idx1].Attributes & 0x1f));
                    //Debug.Log("Light Atrrib2" + (Vertices[Idx2].Attributes & 0x1f));
                }

                nonSharedColor[strideVertIdx + vertOrUVIdx0] = Color.white * (1 - Vertices[Idx0].Lighting2 * 1.220852154804053e-4f); ////Color.white * ((0.5f - ((float)(Vertices[Idx0].Attributes & 0x1f) / 32f)) + 0.5f);
                nonSharedColor[strideVertIdx + vertOrUVIdx1] = Color.white * (1 - Vertices[Idx1].Lighting2 * 1.220852154804053e-4f); ////Color.white * ((0.5f - ((float)(Vertices[Idx1].Attributes & 0x1f) / 32f)) + 0.5f);
                nonSharedColor[strideVertIdx + vertOrUVIdx2] = Color.white * (1 - Vertices[Idx2].Lighting2 * 1.220852154804053e-4f); ////Color.white * ((0.5f - ((float)(Vertices[Idx2].Attributes & 0x1f) / 32f)) + 0.5f);


                ushort texObjectIdx = Triangles[triCount].Texture;
                if (texObjectIdx >= leveldata.ObjectTextures.Length)
                {
                    continue;                                                  //fixed:  outof bound exception for Parser.Tr2Level.ObjectTextures
                }
                Parser.Tr2ObjectTexture texObj = leveldata.ObjectTextures[texObjectIdx];

                //if(texTileIdx != prevTexture)
                //{
                //newMatCount +=1;
                //prevTexture = texTileIdx;
                ////print("newMatCount:"+ newMatCount);
                //}

                SetFaceUVs(nonSharedUVs, strideVertIdx + vertOrUVIdx0, strideVertIdx + vertOrUVIdx1, strideVertIdx + vertOrUVIdx2, texObj);


                ////print("uv[Idx0]"+ uv[Idx0].x + " " + uv[Idx0].y);
                ////print("uv[Idx1]"+ uv[Idx1].x + " " + uv[Idx1].y);

                //ushort opacity = texObj.TransparencyFlags;  //isItOpacq

                nonSharedTris[strideTriIdx + vertOrUVIdx0] = strideVertIdx + vertOrUVIdx0;
                nonSharedTris[strideTriIdx + vertOrUVIdx1] = strideVertIdx + vertOrUVIdx1;
                nonSharedTris[strideTriIdx + vertOrUVIdx2] = strideVertIdx + vertOrUVIdx2;

                ////print ("idx0 - Idx1 - Idx2:" + nonSharedTris[strideTriIdx + vertOrUVIdx0]  + " " +  nonSharedTris[strideTriIdx + vertOrUVIdx1]  + " " + nonSharedTris[strideTriIdx + vertOrUVIdx2] );
            }
            if (has_water_face)
            {
                has_water = true;
            }
            ////print("leveldata.Rooms[5].RoomData.NumRectangles:"+ tr2room.RoomData.NumRectangles);
            //SetTriangles (triangles : int[], submesh : int) : void
            //generate secondary uv set

            for (int i = 0; i < nonSharedVertices.Length; i++)
            {
                nonSharedVertices[i] = nonSharedVertices[i] * Settings.SceneScaling;
            }
            Mesh mesh = new Mesh();
            mesh.Clear();
            mesh.vertices  = nonSharedVertices;
            mesh.uv        = nonSharedUVs;
            mesh.uv2       = nonSharedUVs;
            mesh.colors    = nonSharedColor;
            mesh.triangles = nonSharedTris;
            //mesh.Optimize();
            mesh.RecalculateNormals();
#if UNITY_EDITOR
            Vector4[] tangents = new Vector4[mesh.vertices.Length];
            computeTangentsAndBinormals(nonSharedVertices, mesh.normals, nonSharedUVs, nonSharedTris, tangents);
            mesh.tangents = tangents;
            tangents      = null;
#endif
            //free some memory
            nonSharedVertices = null;
            nonSharedUVs      = null;
            nonSharedUV2s     = null;
            nonSharedTris     = null;
            nonSharedColor    = null;



            //}

            return(mesh);
        }

        return(new Mesh()); //empty mesh
    }
Ejemplo n.º 7
0
    public static Mesh CreateRoomWaterMesh(Parser.Tr2Room tr2room, Parser.Tr2Level leveldata)
    {
        Vector3[] sharedVertices  = null;
        byte[]    is_water_vertex = null;
        bool      has_water       = false;

        if (tr2room.RoomData.NumVertices > 0)
        {
            int NumVertices = tr2room.RoomData.NumVertices;
            sharedVertices  = new Vector3[NumVertices];
            is_water_vertex = new byte[sharedVertices.Length];
            Parser.Tr2VertexRoom[] Vertices = tr2room.RoomData.Vertices;
            for (int vertAttribCount = 0; vertAttribCount < NumVertices; vertAttribCount++)
            {
                float x = Vertices[vertAttribCount].Vertex.x;
                float y = Vertices[vertAttribCount].Vertex.y;
                float z = Vertices[vertAttribCount].Vertex.z;

                ////print("chk vtx x y z:" +x+ " " +y + " " +z);

                sharedVertices[vertAttribCount].x = x;
                sharedVertices[vertAttribCount].y = -y;
                sharedVertices[vertAttribCount].z = z;

                is_water_vertex[vertAttribCount] = 10;
                if ((Vertices[vertAttribCount].Attributes & 0x8000L) == 0x8000L)
                {
                    is_water_vertex[vertAttribCount] = 1;
                    has_water = true;
                }
            }
        }

        //warning: avariable lengh array in a structure can cause access violence


        //if(tr2room.RoomData.NumRectangles > 0)
        //{

        //selected_texObjectIdx = tr2room.RoomData.Rectangles[0].Texture;
        //selected_texObj =  leveldata.ObjectTextures[selected_texObjectIdx];
        //selected_texTileIdx = selected_texObj.Tile;
        int NumRectangles = tr2room.RoomData.NumRectangles;
        int strideVertIdx = (NumRectangles * 4);
        int strideTriIdx  = (NumRectangles * 3 * 2);

        int numNonsharedVertices = strideVertIdx + (tr2room.RoomData.NumTriangles * 3);
        int numNonsharedTris     = strideTriIdx + (tr2room.RoomData.NumTriangles * 3);

        Vector3[] nonSharedVertices = new Vector3[numNonsharedVertices];
        Vector2[] nonSharedUVs      = new Vector2[numNonsharedVertices];
        Vector2[] nonSharedUV2s     = new Vector2[numNonsharedVertices];
        int[]     nonSharedTris     = new int[numNonsharedTris];

        Parser.Tr2Face4[] Rectangles = tr2room.RoomData.Rectangles;

        for (int rectCount = 0; rectCount < NumRectangles; rectCount++)
        {
            int Idx0 = Rectangles[rectCount].Vertices0;
            int Idx1 = Rectangles[rectCount].Vertices1;
            int Idx2 = Rectangles[rectCount].Vertices2;
            int Idx3 = Rectangles[rectCount].Vertices3;

            ////print ("idx0 - Idx1 - Idx2 - Idx3:" + Idx0 + " " + Idx1 + " " + Idx2 +" " + Idx3);


            int vertOrUVIdx0 = rectCount * 4 + 0;
            int vertOrUVIdx1 = rectCount * 4 + 1;
            int vertOrUVIdx2 = rectCount * 4 + 2;
            int vertOrUVIdx3 = rectCount * 4 + 3;

            if (has_water)
            {
                if (!IsFaceInWater(is_water_vertex, Idx0, Idx1, Idx2, Idx3)) //if not all vertices are in water
                {
                    continue;
                }
            }
            else
            {
                if (!IsFaceInWater(leveldata, Rectangles[rectCount].Texture))
                {
                    continue;
                }
            }

            nonSharedVertices[vertOrUVIdx0] = sharedVertices[Idx0];
            nonSharedVertices[vertOrUVIdx1] = sharedVertices[Idx1];
            nonSharedVertices[vertOrUVIdx2] = sharedVertices[Idx2];
            nonSharedVertices[vertOrUVIdx3] = sharedVertices[Idx3];

            ushort texObjectIdx = Rectangles[rectCount].Texture;
            if (texObjectIdx >= leveldata.ObjectTextures.Length)
            {
                continue;                                                  //fixed:  outof bound exception for Parser.Tr2Level.ObjectTextures
            }
            Parser.Tr2ObjectTexture texObj = leveldata.ObjectTextures[texObjectIdx];
            ushort texTileIdx = texObj.Tile;  //bind this textile in material?

            //if(texTileIdx != prevTexture)
            //{
            //newMatCount +=1;
            //prevTexture = texTileIdx;
            ////print("newMatCount:"+ newMatCount);
            //}

            SetFaceUVs(nonSharedUVs, vertOrUVIdx0, vertOrUVIdx1, vertOrUVIdx2, vertOrUVIdx3, texObj);

            ////print("uv[Idx0]"+ uv[Idx0].x + " " + uv[Idx0].y);
            ////print("uv[Idx1]"+ uv[Idx1].x + " " + uv[Idx1].y);


            //ushort opacity = texObj.TransparencyFlags;  //isItOpacq

            //generate secondary uv for animation by going through all animated texture group and checking existance of
            //texObjectIdx in that group
            //leveldata.AnimatedTextures[]  // this is varable length record of animated texture group

            int texture_idx = GetAnimatedTextureIndex(leveldata.AnimatedTextures, leveldata.NumAnimatedTextures, texObjectIdx);
            if (texture_idx > -1)
            {
                texObj = leveldata.ObjectTextures[texture_idx];
                SetFaceUVs(nonSharedUV2s, vertOrUVIdx0, vertOrUVIdx1, vertOrUVIdx2, vertOrUVIdx3, texObj);
            }


            nonSharedTris[rectCount * 6 + 0] = vertOrUVIdx0;
            nonSharedTris[rectCount * 6 + 1] = vertOrUVIdx1;
            nonSharedTris[rectCount * 6 + 2] = vertOrUVIdx2;

            nonSharedTris[rectCount * 6 + 3] = vertOrUVIdx2;
            nonSharedTris[rectCount * 6 + 4] = vertOrUVIdx3;
            nonSharedTris[rectCount * 6 + 5] = vertOrUVIdx0;
        }

        int NumTriangles = tr2room.RoomData.NumTriangles;

        Parser.Tr2Face3[] Triangles = tr2room.RoomData.Triangles;
        for (int triCount = 0; triCount < tr2room.RoomData.NumTriangles; triCount++)
        {
            ////print("tr2room.RoomData.NumTriangles"+ tr2room.RoomData.NumTriangles);

            int Idx0 = Triangles[triCount].Vertices0;
            int Idx1 = Triangles[triCount].Vertices1;
            int Idx2 = Triangles[triCount].Vertices2;

            ////print ("idx0 - Idx1 - Idx2:" + Idx0 + " " + Idx1 + " " + Idx2);
            //[][][][]+[][][]
            int vertOrUVIdx0 = triCount * 3 + 0;
            int vertOrUVIdx1 = triCount * 3 + 1;
            int vertOrUVIdx2 = triCount * 3 + 2;

            if (has_water)
            {
                if (!IsFaceInWater(is_water_vertex, Idx0, Idx1, Idx2)) //if not all vertices are in water
                {
                    continue;
                }
            }
            else
            {
                if (!IsFaceInWater(leveldata, Triangles[triCount].Texture))
                {
                    continue;
                }
            }


            nonSharedVertices[strideVertIdx + vertOrUVIdx0] = sharedVertices[Idx0];
            nonSharedVertices[strideVertIdx + vertOrUVIdx1] = sharedVertices[Idx1];
            nonSharedVertices[strideVertIdx + vertOrUVIdx2] = sharedVertices[Idx2];

            ushort texObjectIdx = Triangles[triCount].Texture;
            if (texObjectIdx >= leveldata.ObjectTextures.Length)
            {
                continue;                                                  //fixed:  outof bound exception for Parser.Tr2Level.ObjectTextures
            }
            Parser.Tr2ObjectTexture texObj = leveldata.ObjectTextures[texObjectIdx];

            //if(texTileIdx != prevTexture)
            //{
            //newMatCount +=1;
            //prevTexture = texTileIdx;
            ////print("newMatCount:"+ newMatCount);
            //}

            SetFaceUVs(nonSharedUVs, strideVertIdx + vertOrUVIdx0, strideVertIdx + vertOrUVIdx1, strideVertIdx + vertOrUVIdx2, texObj);
            int texture_idx = GetAnimatedTextureIndex(leveldata.AnimatedTextures, leveldata.NumAnimatedTextures, texObjectIdx);
            if (texture_idx > -1)
            {
                texObj = leveldata.ObjectTextures[texture_idx];
                SetFaceUVs(nonSharedUV2s, strideVertIdx + vertOrUVIdx0, strideVertIdx + vertOrUVIdx1, strideVertIdx + vertOrUVIdx2, texObj);
            }

            ////print("uv[Idx0]"+ uv[Idx0].x + " " + uv[Idx0].y);
            ////print("uv[Idx1]"+ uv[Idx1].x + " " + uv[Idx1].y);

            //ushort opacity = texObj.TransparencyFlags;  //isItOpacq

            nonSharedTris[strideTriIdx + vertOrUVIdx0] = strideVertIdx + vertOrUVIdx0;
            nonSharedTris[strideTriIdx + vertOrUVIdx1] = strideVertIdx + vertOrUVIdx1;
            nonSharedTris[strideTriIdx + vertOrUVIdx2] = strideVertIdx + vertOrUVIdx2;

            ////print ("idx0 - Idx1 - Idx2:" + nonSharedTris[strideTriIdx + vertOrUVIdx0]  + " " +  nonSharedTris[strideTriIdx + vertOrUVIdx1]  + " " + nonSharedTris[strideTriIdx + vertOrUVIdx2] );
        }

        ////print("leveldata.Rooms[5].RoomData.NumRectangles:"+ tr2room.RoomData.NumRectangles);
        //SetTriangles (triangles : int[], submesh : int) : void
        //generate secondary uv set

        for (int i = 0; i < nonSharedVertices.Length; i++)
        {
            nonSharedVertices[i] = nonSharedVertices[i] * Settings.SceneScaling;
        }
        Mesh mesh = new Mesh();

        mesh.Clear();
        mesh.vertices  = nonSharedVertices;
        mesh.uv        = nonSharedUVs;
        mesh.uv2       = nonSharedUV2s;
        mesh.triangles = nonSharedTris;
        //mesh.Optimize();
        mesh.RecalculateNormals();
#if UNITY_EDITOR
        Vector4[] tangents = new Vector4[mesh.vertices.Length];
        computeTangentsAndBinormals(nonSharedVertices, mesh.normals, nonSharedUVs, nonSharedTris, tangents);
        mesh.tangents = tangents;
        tangents      = null;
#endif
        //free some memory
        nonSharedVertices = null;
        nonSharedUVs      = null;
        nonSharedUV2s     = null;
        nonSharedTris     = null;


        //}

        return(mesh);
    }
Ejemplo n.º 8
0
    public Level(Parser.Tr2Level leveldata, Material sharedmaterial, Transform roottransform)
    {
        m_LevelRoot = roottransform;
        m_leveldata = leveldata;
        //if (m_leveldata != null && m_leveldata.NumRooms > 0)
        {
            //TextureUV.GenerateTextureTile ismoved to Loader.cs for better responsibility managment
            //Trying to set assigned render material property, marks shared material as instance.
            //So change property of shared material before assign it to renderer.
            m_SharedMaterialWater       = Resources.Load("water", typeof(Material)) as Material;
            m_SharedMaterialWaterHolder = Resources.Load("water_holder", typeof(Material)) as Material;
            Shader waterEffectShader = Resources.Load("WaterEffect", typeof(Shader)) as Shader;
            //init materials
            m_SharedMaterial       = sharedmaterial;
            m_SharedMaterial.color = new Color(1f, 1f, 1f, 1.0f);
            m_SharedMaterial.SetFloat("_InSideWater", 0);
            m_SharedMaterial.SetFloat("_WaterPlaneY", 0);

            m_SharedMaterialWater.mainTexture = m_SharedMaterial.mainTexture;
            //m_SharedMaterialWater.color = new Color(0.045f, 0.075f,0.090f, 1) ; //should be set by user
            m_SharedMaterialWaterHolder.mainTexture = m_SharedMaterial.mainTexture;
            //m_SharedMaterialWaterHolder.color = new Color(0.45f * 0.5f, 0.75f * 0.5f, 0.90f * 0.5f, 1);
            m_SharedMaterialWaterHolder.SetFloat("_InSideWater", 0);

            m_RoomExs = new RoomEx[m_leveldata.NumRooms];

            Transform PrefavContainer = new GameObject("PrefavContainer").transform;
            PrefavContainer.parent = m_LevelRoot;

            m_DynamicPrefabs = BuildDynamicPrefabObjects(PrefavContainer);
            m_StaticPrefabs  = BuildStaticPrefabObjects(PrefavContainer);

            //determine animation clip size for each movable object
            for (int i = 0; i < m_DynamicPrefabs.Count - 1; i++)
            {
                int startoffset0 = m_DynamicPrefabs[i].Animation;
                int startoffset1 = m_DynamicPrefabs[i + 1].Animation;
                m_DynamicPrefabs[i].NumClips = startoffset1 - startoffset0;
            }
            if (m_DynamicPrefabs.Count > 0)
            {
                int startoffset0 = m_DynamicPrefabs[m_DynamicPrefabs.Count - 1].Animation;
                m_DynamicPrefabs[m_DynamicPrefabs.Count - 1].NumClips = (int)m_leveldata.NumAnimations - 1 - startoffset0;
            }

            //attach animation and their state change
            for (int i = 0; i < m_DynamicPrefabs.Count; i++)
            {
                List <TRAnimationClip> clips = Animator.AttachAnimation(m_DynamicPrefabs[i], m_leveldata);
                AnimationStateMapper.BuildMap(clips, m_DynamicPrefabs[i].UnityAnimation, m_leveldata);
            }

            //attach 3DText Box to movable objects  to mark their ID
            if (Settings.ShowObjectID)
            {
                for (int i = 0; i < m_DynamicPrefabs.Count; i++)
                {
                    if (m_leveldata.Text3DPrefav != null)
                    {
                        TextMesh text3d = (TextMesh)GameObject.Instantiate(m_leveldata.Text3DPrefav);
                        text3d.transform.position = m_DynamicPrefabs[i].UnityObject.transform.position + Vector3.up * 1000 * Settings.SceneScaling;
                        text3d.transform.parent   = m_DynamicPrefabs[i].UnityObject.transform;
                        text3d.characterSize      = 100 * Settings.SceneScaling;
                        text3d.text = "" + m_DynamicPrefabs[i].ObjectID;
                    }
                }
            }

            //build rooms
            //container for water go
            Transform WaterContainer = new GameObject("WaterContainer").transform;
            WaterContainer.parent = m_LevelRoot;

            Transform RoomContainer = new GameObject("RoomContainer").transform;
            RoomContainer.parent = m_LevelRoot;

            for (int i = 0; i < m_leveldata.NumRooms; i++)
            {
                Parser.Tr2Room tr2room   = leveldata.Rooms[i];
                bool           has_water = false;
                Mesh           roommesh  = MeshBuilder.CreateRoomMesh(tr2room, m_leveldata, ref has_water);
                Vector3        position  = new Vector3(m_leveldata.Rooms[i].info.x, 0, m_leveldata.Rooms[i].info.z);
                GameObject     go        = CreateRoom(roommesh, position * Settings.SceneScaling, i, m_SharedMaterial, FloorAttribute.Solid);
                go.transform.parent = RoomContainer;
                m_RoomExs[i]        = go.AddComponent <RoomEx>();
                //build room object
                List <GameObject> objects = InstantiateStaticObjects(tr2room, i, go.transform);
                m_RoomExs[i].InitRoom(tr2room, objects);



                if ((tr2room.Flags & 1) == 1) //Is room water holder
                {
                    //override water holder material
                    //MeshFilter mf = go.GetComponent<MeshFilter>();
                    //mf.mesh = MeshModifier.VertexWeild(mf.mesh);

                    MeshRenderer mr = go.GetComponent <MeshRenderer>();
                    mr.sharedMaterial = new Material(waterEffectShader); // Generate material instances for water holder using m_SharedMaterialWaterHolder
                    mr.receiveShadows = false;
                    Vector3 center = m_RoomExs[i].GetCenterPoint();
                    mr.sharedMaterial.SetFloat("_CenterX", center.x);
                    mr.sharedMaterial.SetFloat("_CenterY", center.y);
                    mr.sharedMaterial.SetFloat("_CenterZ", center.z);
                    mr.sharedMaterial.SetFloat("_WaterPlaneY", Mathf.Infinity);
                    mr.sharedMaterial.SetTexture("_MainTex", m_SharedMaterialWaterHolder.mainTexture);
                    mr.sharedMaterial.SetColor("_Color", m_SharedMaterialWaterHolder.color);
                    mr.sharedMaterial.SetFloat("_InSideWater", 0);
                    m_InstancedMaterialWaterHolders.Add(mr.sharedMaterial);
                }
                else //regular room
                {
                }

                //create room water surface
                if (has_water) //surface?
                {
                    //create water surface
                    roommesh            = MeshBuilder.CreateRoomWaterMesh(tr2room, m_leveldata);
                    go                  = CreateRoom(roommesh, position * Settings.SceneScaling, i, m_SharedMaterialWater, FloorAttribute.Water);
                    go.name             = "water_" + i;
                    go.transform.parent = WaterContainer;
                }
            }

            Transform ObjectContainer = new GameObject("ObjectContainer").transform;
            ObjectContainer.parent = m_LevelRoot;

            m_MovableInstances = InstantiateDynamicObjects(ObjectContainer);
            try //
            {
                SetupTrigers();
            }
            catch (System.Exception e)
            {
                Debug.LogError(e.Message);
            }

            //attach components to m_MovableInstances
            for (int i = 0; i < m_MovableInstances.Count; i++)
            {
                InitialiseInstance(m_MovableInstances[i]);
            }
        }
    }