Skip to content

Commit

Permalink
fixing dilation/erosion tweaking random and adding AABB
Browse files Browse the repository at this point in the history
  • Loading branch information
patriciogonzalezvivo committed Dec 8, 2022
1 parent 6f0ea4e commit ccedcae
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 13 deletions.
36 changes: 32 additions & 4 deletions generative/fbm.glsl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "snoise.glsl"
#include "gnoise.glsl"


/*
original_author: Patricio Gonzalez Vivo
description: Fractal Brownian Motion
use: fbm(<vec2> pos)
options:
FBM_OCTAVES: numbers of octaves. Default is 4.
FBM_NOISE_FNC(POS_UV): noise function to use Default 'snoise(POS_UV)' (simplex noise)
FBM_NOISE_FNC(UV): noise function to use Default 'snoise(UV)' (simplex noise)
FBM_VALUE_INITIAL: initial value. Default is 0.
FBM_SCALE_SCALAR: scalar. Defualt is 2.
FBM_AMPLITUD_INITIAL: initial amplitud value. Default is 0.5
Expand All @@ -18,17 +20,24 @@ options:
#endif

#ifndef FBM_NOISE_FNC
#define FBM_NOISE_FNC(POS_UV) snoise(POS_UV)
#define FBM_NOISE_FNC(UV) snoise(UV)
#endif

#ifndef FBM_NOISE2_FNC
#define FBM_NOISE2_FNC(POS_UV) FBM_NOISE_FNC(POS_UV)
#define FBM_NOISE2_FNC(UV) FBM_NOISE_FNC(UV)
#endif

#ifndef FBM_NOISE3_FNC
#define FBM_NOISE3_FNC(POS_UV) FBM_NOISE_FNC(POS_UV)
#define FBM_NOISE3_FNC(UV) FBM_NOISE_FNC(UV)
#endif

#ifndef FBM_NOISE_TILABLE_FNC
#define FBM_NOISE_TILABLE_FNC(UV, TILE) gnoise(UV, TILE)
#endif

#ifndef FBM_NOISE3_TILABLE_FNC
#define FBM_NOISE3_TILABLE_FNC(UV, TILE) FBM_NOISE_TILABLE_FNC(UV, TILE)
#endif

#ifndef FBM_NOISE_TYPE
#define FBM_NOISE_TYPE float
Expand Down Expand Up @@ -79,4 +88,23 @@ FBM_NOISE_TYPE fbm(in vec3 pos) {
}
return value;
}

FBM_NOISE_TYPE fbm(vec3 p, float tileLength) {
const float persistence = 0.5;
const float lacunarity = 2.0;

float amplitude = 0.5;
float total = 0.0;
float normalization = 0.0;

for (int i = 0; i < FBM_OCTAVES; ++i) {
float noiseValue = FBM_NOISE3_TILABLE_FNC(p, tileLength * lacunarity * 0.5) * 0.5 + 0.5;
total += noiseValue * amplitude;
normalization += amplitude;
amplitude *= persistence;
p = p * lacunarity;
}

return total / normalization;
}
#endif
46 changes: 46 additions & 0 deletions generative/gnoise.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "random.glsl"
#include "srandom.glsl"
#include "../math/cubic.glsl"
#include "../math/quintic.glsl"

/*
original_author: Patricio Gonzalez Vivo
Expand All @@ -15,4 +18,47 @@ float gnoise(float x) {
return mix(random(i), random(i + 1.0), smoothstep(0.,1.,f));
}

float gnoise(vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
vec2 u = cubic(f);
return mix( a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}

float gnoise(vec3 p) {
vec3 i = floor(p);
vec3 f = fract(p);
vec3 u = quintic(f);
return -1.0 + 2.0 * mix( mix( mix( random(i + vec3(0.0,0.0,0.0)),
random(i + vec3(1.0,0.0,0.0)), u.x),
mix( random(i + vec3(0.0,1.0,0.0)),
random(i + vec3(1.0,1.0,0.0)), u.x), u.y),
mix( mix( random(i + vec3(0.0,0.0,1.0)),
random(i + vec3(1.0,0.0,1.0)), u.x),
mix( random(i + vec3(0.0,1.0,1.0)),
random(i + vec3(1.0,1.0,1.0)), u.x), u.y), u.z );
}

float gnoise(vec3 p, float tileLength) {
vec3 i = floor(p);
vec3 f = fract(p);

vec3 u = quintic(f);

return mix( mix( mix( dot( srandom3(i + vec3(0.0,0.0,0.0), tileLength), f - vec3(0.0,0.0,0.0)),
dot( srandom3(i + vec3(1.0,0.0,0.0), tileLength), f - vec3(1.0,0.0,0.0)), u.x),
mix( dot( srandom3(i + vec3(0.0,1.0,0.0), tileLength), f - vec3(0.0,1.0,0.0)),
dot( srandom3(i + vec3(1.0,1.0,0.0), tileLength), f - vec3(1.0,1.0,0.0)), u.x), u.y),
mix( mix( dot( srandom3(i + vec3(0.0,0.0,1.0), tileLength), f - vec3(0.0,0.0,1.0)),
dot( srandom3(i + vec3(1.0,0.0,1.0), tileLength), f - vec3(1.0,0.0,1.0)), u.x),
mix( dot( srandom3(i + vec3(0.0,1.0,1.0), tileLength), f - vec3(0.0,1.0,1.0)),
dot( srandom3(i + vec3(1.0,1.0,1.0), tileLength), f - vec3(1.0,1.0,1.0)), u.x), u.y), u.z );
}

#endif
27 changes: 27 additions & 0 deletions generative/srandom.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ use: srandomX(<vec2|vec3> x)
#ifndef FNC_SRANDOM
#define FNC_SRANDOM

float srandom(in float x) {
return -1. + 2. * fract(sin(x) * 43758.5453);
}

float srandom(in vec2 st) {
return -1. + 2. * fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453);
}

float srandom(in vec3 pos) {
return -1. + 2. * fract(sin(dot(pos.xyz, vec3(70.9898, 78.233, 32.4355))) * 43758.5453123);
}

float srandom(in vec4 pos) {
float dot_product = dot(pos, vec4(12.9898,78.233,45.164,94.673));
return -1. + 2. * fract(sin(dot_product) * 43758.5453);
}

vec2 srandom2(in vec2 st) {
const vec2 k = vec2(.3183099, .3678794);
st = st * k + k.yx;
Expand All @@ -20,4 +37,14 @@ vec3 srandom3(in vec3 p) {
return -1. + 2. * fract(sin(p) * 43758.5453123);
}

vec2 srandom2(in vec2 p, const in float tileLength) {
p = mod(p, vec2(tileLength));
return srandom2(p);
}

vec3 srandom3(in vec3 p, const in float tileLength) {
p = mod(p, vec3(tileLength));
return srandom3(p);
}

#endif
2 changes: 1 addition & 1 deletion generative/worley.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ float worley(vec2 p){
return 1.0-dis;
}

float worley(vec3 p){
float worley(vec3 p) {
vec3 n = floor( p );
vec3 f = fract( p );

Expand Down
9 changes: 9 additions & 0 deletions lighting/common/henyeyGreenstein.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@
float henyeyGreenstein(const in float mu) {
return max(0.0, (1.0 - HENYEYGREENSTEIN_SCATTERING*HENYEYGREENSTEIN_SCATTERING) / ((4. + PI) * pow(1.0 + HENYEYGREENSTEIN_SCATTERING*HENYEYGREENSTEIN_SCATTERING - 2.0 * HENYEYGREENSTEIN_SCATTERING * mu, 1.5)));
}

float henyeyGreenstein(float mu, float g) {
float gg = g * g;
return (1.0 / (4.0 * PI)) * ((1.0 - gg) / pow(1.0 + gg - 2.0 * g * mu, 1.5));
}

float henyeyGreenstein(float mu, float g, float dual_lobe_weight) {
return mix(henyeyGreenstein( mu, -g), henyeyGreenstein(mu, g), dual_lobe_weight);
}
#endif
9 changes: 9 additions & 0 deletions lighting/common/henyeyGreenstein.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@
float henyeyGreenstein(float mu) {
return max(0.0, (1.0 - HENYEYGREENSTEIN_SCATTERING*HENYEYGREENSTEIN_SCATTERING) / ((4. + PI) * pow(1.0 + HENYEYGREENSTEIN_SCATTERING*HENYEYGREENSTEIN_SCATTERING - 2.0 * HENYEYGREENSTEIN_SCATTERING * mu, 1.5)));
}

float henyeyGreenstein(float mu, float g) {
float gg = g * g;
return (1.0 / (4.0 * PI)) * ((1.0 - gg) / pow(1.0 + gg - 2.0 * g * mu, 1.5));
}

float henyeyGreenstein(float mu, float g, float dual_lobe_weight) {
return mix(henyeyGreenstein( mu, -g), henyeyGreenstein(mu, g), dual_lobe_weight);
}
#endif
3 changes: 3 additions & 0 deletions math/smootherstep.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ use: smoothstep(<float> in, <float> out, <float> value)
#ifndef FNC_SMOOTHERSTEP
#define FNC_SMOOTHERSTEP
float smootherstep(float edge0, float edge1, float x) { return quintic( saturate( (x - edge0)/(edge1 - edge0) )); }
vec2 smootherstep(vec2 edge0, vec2 edge1, vec2 x) { return quintic( saturate( (x - edge0)/(edge1 - edge0) )); }
vec3 smootherstep(vec3 edge0, vec3 edge1, vec3 x) { return quintic( saturate( (x - edge0)/(edge1 - edge0) )); }
vec4 smootherstep(vec4 edge0, vec4 edge1, vec4 x) { return quintic( saturate( (x - edge0)/(edge1 - edge0) )); }
#endif
3 changes: 3 additions & 0 deletions math/smootherstep.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ use: smoothstep(<float> in, <float> out, <float> value)
#ifndef FNC_SMOOTHERSTEP
#define FNC_SMOOTHERSTEP
float smootherstep(float edge0, float edge1, float x) { return quintic( saturate( (x - edge0)/(edge1 - edge0) )); }
float2 smootherstep(float2 edge0, float2 edge1, float2 x) { return quintic( saturate( (x - edge0)/(edge1 - edge0) )); }
float3 smootherstep(float3 edge0, float3 edge1, float3 x) { return quintic( saturate( (x - edge0)/(edge1 - edge0) )); }
float4 smootherstep(float4 edge0, float4 edge1, float4 x) { return quintic( saturate( (x - edge0)/(edge1 - edge0) )); }
#endif
8 changes: 4 additions & 4 deletions morphological/dilation.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ options:
#define DILATION_TYPE float
#endif

#ifndef DILATION_MAP
#define DILATION_MAP_FNC(UV)
#ifndef DILATION_SAMPLE_FNC
#define DILATION_SAMPLE_FNC(TEX, UV) SAMPLER_FNC(TEX, UV).r
#endif

#ifndef FNC_DILATE
Expand All @@ -37,8 +37,8 @@ DILATION_TYPE dilation(sampler2D tex, vec2 st, vec2 pixel, int radius) {
vec2 kst = rxy * invKR * 2.0;
vec2 texOffset = st + rxy * pixel;
float kernel = saturate(1.0 - dot(kst, kst));
DILATION_TYPE tex = DILATION_SAMPLE_FNC(tex, texOffset);
DILATION_TYPE v = tex + kernel;
DILATION_TYPE t = DILATION_SAMPLE_FNC(tex, texOffset);
DILATION_TYPE v = t + kernel;
if (sum(v) > sum(acc)) {
acc = v;
w = kernel;
Expand Down
4 changes: 2 additions & 2 deletions morphological/dilation.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ DILATION_TYPE dilation(sampler2D tex,float2 st,float2 pixel, int radius) {
float2 kst = rxy * invKR;
float2 texOffset = st + rxy * pixel;
float kernel = saturate(1.0 - dot(kst, kst));
DILATION_TYPE tex = DILATION_SAMPLE_FNC(tex, texOffset);
DILATION_TYPE v = tex + kernel;
DILATION_TYPE t = DILATION_SAMPLE_FNC(tex, texOffset);
DILATION_TYPE v = t + kernel;
if (sum(v) > sum(acc)) {
acc = v;
w = kernel;
Expand Down
4 changes: 2 additions & 2 deletions morphological/erosion.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ EROSION_TYPE erosion(sampler2D tex, vec2 st, vec2 pixel, int radius) {
vec2 kst = rxy * invKR;
vec2 texOffset = st + rxy * pixel;
float kernel = saturate(1.0 - dot(kst, kst));
EROSION_TYPE tex = EROSION_SAMPLE_FNC(tex, texOffset);
EROSION_TYPE v = tex - kernel;
EROSION_TYPE t = EROSION_SAMPLE_FNC(tex, texOffset);
EROSION_TYPE v = t - kernel;
if (sum(v) < sum(acc)) {
acc = v;
w = kernel;
Expand Down
9 changes: 9 additions & 0 deletions space/aabb.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef STR_AABB
#define STR_AABB

struct AABB {
vec3 min;
vec3 max;
};

#endif
18 changes: 18 additions & 0 deletions space/aabb/inside.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#include "../aabb.glsl"

/*
original_author: P
description: Compute if point is inside AABB
use: <bool> AABBinside(<AABB> box, <vec3> point )
*/

#ifndef FNC_AABB_INSIDE
#define FNC_AABB_INSIDE

bool AABBinside(const in AABB box, const in vec3 point ) {
return all( lessThanEqual(point, box.max) ) &&
all( lessThan(box.min, point) );
}

#endif
23 changes: 23 additions & 0 deletions space/aabb/intersect.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "../aabb.glsl"

/*
original_author: Dominik Schmid
description: compute the near and far intersections of the cube (stored in the x and y components) using the slab method
// no intersection means vec.x > vec.y (really tNear > tFar) https://gist.github.com/DomNomNom/46bb1ce47f68d255fd5d
use: <vec2> AABBintersect(<AABB> box, <vec3> rayOrigin, <vec3> rayDir)
*/

#ifndef FNC_AABB_INTERSECT
#define FNC_AABB_INTERSECT

vec2 AABBintersect(const in AABB box, const in vec3 rayOrigin, const in vec3 rayDir) {
vec3 tMin = (box.min - rayOrigin) / rayDir;
vec3 tMax = (box.max - rayOrigin) / rayDir;
vec3 t1 = min(tMin, tMax);
vec3 t2 = max(tMin, tMax);
float tNear = max(max(t1.x, t1.y), t1.z);
float tFar = min(min(t2.x, t2.y), t2.z);
return vec2(tNear, tFar);
}

#endif

0 comments on commit ccedcae

Please sign in to comment.