Пример #1
0
    public void Generate()
    {
        if (this != null)         // No idea why
        {
            var oldChild = transform.childCount > 0 ? transform.GetChild(0) : null;
            if (oldChild != null)
            {
                DestroyImmediate(oldChild.gameObject);
            }

            var colorGenerator = new ColorGenerator();             // Rewrite to static method
            var sharedMaterial = new Material(colorGenerator.GenerateMaterial(colorSettings));

            var terrainConfig = new WorldTerrainParameters()
            {
                Material   = sharedMaterial,
                Resolution = resolution,
                ColliderResolutionFraction = colliderResolutionFraction,
                ShapeSettings = shapeSettings
            };

            var world = WorldTerrainGenerator.GenerateWorldTerrain(terrainConfig);
            world.transform.parent   = transform;
            world.transform.position = transform.position;
        }
    }
    /// <summary>
    ///	Generates face meshes for all six directions, attaches them to a root node, and returns the root node
    /// </summary>
    public static GameObject GenerateWorldTerrain(WorldTerrainParameters parameters)
    {
        var rootNode = new GameObject("Terrain");

        Vector3[] faceDirections = { Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back };

        var faces = faceDirections.Select(direction =>
        {
            var face = GenerateWorldFace(rootNode.transform, direction, parameters.Resolution, parameters.ColliderResolutionFraction, parameters.Material, parameters.ShapeSettings);
            face.GameObject.transform.parent = rootNode.transform;
            return(face);
        });

        var combinedElevationMinMax = MinMax.Union(faces.Select(face => face.ElevationRange));

        parameters.Material.SetVector("_elevationMinMax", new Vector4(combinedElevationMinMax.Min, combinedElevationMinMax.Max));

        return(rootNode);
    }