void Generate()
    {
        PoissonDiskGenerator.minDist     = minDistance;
        PoissonDiskGenerator.k           = k;
        PoissonDiskGenerator.sampleRange = sampleRange;
        result = PoissonDiskGenerator.Generate();

        // create result texture.
        if (resultTexture != null)
        {
            if (resultTexture.width != (int)PoissonDiskGenerator.sampleRange)
            {
                DestroyImmediate(resultTexture);
            }
        }
        resultTexture = new Texture2D((int)PoissonDiskGenerator.sampleRange,
                                      (int)PoissonDiskGenerator.sampleRange);
        ClearTetxure(Color.white);
        sampleCount = PoissonDiskGenerator.sampleCount;
        for (int i = 0; i < sampleCount; ++i)
        {
            resultTexture.SetPixel((int)result[i].x, (int)result[i].y, Color.black);
        }
        resultTexture.Apply();
    }
    public void scatterGrass(GameObject grass, float minDist, int k)
    {
        PoissonDiskGenerator.minDist     = minDist;
        PoissonDiskGenerator.k           = k;
        PoissonDiskGenerator.sampleRange = chunkSize - 1;
        List <Vector2> results = PoissonDiskGenerator.Generate();
        int            width   = (int)Mathf.Sqrt(heightMap.Length);

        foreach (Vector2 point in results)
        {
            int        index    = (int)(Mathf.Floor(point.x) + Mathf.Floor(point.y) * width);
            Vector3    worldPos = terrainObject.transform.TransformPoint(mesh.vertices[index]);
            GameObject clone    = Instantiate(grass, worldPos, Quaternion.identity) as GameObject;
            clone.transform.rotation = Quaternion.Euler(0, Random.Range(0.0f, 360.0f), 0);
        }
    }
Пример #3
0
    //---------------------------------------------------------------------
    //
    // HELPER PRIVATE FUNCTIONS
    //
    //---------------------------------------------------------------------

    // Fill an area with prefabs
    private static GameObject PoissonDiskFill(Terrain terrain, PoissonDiskFillData poissonDiskFillData, string name = "Scenery Area")
    {
        var result = new GameObject(name);

        result.transform.position = Vector3.zero;
        result.transform.rotation = Quaternion.identity;

        if (poissonDiskFillData.Prefabs == null || poissonDiskFillData.Prefabs.Length <= 0)
        {
            return(result);
        }

        var levelCreator = terrain.transform.parent.GetComponent <LevelCreator>();
        var size         = poissonDiskFillData.FrameSize;

        PoissonDiskGenerator.minDist     = poissonDiskFillData.MinDist;
        PoissonDiskGenerator.sampleRange = (size.x > size.y ? size.x : size.y);
        PoissonDiskGenerator.Generate();
        foreach (var sample in PoissonDiskGenerator.ResultSet)
        {
            var point  = sample + poissonDiskFillData.FramePosition;
            var height = terrain.SampleHeight(new Vector3(point.x, 0, point.y) - terrain.transform.position);
            if (height <= (levelCreator.WaterHeight + 0.01f) * terrain.terrainData.size.y ||  // not underwater
                !point.IsInsidePolygon(poissonDiskFillData.Polygon) ||                        //not outside of the area
                !poissonDiskFillData.ClearPolygons.TrueForAll(a => !point.IsInsidePolygon(a)) //not inside of any clear polygon
                )
            {
                continue;
            }

            var go = Object.Instantiate(poissonDiskFillData.Prefabs[Random.Range(0, poissonDiskFillData.Prefabs.Length)]);
            go.transform.position = new Vector3(point.x, height, point.y) + terrain.transform.position;
            go.transform.rotation = terrain.GetNormalRotation(go.transform.position) * Quaternion.Euler(go.transform.rotation.eulerAngles.x, Random.Range(0, 360f), go.transform.rotation.eulerAngles.z);
            go.CorrectAngleTolerance(poissonDiskFillData.AngleTolerance);
            go.transform.parent = result.transform;

            if (poissonDiskFillData.NotWalkable)
            {
                var navMod = go.AddComponent <NavMeshModifier>();
                navMod.overrideArea = true;
                navMod.area         = NavMesh.GetAreaFromName("Not Walkable");
            }
        }

        return(result);
    }