Skip to content

Commit 61d8cb4

Browse files
committed
Examples: Improved webgl_framebuffer_texture.
1 parent 4e6436b commit 61d8cb4

File tree

2 files changed

+133
-14
lines changed

2 files changed

+133
-14
lines changed
44.8 KB
Loading

examples/webgl_framebuffer_texture.html

Lines changed: 133 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@
4242
import { OrbitControls } from './jsm/controls/OrbitControls.js';
4343

4444
let camera, scene, renderer;
45-
let mesh, sprite, texture;
45+
let line, sprite, texture;
4646

4747
let cameraOrtho, sceneOrtho;
4848

49+
let offset = 0;
50+
4951
const dpr = window.devicePixelRatio;
5052

5153
const textureSize = 128 * dpr;
5254
const vector = new THREE.Vector2();
55+
const color = new THREE.Color();
5356

5457
init();
5558
animate();
@@ -68,24 +71,26 @@
6871
cameraOrtho.position.z = 10;
6972

7073
scene = new THREE.Scene();
71-
scene.background = new THREE.Color( 0x20252f );
7274
sceneOrtho = new THREE.Scene();
7375

7476
//
7577

76-
const geometry = new THREE.TorusKnotBufferGeometry( 3, 1, 256, 32 );
77-
const material = new THREE.MeshStandardMaterial( { color: 0x6083c2 } );
78+
const points = generatePoints();
7879

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();
8184

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 );
8588

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 );
8994

9095
//
9196

@@ -160,8 +165,10 @@
160165

161166
requestAnimationFrame( animate );
162167

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
165172

166173
renderer.clear();
167174
renderer.render( scene, camera );
@@ -178,6 +185,118 @@
178185

179186
}
180187

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+
181300
</script>
182301

183302
</body>

0 commit comments

Comments
 (0)