示例#1
0
    public void DebugUpdate()
    {
        var   t   = debugTerrain.GetComponent <Terrain>();
        int   hr  = t.terrainData.heightmapResolution;
        float s   = Time.realtimeSinceStartup;
        var   res = GenerateTile(worldToTerrain(debugTerrain.transform.position, t), hr, hr, true);

        debugTerrain.SetTerrainData(res.heightmap, res.alphamap, debugTerrain.transform.position);
        Debug.LogFormat("Generated terrain in {0} seconds", Time.realtimeSinceStartup - s);
    }
示例#2
0
        /// <summary>
        /// First creates a poisson grid based on the passed density.
        /// Positions are then filtered based on the passed object placement
        /// type taking into account height and angle constraints.
        /// </summary>
        /// <param name="m">Mesh to sample height and angle values from</param>
        /// <param name="type">object placement type to sample</param>
        /// <param name="density">How dense should the samples be</param>
        /// <returns>List of vectors within the grid and sample constraints</returns>
        public List <Vector3> GetFilteredGrid(TerrainTile tile, ObjectPlacementType type, float density)
        {
            MeshFilter mf = tile.GetComponent <MeshFilter>();

            if (mf == null)
            {
                throw new ArgumentException("The passed TerrainTile does not have an attached MeshFilter. Has a mesh been created?");
            }

            return(GetFilteredGrid(mf.sharedMesh, type));
        }
示例#3
0
    void ResetTile(TerrainTile tile, IntVector2 tileIndex)
    {
        var gen = tile.GetComponent<HeightMapGenerator> ();
        gen.terrainSize = terrainSize;
        gen.heightSettings = heightSettings;

        tile.ResetTile (tileIndex);

        // TODO: Recompute TerrainData and everything that is on the Terrain!
    }
        /// <summary>
        /// Calculates MeshData incrementally using a coroutine.
        /// TerrainTile instance must have a MeshCollider attached to
        /// the same gameobject
        /// </summary>
        /// <param name="onCalculated">Callback delegate when operations have finished</param>
        ///
        public IEnumerator CalculateCells(CalcFinished onCalculated)
        {
            Random.InitState(TerraSettings.GenerationSeed);
            float variation = TerraSettings.Instance.GrassVariation;

            List <MeshData> data        = new List <MeshData>();
            Bounds          bounds      = Tile.Terrain.bounds;
            Bounds          worldBounds = Tile.GetComponent <MeshRenderer>().bounds;

            int   res          = TerraSettings.Instance.MeshResolution;
            float rayHeight    = worldBounds.max.y + 5;
            float rayMaxLength = rayHeight - (worldBounds.min.y - 5);

            List <Vector3> verts    = new List <Vector3>(MAX_VERTS_PER_MESH);
            List <Vector3> norms    = new List <Vector3>(MAX_VERTS_PER_MESH);
            List <int>     indicies = new List <int>(MAX_VERTS_PER_MESH);

            int idx            = 0;
            int iterationCount = 0;             //Tracks when to wait for next frame

            for (float x = worldBounds.min.x; x < worldBounds.max.x; x += StepLength)
            {
                for (float z = worldBounds.min.z; z < worldBounds.max.z; z += StepLength)
                {
                    float varX = x + Random.Range(-variation, variation);
                    float varZ = z + Random.Range(-variation, variation);

                    Ray        r = new Ray(new Vector3(varX, rayHeight, varZ), Vector3.down);
                    RaycastHit hit;

                    if (MeshCollider.Raycast(r, out hit, rayMaxLength) && CanPlaceAt(hit))
                    {
                        verts.Add(new Vector3(varX, hit.point.y, varZ));
                        norms.Add(hit.normal);
                        indicies.Add(idx);
                        idx++;
                    }

                    //If max verts met, flush to MeshData list
                    if (verts.Count >= MAX_VERTS_PER_MESH)
                    {
                        MeshData d = new MeshData();
                        d.vertices = verts;
                        d.normals  = norms;
                        d.indicies = indicies;
                        data.Add(d);

                        //Clear existing data
                        verts    = new List <Vector3>();
                        norms    = new List <Vector3>();
                        indicies = new List <int>();
                        idx      = 0;
                    }

                    //Possibly wait for next frame
                    iterationCount++;
                    if (iterationCount > MAX_ITERATIONS_PER_FRAME)
                    {
                        iterationCount = 0;
                        yield return(null);                        //Wait for next frame
                    }
                }
            }

            //Pause before returning as we want to create mesh in a different frame
            yield return(null);

            MeshData md = new MeshData();

            md.vertices = verts;
            md.normals  = norms;
            md.indicies = indicies;
            data.Add(md);

            onCalculated(data);
        }
示例#5
0
        static void ClearOnMove(TerrainTile tile)
        {
            VSProMapsTile vsTile = tile.GetComponent <VSProMapsTile>();

            vsTile?.OnDisable();
        }