Skip to content

Instantly share code, notes, and snippets.

@davepape davepape/noisetex.cs
Last active Nov 6, 2018

Embed
What would you like to do?
Create a fractal, procedural texture by combining different frequencies of Perlin noise
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class noisetex : MonoBehaviour {
public int width=256;
public int height=256;
private Texture2D tex;
private Color32[] colors;
void Start ()
{
tex = new Texture2D(width,height);
colors = new Color32[width*height];
GetComponent<Renderer>().material.mainTexture = tex;
generateTexture(0);
}
void Update ()
{
if (Time.frameCount % 100 == 0)
print(1f/Time.deltaTime + " fps");
generateTexture(Time.time/10f);
}
void generateTexture(float offset)
{
int index=0;
for (int j = 0; j < height; j++)
{
for (int i=0; i < width; i++)
{
byte v = (byte)(255 * value(i/(width-1f)+offset,j/(height-1f)));
colors[index] = new Color32(v,v,v,v);
index++;
}
}
tex.SetPixels32(colors);
tex.Apply();
}
float value(float x, float y)
{
float v = 0;
float f = 4, s = 0.75f;
for (int i=0; i < 8; i++)
{
v += s * Mathf.PerlinNoise(f*x, f*y);
f *= 2f;
s /= 3f;
}
return v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.