UnityRose.Game.RosePatch.Load C# (CSharp) Method

Load() public method

public Load ( ) : bool
return bool
        public bool Load()
        {
            if (!m_isValid)
            {
                Debug.LogError("Cannot load patch at path " + this.m_assetDir);
                return false;
            }

            // TODO: add error handling for failure to load the following files
            this.m_HIM = new HIM(this.m_assetDir.Parent.FullName + "/" + this.m_name + ".HIM");
            this.m_TIL = new TIL(this.m_assetDir.Parent.FullName + "/" + this.m_name + ".TIL");
            if (this.m_ZON == null)  // load ZON if it was never passed to the patch constructor
                this.m_ZON = new ZON(this.m_assetDir.Parent.FullName + "/" + this.m_assetDir.Parent.Name + ".ZON");
            this.m_IFO = new IFO(this.m_assetDir.Parent.FullName + "/" + this.m_name + ".IFO");

            //	parent x   :       	4	  3		2	  1	   0
            // original dir: .../3ddata/maps/junon/jpt01/30_30
            // desired dir: .../3ddata/junon/LIST_CNST_JPT.ZSC
            char[] trimChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_' };

            // TODO: use stl's to avoid this mess and be able to load luna/eldeon/oro maps
            string zscPath = m_assetDir.Parent.Name.ToLower().Trim(trimChars);
            if(zscPath.Contains("jd") || zscPath.Contains("jdt") || zscPath.Contains("jg") )
                zscPath = Utils.FixPath(m_3dDataDir + "/" + m_assetDir.Parent.Parent.Name.ToUpper() + "/LIST_CNST_" + zscPath + ".ZSC");// + m_assetDir.Parent.Name.Trim(trimChars).ToUpper() + ".ZSC");
            else
                zscPath = Utils.FixPath(m_3dDataDir + "/" + m_assetDir.Parent.Parent.Name.ToUpper() + "/LIST_" + "CNST_JPT.ZSC");// + m_assetDir.Parent.Name.Trim(trimChars).ToUpper() + ".ZSC");

            string litPath = Utils.FixPath(this.m_assetDir.Parent.FullName + "\\" + this.m_name + "\\LIGHTMAP\\BUILDINGLIGHTMAPDATA.LIT");
            groundLight = Utils.FixPath(this.m_assetDir.Parent.FullName + "\\" + this.m_name + "\\" + this.m_name + "_PLANELIGHTINGMAP.DDS");
            m_ZSC_Cnst = new ZSC(zscPath);
            m_ZSC_Deco = new ZSC(zscPath.ToLower().Replace("cnst","deco"));
            m_LIT_Cnst = new LIT(litPath);
            m_LIT_Deco = new LIT(litPath.Replace("building","object"));
            // TODO: add any new file loads here

            edgeVertexLookup = new Dictionary<string, List<int>>();

            return true;
        }

Usage Example

Example #1
0
    private void ImportMap(string mapName)
    {
        Debug.Log ("Importing map from " + m_inputDir + "...");
        bool success = true;

        DirectoryInfo dirs = new DirectoryInfo(m_inputDir);

        GameObject map = new GameObject();
        map.name = mapName;

        GameObject terrain = new GameObject();
        terrain.name = "Ground";
        terrain.transform.parent = map.transform;
        terrain.layer = LayerMask.NameToLayer("Floor");

        GameObject terrainObjects = new GameObject();
        terrainObjects.name = "Objects";
        terrainObjects.transform.parent = map.transform;
        terrainObjects.layer = LayerMask.NameToLayer("MapObjects");

        List<RosePatch> patches = new List<RosePatch>();
        Dictionary<string, Rect> atlasRectHash = new Dictionary<string, Rect>();
        Dictionary<string, Texture2D> atlasTexHash = new Dictionary< string, Texture2D >();
        List<Texture2D> textures = new List<Texture2D>();

        // Instantiate all patches
        foreach(DirectoryInfo dir in dirs.GetDirectories())
        {
            if(!dir.Name.Contains("."))
            {
                RosePatch patch = new RosePatch( dir );
                patch.Load();
                patch.UpdateAtlas( ref atlasRectHash, ref atlasTexHash, ref textures );
                patches.Add ( patch );
            }
        }
        // Create a texture atlas from the textures of all patches and populate the rectangles in the hash

        // Figure out the required size of the atlas from the number of textures in the atlas
        int height, width;  // these must be powers of 2 to be compatible with iPhone
        if( atlasRectHash.Count <= 16 )  width = height = 4*256;
        else if( atlasRectHash.Count <= 32 )  { width = 8*256; height = 4*256; }
        else if( atlasRectHash.Count <= 64 )  { width = 8*256; height = 8*256; }
        else if( atlasRectHash.Count <= 128 ) { width = 16*256; height = 8*256; }
        else if( atlasRectHash.Count <= 256 ) { width = 16*256; height = 16*256; }
        else throw new Exception("Number of tiles in terrain is larger than supported by terrain atlas");

        Texture2D atlas = new Texture2D(width, height);

        // Pack the textures into one texture atlas
        Rect[] rects = atlas.PackTextures( textures.ToArray(), 0, Math.Max(width, height) );
        atlas.anisoLevel = 11;

        Texture2D myAtlas = new Texture2D(width, height);
        myAtlas.SetPixels32( atlas.GetPixels32(0), 0);

        string atlasPath = "Assets/Terrain/Textures/" + mapName + "_atlas.png";

        if( !File.Exists( atlasPath ))
        {
            FileStream fs = new FileStream( atlasPath, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(myAtlas.EncodeToPNG());
            bw.Close();
            fs.Close();

            AssetDatabase.Refresh();
        }

        myAtlas = Utils.loadTex(ref atlasPath);

        // copy rects back to hash (should update rect refs in Tile objects
        int rectID = 0;
        foreach( string key in atlasTexHash.Keys)
            atlasRectHash[key] = rects[rectID++];

        // Generate the patches
        foreach(RosePatch patch in patches)
            patch.Import(terrain.transform, terrainObjects.transform, myAtlas, myAtlas, atlasRectHash);

        //blend vertex normals at the seams between patches
        Dictionary<string, List<PatchNormalIndex>> patchNormalLookup = new Dictionary<string, List<PatchNormalIndex>>();
        int patchID = 0;
        // combine all normal lookups into one big lookup containing patch ID and normal ID
        foreach(RosePatch patch in patches)
        {

            // go through the lookup of this patch and append all normal id's to big lookup
            foreach(string vertex in patch.edgeVertexLookup.Keys)
            {
                List<PatchNormalIndex> ids = new List<PatchNormalIndex>();
                foreach(int id in patch.edgeVertexLookup[vertex])
                    ids.Add(new PatchNormalIndex(patchID, id));

                if(!patchNormalLookup.ContainsKey(vertex))
                    patchNormalLookup.Add(vertex, ids);
                else
                    patchNormalLookup[vertex].AddRange(ids);

            }

            patchID++;
        }

        // go through each enttry in the big lookup and calculate avg normal, then assign to corresponding patches
        foreach(string vertex in patchNormalLookup.Keys)
        {
            Vector3 avg = Vector3.zero;
            // First pass: calculate average normal
            foreach(PatchNormalIndex entry in patchNormalLookup[vertex])
                avg += patches[entry.patchID].m_mesh.normals[entry.normalID];

            avg.Normalize();

            // Second pass: assign new normal to corresponding patches
            foreach(PatchNormalIndex entry in patchNormalLookup[vertex])
                patches[entry.patchID].m_mesh.normals[entry.normalID] = avg;

        }

        terrainObjects.transform.localScale = new Vector3(1.0f, 1.0f, -1.0f);
        terrainObjects.transform.Rotate (0.0f, -90.0f, 0.0f);
        terrainObjects.transform.position = new Vector3(5200.0f, 0.0f, 5200.0f);

        if(success)
            Debug.Log ("Map Import Complete");
        else
            Debug.Log ("!Map Import Failed");
    }