Created
June 5, 2018 08:52
-
-
Save Frooxius/cd44516f885622c8509abc0d4c5619a4 to your computer and use it in GitHub Desktop.
Procedural Simplex Noise Texture
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using BaseX; | |
namespace FrooxEngine | |
{ | |
[Category("Assets/Procedural Textures")] | |
public class SimplexTexture : ProceduralTexture | |
{ | |
public readonly Sync<float2> Offset; | |
public readonly Sync<float2> Scale; | |
public readonly Sync<color> Background; | |
public readonly Sync<color> Foreground; | |
public readonly Sync<bool> Use3D; | |
public readonly Sync<float> ZOffset; | |
protected override IEnumerator<Context> UpdateTexture() | |
{ | |
float2 off = Offset; | |
float2 scale = Scale; | |
bool _3d = Use3D; | |
float zoff = ZOffset; | |
color bg = Background; | |
color fg = Foreground; | |
yield return Context.ToBackground(); | |
for(int y = 0; y < tex2D.Size.y; y++) | |
for (int x = 0; x < tex2D.Size.x; x++) | |
{ | |
// compute normalized position | |
float2 uv = new int2(x, y) / (float2)tex2D.Size; | |
uv *= scale; | |
uv += off; | |
float val = _3d ? MathX.SimplexNoise(new float3(uv, zoff)) : MathX.SimplexNoise(uv); | |
val *= 0.5f; | |
val += 0.5f; | |
tex2D.SetPixel(x, y, MathX.Lerp(bg, fg, val)); | |
} | |
} | |
protected override void OnInit() | |
{ | |
base.OnInit(); | |
Scale.Value = float2.One; | |
Background.Value = color.Black; | |
Foreground.Value = color.White; | |
} | |
protected override void OnAttach() | |
{ | |
base.OnAttach(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment