Moved demo files into demo folder, did a bunch of cleanup on noise lib
file.
This commit is contained in:
parent
6fe4a2b851
commit
1f6f4de7b8
@ -1,5 +1,7 @@
|
||||
local Histogram = require 'histogram'
|
||||
-- Update the package path because noise.lua is in the parent directory:
|
||||
package.path = package.path .. ";../noise.lua"
|
||||
local Noise = require "noise"
|
||||
local Histogram = require 'histogram'
|
||||
local DRAW_RES = love.window.toPixels(4)
|
||||
local SCALE = 100
|
||||
|
||||
@ -8,6 +10,8 @@ local n1,g1 = Noise.make1d{resolution=5, distribution=decay, seed=1}
|
||||
local n2,g2 = Noise.make2d{resolution=7, distribution=decay, seed=1}
|
||||
local n3,g3 = Noise.make3d{resolution=9, distribution=decay, seed=1}
|
||||
local s1,s1g = Noise.make1dShader{resolution=10, distribution=decay, seed=1}
|
||||
s1:send("draw_outline",true)
|
||||
s1g:send("draw_outline",true)
|
||||
local s2,s2g = Noise.make2dShader{resolution=15, distribution=decay, seed=1}
|
||||
local s3,s3g = Noise.make3dShader{resolution=20, distribution=decay, seed=1}
|
||||
|
76
noise.lua
76
noise.lua
@ -38,8 +38,15 @@
|
||||
-- love.graphics.setShader(noise_shader)
|
||||
-- love.graphics.draw(canvas)
|
||||
-- love.graphics.setShader()
|
||||
-- The noise gradient shaders populate the RGB channels with dn/dx, dn/dy, dn/dz partial
|
||||
-- derivatives, where [-1,0,1] is remapped to [0,0.5,1].
|
||||
-- The noise gradient shaders populate the RGB channels with:
|
||||
-- * for 1D: sigmoid(2*dn/dx), sigmoid(2*dn/dx), sigmoid(2*dn/dx)
|
||||
-- * for 2D: sigmoid(1.5*dn/dx), sigmoid(1.5*dn/dy), 0
|
||||
-- * for 3D: sigmoid(4*dn/dx), sigmoid(4*dn/dy), sigmoid(4*dn/dz)
|
||||
-- (coefficients were arbitrarily chosen to best squash typical gradients for the default
|
||||
-- parameters evenly into (0,1))
|
||||
-- Additionally, the 1D noise shader has an extra variable "draw_outline" that, if set to
|
||||
-- true, makes the 1D shader render output as white or black if the y-coordinate is
|
||||
-- above/below the noise value for a given x-coordinate.
|
||||
--
|
||||
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
-- this software and associated documentation files (the "Software"), to deal in
|
||||
@ -274,10 +281,14 @@ if love and love.graphics then
|
||||
local shader1d = [[
|
||||
#define TAU 6.283185307179586476925286766559005768394338798750211641949
|
||||
#define MAX_RESOLUTION 64
|
||||
#define SIGMOID(x) (1./(1.+exp(-(x))))
|
||||
#define CDF(x) (.5 + .5*sign(x)*sqrt(1.-exp(-4./TAU * (x)*(x))))
|
||||
#define CDF_PRIME(x, dx) ((0.31831 * exp(-4./TAU * (x)*(x)) * abs(x)*(dx))/sqrt(1.0-exp(-4./TAU * (x)*(x))))
|
||||
extern int resolution;
|
||||
extern float sigma, range_min, range_max;
|
||||
extern float amplitudes[MAX_RESOLUTION];
|
||||
extern float offsets[MAX_RESOLUTION];
|
||||
extern bool draw_outline = false;
|
||||
|
||||
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
|
||||
{
|
||||
@ -295,14 +306,20 @@ if love and love.graphics then
|
||||
}
|
||||
noise /= sigma;
|
||||
#ifdef GRADIENT
|
||||
dndx /= sigma;
|
||||
dndx = (0.31831 * exp(-4./TAU * noise*noise) * abs(noise)*dndx)/sqrt(1.0-exp(-4./TAU * noise*noise));
|
||||
// TODO: normalize properly
|
||||
dndx = .5 + .5*dndx;
|
||||
return vec4(dndx,dndx,dndx, 1.);
|
||||
dndx = CDF_PRIME(noise, dndx/sigma);
|
||||
dndx = SIGMOID(dndx*2.);
|
||||
if (draw_outline) {
|
||||
return texture_coords.y > (1.-dndx) ? vec4(1.,1.,1.,1.) : vec4(0.,0.,0.,1.);
|
||||
} else {
|
||||
return vec4(dndx,dndx,dndx,1.);
|
||||
}
|
||||
#else
|
||||
noise = .5 + .5*sign(noise)*sqrt(1.-exp(-4./TAU * noise*noise));
|
||||
return vec4(noise,noise,noise, 1.);
|
||||
noise = CDF(noise);
|
||||
if (draw_outline) {
|
||||
return texture_coords.y > (1.-noise) ? vec4(1.,1.,1.,1.) : vec4(0.,0.,0.,1.);
|
||||
} else {
|
||||
return vec4(noise,noise,noise,1.);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
]]
|
||||
@ -333,6 +350,9 @@ if love and love.graphics then
|
||||
local shader2d = [[
|
||||
#define TAU 6.283185307179586476925286766559005768394338798750211641949
|
||||
#define PHI 1.618033988749894848204586834365638117720309179805762862135
|
||||
#define CDF(x) (.5 + .5*sign(x)*sqrt(1.-exp(-4./TAU * (x)*(x))))
|
||||
#define CDF_PRIME(x, dx) ((0.31831 * exp(-4./TAU * (x)*(x)) * abs(x)*(dx))/sqrt(1.0-exp(-4./TAU * (x)*(x))))
|
||||
#define SIGMOID(x) (1./(1.+exp(-2.*(x))))
|
||||
extern float sigma;
|
||||
extern float amplitudes[RESOLUTION];
|
||||
extern vec2 offsets[RESOLUTION];
|
||||
@ -358,20 +378,19 @@ if love and love.graphics then
|
||||
dndy += sina*sin(u/a + k) + cosa*sin(v/a + w);
|
||||
#endif
|
||||
}
|
||||
noise /= 2.*sigma;
|
||||
|
||||
float _sigma = 2.*sigma;
|
||||
noise /= _sigma;
|
||||
#ifdef GRADIENT
|
||||
dndx /= 2.*sigma;
|
||||
dndx = (0.31831 * exp(-4./TAU * noise*noise) * abs(noise)*dndx)/sqrt(1.0-exp(-4./TAU * noise*noise));
|
||||
dndx = .5 + .5*dndx;
|
||||
dndx = CDF_PRIME(noise, dndx/_sigma);
|
||||
dndx = SIGMOID(dndx*1.5);
|
||||
|
||||
dndy /= 2.*sigma;
|
||||
dndy = (0.31831 * exp(-4./TAU * noise*noise) * abs(noise)*dndy)/sqrt(1.0-exp(-4./TAU * noise*noise));
|
||||
dndy = .5 + .5*dndy;
|
||||
dndy = CDF_PRIME(noise, dndy/_sigma);
|
||||
dndy = SIGMOID(dndy*1.5);
|
||||
|
||||
return vec4(dndx,dndy,0.,1.);
|
||||
#else
|
||||
noise = .5 + .5*sign(noise)*sqrt(1.-exp(-4./TAU * noise*noise));
|
||||
noise = CDF(noise);
|
||||
return vec4(noise,noise,noise,1.);
|
||||
#endif
|
||||
}
|
||||
@ -409,6 +428,9 @@ if love and love.graphics then
|
||||
#define PHI 1.618033988749894848204586834365638117720309179805762862135
|
||||
#define LOG_PHI 0.481211825059603447497758913424368423135184334385660519660
|
||||
#define SQRT5 2.236067977499789696409173668731276235440618359611525724270
|
||||
#define CDF(x) (.5 + .5*sign(x)*sqrt(1.-exp(-4./TAU * (x)*(x))))
|
||||
#define CDF_PRIME(x, dx) ((0.31831 * exp(-4./TAU * (x)*(x)) * abs(x)*(dx))/sqrt(1.0-exp(-4./TAU * (x)*(x))))
|
||||
#define SIGMOID(x) (1./(1.+exp(-2.*(x))))
|
||||
extern float sigma, z;
|
||||
extern float amplitudes[RESOLUTION];
|
||||
extern vec3 offsets[RESOLUTION];
|
||||
@ -460,23 +482,21 @@ if love and love.graphics then
|
||||
dndz += (n1.z*k.x + n2.z*k.y + n3.z*k.z)/3.;
|
||||
#endif
|
||||
}
|
||||
n /= 3.*sigma;
|
||||
float _sigma = 3.*sigma;
|
||||
n /= _sigma;
|
||||
#ifdef GRADIENT
|
||||
dndx /= sigma;
|
||||
dndx = (0.31831 * exp(-4./TAU * n*n) * abs(n)*dndx)/sqrt(1.0-exp(-4./TAU * n*n));
|
||||
dndx = .5 + .5*dndx;
|
||||
dndx = CDF_PRIME(n, dndx/_sigma);
|
||||
dndx = SIGMOID(dndx*4.);
|
||||
|
||||
dndy /= sigma;
|
||||
dndy = (0.31831 * exp(-4./TAU * n*n) * abs(n)*dndy)/sqrt(1.0-exp(-4./TAU * n*n));
|
||||
dndy = .5 + .5*dndy;
|
||||
dndy = CDF_PRIME(n, dndy/_sigma);
|
||||
dndy = SIGMOID(dndy*4.);
|
||||
|
||||
dndz /= sigma;
|
||||
dndz = (0.31831 * exp(-4./TAU * n*n) * abs(n)*dndz)/sqrt(1.0-exp(-4./TAU * n*n));
|
||||
dndz = .5 + .5*dndz;
|
||||
dndz = CDF_PRIME(n, dndz);
|
||||
dndz = SIGMOID(dndz*4.);
|
||||
|
||||
return vec4(dndx,dndy,dndz, 1.);
|
||||
#else
|
||||
n = .5 + .5*sign(n)*sqrt(1.-exp(-4./TAU * n*n));
|
||||
n = CDF(n);
|
||||
return vec4(n,n,n,1.);
|
||||
#endif
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ extern float sigma;
|
||||
extern float amplitudes[MAX_RESOLUTION];
|
||||
extern float offsets[MAX_RESOLUTION];
|
||||
extern float range_min, range_max;
|
||||
extern bool draw_outline = false;
|
||||
|
||||
float cdf(float x) {
|
||||
return .5 + .5*sign(x)*sqrt(1.-exp(-4./TAU * x*x));
|
||||
@ -23,6 +24,9 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
|
||||
{
|
||||
float x = mix(range_min,range_max,texture_coords.x);
|
||||
float n = noise1d(x);
|
||||
//return vec4(n,n,n,1.);
|
||||
return texture_coords.y > (1.-n) ? vec4(1.,1.,1.,1.) : vec4(0.,0.,0.,1.);
|
||||
if (draw_outline) {
|
||||
return texture_coords.y > (1.-n) ? vec4(1.,1.,1.,1.) : vec4(0.,0.,0.,1.);
|
||||
} else {
|
||||
return vec4(n,n,n,1.);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user