Skip to content

Instantly share code, notes, and snippets.

@davepape
Last active November 6, 2018 18:49
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 davepape/e056eafa68d30f9453c3745963c0423c to your computer and use it in GitHub Desktop.
Save davepape/e056eafa68d30f9453c3745963c0423c to your computer and use it in GitHub Desktop.
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