|
42 | 42 | import { OrbitControls } from './jsm/controls/OrbitControls.js'; |
43 | 43 |
|
44 | 44 | let camera, scene, renderer; |
45 | | - let mesh, sprite, texture; |
| 45 | + let line, sprite, texture; |
46 | 46 |
|
47 | 47 | let cameraOrtho, sceneOrtho; |
48 | 48 |
|
| 49 | + let offset = 0; |
| 50 | + |
49 | 51 | const dpr = window.devicePixelRatio; |
50 | 52 |
|
51 | 53 | const textureSize = 128 * dpr; |
52 | 54 | const vector = new THREE.Vector2(); |
| 55 | + const color = new THREE.Color(); |
53 | 56 |
|
54 | 57 | init(); |
55 | 58 | animate(); |
|
68 | 71 | cameraOrtho.position.z = 10; |
69 | 72 |
|
70 | 73 | scene = new THREE.Scene(); |
71 | | - scene.background = new THREE.Color( 0x20252f ); |
72 | 74 | sceneOrtho = new THREE.Scene(); |
73 | 75 |
|
74 | 76 | // |
75 | 77 |
|
76 | | - const geometry = new THREE.TorusKnotBufferGeometry( 3, 1, 256, 32 ); |
77 | | - const material = new THREE.MeshStandardMaterial( { color: 0x6083c2 } ); |
| 78 | + const points = generatePoints(); |
78 | 79 |
|
79 | | - mesh = new THREE.Mesh( geometry, material ); |
80 | | - scene.add( mesh ); |
| 80 | + const geometry = new THREE.BufferGeometry(); |
| 81 | + const positionAttribute = new THREE.Float32BufferAttribute( points, 3 ); |
| 82 | + geometry.setAttribute( 'position', positionAttribute ); |
| 83 | + geometry.center(); |
81 | 84 |
|
82 | | - // |
83 | | - const ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 ); |
84 | | - scene.add( ambientLight ); |
| 85 | + const colorAttribute = new THREE.BufferAttribute( new Float32Array( positionAttribute.array.length ), 3 ); |
| 86 | + colorAttribute.setUsage( THREE.DynamicDrawUsage ); |
| 87 | + geometry.setAttribute( 'color', colorAttribute ); |
85 | 88 |
|
86 | | - const pointLight = new THREE.PointLight( 0xffffff, 0.8 ); |
87 | | - camera.add( pointLight ); |
88 | | - scene.add( camera ); |
| 89 | + const material = new THREE.LineBasicMaterial( { vertexColors: true } ); |
| 90 | + |
| 91 | + line = new THREE.Line( geometry, material ); |
| 92 | + line.scale.setScalar( 0.05 ); |
| 93 | + scene.add( line ); |
89 | 94 |
|
90 | 95 | // |
91 | 96 |
|
|
160 | 165 |
|
161 | 166 | requestAnimationFrame( animate ); |
162 | 167 |
|
163 | | - mesh.rotation.x += 0.005; |
164 | | - mesh.rotation.y += 0.01; |
| 168 | + const colorAttribute = line.geometry.getAttribute( 'color' ); |
| 169 | + updateColors( colorAttribute ); |
| 170 | + |
| 171 | + // scene rendering |
165 | 172 |
|
166 | 173 | renderer.clear(); |
167 | 174 | renderer.render( scene, camera ); |
|
178 | 185 |
|
179 | 186 | } |
180 | 187 |
|
| 188 | + function updateColors( colorAttribute ) { |
| 189 | + |
| 190 | + const l = colorAttribute.count; |
| 191 | + |
| 192 | + for ( let i = 0; i < l; i ++ ) { |
| 193 | + |
| 194 | + const h = ( ( offset + i ) % l ) / l; |
| 195 | + |
| 196 | + color.setHSL( h, 1, 0.5 ); |
| 197 | + colorAttribute.setX( i, color.r ); |
| 198 | + colorAttribute.setY( i, color.g ); |
| 199 | + colorAttribute.setZ( i, color.b ); |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | + colorAttribute.needsUpdate = true; |
| 204 | + |
| 205 | + offset -= 25; |
| 206 | + |
| 207 | + } |
| 208 | + |
| 209 | + // |
| 210 | + |
| 211 | + function generatePoints() { |
| 212 | + |
| 213 | + // generate gosper curve (from https://gist.github.com/nitaku/6521802) |
| 214 | + |
| 215 | + const gosper = fractalize( { |
| 216 | + axiom: 'A', |
| 217 | + steps: 4, |
| 218 | + rules: { |
| 219 | + A: 'A+BF++BF-FA--FAFA-BF+', |
| 220 | + B: '-FA+BFBF++BF+FA--FA-B' |
| 221 | + } |
| 222 | + } ); |
| 223 | + |
| 224 | + const points = toPoints( { |
| 225 | + fractal: gosper, |
| 226 | + side: 8, |
| 227 | + angle: Math.PI / 3 |
| 228 | + } ); |
| 229 | + |
| 230 | + return points; |
| 231 | + |
| 232 | + } |
| 233 | + |
| 234 | + function fractalize( config ) { |
| 235 | + |
| 236 | + let output; |
| 237 | + let input = config.axiom; |
| 238 | + |
| 239 | + for ( let i = 0, il = config.steps; 0 <= il ? i < il : i > il; 0 <= il ? i ++ : i -- ) { |
| 240 | + |
| 241 | + output = ''; |
| 242 | + |
| 243 | + for ( let j = 0, jl = input.length; j < jl; j ++ ) { |
| 244 | + |
| 245 | + const char = input[ j ]; |
| 246 | + |
| 247 | + if ( char in config.rules ) { |
| 248 | + |
| 249 | + output += config.rules[ char ]; |
| 250 | + |
| 251 | + } else { |
| 252 | + |
| 253 | + output += char; |
| 254 | + |
| 255 | + } |
| 256 | + |
| 257 | + } |
| 258 | + |
| 259 | + input = output; |
| 260 | + |
| 261 | + } |
| 262 | + |
| 263 | + return output; |
| 264 | + |
| 265 | + } |
| 266 | + |
| 267 | + function toPoints( config ) { |
| 268 | + |
| 269 | + let currX = 0, currY = 0; |
| 270 | + let angle = 0; |
| 271 | + const path = [ 0, 0, 0 ]; |
| 272 | + const fractal = config.fractal; |
| 273 | + |
| 274 | + for ( let i = 0, l = fractal.length; i < l; i ++ ) { |
| 275 | + |
| 276 | + const char = fractal[ i ]; |
| 277 | + |
| 278 | + if ( char === '+' ) { |
| 279 | + |
| 280 | + angle += config.angle; |
| 281 | + |
| 282 | + } else if ( char === '-' ) { |
| 283 | + |
| 284 | + angle -= config.angle; |
| 285 | + |
| 286 | + } else if ( char === 'F' ) { |
| 287 | + |
| 288 | + currX += config.side * Math.cos( angle ); |
| 289 | + currY += - config.side * Math.sin( angle ); |
| 290 | + path.push( currX, currY, 0 ); |
| 291 | + |
| 292 | + } |
| 293 | + |
| 294 | + } |
| 295 | + |
| 296 | + return path; |
| 297 | + |
| 298 | + } |
| 299 | + |
181 | 300 | </script> |
182 | 301 |
|
183 | 302 | </body> |
|
0 commit comments