Skip to content

Instantly share code, notes, and snippets.

@neoascetic
Created April 1, 2024 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neoascetic/768e898374c37f0c9442123130dd22c2 to your computer and use it in GitHub Desktop.
Save neoascetic/768e898374c37f0c9442123130dd22c2 to your computer and use it in GitHub Desktop.
Simple Unity script to preview various Perlin noise settings
using UnityEngine;
using Unity.Mathematics;
public class Noise : MonoBehaviour {
public int width;
public int height;
public float scaleX;
public float scaleY;
public float offsetX;
public float offsetY;
public bool useThreshold;
public float threshold;
public Color color;
public void Generate() {
var renderer = GetComponent<Renderer>();
var texture = new Texture2D(width, height) {
filterMode = FilterMode.Point,
alphaIsTransparency = true
};
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
var color = CalcColor(x, y, offsetX, offsetY);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
renderer.material.mainTexture = texture;
}
Color CalcColor(int x, int y, float offsetX, float offsetY) {
float xCoord = (float) x / width * scaleX + offsetX;
float yCoord = (float) y / height * scaleY + offsetY;
var sample = noise.cnoise(new float2(xCoord, yCoord));
if (useThreshold) {
if (sample >= threshold) {
return color;
}
return new Color(0, 0, 0, 0);
}
return new Color(sample, sample, sample);
}
void OnValidate() => Generate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment