Exemple #1
0
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            Vector3ToHash other = (Vector3ToHash)obj;

            return(Mathf.Approximately(m_v.x, other.m_v.x) && Mathf.Approximately(m_v.y, other.m_v.y) && Mathf.Approximately(m_v.z, other.m_v.z));
        }
Exemple #2
0
        public override void Paint(Vector3 pos, float value, float opacity)
        {
            if (TerrainTreeIndex < 0)
            {
                return;
            }

            var data = Terrain.terrainData;

            Vector2 center     = new Vector2(pos.x / Scale.x, pos.z / Scale.y);
            Vector2 radius_ext = new Vector2(Radius / Scale.x, Radius / Scale.y);

            Vector2Int minPos = Floor(center - radius_ext);
            Vector2Int maxPos = Ceil(center + radius_ext);

            if (value > 0)
            {
                if (m_firstModification)
                {
                    m_nextModificationTime = Time.time + 0.3f;
                    m_firstModification    = false;
                    m_pos = pos;
                }
                else
                {
                    if (Time.time < m_nextModificationTime && m_pos == pos)
                    {
                        return;
                    }
                }
            }

            Dictionary <Vector3ToHash, TreeInstance> treeInstances = new Dictionary <Vector3ToHash, TreeInstance>();

            foreach (TreeInstance tree in data.treeInstances)
            {
                Vector3ToHash vth = new Vector3ToHash(new Vector3(tree.position.x * data.size.x, 0, tree.position.z * data.size.z));
                if (!treeInstances.ContainsKey(vth))
                {
                    treeInstances.Add(vth, tree);
                }
            }

            int sizeY = maxPos.y - minPos.y;
            int sizeX = maxPos.x - minPos.x;

            if (minPos.x > 0)
            {
                minPos.x = 0;
            }
            if (minPos.y > 0)
            {
                minPos.y = 0;
            }

            bool any = false;

            for (int y = 0; y < sizeY; y++)
            {
                for (int x = 0; x < sizeX; x++)
                {
                    float u = (x - minPos.x) / (float)(sizeX - 1);
                    float v = (y - minPos.y) / (float)(sizeY - 1);
                    float f = Eval(u, v);

                    if (f > 0.5f && Random.value < opacity * 0.05f)
                    {
                        Vector3 position = new Vector3((center.x + (x - minPos.x) - sizeX / 2), 0, (center.y + (y - minPos.y) - sizeY / 2));
                        if (value > 0)
                        {
                            CreateTreeInstance(treeInstances, position, data.size);
                        }
                        else
                        {
                            treeInstances.Remove(new Vector3ToHash(position));
                        }

                        any = true;
                    }
                }
            }

            if (!any)
            {
                Vector3 position = new Vector3(pos.x, 0, pos.z);
                if (value > 0)
                {
                    CreateTreeInstance(treeInstances, position, data.size);
                }
                else
                {
                    treeInstances.Remove(new Vector3ToHash(position));
                }
            }

            Terrain.SetTreeInstances(treeInstances.Values.ToArray(), true);
        }