Set color by perlin noise value?

I’m trying to generate a plane of cubes each diffrent colored by perlin noise so that I can use values. I don’t know how to use the value given to generate a color. If it helps I only want to generate green colors.

Heres my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Generator : MonoBehaviour
{
    public GameObject ground;
    GameObject[,] tiles;
   
    public int width = 100;
    public int length = 100;
   
    [Range(0.01f, 0.99f)]
    public float refinement = 0.1f;
   
    // Start is called before the first frame update
    void Start()
    {
        tiles = new GameObject[width, length];
       
        for(int x = 0; x < width; x++)
        {
            for(int z = 0; z < length; z++)
            {
                var block = Instantiate(ground, new Vector3(x, 0, z), Quaternion.identity);
                tiles[x,z] = block;
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

Perlin noise generates a value between 0 and 1. So you could simply define two color values (for example a dark green and a light green) and then lerp between the two values depending on the calculated perlin noise value.

In case you are not familiar with perlin noise, you will have to scale your integer coordinates with some arbitrary value to make them floats, since perlin noise will always return the same value for integers. Effectively this scale-value zooms in or out of the perlin noise you will see displayed by your colors.
Also, instantiating one object per coordinate, even for 100x100, already generates 10k objects. This will quickly run into performance problems. If you are trying something similar to a voxel based world, those only look like they are made out of ‘blocks’, while actually being one single object / mesh that looks as if it was hundreds of blocks, which then gets recreated on alteration. Even if you dont plan something like that, if your project will end up being bigger than 100x100 you will probably have to look into some optimizations such as these as you will run into similar problems.

I already worked with perlin noise so I knew I would have to convert my intergers to floats I just had it not fully optimised yet. I integerated the refinement value to have a diffrent value than 1, 2 , 3, you name it. I thought of using a gradient to pick my colors as I read your post. It really kicked my ideas off! I also thought of makeing a single mesh but I don’t know how to color it then.

That’s usually done using an texture atlas. Basically one large texture containing all textures, where you then decide while ‘part’ of it to use on a given part of the mesh.

I could work out how to paint perlin values on a quad! Thanks for replies! Now I just need to clean up the perlin noise.

6220965--684075--Screenshot (7).png

Unless I’m mistaken doesn’t Color.Lerp set the color to one of the colors instead of getting one from the middle? When I use Color.Lerp between a light green and a dark green with my perlin noise value it just returns black. I know I’m late but a response would be appreciated.

Please start your own thread, as specified in the forum rules. It’s free!

https://discussions.unity.com/t/757848

Point #2:

  • Start new threads in appropriate forums – Look at the sections and their descriptions before you post.