-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NoiseLib.glslib to Common/ShaderLib in jme3-core
- Loading branch information
1 parent
2e70462
commit 15265a5
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
jme3-core/src/main/resources/Common/ShaderLib/NoiseLib.glsllib
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//2d noise functions | ||
float rand(float n){return fract(sin(n) * 43758.5453123);} | ||
float rand(vec2 n) { | ||
return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453); | ||
} | ||
|
||
float noise(vec2 n) { | ||
const vec2 d = vec2(0.0, 1.0); | ||
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n)); | ||
return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y); | ||
} | ||
|
||
|
||
float prand(vec2 c){ | ||
return fract(sin(dot(c.xy ,vec2(12.9898,78.233))) * 43758.5453); | ||
} | ||
|
||
float pnoise(vec2 p, float freqPct){ | ||
//float unit = circ/freq; | ||
float unit = freqPct; | ||
|
||
vec2 ij = floor(p/unit); | ||
vec2 xy = mod(p,unit)/unit; | ||
//xy = 3.*xy*xy-2.*xy*xy*xy; | ||
xy = .5*(1.-cos(3.1415926535*xy)); | ||
float a = prand((ij+vec2(0.,0.))); | ||
float b = prand((ij+vec2(1.,0.))); | ||
float c = prand((ij+vec2(0.,1.))); | ||
float d = prand((ij+vec2(1.,1.))); | ||
float x1 = mix(a, b, xy.x); | ||
float x2 = mix(c, d, xy.x); | ||
return mix(x1, x2, xy.y); | ||
} | ||
|
||
float rand2D(in vec2 co){ | ||
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); | ||
} | ||
|
||
// - - - - | ||
|
||
//3d noise functions | ||
float rand3D(in vec3 co){ | ||
return fract(sin(dot(co.xyz ,vec3(12.9898,78.233,144.7272))) * 43758.5453); | ||
} | ||
|
||
float simple_interpolate(in float a, in float b, in float x) | ||
{ | ||
return a + smoothstep(0.0,1.0,x) * (b-a); | ||
} | ||
float interpolatedNoise3D(in float x, in float y, in float z) | ||
{ | ||
float integer_x = x - fract(x); | ||
float fractional_x = x - integer_x; | ||
|
||
float integer_y = y - fract(y); | ||
float fractional_y = y - integer_y; | ||
|
||
float integer_z = z - fract(z); | ||
float fractional_z = z - integer_z; | ||
|
||
float v1 = rand3D(vec3(integer_x, integer_y, integer_z)); | ||
float v2 = rand3D(vec3(integer_x+1.0, integer_y, integer_z)); | ||
float v3 = rand3D(vec3(integer_x, integer_y+1.0, integer_z)); | ||
float v4 = rand3D(vec3(integer_x+1.0, integer_y +1.0, integer_z)); | ||
|
||
float v5 = rand3D(vec3(integer_x, integer_y, integer_z+1.0)); | ||
float v6 = rand3D(vec3(integer_x+1.0, integer_y, integer_z+1.0)); | ||
float v7 = rand3D(vec3(integer_x, integer_y+1.0, integer_z+1.0)); | ||
float v8 = rand3D(vec3(integer_x+1.0, integer_y +1.0, integer_z+1.0)); | ||
|
||
float i1 = simple_interpolate(v1,v5, fractional_z); | ||
float i2 = simple_interpolate(v2,v6, fractional_z); | ||
float i3 = simple_interpolate(v3,v7, fractional_z); | ||
float i4 = simple_interpolate(v4,v8, fractional_z); | ||
|
||
float ii1 = simple_interpolate(i1,i2,fractional_x); | ||
float ii2 = simple_interpolate(i3,i4,fractional_x); | ||
|
||
return simple_interpolate(ii1 , ii2 , fractional_y); | ||
} | ||
|
||
float Noise3D(in vec3 coord, in float wavelength) | ||
{ | ||
return interpolatedNoise3D(coord.x/wavelength, coord.y/wavelength, coord.z/wavelength); | ||
} | ||
|
||
|
||
//used to reference the same float generated by noise for all shaders, so affliction appears to spread and splat naturally | ||
float getStaticNoiseVar0(vec3 wPos, float afflictionVar){ | ||
float noiseVar0 = Noise3D(wPos, 28); | ||
float noiseVar1 = Noise3D(wPos, 3); | ||
float noiseVar2 = Noise3D(wPos, 0.8); | ||
|
||
float noiseVar = ((noiseVar0 + noiseVar1) * 0.4) + (noiseVar2 * 0.2 * afflictionVar); | ||
|
||
|
||
if(noiseVar > 0.7){ | ||
noiseVar -= noiseVar2 * 0.07; | ||
// noiseVar = min(noiseVar, 0.3); | ||
|
||
} | ||
|
||
noiseVar = min(noiseVar, 1.0); | ||
noiseVar = max(noiseVar, 0.0); | ||
|
||
return noiseVar; | ||
} |