Exemplo n.º 1
0
    public bool TryFill(IntVector3 ipos, Dictionary <int, LSubTerrain> dicNodes)
    {
        int nExpand = LSubTerrainMgr.Instance.NumDataExpands;

        for (int x = ipos.x - nExpand; x <= ipos.x + nExpand; x++)
        {
            for (int z = ipos.z - nExpand; z <= ipos.z + nExpand; z++)
            {
                if (x < 0 || x >= LSubTerrConstant.XCount ||
                    z < 0 || z >= LSubTerrConstant.ZCount)
                {
                    continue;
                }

                int idx = LSubTerrUtils.PosToIndex(x, z);
                if (!dicNodes.ContainsKey(idx))
                {
                    LSubTerrain node = new LSubTerrain();
                    node.Index = idx;
                    ReadOrgDataToBuff(node.Index);
                    node.ApplyData(DataBuff, DataLen);
                    dicNodes.Add(idx, node);
                    return(false);
                }
            }
        }
        return(true);
    }
Exemplo n.º 2
0
    public bool TryFill_T(IntVector3 ipos, Dictionary <int, LSubTerrain> dicNodes)
    {
        if (CntInProcess > 0)
        {
            return(false);
        }

        int n = _lstNodes.Count;

        if (n > 0)
        {
            for (int i = 0; i < n; i++)
            {
                int index = _lstNodes [i].Index;
                if (dicNodes.ContainsKey(index))
                {
                    Debug.LogError("Adding an LSubTerrain node but it already exist in the map, the old node will be replaced !");
                    dicNodes [index] = _lstNodes [i];
                }
                else
                {
                    dicNodes.Add(index, _lstNodes [i]);
                }
            }
            _lstNodes.Clear();
        }

        // Add into process
        bool bRet    = true;
        int  nExpand = LSubTerrainMgr.Instance.NumDataExpands;

        lock (_lstNodesIdx) {
            for (int x = ipos.x - nExpand; x <= ipos.x + nExpand; x++)
            {
                for (int z = ipos.z - nExpand; z <= ipos.z + nExpand; z++)
                {
                    if (x < 0 || x >= LSubTerrConstant.XCount ||
                        z < 0 || z >= LSubTerrConstant.ZCount)
                    {
                        continue;
                    }

                    int idx = LSubTerrUtils.PosToIndex(x, z);
                    if (!dicNodes.ContainsKey(idx))
                    {
                        _lstNodesIdx.Add(idx);
                        bRet = false;
                    }
                }
            }
        }
        return(bRet);
    }
Exemplo n.º 3
0
    private void CreateOneBTerrain(int x, int z)
    {
        int             idx           = LSubTerrUtils.PosToIndex(x, z);
        List <TreeInfo> trees_in_zone = m_allTreesInLayer[idx];

        BillboardTerrain bt = BillboardTerrain.Instantiate(LSubTerrainMgr.Instance.BTerrainRes) as BillboardTerrain;

        bt.transform.parent     = LSubTerrainMgr.Instance.BTerrainGroup.transform;
        bt.gameObject.name      = "BTerrain [" + x.ToString() + " " + z.ToString() + "]";
        bt.transform.position   = new Vector3(x * LSubTerrConstant.SizeF, 0, z * LSubTerrConstant.SizeF);
        bt.transform.localScale = Vector3.one;
        bt.xPos     = x;
        bt.zPos     = z;
        bt.m_Center = new Vector3((xIndex + 0.5f) * LSubTerrConstant.SizeF, 0, (zIndex + 0.5f) * LSubTerrConstant.SizeF);

        if (m_BillboardTerrains.ContainsKey(idx))
        {
            m_BillboardTerrains[idx].Reset();
            GameObject.Destroy(m_BillboardTerrains[idx].gameObject);
            m_BillboardTerrains.Remove(idx);
        }
        bt.SetTrees(trees_in_zone);
        m_BillboardTerrains.Add(idx, bt);
    }
Exemplo n.º 4
0
    private IEnumerator RefreshRegion()
    {
        bProcessing = true;

        #region PREPARE_DATA
        yield return(0);

        m_mapPrototype.Clear();
        m_listPrototype.Clear();
        for (int x = xIndex - 1; x <= xIndex + 1; ++x)
        {
            for (int z = zIndex - 1; z <= zIndex + 1; ++z)
            {
                int idx = LSubTerrUtils.PosToIndex(x, z);
                while (!m_allTreesInLayer.ContainsKey(idx) && LSubTerrainMgr.Node(idx) != null && LSubTerrainMgr.Node(idx).HasData)
                {
                    yield return(0);
                }
                if (m_allTreesInLayer.ContainsKey(idx))
                {
                    List <TreeInfo> trees_in_zone = m_allTreesInLayer[idx];
                    foreach (TreeInfo ti in trees_in_zone)
                    {
                        // New prototype ?
                        if (!m_mapPrototype.ContainsKey(ti.m_protoTypeIdx))
                        {
                            int next_index = m_listPrototype.Count;
                            m_mapPrototype.Add(ti.m_protoTypeIdx, next_index);
                            m_listPrototype.Add(ti.m_protoTypeIdx);
                        }
                    }
                }
            }
            yield return(0);
        }

        TreePrototype [] FinalPrototypeArray = new TreePrototype [m_listPrototype.Count];
        for (int i = 0; i < m_listPrototype.Count; ++i)
        {
            FinalPrototypeArray[i]            = new TreePrototype();
            FinalPrototypeArray[i].bendFactor = LSubTerrainMgr.Instance.GlobalPrototypeBendFactorList[m_listPrototype[i]];
            FinalPrototypeArray[i].prefab     = LSubTerrainMgr.Instance.GlobalPrototypePrefabList[m_listPrototype[i]];
        }

        // Calc Count
        int tree_count = 0;
        for (int x = xIndex - 1; x <= xIndex + 1; ++x)
        {
            for (int z = zIndex - 1; z <= zIndex + 1; ++z)
            {
                int idx = LSubTerrUtils.PosToIndex(x, z);
                if (m_allTreesInLayer.ContainsKey(idx))
                {
                    tree_count += m_allTreesInLayer[idx].Count;
                }
            }
        }

        TreeInstance [] FinalTreeInstanceArray = new TreeInstance [tree_count];

        int t = 0;
        for (int x = xIndex - 1; x <= xIndex + 1; ++x)
        {
            for (int z = zIndex - 1; z <= zIndex + 1; ++z)
            {
                int idx = LSubTerrUtils.PosToIndex(x, z);
                if (m_allTreesInLayer.ContainsKey(idx))
                {
                    List <TreeInfo> trees_in_zone = m_allTreesInLayer[idx];
                    foreach (TreeInfo ti in trees_in_zone)
                    {
                        if (t < FinalTreeInstanceArray.Length)
                        {
                            Vector3 new_pos = ti.m_pos;
                            new_pos   += new Vector3(x - xIndex + 1, 0, z - zIndex + 1);
                            new_pos.x /= 3;
                            new_pos.z /= 3;
                            FinalTreeInstanceArray[t].color         = ti.m_clr;
                            FinalTreeInstanceArray[t].heightScale   = ti.m_heightScale;
                            FinalTreeInstanceArray[t].widthScale    = ti.m_widthScale;
                            FinalTreeInstanceArray[t].lightmapColor = ti.m_lightMapClr;
                            FinalTreeInstanceArray[t].position      = new_pos;
                            if (!m_mapPrototype.ContainsKey(ti.m_protoTypeIdx))
                            {
                                FinalTreeInstanceArray[t].heightScale    = 0;
                                FinalTreeInstanceArray[t].widthScale     = 0;
                                FinalTreeInstanceArray[t].position       = Vector3.zero;
                                FinalTreeInstanceArray[t].prototypeIndex = 0;
                                continue;
                            }
                            FinalTreeInstanceArray[t].prototypeIndex = m_mapPrototype[ti.m_protoTypeIdx];
                        }
                        t++;
                    }
                }
            }
        }
        yield return(0);

        #endregion

        #region ASSIGN_DATA
        gameObject.SetActive(false);
        m_TerrData.treeInstances  = new TreeInstance[0];
        m_TerrData.treePrototypes = FinalPrototypeArray;
        m_TerrData.treeInstances  = FinalTreeInstanceArray;

        if (Application.isEditor)
        {
            _TreePrototypeCount = m_TerrData.treePrototypes.Length;
            _TreeInstanceCount  = m_TerrData.treeInstances.Length;
        }
        transform.position = LSubTerrUtils.PosToWorldPos(new IntVector3(xIndex - 1, 0, zIndex - 1));
        gameObject.SetActive(true);
        #endregion

        bProcessing = false;

        if (OnRefreshRegion != null)
        {
            OnRefreshRegion();
        }
        StartCoroutine("RefreshBillboards");
    }
Exemplo n.º 5
0
    private IEnumerator RefreshBillboards()
    {
        // Check if the highest layer
        if (LSubTerrainMgr.Instance.Layers[LayerIndex].MaxTreeHeight < 50)
        {
            bBillboardProcessing = false;
            yield break;
        }

        bBillboardProcessing = true;

        // Delete Far BTerrains
        List <int> del_list = new List <int> ();
        long       tempIndexX, tempIndexZ;

        foreach (KeyValuePair <int, BillboardTerrain> kvp in m_BillboardTerrains)
        {
            IntVector3 pos = LSubTerrUtils.IndexToPos(kvp.Key);
            tempIndexX = pos.x - xIndex;
            tempIndexZ = pos.z - zIndex;
            //lz-2017.07.27 差值如果是Int.MinValue用Mathf.Abs会报错: OverflowException: Value is too small
            if (System.Math.Abs(tempIndexX) > 3 || System.Math.Abs(tempIndexZ) > 3 ||
                System.Math.Abs(tempIndexX) <= 1 && System.Math.Abs(tempIndexZ) <= 1)
            {
                kvp.Value.Reset();
                GameObject.Destroy(kvp.Value.gameObject);
                del_list.Add(kvp.Key);
            }
        }
        foreach (int del in del_list)
        {
            m_BillboardTerrains.Remove(del);
        }

        // Add new BTerrains
        for (int x = Last_x - 1; x <= Last_x + 1; ++x)
        {
            for (int z = Last_z - 1; z <= Last_z + 1; ++z)
            {
                if (x >= xIndex - 1 && x <= xIndex + 1 &&
                    z >= zIndex - 1 && z <= zIndex + 1)
                {
                    continue;
                }
                if (x > xIndex + 3 || x < xIndex - 3 ||
                    z > zIndex + 3 || z < zIndex - 3)
                {
                    continue;
                }
                int idx = LSubTerrUtils.PosToIndex(x, z);
                while (!m_allTreesInLayer.ContainsKey(idx) && LSubTerrainMgr.Node(idx) != null && LSubTerrainMgr.Node(idx).HasData)
                {
                    yield return(0);
                }
                if (m_allTreesInLayer.ContainsKey(idx))
                {
                    CreateOneBTerrain(x, z);
                }
            }
        }
        yield return(0);

        // Add new BTerrains
        for (int x = xIndex - 3; x <= xIndex + 3; ++x)
        {
            for (int z = zIndex - 3; z <= zIndex + 3; ++z)
            {
                if (x >= xIndex - 1 && x <= xIndex + 1 &&
                    z >= zIndex - 1 && z <= zIndex + 1)
                {
                    continue;
                }
                if (x >= Last_x - 1 && x <= Last_x + 1 &&
                    z >= Last_z - 1 && z <= Last_z + 1)
                {
                    continue;
                }
                int idx = LSubTerrUtils.PosToIndex(x, z);
                while (!m_allTreesInLayer.ContainsKey(idx) && LSubTerrainMgr.Node(idx) != null && LSubTerrainMgr.Node(idx).HasData)
                {
                    yield return(0);
                }
                if (m_allTreesInLayer.ContainsKey(idx))
                {
                    CreateOneBTerrain(x, z);
                }
            }
            yield return(0);
        }
        Last_x = xIndex;
        Last_z = zIndex;
        bBillboardProcessing = false;
    }
Exemplo n.º 6
0
 public GlobalTreeInfo(int xindex, int zindex, TreeInfo treeinfo)
 {
     _terrainIndex = LSubTerrUtils.PosToIndex(xindex, zindex);
     _treeInfo     = treeinfo;
 }