Example #1
0
        /// <summary>
        /// Get a FacetSector given an ID. FacetSector contains tile information about current block.
        /// UOPs related.
        /// </summary>
        /// <param name="sector"></param>
        /// <returns></returns>
        public static FacetSector getSector(int sector)
        {
            //Fast search
            if (facetSectors.ContainsKey(sector))
            {
                return(facetSectors[sector]);
            }

            int currentFacet = 0;
            //Look for the file in facet hashes
            string toHash = string.Format("build/sectors/facet_{0:D2}/{1:D8}.bin", currentFacet, sector);
            ulong  hash   = HashDictionary.HashFileName(toHash);

            if (!UOResourceManager.uopHashes.facet0Hashes.ContainsKey(hash))
            {
                UOConsole.Fatal("Cannot find {0} in facet0Hashes", toHash);
                return(null);
            }
            UOResourceManager.uopMapping_t map = UOResourceManager.uopHashes.facet0Hashes[hash];

            MythicPackage _uop = new MythicPackage(fileDirectory + "facet0.uop");

            byte[] raw = _uop.Blocks[map.block].Files[map.file].Unpack(_uop.FileInfo.FullName);

            FacetSector fs;

            using (MemoryStream ms = new MemoryStream(raw)){
                using (BinaryReader r = new BinaryReader(ms)) {
                    fs = FacetSector.readSector(r);
                }
            }

            facetSectors[sector] = fs;
            return(fs);
        }
Example #2
0
        /// <summary>
        /// MAIN THREAD -> TODO : Separate loading from GameObjects.
        /// Build the terrain mesh for the current sectorID
        /// </summary>
        /// <param name="fs"></param>
        /// <returns></returns>
        private static facetSectorMesh buildTerrainMesh(FacetSector fs)
        {
            facetSectorMesh mesh = new facetSectorMesh();

            int worldY = (fs.sectorID % 64) * 64;
            int worldX = (fs.sectorID / 64) * 64;

            mesh.vertices = new Vector3[4 * (64 * 64)];
            mesh.normals  = new Vector3[4 * (64 * 64)];
            mesh.uvs      = new Vector2[4 * 64 * 64];

            mesh.triangles = new int[3 * (64 * 64 * 2)];

            int flipper = -1;

            for (int x = 0; x < fs.tiles.Length; ++x)
            {
                for (int y = 0; y < fs.tiles[x].Length; ++y)
                {
                    float z           = +fs.tiles[x][y].z * 6 / UOSprite.UOEC_SIZE;
                    int   idxVertices = 4 * (x + y * 64);

                    float z0, z1, z2, z3;
                    z0 = z;
                    z1 = z;
                    z2 = z;
                    z3 = z;
                    if (x == 0)
                    {
                        //TODO: take information from other sector delimiters
                    }
                    else if (y == 0)
                    {
                    }
                    else
                    {
                        z0 = (fs.tiles[x - 1][y - 1].z) * 6 / (UOSprite.UOEC_SIZE);
                        z1 = (fs.tiles[x][y - 1].z) * 6 / (UOSprite.UOEC_SIZE);
                        z2 = (fs.tiles[x - 1][y].z) * 6 / (UOSprite.UOEC_SIZE);
                        z3 = (fs.tiles[x][y].z) * 6 / (UOSprite.UOEC_SIZE);
                    }

                    int _x = x + worldX, _y = y + worldY;
                    // 0	1
                    // |  \ |
                    // 2	3
                    //Vertices
                    mesh.vertices[idxVertices + 0] = new Vector3(_x - z0, flipper * _y + z0, 100 - z0);
                    mesh.vertices[idxVertices + 1] = new Vector3(_x + 1 - z1, flipper * _y + z1, 100 - z1);
                    mesh.vertices[idxVertices + 2] = new Vector3(_x - z2, flipper * (_y + 1) + z2, 100 - z2);
                    mesh.vertices[idxVertices + 3] = new Vector3(_x + 1 - z3, flipper * (_y + 1) + z3, 100 - z3);

                    //Normals
                    mesh.normals[idxVertices + 0] = mesh.normals[idxVertices + 1] = mesh.normals[idxVertices + 2] = mesh.normals[idxVertices + 3] = Vector3.up;


                    //Triangles - Clockwise
                    List <int> subTriangles;                   // List containin all indices of sametexture
                    uvStatus   uvOffset;

                    TextureImageInfo textureInfo = UOResourceManager.getLandtileTextureID(fs.tiles[x][y].landtileGraphic);
                    if (mesh.subMeshes.ContainsKey(textureInfo.textureIDX))
                    {
                        subTriangles = mesh.subMeshes[textureInfo.textureIDX];                        //Get the already instanced list
                        uvOffset     = mesh.subMeshTextureOffset[textureInfo.textureIDX];
                    }
                    else
                    {
                        //Create a new list and set it
                        subTriangles = new List <int>();
                        mesh.subMeshes.Add(textureInfo.textureIDX, subTriangles);
                        //Each subMesh has a material
                        Material mat = UOResourceManager.getResource(textureInfo, ShaderTypes.Terrain).getMaterial();
                        mesh.materials.Add(mat);
                        //
                        uvOffset   = new uvStatus();
                        uvOffset.u = 0;
                        uvOffset.v = 0;
                        mesh.subMeshTextureOffset[textureInfo.textureIDX] = uvOffset;
                    }

                    //Bottom triangles
                    int offset = idxVertices;
                    subTriangles.Add(offset);
                    subTriangles.Add(offset + 3);
                    subTriangles.Add(offset + 2);
                    //Upper triangles
                    subTriangles.Add(offset);
                    subTriangles.Add(offset + 1);
                    subTriangles.Add(offset + 3);
                    //End Triangles

                    //UVs
                    float us, ue, vs, ve;
                    float texSize = 1.0f / textureInfo.repetition;
                    us = uvOffset.u;
                    ue = uvOffset.u + texSize;
                    vs = uvOffset.v;
                    ve = uvOffset.v + texSize;

                    if (ue > 1.0f)
                    {
                        ue -= 1.0f;
                    }
                    if (ve > 1.0f)
                    {
                        ve -= 1.0f;
                    }
                    mesh.uvs[idxVertices + 0] = new Vector2(us, ve);
                    mesh.uvs[idxVertices + 1] = new Vector2(ue, ve);
                    mesh.uvs[idxVertices + 2] = new Vector2(us, vs);
                    mesh.uvs[idxVertices + 3] = new Vector2(ue, vs);

                    //TODO Correct handling
                    //uvOffset.u += texSize;
                    //uvOffset.v += texSize;
                    if (uvOffset.v >= 1.0f)
                    {
                        uvOffset.v = 0;
                    }
                    if (uvOffset.u >= 1.0f)
                    {
                        uvOffset.u = 0;
                    }
                }
            }


            return(mesh);
        }