# source:trunk/tutorial/03_noise.lolfx@1825Tweet

Last change on this file since 1825 was 1825, checked in by sam, 8 years ago

tutorial: the noise demo is now really, really, ridiculously good looking.

File size: 2.9 KB
Line
1-- GLSL.Vert --
2
3#version 120
4
5attribute vec2 in_Position;
6
7void main(void)
8{
9    gl_Position = vec4(in_Position, 0.0, 1.0);
10}
11
12-- GLSL.Frag --
13
14#version 120
15
16uniform float in_Time;
17
18vec3 noise3(vec2 p)
19{
20    return vec3(fract(sin(dot(p, vec2(12.3453, -54.3353432))) * 8433.235443),
21                fract(sin(dot(p, vec2(34.23232, 98.03537))) * 8294.5345433),
22                fract(sin(dot(p, vec2(82.91234, -19.243533))) * 2912.454369));
23}
24
25vec3 noise3(vec3 p)
26{
27    return vec3(fract(sin(54.343909 + dot(p, vec3(12.3453, -54.3353432, 34.3490432))) * 8433.235443),
28                fract(sin(93.329945 - dot(p, vec3(34.23232, 98.0353743, 90.2390984))) * 8294.5345433),
29                fract(sin(32.039284 + dot(p, vec3(82.91234, -19.243533, 22.3294090))) * 2912.454369));
30}
31
32vec3 perlin2d(vec2 p)
33{
34    vec2 a = floor(p);
35    vec2 d = p - a;
36    d = d * d * (3.0 - 2.0 * d);
37
38    vec3 n1 = noise3(a);
39    vec3 n2 = noise3(a + vec2(1.0, 0.0));
40    vec3 n3 = noise3(a + vec2(0.0, 1.0));
41    vec3 n4 = noise3(a + vec2(1.0, 1.0));
42
43    vec3 q1 = n2 * d.x + n1 * (1.0 - d.x);
44    vec3 q2 = n4 * d.x + n3 * (1.0 - d.x);
45
46    vec3 q3 = q2 * d.y + q1 * (1.0 - d.y);
47
48    return q3;
49}
50
51vec3 perlin3d(vec3 p)
52{
53    mat3 r = mat3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);
54    p = r * p;
55
56    vec3 a = floor(p);
57    vec3 d = p - a;
58    d = d * d * (3.0 - 2.0 * d);
59
60    vec3 o1 = noise3(a);
61    vec3 o2 = noise3(a + vec3(1.0, 0.0, 0.0));
62    vec3 o3 = noise3(a + vec3(0.0, 1.0, 0.0));
63    vec3 o4 = noise3(a + vec3(1.0, 1.0, 0.0));
64    vec3 o5 = noise3(a + vec3(0.0, 0.0, 1.0));
65    vec3 o6 = noise3(a + vec3(1.0, 0.0, 1.0));
66    vec3 o7 = noise3(a + vec3(0.0, 1.0, 1.0));
67    vec3 o8 = noise3(a + vec3(1.0, 1.0, 1.0));
68
69    vec3 n1 = o2 * d.x + o1 * (1.0 - d.x);
70    vec3 n2 = o4 * d.x + o3 * (1.0 - d.x);
71    vec3 n3 = o6 * d.x + o5 * (1.0 - d.x);
72    vec3 n4 = o8 * d.x + o7 * (1.0 - d.x);
73
74    vec3 q1 = n2 * d.y + n1 * (1.0 - d.y);
75    vec3 q2 = n4 * d.y + n3 * (1.0 - d.y);
76
77    vec3 q3 = q2 * d.z + q1 * (1.0 - d.z);
78
79    return q3;
80}
81
82void main(void)
83{
84    vec2 xy = gl_FragCoord.xy;
85    float z = in_Time * 2.0;
86
87    vec3 p = perlin3d(vec3(xy / 160.0, z)) / 2.0
88           + perlin3d(vec3(xy / 80.0, z + 1.5)) / 4.0
89           + perlin3d(vec3(xy / 40.0, z + 0.3)) / 8.0
90           + perlin3d(vec3(xy / 20.0, z + 3.3)) / 16.0
91           + perlin3d(vec3(xy / 10.0, z + 2.0)) / 32.0
92           + perlin3d(vec3(xy / 5.0, z + 1.7)) / 64.0
93           + perlin3d(vec3(xy / 2.5, z + 4.2)) / 128.0;
94    p = p * p * (3.0 - 2.0 * p);
95    p = p * p * (3.0 - 2.0 * p);
96    gl_FragColor = vec4(min(p.x * 2.0, 1.0), max(p.x * 1.5 - 0.5, 0.0), 0.0, 1.0);
97}
98
99-- HLSL.Vert --
100
101void main(float2 in_Position : POSITION,
102          out float4 out_Position : POSITION)
103{
104    out_Position = float4(in_Position, 0.0, 1.0);
105}
106
107-- HLSL.Frag --
108
109void main(out float4 out_FragColor : COLOR)
110{
111    out_FragColor = float4(0.7, 0.2, 0.5, 1.0);
112}
113
Note: See TracBrowser for help on using the repository browser.