Changeset 1852 for trunk/tutorial
 Timestamp:
 Sep 1, 2012, 11:56:06 AM (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/tutorial/03_noise.lolfx
r1842 r1852 3 3 #version 120 4 4 5 uniform float u_Time; 6 5 7 attribute vec2 in_Position; 6 8 9 varying vec2 pass_Position; 10 varying mat2 pass_Rotation; 11 varying vec3 water[4]; 12 varying vec3 fire[4]; 13 14 float mod289(float x) 15 { 16 return x  floor(x * (1.0 / 289.0)) * 289.0; 17 } 18 7 19 void main(void) 8 20 { 21 mat3 r = mat3(0.36, 0.48, 0.8, 0.8, 0.60, 0.0, 0.48, 0.64, 0.60); 22 vec3 p_pos = r * vec3(in_Position * vec2(16.0, 9.0), 0.0); 23 vec3 p_time = r * vec3(0.0, 0.0, u_Time * 2.0); 24 25 /* Noise sampling points for water */ 26 water[0] = p_pos / 2.0 + p_time; 27 water[1] = p_pos / 4.0 + p_time; 28 water[2] = p_pos / 8.0 + p_time; 29 water[3] = p_pos / 16.0 + p_time; 30 31 /* Noise sampling points for fire */ 32 p_pos = 16.0 * p_pos  r * vec3(0.0, mod289(u_Time) * 128.0, 0.0); 33 fire[0] = p_pos / 2.0 + p_time; 34 fire[1] = p_pos / 4.0 + p_time; 35 fire[2] = p_pos / 8.0 + p_time; 36 fire[3] = p_pos / 16.0 + p_time; 37 38 pass_Position = in_Position; 39 pass_Rotation = mat2(cos(u_Time), sin(u_Time), sin(u_Time), cos(u_Time)); 40 9 41 gl_Position = vec4(in_Position, 0.0, 1.0); 10 42 } … … 16 48 uniform float u_Time; 17 49 50 varying vec2 pass_Position; 51 varying mat2 pass_Rotation; 52 varying vec3 water[4]; 53 varying vec3 fire[4]; 54 18 55 vec4 mod289(vec4 x) 19 56 { … … 28 65 float noise3d(vec3 p) 29 66 { 30 mat3 r = mat3(0.36, 0.48, 0.8, 0.8, 0.60, 0.0, 0.48, 0.64, 0.60);31 p = r * p;32 33 67 vec3 a = floor(p); 34 68 vec3 d = p  a; … … 54 88 void main(void) 55 89 { 56 vec2 xy = gl_FragCoord.xy * 2.0; 57 xy.y = u_Time * 400.0; 58 float z = u_Time * 2.0; 59 60 float p = noise3d(vec3(xy / 80.0, z)) / 2.0 61 + noise3d(vec3(xy / 40.0, z)) / 4.0 62 + noise3d(vec3(xy / 20.0, z)) / 8.0 63 + noise3d(vec3(xy / 10.0, z)) / 16.0; 64 65 /* Scroll by adding [.5,.5] */ 66 p = gl_FragCoord.y / 720.0  0.5; 67 p = max(p, 0.0); 68 p = min(p, 1.0); 69 70 float q = p * p * (3.0  2.0 * p); 71 float r = q * q * (3.0  2.0 * q); 72 gl_FragColor = vec4(min(q * 2.0, 1.0), 73 max(r * 1.5  0.5, 0.0), 74 max(q * 8.0  7.3, 0.0), 75 1.0); 90 vec2 tmp = pass_Rotation * pass_Position; 91 float test = tmp.x * tmp.y + 0.75 * sin(3.0 * u_Time); 92 93 vec3 points[4] = (test > 0.0) ? fire : water; 94 95 vec4 n = vec4(noise3d(points[0]), 96 noise3d(points[1]), 97 noise3d(points[2]), 98 noise3d(points[3])); 99 100 vec4 color; 101 102 if (test > 0.0) 103 { 104 float p = dot(n, vec4(0.125, 0.125, 0.25, 0.5)); 105 106 /* Fade out by adding [.5,.5] */ 107 p = pass_Position.y * 0.8 + 0.25; 108 p = max(p, 0.0); 109 p = min(p, 1.0); 110 111 float q = p * p * (3.0  2.0 * p); 112 float r = q * q * (3.0  2.0 * q); 113 color = vec4(min(q * 2.0, 1.0), 114 max(r * 1.5  0.5, 0.0), 115 max(q * 8.0  7.3, 0.0), 116 1.0); 117 } 118 else 119 { 120 float p = dot(abs(2.0 * n  1.0), 121 vec4(0.5, 0.25, 0.125, 0.125)); 122 float q = sqrt(p); 123 124 color = vec4(1.0  q, 125 1.0  0.5 * q, 126 1.0, 127 1.0); 128 } 129 130 gl_FragColor = color; 76 131 } 77 132 … … 131 186 { 132 187 float2 xy = pass_Position; 133 xy.y = u_Time * 400.0;134 188 float z = u_Time * 2.0; 135 189 136 float p = noise3d(float3(xy / 80.0, z)) / 2.0 137 + noise3d(float3(xy / 40.0, z)) / 4.0 138 + noise3d(float3(xy / 20.0, z)) / 8.0 139 + noise3d(float3(xy / 10.0, z)) / 16.0; 140 141 /* Scroll by adding [.5,.5] */ 142 p = pass_Position.y / 1440.0; 143 p = max(p, 0.0); 144 p = min(p, 1.0); 145 146 float q = p * p * (3.0  2.0 * p); 147 float r = q * q * (3.0  2.0 * q); 148 out_FragColor = float4(min(q * 2.0, 1.0), 149 max(r * 1.5  0.5, 0.0), 150 max(q * 8.0  7.3, 0.0), 151 1.0); 152 } 190 float tx = pass_Position.x * cos(u_Time)  pass_Position.y * sin(u_Time); 191 float ty = pass_Position.x * sin(u_Time) + pass_Position.y * cos(u_Time); 192 193 if (tx * ty > 0.0) 194 xy.y = z * 200.0; 195 196 float p1 = noise3d(float3(xy / 80.0, z)); 197 float p2 = noise3d(float3(xy / 40.0, z)); 198 float p3 = noise3d(float3(xy / 20.0, z)); 199 float p4 = noise3d(float3(xy / 10.0, z)); 200 201 float4 color; 202 203 if (tx * ty > 0.0) 204 { 205 float p = 0.5 * p1 + 0.25 * p2 + 0.125 * (p3 + p4); 206 207 /* Scroll by adding [.5,.5] */ 208 p = pass_Position.y / 1440.0; 209 p = max(p, 0.0); 210 p = min(p, 1.0); 211 212 float q = p * p * (3.0  2.0 * p); 213 float r = q * q * (3.0  2.0 * q); 214 color = float4(min(q * 2.0, 1.0), 215 max(r * 1.5  0.5, 0.0), 216 max(q * 8.0  7.3, 0.0), 217 1.0); 218 } 219 else 220 { 221 float p = 0.5 * p1 + 0.25 * p2 + 0.125 * (p3 + p4); 222 223 float q = p * p * (3.0  2.0 * p); 224 float r = q * q * (3.0  2.0 * q); 225 color = float4(1.0  q, 226 1.0  0.5 * r, 227 1.0, 228 1.0); 229 } 230 231 out_FragColor = color; 232 }
Note: See TracChangeset
for help on using the changeset viewer.