Seb
Seb

Reputation: 11

Perlin noise procedural generation - issue with generating chunks within unity

I am trying to develop a map generation system in Unity. I want to have a system for chunks and have simplified the problem down to a small amount of code, which is not attempting a great deal. I just can't understand why this is not tiling properly. I have searched up a lot to see if other people have come across similar issues, which they have, but there doesn't seem to be clear explanations of what is wrong. It is simple to copy code in that works, however I want to understand what is wrong with this.

Below, I have put the code for the only script within this project generating the noise map, as well a screenshot of the result in play mode within the editor.

I appreciate the time that anyone spends looking at this.

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

public class Noise : MonoBehaviour
{
    Color[] colorMap;
    Texture2D texture;

    public Transform[] chunks;
    public int dimension = 16;
    public float scale = 5;

    private void Start()
    {
        foreach (var chunk in chunks)
        {
            Renderer rend = chunk.GetComponent<Renderer>();
            texture = new Texture2D(dimension, dimension);
            texture.wrapMode = TextureWrapMode.Clamp;
            texture.filterMode = FilterMode.Point;
            colorMap = new Color[dimension * dimension];
            rend.material.mainTexture = texture;
            SetPixels(new Vector2(chunk.position.x, chunk.position.z));
        }
    }

    public void SetPixels(Vector2 offset)
    {
        for (int x = 0; x < dimension; x++)
        {
            for (int y = 0; y < dimension; y++)
            {
                float xCoord = (x + offset.x) / scale;
                float yCoord = (y + offset.y) / scale;
                float sample = Mathf.PerlinNoise(xCoord, yCoord);
                colorMap[y * dimension + x] = new Color(sample, sample, sample);
            }
        }

        texture.SetPixels(colorMap);
        texture.Apply();
    }
}

Screenshot of the result

Upvotes: 0

Views: 45

Answers (0)

Related Questions