Created
April 1, 2024 15:58
-
-
Save neoascetic/768e898374c37f0c9442123130dd22c2 to your computer and use it in GitHub Desktop.
Simple Unity script to preview various Perlin noise settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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