Пример #1
0
 // Check a road-cell position
 public bool CheckRoadCellPos(INTVECTOR3 rcpos)
 {
     if (rcpos.x < 0)
     {
         return(false);
     }
     if (rcpos.y < 0)
     {
         return(false);
     }
     if (rcpos.z < 0)
     {
         return(false);
     }
     if (rcpos.x >= CELL_AXIS_COUNT)
     {
         return(false);
     }
     if (rcpos.y >= CELL_YAXIS_COUNT)
     {
         return(false);
     }
     if (rcpos.z >= CELL_AXIS_COUNT)
     {
         return(false);
     }
     return(true);
 }
Пример #2
0
 void CullObject()
 {
     #region Culling_Object
     // Culling Object
     Plane[] camraPlanes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
     foreach (KeyValuePair <int, RGLODQuadTreeNode> kvp in mRootNodes)
     {
         INTVECTOR3 pos = Utils.IndexToPos(kvp.Key);
         pos.x = (pos.x << evniAsset.SHIFT);
         pos.z = (pos.z << evniAsset.SHIFT);
         int    size     = evniAsset.CHUNKSIZE * (1 << kvp.Value.LOD);
         int    halfSize = size / 2;
         Bounds bound    = new Bounds(new Vector3(pos.x + halfSize, 500, pos.z + halfSize), new Vector3(size, 1100, size));
         if (GeometryUtility.TestPlanesAABB(camraPlanes, bound))
         {
             kvp.Value.visible = true;
             mTree.TraversalNode(kvp.Value, _delTrue);
         }
         else
         {
             kvp.Value.visible = false;
             mTree.TraversalNode(kvp.Value, _delFalse);
         }
     }
     #endregion
 }
Пример #3
0
 public INTVECTOR4(INTVECTOR3 v3, int w_)
 {
     x = v3.x;
     y = v3.y;
     z = v3.z;
     w = w_;
 }
Пример #4
0
 public INTVECTOR3 ClampInBound(INTVECTOR3 pos)
 {
     if (pos.x < min.x)
     {
         pos.x = min.x;
     }
     if (pos.x > max.x - 1 + close_max)
     {
         pos.x = max.x - 1 + close_max;
     }
     if (pos.y < min.y)
     {
         pos.y = min.y;
     }
     if (pos.y > max.y - 1 + close_max)
     {
         pos.y = max.y - 1 + close_max;
     }
     if (pos.z < min.z)
     {
         pos.z = min.z;
     }
     if (pos.z > max.z - 1 + close_max)
     {
         pos.z = max.z - 1 + close_max;
     }
     return(pos);
 }
Пример #5
0
    public void OutputVoxels(Vector3 offset, OnOutputVoxel output_function)
    {
        if (ISO == null)
        {
            return;
        }
        if (output_function == null)
        {
            return;
        }
        foreach (KeyValuePair <int, VCVoxel> kvp in ISO.m_Voxels)
        {
            Vector3 lpos = new Vector3(kvp.Key & 0x3ff, kvp.Key >> 20, (kvp.Key >> 10) & 0x3ff);
            Vector3 wpos = OriginTransform.position
                           + lpos.x * OriginTransform.right
                           + lpos.y * OriginTransform.up
                           + lpos.z * OriginTransform.forward;
            wpos += offset;

            INTVECTOR3 wpos_floor = new INTVECTOR3(Mathf.FloorToInt(wpos.x), Mathf.FloorToInt(wpos.y), Mathf.FloorToInt(wpos.z));
            INTVECTOR3 wpos_ceil  = new INTVECTOR3(Mathf.CeilToInt(wpos.x), Mathf.CeilToInt(wpos.y), Mathf.CeilToInt(wpos.z));

            if (wpos_floor == wpos_ceil)
            {
                output_function(wpos_floor.x, wpos_floor.y, wpos_floor.z, kvp.Value);
            }
            else
            {
                for (int x = wpos_floor.x; x <= wpos_ceil.x; ++x)
                {
                    for (int y = wpos_floor.y; y <= wpos_ceil.y; ++y)
                    {
                        for (int z = wpos_floor.z; z <= wpos_ceil.z; ++z)
                        {
                            float deltax = 1 - Mathf.Abs(wpos.x - x);
                            float deltay = 1 - Mathf.Abs(wpos.y - y);
                            float deltaz = 1 - Mathf.Abs(wpos.z - z);
                            float u      = deltax * deltay * deltaz;
                            if (u < 0.5f)
                            {
                                u = u / (0.5f + u);
                            }
                            else
                            {
                                u = 0.5f / (1.5f - u);
                            }
                            VCVoxel voxel = kvp.Value;
                            voxel.Volume = (byte)Mathf.CeilToInt(voxel.Volume * u);
                            if (voxel.Volume > 1)
                            {
                                output_function(x, y, z, voxel);
                            }
                        }
                    }
                }
            }
        }
    }
Пример #6
0
        public void DrawAreaInEditor()
        {
            INTVECTOR3 size  = new INTVECTOR3(mEvni.CHUNKSIZE, 0, mEvni.CHUNKSIZE);
            INTVECTOR3 pos   = new INTVECTOR3(xIndex, 0, zIndex);
            Color      color = Color.yellow;


            Pathea.Graphic.EditorGraphics.DrawXZRect((Vector3)(pos * size), (Vector3)size, color);
        }
Пример #7
0
    public RoadCell GetRoadCell(INTVECTOR3 rcpos)
    {
        int hash = rcpos.hash;

        if (m_Road.ContainsKey(hash))
        {
            return(m_Road[hash]);
        }
        return(new RoadCell(0));
    }
Пример #8
0
            public static INTBOUNDS3 operator *(INTBOUNDS3 lhs, int rhs)
            {
                if (lhs.close_max == 0)
                {
                    return(new INTBOUNDS3(lhs.min * rhs, lhs.max * rhs));
                }
                INTVECTOR3 ofs    = new INTVECTOR3(lhs.close_max, lhs.close_max, lhs.close_max);
                INTBOUNDS3 retval = new INTBOUNDS3(lhs.min * rhs, (lhs.max + ofs) * rhs - ofs);

                retval.close_max = lhs.close_max;
                return(retval);
            }
Пример #9
0
 public override bool Equals(object obj)
 {
     if (null == obj)
     {
         return(false);
     }
     if (obj is INTVECTOR3)
     {
         INTVECTOR3 vec = (INTVECTOR3)obj;
         return(x == vec.x && y == vec.y && z == vec.z);
     }
     return(false);
 }
Пример #10
0
    public static void AddDeletedGrass(INTVECTOR3 pos)
    {
        if (m_mapDelPos == null)
        {
            Debug.LogError("LSubTerrSL haven't initialized!");
            return;
        }

        if (!m_mapDelPos.ContainsKey(pos))
        {
            m_mapDelPos.Add(pos, pos);
        }
    }
Пример #11
0
    public static bool DeleteAtPos(Vector3 voxelPos)
    {
        if (Self == null)
        {
            Debug.LogError("The Grass System is not initialized");
            return(false);
        }

        INTVECTOR3 pos = new INTVECTOR3((int)voxelPos.x, (int)(voxelPos.y + 0.5f), (int)voxelPos.z);

        if (_self.scene.data.Remove(pos.x, pos.y, pos.z))
        {
            GrassDataSL.AddDeletedGrass(pos);
            return(true);
        }

        return(false);
    }
Пример #12
0
    public override void ProcessReqsImmediatly()
    {
        try
        {
            while (mReqs.Count != 0)
            {
                RedGrass.RGChunk chunk = null;

                lock (mReqs)
                {
                    chunk = mReqs.Dequeue();
                }

                BinaryReader _in        = new BinaryReader(mOrgnGrassFile);
                INTVECTOR3   chunk32Pos = ChunkPosToPos32(chunk.xIndex, chunk.zIndex);
                int          expands    = mEvni.CHUNKSIZE / mEvni.Tile;

                lock (chunk)
                {
                    for (int _x = chunk32Pos.x; _x < chunk32Pos.x + expands; ++_x)
                    {
                        for (int _z = chunk32Pos.z; _z < chunk32Pos.z + expands; ++_z)
                        {
                            int index32 = Pos32ToIndex32(_x, _z);
                            _in.BaseStream.Seek(mOrgnOfsData[index32], SeekOrigin.Begin);
                            int count = mOrgnLenData[index32];
                            for (int i = 0; i < count; ++i)
                            {
                                RedGrass.RedGrassInstance rgi = new RedGrassInstance();

                                rgi.ReadFromStream(_in);

                                chunk.Write(rgi);
                            }
                        }
                    }
                }
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.ToString());
        }
    }
Пример #13
0
    public static void Import(byte[] buffer)
    {
        if (buffer == null)
        {
            return;
        }
        if (buffer.Length < 8)
        {
            return;
        }

        MemoryStream ms      = new MemoryStream(buffer);
        BinaryReader r       = new BinaryReader(ms);
        int          version = r.ReadInt32();

        if (VERSION != version)
        {
            Debug.LogWarning("The version of LSubTerrSL is newer than the record.");
        }

        switch (version)
        {
        case 0x0101:
        {
            int conut = r.ReadInt32();
            for (int i = 0; i < conut; ++i)
            {
                INTVECTOR3 pos = new INTVECTOR3();
                pos.x = r.ReadInt32();
                pos.y = r.ReadInt32();
                pos.z = r.ReadInt32();

                m_mapDelPos.Add(pos, pos);
            }
        } break;

        default:
            break;
        }

        r.Close();
        ms.Close();
    }
Пример #14
0
 public INTBOUNDS3(INTVECTOR3 _min, INTVECTOR3 _max)
 {
     close_max = 0;
     min       = _min;
     max       = _max;
 }
Пример #15
0
    public static void SaveToOriginalFile()
    {
        RedGrass.EvniAsset evni = Resources.Load(s_EvniPath) as RedGrass.EvniAsset;
        if (evni == null)
        {
            Debug.LogError("EvniAsset is missiing");
        }

        // Open original subterrain data file
        FileStream[] orgnGrassFiles = new FileStream[9];
        int[,]  orgnOfsData = new int[9, evni.XZTileCount];

        for (int i = 0; i < 9; ++i)
        {
            orgnGrassFiles[i] = new FileStream(s_orgnFilePath[i], FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            BinaryReader _in = new BinaryReader(orgnGrassFiles[i]);
            for (int j = 0; j < evni.XZTileCount; ++j)
            {
                orgnOfsData[i, j] = _in.ReadInt32();
            }
        }

        #region MERGE_CACHES
        // Merge the cache
        Dictionary <int, List <RedGrassInstance> > file_add_dic = new Dictionary <int, List <RedGrassInstance> >();

        string[] raw_file_path = Directory.GetFiles(GameConfig.GetUserDataPath() + GameConfig.CreateSystemData + "/Grasses/");
        Dictionary <int, Dictionary <INTVECTOR3, RedGrassInstance> > raw_file_add = new Dictionary <int, Dictionary <INTVECTOR3, RedGrassInstance> >();
        List <int>            version_list      = new List <int>();
        Dictionary <int, int> modify_file_index = new Dictionary <int, int>();
        List <string>         tempfile_to_del   = new List <string>();

        for (int i = 0; i < raw_file_path.Length; i++)
        {
            EditorUtility.DisplayProgressBar("Merging Grasses...", "Read raw data.. (" + (i + 1).ToString() + " of " + raw_file_path.Length.ToString() + ")", 0.2f * ((float)(i) / (float)(raw_file_path.Length)));

            // the expanded-name must be "gs"
            if (raw_file_path[i].Substring(raw_file_path[i].LastIndexOf(".") + 1) != "gs")
            {
                continue;
            }

            // Read the caches
            // Read the caches
            using (FileStream raw_files = new FileStream(raw_file_path[i], FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                BinaryReader _in     = new BinaryReader(raw_files);
                int          version = _in.ReadInt32();
                version_list.Add(version);
                int cnt = _in.ReadInt32();

                raw_file_add.Add(version, new Dictionary <INTVECTOR3, RedGrassInstance>());
                for (int j = 0; j < cnt; ++j)
                {
                    RedGrassInstance rgi = new RedGrassInstance();
                    rgi.ReadFromStream(_in);
                    INTVECTOR3 index = Utils.WorldPosToVoxelPos(rgi.Position);
                    raw_file_add[version][index] = rgi;
                }
            }

            tempfile_to_del.Add(raw_file_path[i]);
        }

        if (version_list.Count == 0)
        {
            EditorUtility.ClearProgressBar();
            EditorUtility.DisplayDialog("Merging Grasses", "Nothing changed !", "OK");
            return;
        }

        // Descending order the version_list
        version_list.Sort(delegate(int small, int big) {
            if (small > big)
            {
                return(-1);
            }
            else if (small == big)
            {
                return(0);
            }
            else
            {
                return(1);
            }
        });

        // Merge cache to a list
        for (int i = 0; i < version_list.Count; ++i)
        {
            int version = version_list[i];


            // Add to list
            foreach (KeyValuePair <INTVECTOR3, RedGrassInstance> kvp in raw_file_add[version])
            {
                for (int j = i + 1; j < version_list.Count; ++j)
                {
                    int l_version = version_list[j];
                    if (raw_file_add[l_version].ContainsKey(kvp.Key))
                    {
                        raw_file_add[l_version].Remove(kvp.Key);
                    }
                }

                // which the orgin file the vgi in
                Vector3    pos        = kvp.Value.Position;
                INTVECTOR3 chunkPos   = new INTVECTOR3((int)pos.x >> evni.SHIFT, 0, (int)pos.z >> evni.SHIFT);
                INTVECTOR3 chunk32Pos = ChunkPosToPos32(chunkPos.x, chunkPos.z, evni);
                int        f_index    = FindOrginFileIndex(chunk32Pos, evni);
                if (f_index != -1)
                {
                    if (file_add_dic.ContainsKey(f_index))
                    {
                        file_add_dic[f_index].Add(kvp.Value);
                    }
                    else
                    {
                        file_add_dic.Add(f_index, new List <RedGrassInstance>());
                        file_add_dic[f_index].Add(kvp.Value);
                    }

                    if (!modify_file_index.ContainsKey(f_index))
                    {
                        modify_file_index.Add(f_index, f_index);
                    }
                }
            }
        }
        #endregion

        #region READ_ORIGINAL_FILE & REFRESH_DATA

        // Read old original file data
        Dictionary <int, Dictionary <INTVECTOR3, RedGrassInstance>[]> old_datas = new Dictionary <int, Dictionary <INTVECTOR3, RedGrassInstance>[]>();

        foreach (int f_i in modify_file_index.Keys)
        {
            string fileName = s_orgnFilePath[f_i].Substring(s_orgnFilePath[f_i].LastIndexOf("/") + 1);

            old_datas.Add(f_i, new Dictionary <INTVECTOR3, RedGrassInstance> [evni.XZTileCount]);
            //Read now
            BinaryReader _in = new BinaryReader(orgnGrassFiles[f_i]);
            for (int j = 0; j < evni.XZTileCount; ++j)
            {
                if (j % 100 == 0)
                {
                    EditorUtility.DisplayProgressBar("Merging Grasses...", "Read file " + fileName + " data.." + (j + 1).ToString() + " of " + evni.XZTileCount.ToString() + ")",
                                                     ((float)(j) / (float)evni.XZTileCount));
                }

                old_datas[f_i][j] = new Dictionary <INTVECTOR3, RedGrassInstance>();

                if (orgnOfsData[f_i, j] == 0)
                {
                    continue;
                }

                _in.BaseStream.Seek(orgnOfsData[f_i, j], SeekOrigin.Begin);
                int count = _in.ReadInt32();
                for (int k = 0; k < count; ++k)
                {
                    RedGrassInstance vgi = new RedGrassInstance();
                    vgi.ReadFromStream(_in);
                    INTVECTOR3 index = Utils.WorldPosToVoxelPos(vgi.Position);
                    old_datas[f_i][j][index] = vgi;
                }
            }
        }

        // Add

        foreach (int f_i in file_add_dic.Keys)
        {
            int _per = 0;
            foreach (RedGrassInstance rgi in file_add_dic[f_i])
            {
                INTVECTOR3 w_i      = Utils.WorldPosToVoxelPos(rgi.Position);
                int        fileX    = f_i % evni.FileXCount;
                int        fileZ    = f_i / evni.FlieZCount;
                int        f_startX = evni.XStart + fileX * evni.XTileCount * evni.Tile;
                int        f_startZ = evni.ZStart + fileZ * evni.ZTileCount * evni.Tile;

                if (_per % 100 == 0)
                {
                    EditorUtility.DisplayProgressBar("Merging Grasses...", "Calculate the Grasses of file " + f_i.ToString() + "...",
                                                     ((float)(_per) / (float)file_add_dic[f_i].Count));
                }

                int chunk_x = ((int)w_i.x - f_startX) / evni.Tile;
                int chunk_z = ((int)w_i.z - f_startZ) / evni.Tile;
                int key     = chunk_x + chunk_z * evni.XTileCount;

                old_datas[f_i][key][w_i] = rgi;

                _per++;
            }
        }

        file_add_dic.Clear();
        #endregion


        // Save to a series of Temp file
        foreach (int f_i in modify_file_index.Keys)
        {
            string file_name = s_mergedFilePath[f_i];

            using (FileStream fs = new FileStream(file_name, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                BinaryWriter _w = new BinaryWriter(fs);

                int[] offsets = new int[evni.XZTileCount];
                _w.Seek(evni.XZTileCount * 4, SeekOrigin.Begin);
                // Write Grass data
                for (int i = 0; i < evni.XZTileCount; i++)
                {
                    if (i % 100 == 0)
                    {
                        EditorUtility.DisplayProgressBar("Merging Grasses...", "Merge file data.. (" + (i + 1).ToString() + " of " + evni.XZTileCount.ToString() + ")",
                                                         ((float)(i) / (float)evni.XZTileCount));
                    }

                    offsets[i] = (int)fs.Position;

                    if (old_datas[f_i][i].Count == 0)
                    {
                        offsets[i] = 0;
                        continue;
                    }

                    _w.Write(old_datas[f_i][i].Count);
                    foreach (RedGrassInstance rgi in old_datas[f_i][i].Values)
                    {
                        rgi.WriteToStream(_w);
                    }
                }

                // write offset
                _w.Seek(0, SeekOrigin.Begin);
                for (int i = 0; i < evni.XZTileCount; i++)
                {
                    _w.Write(offsets[i]);
                }

                fs.Close();
            }
        }

        // Merge complete
        EditorUtility.ClearProgressBar();
        if (EditorUtility.DisplayDialog("Merge complete!", "Do you want to make it work ?", "Yes", "No"))
        {
            // Make it work
            foreach (int f_i in modify_file_index.Keys)
            {
                File.Copy(s_mergedFilePath[f_i], s_orgnFilePath[f_i], true);

                // delete temp file
                File.Delete(s_mergedFilePath[f_i]);
                if (File.Exists(s_mergedFilePath[f_i] + ".meta"))
                {
                    File.Delete(s_mergedFilePath[f_i] + ".meta");
                }

                foreach (string tmpfile in tempfile_to_del)
                {
                    if (File.Exists(tmpfile))
                    {
                        File.Copy(tmpfile, tmpfile + ".bak", true);
                        File.Delete(tmpfile);
                    }
                }
            }
        }
        else
        {
            // Don't make it work
            foreach (int f_i in modify_file_index.Keys)
            {
                File.Delete(s_mergedFilePath[f_i]);
                if (File.Exists(s_mergedFilePath[f_i] + ".meta"))
                {
                    File.Delete(s_mergedFilePath[f_i] + ".meta");
                }
            }
        }

        // Close the file
        for (int i = 0; i < 9; ++i)
        {
            orgnGrassFiles[i].Close();
        }
    }
Пример #16
0
        void DrawGrass(Vector3 point, Vector3 nml, int[] protos)
        {
            float size = radius * 2;
            int   step = Mathf.Clamp(Mathf.RoundToInt(size / 30) + 1, 1, 4);

            float begin_x = Mathf.Max(0, point.x - radius - step);
            float end_x   = point.x + radius + step * 2;
            float begin_z = Mathf.Max(0, point.z - radius - step);
            float end_z   = point.z + radius + step * 2;

            bool up = (point.y >= -0.1f && Camera.main.transform.forward.y < 0.7f);


            // Calc heights matrixs
            int count = Mathf.CeilToInt(size / step) + 1;

            float[,] heights = new float[count + 2, count + 2];

            Vector3 dir = up ? Vector3.down : Vector3.up;
            float   h   = up ? point.y + 100 : point.y - 100;

            {
                int i = 0;
                for (float x = begin_x; x < end_x; x += step, i++)
                {
                    int j = 0;
                    for (float z = begin_z; z < end_z; z += step, j++)
                    {
                        Vector3 p = new Vector3(x, h, z);

                        RaycastHit rch;
                        Ray        ray = new Ray(p, dir);
                        if (Physics.Raycast(ray, out rch, 1000, 1 << Pathea.Layer.VFVoxelTerrain))
                        {
                            heights[i, j] = rch.point.y;
                        }
                    }
                }
            }

            // Calc normal matrixs
            _normals = new Vector3[count, count];
            for (int i = 0; i < count; ++i)
            {
                for (int j = 0; j < count; ++j)
                {
                    _normals[i, j] = CalculateNormal(heights[i + 2, j + 1], heights[i, j + 1], heights[i + 1, j + 2], heights[i + 1, j], step * 2);
                }
            }

            // readly draw
            int bound = Mathf.CeilToInt(radius);

            Color[] pixels = mapProjector.MapTex.GetPixels();
            int     tex_h  = mapProjector.MapTex.height;
            int     tex_w  = mapProjector.MapTex.width;

            for (int x = -bound; x <= bound; ++x)
            {
                for (int z = -bound; z <= bound; ++z)
                {
                    Vector3 p = point + new Vector3(x, h, z);

                    Vector2 tex_pos     = new Vector2((float)(x + radius) / (radius * 2) * tex_w, (float)(z + radius) / (radius * 2) * tex_h);
                    float   pic_density = CalcHeight(tex_pos, pixels, tex_w, tex_h);
                    float   den         = Mathf.Clamp01(pic_density * density);

                    if (pic_density < 0.02f || den < 0.002f)
                    {
                        continue;
                    }

                    RaycastHit rch;
                    if (Physics.Raycast(p, dir, out rch, 1000, (1 << Pathea.Layer.VFVoxelTerrain)))
                    {
                        INTVECTOR3       ipos    = new INTVECTOR3((int)rch.point.x, (int)rch.point.y, (int)rch.point.z);
                        RedGrassInstance old_rgi = scene.data.Read(ipos.x, ipos.y, ipos.z);

                        if (old_rgi.Density < 0.001f)
                        {
                            RedGrassInstance rgi = new RedGrassInstance();
                            rgi.Density  = den;
                            rgi.Position = rch.point;

                            rgi.Prototype = protos[Random.Range(0, protos.Length) % protos.Length];
                            rgi.ColorF    = Color.white;

                            if (scene.data.Write(rgi))
                            {
                                mAddGrasses[ipos] = rgi;
                            }
                        }
                        else
                        {
                            RedGrassInstance rgi = new RedGrassInstance();
                            rgi.Density  = Mathf.Clamp01(den + old_rgi.Density);
                            rgi.Position = old_rgi.Position;

                            if (Random.value < density)
                            {
                                rgi.Prototype = protos[Random.Range(0, protos.Length) % protos.Length];
                            }
                            else
                            {
                                rgi.Prototype = old_rgi.Prototype;
                            }

                            rgi.ColorF = old_rgi.ColorF;
                            rgi.Normal = old_rgi.Normal;

                            if (scene.data.Write(rgi))
                            {
                                mAddGrasses[ipos] = rgi;
                            }
                        }
                    }
                }
            }
        }
Пример #17
0
    private int WriteRoadCellToTextures(int hash, RoadCell rc)
    {
        INTVECTOR3 rcpos = new INTVECTOR3();

        rcpos.hash = hash;
        byte h = (byte)(rcpos.y + 1);

        if (rc.type > 0)
        {
            m_RoadGraph.SetPixel(rcpos.x, rcpos.z, (Color)(rc.color_type));
            Color32 hc = new Color32(0, 0, 0, 0);

            hc = SafeColorConvert(m_HeightGraph0.GetPixel(rcpos.x, rcpos.z));
            if (hc.r == h || hc.g == h || hc.b == h)
            {
                return(-1);
            }
            if (hc.r == 0)
            {
                hc.r = h;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            if (hc.g == 0)
            {
                hc.g = h;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            if (hc.b == 0)
            {
                hc.b = h;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            hc = SafeColorConvert(m_HeightGraph1.GetPixel(rcpos.x, rcpos.z));
            if (hc.r == h || hc.g == h || hc.b == h)
            {
                return(-1);
            }
            if (hc.r == 0)
            {
                hc.r = h;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            if (hc.g == 0)
            {
                hc.g = h;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            if (hc.b == 0)
            {
                hc.b = h;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            return(-1);
        }
        else
        {
            m_RoadGraph.SetPixel(rcpos.x, rcpos.z, new Color(0, 0, 0, 0));
            Color32 hc = new Color32(0, 0, 0, 0);
            hc = SafeColorConvert(m_HeightGraph0.GetPixel(rcpos.x, rcpos.z));
            if (hc.r == h)
            {
                hc.r = 0;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            if (hc.g == h)
            {
                hc.g = 0;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            if (hc.b == h)
            {
                hc.b = 0;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            hc = SafeColorConvert(m_HeightGraph1.GetPixel(rcpos.x, rcpos.z));
            if (hc.r == h)
            {
                hc.r = 0;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            if (hc.g == h)
            {
                hc.g = 0;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            if (hc.b == h)
            {
                hc.b = 0;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            return(-1);
        }
    }
Пример #18
0
    public void SetRoadCell(INTVECTOR3 rcpos, RoadCell rc)
    {
        int hash = rcpos.hash;

        SetRoadCell(hash, rc);
    }
Пример #19
0
 public float Distance(INTVECTOR3 vec)
 {
     return(Mathf.Sqrt((vec.x - x) * (vec.x - x) +
                       (vec.y - y) * (vec.y - y) +
                       (vec.z - z) * (vec.z - z)));
 }
Пример #20
0
 public INTVECTOR3(INTVECTOR3 vec)
 {
     x = vec.x;
     y = vec.y;
     z = vec.z;
 }
Пример #21
0
 public INTBOUNDS3(INTVECTOR3 _min, INTVECTOR3 _max, bool _close_max)
 {
     close_max = _close_max ? 1 : 0;
     min       = _min;
     max       = _max;
 }
    void Run()
    {
        try
        {
            while (mRunning)
            {
                if (!mActive)
                {
                    Thread.Sleep(10);
                    continue;
                }

                Monitor.TryEnter(VFDataRTGen.s_dicGrassInstList);

                try
                {
                    while (true)
                    {
                        RGChunk chunk = null;

                        lock (mReqs)
                        {
                            if (mReqs.Count == 0)
                            {
                                break;
                            }
                            chunk = mReqs.Dequeue();
                        }


                        INTVECTOR3 chunk32Pos = ChunkPosToPos32(chunk.xIndex, chunk.zIndex);

                        int expands = mEvni.CHUNKSIZE / mEvni.Tile;

                        lock (chunk)
                        {
                            for (int x = chunk32Pos.x; x < chunk32Pos.x + expands; ++x)
                            {
                                for (int z = chunk32Pos.z; z < chunk32Pos.z + expands; ++z)
                                {
                                    IntVector2 key = new IntVector2(x, z);
                                    if (VFDataRTGen.s_dicGrassInstList.ContainsKey(key))
                                    {
                                        foreach (VoxelGrassInstance vgi in VFDataRTGen.s_dicGrassInstList[key])
                                        {
                                            RedGrassInstance rgi = new RedGrassInstance();
                                            rgi.CopyTo(vgi);

                                            Vector3 pos = rgi.Position;
                                            if (!GrassDataSL.m_mapDelPos.ContainsKey(new INTVECTOR3((int)pos.x, (int)pos.y, (int)pos.z)))
                                            {
                                                chunk.Write(rgi);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    Monitor.Exit(VFDataRTGen.s_dicGrassInstList);
                }

                mStart = false;
                Thread.Sleep(10);
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.ToString());
        }
    }
Пример #23
0
    //public static void OutputVoxels(Vector3 worldPos, ulong guid, VArtifactTown newTown)
    //{
    //    ISO = isos[guid];
    //    OutputVoxels(worldPos,newTown);
    //}

    //public static void OutputVoxels(Vector3 worldPos, int townId, VArtifactTown newTown)
    //{
    //    ulong guid = townIdIso[townId];
    //    ISO = isos[guid];
    //    OutputVoxels(worldPos,newTown);
    //}


    public static void OutputVoxels(Vector3 worldPos, VArtifactUnit newTown, float rotation = 0)
    {
        if (!isos.ContainsKey(newTown.isoGuId))
        {
            LoadIso(GetISONameFullPath(newTown.isoName));
        }
        if (!isos.ContainsKey(newTown.isoGuId))
        {
            Debug.LogError("isoGuId error: " + newTown.isoGuId + "isoName: " + newTown.isoName);
            return;
        }
        VArtifactData isoData = isos[newTown.isoGuId];
        //long tick = System.DateTime.Now.Ticks;
        Quaternion q = new Quaternion();

        q.eulerAngles = new Vector3(0, rotation, 0);
        Vector3 XDir = (q * Vector3.right).normalized;
        Vector3 YDir = (q * Vector3.up).normalized;
        Vector3 ZDir = (q * Vector3.forward).normalized;
        //Vector3 XDir = Vector3.right;
        //Vector3 YDir = Vector3.up;
        //Vector3 ZDir = Vector3.forward;
        Vector3 ofs     = new Vector3(isoData.m_HeadInfo.xSize, 0, isoData.m_HeadInfo.zSize) * (-0.5f);
        Vector3 new_pos = worldPos + q * ofs;

        foreach (KeyValuePair <int, VCVoxel> kvp in isoData.m_Voxels)
        {
            Vector3 lpos = new Vector3(kvp.Key & 0x3ff, kvp.Key >> 20, (kvp.Key >> 10) & 0x3ff);
            Vector3 wpos = new_pos
                           + lpos.x * XDir
                           + lpos.y * YDir
                           + lpos.z * ZDir;

            INTVECTOR3 wpos_floor = new INTVECTOR3(Mathf.FloorToInt(wpos.x), Mathf.FloorToInt(wpos.y), Mathf.FloorToInt(wpos.z));
            INTVECTOR3 wpos_ceil  = new INTVECTOR3(Mathf.CeilToInt(wpos.x), Mathf.CeilToInt(wpos.y), Mathf.CeilToInt(wpos.z));

            if (wpos_floor == wpos_ceil)
            {
                OutputTownVoxel(wpos_floor.x, wpos_floor.y, wpos_floor.z, kvp.Value, newTown);
            }
            else
            {
                for (int x = wpos_floor.x; x <= wpos_ceil.x; ++x)
                {
                    for (int y = wpos_floor.y; y <= wpos_ceil.y; ++y)
                    {
                        for (int z = wpos_floor.z; z <= wpos_ceil.z; ++z)
                        {
                            float deltax = 1 - Mathf.Abs(wpos.x - x);
                            float deltay = 1 - Mathf.Abs(wpos.y - y);
                            float deltaz = 1 - Mathf.Abs(wpos.z - z);
                            float u      = deltax * deltay * deltaz;
                            if (u < 0.5f)
                            {
                                u = u / (0.5f + u);
                            }
                            else
                            {
                                u = 0.5f / (1.5f - u);
                            }
                            VCVoxel voxel = kvp.Value;
                            voxel.Volume = (byte)Mathf.CeilToInt(voxel.Volume * u);
                            if (voxel.Volume > 1)
                            {
                                OutputTownVoxel(x, y, z, voxel, newTown);
                            }
                        }
                    }
                }
            }
        }
        // Debug.LogError("Output Time: " + (System.DateTime.Now.Ticks - tick) + " townCount:" + (++townCount));
    }
Пример #24
0
 public void OpenBounds()
 {
     max       = max + INTVECTOR3.one * close_max;
     close_max = 0;
 }
Пример #25
0
 public bool Contains(INTVECTOR3 point)
 {
     return(point.x >= min.x && point.x < max.x + close_max &&
            point.y >= min.y && point.y < max.y + close_max &&
            point.z >= min.z && point.z < max.z + close_max);
 }
Пример #26
0
    // Thread function
    void Run()
    {
        try
        {
            while (mRunning)
            {
                if (!mActive)
                {
                    Thread.Sleep(10);
                    continue;
                }

                while (true)
                {
                    RedGrass.RGChunk chunk = null;

                    lock (mReqs)
                    {
                        if (mReqs.Count == 0)
                        {
                            break;
                        }
                        chunk = mReqs.Dequeue();
                    }

                    if (mOrgnGrassFile == null)
                    {
                        continue;
                    }
                    BinaryReader _in        = new BinaryReader(mOrgnGrassFile);
                    INTVECTOR3   chunk32Pos = ChunkPosToPos32(chunk.xIndex, chunk.zIndex);
                    int          expands    = mEvni.CHUNKSIZE / mEvni.Tile;

                    lock (chunk)
                    {
                        for (int _x = chunk32Pos.x; _x < chunk32Pos.x + expands; ++_x)
                        {
                            for (int _z = chunk32Pos.z; _z < chunk32Pos.z + expands; ++_z)
                            {
                                int index32 = Pos32ToIndex32(_x, _z);
                                _in.BaseStream.Seek(mOrgnOfsData[index32], SeekOrigin.Begin);
                                int count = mOrgnLenData[index32];
                                for (int i = 0; i < count; ++i)
                                {
                                    RedGrass.RedGrassInstance rgi = new RedGrassInstance();

                                    rgi.ReadFromStream(_in);

                                    chunk.Write(rgi);
                                }
                            }
                        }
                    }
                }
                mStart = false;

                Thread.Sleep(10);
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogError("<<<< Data IO Thread error >>>> \r\n" + ex);
        }
    }
Пример #27
0
    public override void ProcessReqsImmediatly()
    {
        try
        {
            while (mReqs.Count != 0)
            {
                RedGrass.RGChunk chunk = null;

                lock (mReqs)
                {
                    chunk = mReqs.Dequeue();
                }

                INTVECTOR3 chunk32Pos = ChunkPosToPos32(chunk.xIndex, chunk.zIndex);

                // Find the file index
                int file_index = FindOrginFileIndex(chunk32Pos);

                if (file_index != -1 && mOrgnGrassFile[file_index] != null)
                {
                    BinaryReader _in = new BinaryReader(mOrgnGrassFile[file_index]);

                    int expands = mEvni.CHUNKSIZE / mEvni.Tile;

                    lock (chunk)
                    {
                        for (int x = chunk32Pos.x; x < chunk32Pos.x + expands; ++x)
                        {
                            for (int z = chunk32Pos.z; z < chunk32Pos.z + expands; ++z)
                            {
                                int _x = x % mEvni.XTileCount;
                                int _z = z % mEvni.ZTileCount;

                                int index32 = Pos32ToIndex32(_x, _z);

                                if (mOrgnOfsData[file_index, index32] > 0)
                                {
                                    _in.BaseStream.Seek(mOrgnOfsData[file_index, index32], SeekOrigin.Begin);
                                    int count = _in.ReadInt32();

                                    for (int i = 0; i < count; ++i)
                                    {
                                        RedGrassInstance rgi = new RedGrassInstance();
                                        rgi.ReadFromStream(_in);

                                        Vector3 pos = rgi.Position;
                                        if (!GrassDataSL.m_mapDelPos.ContainsKey(new INTVECTOR3((int)pos.x, (int)pos.y, (int)pos.z)))
                                        {
                                            chunk.Write(rgi);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogWarning(ex.ToString());
        }
    }
Пример #28
0
    public static void SaveToOriginalFile()
    {
        RedGrass.EvniAsset evni = Resources.Load(s_EvniPath) as RedGrass.EvniAsset;
        if (evni == null)
        {
            Debug.LogError("EvniAsset is missiing");
        }

        // Open or Create original subterrain data file
        FileStream orgnGrassFile = null;

        int[] orgnOfsData = new int[evni.XZTileCount];
        int[] orgnLenData = new int[evni.XZTileCount];
        if (File.Exists(s_orgnFilePath))
        {
            orgnGrassFile = new FileStream(s_orgnFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            {
                BinaryReader _in = new BinaryReader(orgnGrassFile);

                for (int i = 0; i < evni.XZTileCount; ++i)
                {
                    orgnOfsData[i] = _in.ReadInt32();
                    orgnLenData[i] = _in.ReadInt32();
                }
            }
        }
        else
        {
            orgnGrassFile = new FileStream(s_orgnFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
        }

        // Merge the cache
        string[] raw_file_path = Directory.GetFiles(RGDemoEditorSaver.s_FileDirectory);
        Dictionary <int, Dictionary <INTVECTOR3, RedGrassInstance> > raw_file_add = new Dictionary <int, Dictionary <INTVECTOR3, RedGrassInstance> >();
        List <int>    version_list    = new List <int>();
        List <string> tempfile_to_del = new List <string>();

        for (int i = 0; i < raw_file_path.Length; i++)
        {
            EditorUtility.DisplayProgressBar("Merging Grasses...", "Read raw data.. (" + (i + 1).ToString() + " of " + raw_file_path.Length.ToString() + ")", 0.2f * ((float)(i) / (float)(raw_file_path.Length)));

            // the expanded-name must be "gs"
            if (raw_file_path[i].Substring(raw_file_path[i].LastIndexOf(".") + 1) != "gs")
            {
                continue;
            }

            // Read the caches
            using (FileStream raw_files = new FileStream(raw_file_path[i], FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                BinaryReader _in     = new BinaryReader(raw_files);
                int          version = _in.ReadInt32();
                version_list.Add(version);
                int cnt = _in.ReadInt32();

                raw_file_add.Add(version, new Dictionary <INTVECTOR3, RedGrassInstance>());
                for (int j = 0; j < cnt; ++j)
                {
                    RedGrassInstance rgi = new RedGrassInstance();
                    rgi.ReadFromStream(_in);
                    INTVECTOR3 index = Utils.WorldPosToVoxelPos(rgi.Position);
                    raw_file_add[version][index] = rgi;
                }
            }

            tempfile_to_del.Add(raw_file_path[i]);
        }

        if (version_list.Count == 0)
        {
            EditorUtility.ClearProgressBar();
            EditorUtility.DisplayDialog("Merging Grasses", "Nothing changed !", "OK");
            return;
        }

        // Descending order the version_list
        version_list.Sort(delegate(int small, int big) {
            if (small > big)
            {
                return(-1);
            }
            else if (small == big)
            {
                return(0);
            }
            else
            {
                return(1);
            }
        });

        // Merge cache to a list

        List <RedGrassInstance> file_add = new List <RedGrassInstance>();

        for (int i = 0; i < version_list.Count; ++i)
        {
            int version = version_list[i];

            // Add to list
            foreach (KeyValuePair <INTVECTOR3, RedGrassInstance> kvp in raw_file_add[version])
            {
                for (int j = i + 1; j < version_list.Count; ++j)
                {
                    int l_version = version_list[j];

                    if (raw_file_add[l_version].ContainsKey(kvp.Key))
                    {
                        raw_file_add[l_version].Remove(kvp.Key);
                    }
                }

                file_add.Add(kvp.Value);
            }
        }

        #region READ_ORIGINAL_FILE & REFRESH_DATA

        // Read old original file data
        Dictionary <INTVECTOR3, RedGrassInstance>[] old_datas = new Dictionary <INTVECTOR3, RedGrassInstance> [evni.XZTileCount];
        {
            string fileName = s_orgnFilePath.Substring(s_orgnFilePath.LastIndexOf("/") + 1);
            // Read now
            BinaryReader _in = new BinaryReader(orgnGrassFile);
            for (int j = 0; j < evni.XZTileCount; ++j)
            {
                if (j % 100 == 0)
                {
                    EditorUtility.DisplayProgressBar("Merging Grasses...", "Read file " + fileName + " data.." + (j + 1).ToString() + " of " + evni.XZTileCount.ToString() + ")",
                                                     ((float)(j) / (float)evni.XZTileCount));
                }

                old_datas[j] = new Dictionary <INTVECTOR3, RedGrassInstance>();

                if (orgnLenData[j] == 0)
                {
                    continue;
                }

                _in.BaseStream.Seek(orgnOfsData[j], SeekOrigin.Begin);
                int count = orgnLenData[j];
                for (int k = 0; k < count; ++k)
                {
                    RedGrassInstance rgi = new RedGrassInstance();
                    rgi.ReadFromStream(_in);
                    INTVECTOR3 index = Utils.WorldPosToVoxelPos(rgi.Position);
                    old_datas[j][index] = rgi;
                }
            }
        }

        // Add

        int _per = 0;
        foreach (RedGrassInstance rgi in file_add)
        {
            INTVECTOR3 w_i      = Utils.WorldPosToVoxelPos(rgi.Position);
            int        f_startX = evni.XStart;
            int        f_startZ = evni.ZStart;

            if (_per % 100 == 0)
            {
                EditorUtility.DisplayProgressBar("Merging Grasses...", "Calculate the Grasses...",
                                                 ((float)(_per) / (float)file_add.Count));
            }

//            for (int i = 0; i < evni.XZTileCount; ++i)
//			{
//				if (old_datas[i].ContainsKey(w_i))
//				{
//					old_datas[i][w_i] = rgi;
//					break;
//				}
//				else
//				{
//
//					int x = i % evni.XTileCount;
//					int z = i / evni.ZTileCount;
//					int startX = f_startX + x * evni.Tile;
//					int startZ = f_startZ + z * evni.Tile;
//					if (w_i.x >= startX && w_i.x < startX + evni.Tile
//					    && w_i.z >= startZ && w_i.z < startZ + evni.Tile)
//					{
//						old_datas[i].Add(w_i, rgi);
//						break;
//					}
//
//				}
//			}

            int chunk_x = ((int)w_i.x - f_startX) / evni.Tile;
            int chunk_z = ((int)w_i.z - f_startZ) / evni.Tile;
            int key     = chunk_x + chunk_z * evni.XTileCount;


            old_datas[key][w_i] = rgi;
//			RGScene
            _per++;
        }

        file_add.Clear();

        #endregion



        // Save to a series of Temp file
        string file_name = s_mergedFilePath;


        using (FileStream fs = new FileStream(file_name, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            BinaryWriter _w = new BinaryWriter(fs);

            int[] offsets = new int[evni.XZTileCount];
            int[] lens    = new int[evni.XZTileCount];
            _w.Seek(evni.XZTileCount * 4 * 2, SeekOrigin.Begin);
            // Write Grass data
            for (int i = 0; i < evni.XZTileCount; i++)
            {
                if (i % 100 == 0)
                {
                    EditorUtility.DisplayProgressBar("Merging Grasses...", "Merge file data.. (" + (i + 1).ToString() + " of " + evni.XZTileCount.ToString() + ")",
                                                     ((float)(i) / (float)evni.XZTileCount));
                }
                offsets[i] = (int)fs.Position;

                if (old_datas[i].Count == 0)
                {
                    offsets[i] = 0;
                    continue;
                }

//				_w.Write(old_datas[i].Count);
                lens[i] = old_datas[i].Count;
                foreach (RedGrassInstance rgi in old_datas[i].Values)
                {
                    rgi.WriteToStream(_w);
                }
            }
            // Write Offset
            _w.Seek(0, SeekOrigin.Begin);
            for (int i = 0; i < evni.XZTileCount; i++)
            {
                _w.Write(offsets[i]);
                _w.Write(lens[i]);
            }


            fs.Close();
        }

        // Merge complete
        EditorUtility.ClearProgressBar();
        if (EditorUtility.DisplayDialog("Merge complete!", "Do you want to make it work ?", "Yes", "No"))
        {
            // Make it work

            File.Copy(s_mergedFilePath, s_orgnFilePath, true);

            // delete temp file
            File.Delete(s_mergedFilePath);
            if (File.Exists(s_mergedFilePath + ".meta"))
            {
                File.Delete(s_mergedFilePath + ".meta");
            }

            foreach (string tmpfile in tempfile_to_del)
            {
                if (File.Exists(tmpfile))
                {
                    File.Copy(tmpfile, tmpfile + ".bak", true);
                    File.Delete(tmpfile);
                }
            }
        }
        else
        {
            // Don't make it work
            File.Delete(s_mergedFilePath);
            if (File.Exists(s_mergedFilePath + ".meta"))
            {
                File.Delete(s_mergedFilePath + ".meta");
            }
        }
    }
Пример #29
0
    void Run()
    {
        try
        {
            while (mRunning)
            {
                if (!mActive)
                {
                    Thread.Sleep(10);
                    continue;
                }

                while (true)
                {
                    RGChunk chunk = null;

                    lock (mReqs)
                    {
                        if (mReqs.Count == 0)
                        {
                            break;
                        }
                        chunk = mReqs.Dequeue();
                    }

                    lock (mDestoryLockObj)
                    {
                        INTVECTOR3 chunk32Pos = ChunkPosToPos32(chunk.xIndex, chunk.zIndex);

                        // Find the file index
                        int file_index = FindOrginFileIndex(chunk32Pos);

                        if (file_index != -1 && mOrgnGrassFile[file_index] != null)
                        {
                            BinaryReader _in = new BinaryReader(mOrgnGrassFile[file_index]);

                            int expands = mEvni.CHUNKSIZE / mEvni.Tile;

                            lock (chunk)
                            {
                                for (int x = chunk32Pos.x; x < chunk32Pos.x + expands; ++x)
                                {
                                    for (int z = chunk32Pos.z; z < chunk32Pos.z + expands; ++z)
                                    {
                                        int _x = x % mEvni.XTileCount;
                                        int _z = z % mEvni.ZTileCount;

                                        int index32 = Pos32ToIndex32(_x, _z);

                                        if (mOrgnOfsData[file_index, index32] > 0)
                                        {
                                            _in.BaseStream.Seek(mOrgnOfsData[file_index, index32], SeekOrigin.Begin);
                                            int count = _in.ReadInt32();

                                            for (int i = 0; i < count; ++i)
                                            {
                                                RedGrassInstance rgi = new RedGrassInstance();
                                                rgi.ReadFromStream(_in);

                                                Vector3 pos = rgi.Position;
                                                if (!GrassDataSL.m_mapDelPos.ContainsKey(new INTVECTOR3((int)pos.x, (int)pos.y, (int)pos.z)))
                                                {
                                                    chunk.Write(rgi);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                mStart = false;

                Thread.Sleep(10);
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogWarning("<<<< Data IO Thread error >>>> \r\n" + ex);
        }
    }