Skip to content

Commit 6d71091

Browse files
committed
added a Perlin noise shader
1 parent 2724826 commit 6d71091

4 files changed

Lines changed: 254 additions & 2 deletions

File tree

lib/chronosgl.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ part "src/shader/plane_shader.dart";
3131
part "src/shader/ssao_shader.dart";
3232
part "src/shader/sobel_shader.dart";
3333
part "src/shader/debug_shader.dart";
34+
part "src/shader/perlin_noise_func.dart";
35+
part "src/shader/perlin_noise_shader.dart";
3436

3537
abstract class Animatable {
3638
void animate( double elapsed);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
part of chronosgl;
2+
3+
// https://github.com/ashima/webgl-noise
4+
5+
const String perlinNoisefunctions = """
6+
vec3 mod289(vec3 x)
7+
{
8+
return x - floor(x * (1.0 / 289.0)) * 289.0;
9+
}
10+
11+
vec4 mod289(vec4 x)
12+
{
13+
return x - floor(x * (1.0 / 289.0)) * 289.0;
14+
}
15+
16+
vec4 permute(vec4 x)
17+
{
18+
return mod289(((x*34.0)+1.0)*x);
19+
}
20+
21+
vec4 taylorInvSqrt(vec4 r)
22+
{
23+
return 1.79284291400159 - 0.85373472095314 * r;
24+
}
25+
26+
vec3 fade(vec3 t) {
27+
return t*t*t*(t*(t*6.0-15.0)+10.0);
28+
}
29+
30+
31+
// Classic Perlin noise, periodic variant
32+
float pnoise(vec3 P, vec3 rep)
33+
{
34+
vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period
35+
vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period
36+
Pi0 = mod289(Pi0);
37+
Pi1 = mod289(Pi1);
38+
vec3 Pf0 = fract(P); // Fractional part for interpolation
39+
vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
40+
vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
41+
vec4 iy = vec4(Pi0.yy, Pi1.yy);
42+
vec4 iz0 = Pi0.zzzz;
43+
vec4 iz1 = Pi1.zzzz;
44+
45+
vec4 ixy = permute(permute(ix) + iy);
46+
vec4 ixy0 = permute(ixy + iz0);
47+
vec4 ixy1 = permute(ixy + iz1);
48+
49+
vec4 gx0 = ixy0 * (1.0 / 7.0);
50+
vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;
51+
gx0 = fract(gx0);
52+
vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
53+
vec4 sz0 = step(gz0, vec4(0.0));
54+
gx0 -= sz0 * (step(0.0, gx0) - 0.5);
55+
gy0 -= sz0 * (step(0.0, gy0) - 0.5);
56+
57+
vec4 gx1 = ixy1 * (1.0 / 7.0);
58+
vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;
59+
gx1 = fract(gx1);
60+
vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
61+
vec4 sz1 = step(gz1, vec4(0.0));
62+
gx1 -= sz1 * (step(0.0, gx1) - 0.5);
63+
gy1 -= sz1 * (step(0.0, gy1) - 0.5);
64+
65+
vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);
66+
vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);
67+
vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);
68+
vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);
69+
vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);
70+
vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);
71+
vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);
72+
vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);
73+
74+
vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
75+
g000 *= norm0.x;
76+
g010 *= norm0.y;
77+
g100 *= norm0.z;
78+
g110 *= norm0.w;
79+
vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
80+
g001 *= norm1.x;
81+
g011 *= norm1.y;
82+
g101 *= norm1.z;
83+
g111 *= norm1.w;
84+
85+
float n000 = dot(g000, Pf0);
86+
float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
87+
float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
88+
float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
89+
float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
90+
float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
91+
float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
92+
float n111 = dot(g111, Pf1);
93+
94+
vec3 fade_xyz = fade(Pf0);
95+
vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
96+
vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
97+
float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
98+
return 2.2 * n_xyz;
99+
}
100+
""";
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
part of chronosgl;
2+
3+
ShaderObject getPerlinNoiseVertexColorShader() {
4+
ShaderObject shaderObject = new ShaderObject();
5+
6+
shaderObject.vertexShader = perlinNoisefunctions + """
7+
attribute vec3 aVertexPosition;
8+
9+
uniform mat4 uMVMatrix;
10+
uniform mat4 uPMatrix;
11+
uniform float time;
12+
uniform float fadeFactor;
13+
14+
varying vec4 vColor;
15+
16+
vec4 lerp( vec4 vec, float factor) {
17+
vec.x += factor * (1. - vec.x);
18+
vec.y += factor * (1. - vec.y);
19+
vec.z += factor * (1. - vec.z);
20+
return vec;
21+
}
22+
23+
void main() {
24+
25+
float mytime = time / 4.0;
26+
27+
//float sint = (sin( mytime*10.)+1.0)/2.0;
28+
29+
float r = pnoise( .75 * ( aVertexPosition*0.25 + mytime ), vec3( 2.0 ) );
30+
float g = pnoise( 0.8 * ( aVertexPosition*0.25 + mytime ), vec3( 2.0 ) );
31+
float b = pnoise( 0.9 * ( aVertexPosition*0.25 + mytime ), vec3( 2.0 ) );
32+
float n = pnoise( 1.5 * ( aVertexPosition*0.25 + mytime ), vec3( 2.0 ) );
33+
n = pow( 1., n );
34+
//float n = 10.0 * pnoise( 5.0 * ( vNormal + time ), vec3( 10.0 ) ) * pnoise( .5 * ( vNormal + time ), vec3( 10.0 ) );
35+
//n += .5 * pnoise( 4.0 * vNormal, vec3( 10.0 ) );
36+
//vColor = vec4( 1.0-(r + n), 1.0-(g + n), 1.0-(b + n), 1.0 );
37+
//n=0.0;
38+
vColor = vec4( (r + n), (g + n), (b + n), 1.0 );
39+
vColor = lerp( vColor, fadeFactor);
40+
41+
// float f = 0.5 * pnoise( normal + time, vec3( 10.0 ) );
42+
gl_Position = uPMatrix * uMVMatrix * vec4( aVertexPosition, 1.0 );
43+
}
44+
""";
45+
46+
shaderObject.fragmentShader = """
47+
precision mediump float;
48+
49+
varying vec4 vColor;
50+
51+
void main(void) {
52+
gl_FragColor = vColor;
53+
}
54+
""";
55+
56+
shaderObject.vertexPositionAttribute = "aVertexPosition";
57+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
58+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
59+
shaderObject.timeUniform = "time";
60+
61+
return shaderObject;
62+
}
63+
64+
const String fs_precision = "precision mediump float;\n";
65+
66+
// this shader is build for use with an icosahedron
67+
ShaderObject getPerlinNoiseColorShader([bool blackVariant]) {
68+
ShaderObject shaderObject = new ShaderObject();
69+
70+
shaderObject.vertexShader = fs_precision + perlinNoisefunctions + """
71+
attribute vec3 aVertexPosition;
72+
73+
uniform mat4 uMVMatrix;
74+
uniform mat4 uPMatrix;
75+
76+
uniform float time;
77+
varying vec3 vNormal;
78+
79+
void main() {
80+
vec3 normal = normalize( aVertexPosition);
81+
float f = 0.5 * pnoise( normal + time/3.0, vec3( 10.0 ) );
82+
//vNormal = aVertexPosition + f * normal;
83+
//vNormal = f*normal;
84+
vNormal = normal;
85+
gl_Position = uPMatrix * uMVMatrix * vec4( aVertexPosition + f * normal, 1.0 );
86+
}
87+
""";
88+
89+
shaderObject.fragmentShader = fs_precision + perlinNoisefunctions+
90+
91+
(blackVariant==true ? "#define BLACK 1\n" : "")
92+
93+
+ """
94+
95+
varying vec3 vNormal;
96+
uniform mat4 uTMatrix;
97+
98+
#define VARIANT 1
99+
100+
uniform float time;
101+
void main() {
102+
#if VARIANT == 1
103+
float mytime = time/2.0;
104+
#ifdef BLACK
105+
mytime = time/10.0;
106+
#endif
107+
float period = 10.0;
108+
float factor = 1.0; // sin( time)/4.0+2.0;
109+
vec3 translation = uTMatrix[3].xyz;
110+
float r = pnoise( .75 * ( vNormal*factor + mytime ), vec3( period ) );
111+
float g = pnoise( 0.8 * ( vNormal*factor + mytime ), vec3( period ) );
112+
float b = pnoise( 0.9 * ( vNormal*factor + mytime ), vec3( period ) );
113+
float n = pnoise( 1.5 * ( vNormal*factor + mytime ), vec3( period ) );
114+
#else
115+
float mytime = 0.0 ; //time/50.0;
116+
float period = 5.0;
117+
float factor = 1.0; // sin( time)/4.0+2.0;
118+
vec3 translation = uTMatrix[3].xyz;
119+
float r = pnoise( .75 * ( translation*0.175 + vNormal*factor + mytime ), vec3( period ) );
120+
float g = pnoise( 0.8 * ( translation*0.175 + vNormal*factor + mytime ), vec3( period ) );
121+
float b = pnoise( 0.9 * ( translation*0.175 + vNormal*factor + mytime ), vec3( period ) );
122+
float n = pnoise( 1.5 * ( translation*0.175 + vNormal*factor + mytime ), vec3( period ) );
123+
#endif
124+
125+
n = pow( .001, n );
126+
//float n = 10.0 * pnoise( 5.0 * ( vNormal + time ), vec3( 10.0 ) ) * pnoise( .5 * ( vNormal + time ), vec3( 10.0 ) );
127+
//n += .5 * pnoise( 4.0 * vNormal, vec3( 10.0 ) );
128+
#ifdef BLACK
129+
vec3 color = vec3( 1.0-(r + n), 1.0-(g + n), 1.0-(b + n) );
130+
#else
131+
vec3 color = vec3( (r + n), (g + n), (b + n) );
132+
#endif
133+
gl_FragColor = vec4( color, 1.0 );
134+
}
135+
""";
136+
137+
shaderObject.vertexPositionAttribute = "aVertexPosition";
138+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
139+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
140+
shaderObject.transformationMatrixUniform = "uTMatrix";
141+
shaderObject.timeUniform = "time";
142+
143+
return shaderObject;
144+
}

lib/src/utils.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,12 @@ class Utils
195195

196196
}
197197

198-
Future<Object> loadBinaryFile(String url)
198+
Future<Object> loadFile(String url, [bool binary=false])
199199
{
200200
Completer c = new Completer();
201201
HTML.HttpRequest hr = new HTML.HttpRequest();
202-
hr.responseType = "arraybuffer";
202+
if( binary)
203+
hr.responseType = "arraybuffer";
203204
hr.open("GET", url);
204205
hr.onLoadEnd.listen( (e) {
205206
c.complete(hr.response);
@@ -208,6 +209,11 @@ class Utils
208209
return c.future;
209210
}
210211

212+
Future<Object> loadBinaryFile(String url)
213+
{
214+
return loadFile( url, true);
215+
}
216+
211217
Future<Object> loadJsonFile(String url)
212218
{
213219
Completer c = new Completer();

0 commit comments

Comments
 (0)