Changeset 1830 for trunk/tutorial
 Timestamp:
 Aug 28, 2012, 8:58:57 AM (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/tutorial/03_noise.lolfx
r1827 r1830 16 16 uniform float u_Time; 17 17 18 vec 3 noise2d(vec2 p)18 vec4 noise3d(vec3 p1, vec3 p2, vec3 p3, vec3 p4) 19 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));20 vec3 q = vec3(12.3453, 54.3353432, 34.3490432); 21 vec4 s = vec4(dot(p1, q), dot(p2, q), dot(p3, q), dot(p4, q)); 22 return fract(sin(54.343909 + s) * 8433.235443); 23 23 } 24 24 25 vec3 noise3d(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 32 vec3 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 = noise2d(a); 39 vec3 n2 = noise2d(a + vec2(1.0, 0.0)); 40 vec3 n3 = noise2d(a + vec2(0.0, 1.0)); 41 vec3 n4 = noise2d(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 51 vec3 perlin3d(vec3 p) 25 float perlin3d(vec3 p) 52 26 { 53 27 mat3 r = mat3(0.36, 0.48, 0.8, 0.8, 0.60, 0.0, 0.48, 0.64, 0.60); … … 58 32 d = d * d * (3.0  2.0 * d); 59 33 60 vec 3 o1 = noise3d(a);61 vec3 o2 = noise3d(a + vec3(1.0, 0.0, 0.0));62 vec3 o3 = noise3d(a + vec3(0.0, 1.0, 0.0));63 vec3 o4 = noise3d(a + vec3(1.0, 1.0, 0.0));64 vec 3 o5 = noise3d(a + vec3(0.0, 0.0, 1.0));65 vec3 o6 = noise3d(a + vec3(1.0, 0.0, 1.0));66 vec3 o7 = noise3d(a + vec3(0.0, 1.0, 1.0));67 vec3 o8 = noise3d(a + vec3(1.0, 1.0, 1.0));34 vec4 o1 = noise3d(a, 35 a + vec3(0.0, 1.0, 0.0), 36 a + vec3(0.0, 0.0, 1.0), 37 a + vec3(0.0, 1.0, 1.0)); 38 vec4 o2 = noise3d(a + vec3(1.0, 0.0, 0.0), 39 a + vec3(1.0, 1.0, 0.0), 40 a + vec3(1.0, 0.0, 1.0), 41 a + vec3(1.0, 1.0, 1.0)); 68 42 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); 43 vec4 o3 = o2 * d.x + o1 * (1.0  d.x); 44 vec2 o4 = o3.yw * d.y + o3.xz * (1.0  d.y); 73 45 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; 46 return o4.y * d.z + o4.x * (1.0  d.z); 80 47 } 81 48 … … 86 53 float z = u_Time * 2.0; 87 54 88 vec3 p = perlin3d(vec3(xy / 160.0, z)) / 2.0 89 + perlin3d(vec3(xy / 80.0, z + 1.5)) / 4.0 90 + perlin3d(vec3(xy / 40.0, z + 0.3)) / 8.0 91 + perlin3d(vec3(xy / 20.0, z + 3.3)) / 16.0 92 + perlin3d(vec3(xy / 10.0, z + 2.0)) / 32.0 93 + perlin3d(vec3(xy / 5.0, z + 1.7)) / 64.0 94 + perlin3d(vec3(xy / 2.5, z + 4.2)) / 128.0; 55 float p = perlin3d(vec3(xy / 80.0, z + 1.5)) / 2.0 56 + perlin3d(vec3(xy / 40.0, z + 0.3)) / 4.0 57 + perlin3d(vec3(xy / 20.0, z + 3.3)) / 8.0 58 + perlin3d(vec3(xy / 10.0, z + 2.0)) / 16.0; 95 59 96 60 /* Scroll by adding [.5,.5] */ … … 99 63 p = min(p, 1.0); 100 64 101 p = p * p * (3.0  2.0 * p); 102 p = p * p * (3.0  2.0 * p); 103 gl_FragColor = vec4(min(p.x * 2.0, 1.0), max(p.x * 1.5  0.5, 0.0), 0.0, 1.0); 65 float q = p * p * (3.0  2.0 * p); 66 float r = q * q * (3.0  2.0 * q); 67 gl_FragColor = vec4(min(q * 2.0, 1.0), 68 max(r * 1.5  0.5, 0.0), 69 max(q * 8.0  7.3, 0.0), 70 1.0); 104 71 } 105 72 … … 118 85  HLSL.Frag  119 86 120 float 3 noise2d(float2 p)87 float4 noise3d(float3 p1, float3 p2, float3 p3, float3 p4) 121 88 { 122 return float3(frac(sin(dot(p, float2(12.3453, 54.3353432))) * 8433.235443),123 frac(sin(dot(p, float2(34.23232, 98.03537))) * 8294.5345433),124 frac(sin(dot(p, float2(82.91234, 19.243533))) * 2912.454369));89 float3 q = float3(12.3453, 54.3353432, 34.3490432); 90 float4 s = float4(dot(p1, q), dot(p2, q), dot(p3, q), dot(p4, q)); 91 return frac(sin(54.343909 + s) * 8433.235443); 125 92 } 126 93 127 float3 noise3d(float3 p) 128 { 129 return float3(frac(sin(54.343909 + dot(p, float3(12.3453, 54.3353432, 34.3490432))) * 8433.235443), 130 frac(sin(93.329945  dot(p, float3(34.23232, 98.0353743, 90.2390984))) * 8294.5345433), 131 frac(sin(32.039284 + dot(p, float3(82.91234, 19.243533, 22.3294090))) * 2912.454369)); 132 } 133 134 float3 perlin2d(float2 p) 135 { 136 float2 a = floor(p); 137 float2 d = p  a; 138 d = d * d * (3.0  2.0 * d); 139 140 float3 n1 = noise2d(a); 141 float3 n2 = noise2d(a + float2(1.0, 0.0)); 142 float3 n3 = noise2d(a + float2(0.0, 1.0)); 143 float3 n4 = noise2d(a + float2(1.0, 1.0)); 144 145 float3 q1 = n2 * d.x + n1 * (1.0  d.x); 146 float3 q2 = n4 * d.x + n3 * (1.0  d.x); 147 148 float3 q3 = q2 * d.y + q1 * (1.0  d.y); 149 150 return q3; 151 } 152 153 float3 perlin3d(float3 p) 94 float perlin3d(float3 p) 154 95 { 155 96 float3x3 r = float3x3(0.36, 0.48, 0.8, 0.8, 0.60, 0.0, 0.48, 0.64, 0.60); … … 160 101 d = d * d * (3.0  2.0 * d); 161 102 162 float 3 o1 = noise3d(a);163 float3 o2 = noise3d(a + float3(1.0, 0.0, 0.0));164 float3 o3 = noise3d(a + float3(0.0, 1.0, 0.0));165 float3 o4 = noise3d(a + float3(1.0, 1.0, 0.0));166 float 3 o5 = noise3d(a + float3(0.0, 0.0, 1.0));167 float3 o6 = noise3d(a + float3(1.0, 0.0, 1.0));168 float3 o7 = noise3d(a + float3(0.0, 1.0, 1.0));169 float3 o8 = noise3d(a + float3(1.0, 1.0, 1.0));103 float4 o1 = noise3d(a, 104 a + float3(0.0, 1.0, 0.0), 105 a + float3(0.0, 0.0, 1.0), 106 a + float3(0.0, 1.0, 1.0)); 107 float4 o2 = noise3d(a + float3(1.0, 0.0, 0.0), 108 a + float3(1.0, 1.0, 0.0), 109 a + float3(1.0, 0.0, 1.0), 110 a + float3(1.0, 1.0, 1.0)); 170 111 171 float3 n1 = o2 * d.x + o1 * (1.0  d.x); 172 float3 n2 = o4 * d.x + o3 * (1.0  d.x); 173 float3 n3 = o6 * d.x + o5 * (1.0  d.x); 174 float3 n4 = o8 * d.x + o7 * (1.0  d.x); 112 float4 o3 = o2 * d.x + o1 * (1.0  d.x); 113 float2 o4 = o3.yw * d.y + o3.xz * (1.0  d.y); 175 114 176 float3 q1 = n2 * d.y + n1 * (1.0  d.y); 177 float3 q2 = n4 * d.y + n3 * (1.0  d.y); 178 179 float3 q3 = q2 * d.z + q1 * (1.0  d.z); 180 181 return q3; 115 return o4.y * d.z + o4.x * (1.0  d.z); 182 116 } 183 117 … … 190 124 float z = u_Time * 2.0; 191 125 192 float3 p = perlin3d(float3(xy / 160.0, z)) / 2.0 193 + perlin3d(float3(xy / 80.0, z + 1.5)) / 4.0 194 + perlin3d(float3(xy / 40.0, z + 0.3)) / 8.0 195 + perlin3d(float3(xy / 20.0, z + 3.3)) / 16.0 196 + perlin3d(float3(xy / 10.0, z + 2.0)) / 32.0 197 + perlin3d(float3(xy / 5.0, z + 1.7)) / 64.0 198 + perlin3d(float3(xy / 2.5, z + 4.2)) / 128.0; 126 float p = perlin3d(float3(xy / 80.0, z + 1.5)) / 2.0 127 + perlin3d(float3(xy / 40.0, z + 0.3)) / 4.0 128 + perlin3d(float3(xy / 20.0, z + 3.3)) / 8.0 129 + perlin3d(float3(xy / 10.0, z + 2.0)) / 16.0; 199 130 200 131 /* Scroll by adding [.5,.5] */ … … 203 134 p = min(p, 1.0); 204 135 205 p = p * p * (3.0  2.0 * p); 206 p = p * p * (3.0  2.0 * p); 207 out_FragColor = float4(min(p.x * 2.0, 1.0), max(p.x * 1.5  0.5, 0.0), 0.0, 1.0); 136 float q = p * p * (3.0  2.0 * p); 137 float r = q * q * (3.0  2.0 * q); 138 out_FragColor = float4(min(q * 2.0, 1.0), 139 max(r * 1.5  0.5, 0.0), 140 max(q * 8.0  7.3, 0.0), 141 1.0); 208 142 }
Note: See TracChangeset
for help on using the changeset viewer.