Skip to content

Instantly share code, notes, and snippets.

@Frooxius
Last active February 10, 2020 04:57
Show Gist options
  • Save Frooxius/2616476eb51c3e8bf1b26913d4bd08a7 to your computer and use it in GitHub Desktop.
Save Frooxius/2616476eb51c3e8bf1b26913d4bd08a7 to your computer and use it in GitHub Desktop.
Value Noise Texture
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseX;
using CodeX;
namespace FrooxEngine
{
[Category("Assets/Procedural Textures")]
public class NoiseTexture : ProceduralTexture
{
public readonly Sync<int> Seed;
public readonly Sync<bool> Monochrome;
public readonly Sync<color> MonochromeMax;
public readonly Sync<color> MonochromeMin;
[RangeAttribute(-1f, 1f)]
public readonly Sync<float> Bias;
public readonly Sync<float> Gain;
public readonly Sync<bool> Clamp;
color _min;
color _max;
float _bias;
float _gain;
bool _clamp;
protected override void PrepareAssetUpdateData()
{
_min = MonochromeMin.Value;
_max = MonochromeMax.Value;
_bias = Bias.Value;
_gain = Gain.Value;
_clamp = Clamp.Value;
}
protected override void ClearTextureData()
{
}
protected override void UpdateTextureData(Bitmap2D tex2D)
{
var r = new Random(Seed.Value);
if(Monochrome.Value)
{
for (int y = 0; y < tex2D.Size.y; y++)
for (int x = 0; x < tex2D.Size.x; x++)
{
var value = (float)r.NextDouble();
tex2D.SetPixel(x, y, MathX.LerpUnclamped(_min, _max, value));
}
}
else
r.NextBytes(tex2D.RawData);
if(!(MathX.Approximatelly(_bias, 0f) && MathX.Approximatelly(_gain, 1f)))
{
for (int y = 0; y < tex2D.Size.y; y++)
for (int x = 0; x < tex2D.Size.x; x++)
{
var c = tex2D.GetPixel(x, y);
c = c * _gain + _bias;
if (_clamp)
c = MathX.Clamp01(c);
tex2D.SetPixel(x, y, c);
}
}
}
protected override void OnAwake()
{
base.OnAwake();
MonochromeMin.Value = color.Black;
MonochromeMax.Value = color.White;
Bias.Value = 0f;
Gain.Value = 1f;
Clamp.Value = true;
}
protected override void OnAttach()
{
base.OnAttach();
Seed.Value = RandomX.Int;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment