4 <title>three.js webgl - geometry - shapes</title>
6 <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
9 font-family: Monospace;
10 background-color: #f0f0f0;
18 <script src="../build/three.js"></script>
19 <script src="js/libs/stats.min.js"></script>
25 var camera, scene, renderer;
29 var targetRotation = 0;
30 var targetRotationOnMouseDown = 0;
33 var mouseXOnMouseDown = 0;
35 var windowHalfX = window.innerWidth / 2;
42 container = document.createElement( 'div' );
43 document.body.appendChild( container );
45 var info = document.createElement( 'div' );
46 info.style.position = 'absolute';
47 info.style.top = '10px';
48 info.style.width = '100%';
49 info.style.textAlign = 'center';
50 info.innerHTML = 'Simple procedurally-generated shapes<br/>Drag to spin';
51 container.appendChild( info );
53 scene = new THREE.Scene();
54 scene.background = new THREE.Color( 0xf0f0f0 );
56 camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
57 camera.position.set( 0, 150, 500 );
60 var light = new THREE.PointLight( 0xffffff, 0.8 );
63 group = new THREE.Group();
64 group.position.y = 50;
67 var loader = new THREE.TextureLoader();
68 var texture = loader.load( "textures/UV_Grid_Sm.jpg" );
70 // it's necessary to apply these settings in order to correctly display the texture on a shape geometry
72 texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
73 texture.repeat.set( 0.008, 0.008 );
75 function addShape( shape, extrudeSettings, color, x, y, z, rx, ry, rz, s ) {
77 // flat shape with texture
78 // note: default UVs generated by ShapeBufferGeometry are simply the x- and y-coordinates of the vertices
80 var geometry = new THREE.ShapeBufferGeometry( shape );
82 var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { side: THREE.DoubleSide, map: texture } ) );
83 mesh.position.set( x, y, z - 175 );
84 mesh.rotation.set( rx, ry, rz );
85 mesh.scale.set( s, s, s );
90 var geometry = new THREE.ShapeBufferGeometry( shape );
92 var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: color, side: THREE.DoubleSide } ) );
93 mesh.position.set( x, y, z - 125 );
94 mesh.rotation.set( rx, ry, rz );
95 mesh.scale.set( s, s, s );
100 var geometry = new THREE.ExtrudeGeometry( shape, extrudeSettings );
102 var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: color } ) );
103 mesh.position.set( x, y, z - 75 );
104 mesh.rotation.set( rx, ry, rz );
105 mesh.scale.set( s, s, s );
108 addLineShape( shape, color, x, y, z, rx, ry, rz, s );
112 function addLineShape( shape, color, x, y, z, rx, ry, rz, s ) {
116 shape.autoClose = true;
117 var points = shape.createPointsGeometry();
118 var spacedPoints = shape.createSpacedPointsGeometry( 50 );
122 var line = new THREE.Line( points, new THREE.LineBasicMaterial( { color: color, linewidth: 3 } ) );
123 line.position.set( x, y, z - 25 );
124 line.rotation.set( rx, ry, rz );
125 line.scale.set( s, s, s );
128 // line from equidistance sampled points
130 var line = new THREE.Line( spacedPoints, new THREE.LineBasicMaterial( { color: color, linewidth: 3 } ) );
131 line.position.set( x, y, z + 25 );
132 line.rotation.set( rx, ry, rz );
133 line.scale.set( s, s, s );
136 // vertices from real points
138 var particles = new THREE.Points( points, new THREE.PointsMaterial( { color: color, size: 4 } ) );
139 particles.position.set( x, y, z + 75 );
140 particles.rotation.set( rx, ry, rz );
141 particles.scale.set( s, s, s );
142 group.add( particles );
144 // equidistance sampled points
146 var particles = new THREE.Points( spacedPoints, new THREE.PointsMaterial( { color: color, size: 4 } ) );
147 particles.position.set( x, y, z + 125 );
148 particles.rotation.set( rx, ry, rz );
149 particles.scale.set( s, s, s );
150 group.add( particles );
157 var californiaPts = [];
159 californiaPts.push( new THREE.Vector2( 610, 320 ) );
160 californiaPts.push( new THREE.Vector2( 450, 300 ) );
161 californiaPts.push( new THREE.Vector2( 392, 392 ) );
162 californiaPts.push( new THREE.Vector2( 266, 438 ) );
163 californiaPts.push( new THREE.Vector2( 190, 570 ) );
164 californiaPts.push( new THREE.Vector2( 190, 600 ) );
165 californiaPts.push( new THREE.Vector2( 160, 620 ) );
166 californiaPts.push( new THREE.Vector2( 160, 650 ) );
167 californiaPts.push( new THREE.Vector2( 180, 640 ) );
168 californiaPts.push( new THREE.Vector2( 165, 680 ) );
169 californiaPts.push( new THREE.Vector2( 150, 670 ) );
170 californiaPts.push( new THREE.Vector2( 90, 737 ) );
171 californiaPts.push( new THREE.Vector2( 80, 795 ) );
172 californiaPts.push( new THREE.Vector2( 50, 835 ) );
173 californiaPts.push( new THREE.Vector2( 64, 870 ) );
174 californiaPts.push( new THREE.Vector2( 60, 945 ) );
175 californiaPts.push( new THREE.Vector2( 300, 945 ) );
176 californiaPts.push( new THREE.Vector2( 300, 743 ) );
177 californiaPts.push( new THREE.Vector2( 600, 473 ) );
178 californiaPts.push( new THREE.Vector2( 626, 425 ) );
179 californiaPts.push( new THREE.Vector2( 600, 370 ) );
180 californiaPts.push( new THREE.Vector2( 610, 320 ) );
182 for( var i = 0; i < californiaPts.length; i ++ ) californiaPts[ i ].multiplyScalar( 0.25 );
184 var californiaShape = new THREE.Shape( californiaPts );
189 var triangleShape = new THREE.Shape();
190 triangleShape.moveTo( 80, 20 );
191 triangleShape.lineTo( 40, 80 );
192 triangleShape.lineTo( 120, 80 );
193 triangleShape.lineTo( 80, 20 ); // close path
200 var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
202 heartShape.moveTo( x + 25, y + 25 );
203 heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
204 heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35, x - 30, y + 35 );
205 heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
206 heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
207 heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
208 heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
215 var squareShape = new THREE.Shape();
216 squareShape.moveTo( 0, 0 );
217 squareShape.lineTo( 0, sqLength );
218 squareShape.lineTo( sqLength, sqLength );
219 squareShape.lineTo( sqLength, 0 );
220 squareShape.lineTo( 0, 0 );
225 var rectLength = 120, rectWidth = 40;
227 var rectShape = new THREE.Shape();
228 rectShape.moveTo( 0, 0 );
229 rectShape.lineTo( 0, rectWidth );
230 rectShape.lineTo( rectLength, rectWidth );
231 rectShape.lineTo( rectLength, 0 );
232 rectShape.lineTo( 0, 0 );
237 var roundedRectShape = new THREE.Shape();
239 ( function roundedRect( ctx, x, y, width, height, radius ) {
241 ctx.moveTo( x, y + radius );
242 ctx.lineTo( x, y + height - radius );
243 ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
244 ctx.lineTo( x + width - radius, y + height );
245 ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
246 ctx.lineTo( x + width, y + radius );
247 ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
248 ctx.lineTo( x + radius, y );
249 ctx.quadraticCurveTo( x, y, x, y + radius );
251 } )( roundedRectShape, 0, 0, 50, 50, 20 );
256 var trackShape = new THREE.Shape();
258 trackShape.moveTo( 40, 40 );
259 trackShape.lineTo( 40, 160 );
260 trackShape.absarc( 60, 160, 20, Math.PI, 0, true );
261 trackShape.lineTo( 80, 40 );
262 trackShape.absarc( 60, 40, 20, 2 * Math.PI, Math.PI, true );
267 var circleRadius = 40;
268 var circleShape = new THREE.Shape();
269 circleShape.moveTo( 0, circleRadius );
270 circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
271 circleShape.quadraticCurveTo( circleRadius, - circleRadius, 0, - circleRadius );
272 circleShape.quadraticCurveTo( - circleRadius, - circleRadius, - circleRadius, 0 );
273 circleShape.quadraticCurveTo( - circleRadius, circleRadius, 0, circleRadius );
280 var fishShape = new THREE.Shape();
282 fishShape.moveTo( x, y );
283 fishShape.quadraticCurveTo( x + 50, y - 80, x + 90, y - 10 );
284 fishShape.quadraticCurveTo( x + 100, y - 10, x + 115, y - 40 );
285 fishShape.quadraticCurveTo( x + 115, y, x + 115, y + 40 );
286 fishShape.quadraticCurveTo( x + 100, y + 10, x + 90, y + 10 );
287 fishShape.quadraticCurveTo( x + 50, y + 80, x, y );
292 var arcShape = new THREE.Shape();
293 arcShape.moveTo( 50, 10 );
294 arcShape.absarc( 10, 10, 40, 0, Math.PI * 2, false );
296 var holePath = new THREE.Path();
297 holePath.moveTo( 20, 10 );
298 holePath.absarc( 10, 10, 10, 0, Math.PI * 2, true );
299 arcShape.holes.push( holePath );
304 var smileyShape = new THREE.Shape();
305 smileyShape.moveTo( 80, 40 );
306 smileyShape.absarc( 40, 40, 40, 0, Math.PI * 2, false );
308 var smileyEye1Path = new THREE.Path();
309 smileyEye1Path.moveTo( 35, 20 );
310 smileyEye1Path.absellipse( 25, 20, 10, 10, 0, Math.PI * 2, true );
312 smileyShape.holes.push( smileyEye1Path );
314 var smileyEye2Path = new THREE.Path();
315 smileyEye2Path.moveTo( 65, 20 );
316 smileyEye2Path.absarc( 55, 20, 10, 0, Math.PI * 2, true );
317 smileyShape.holes.push( smileyEye2Path );
319 var smileyMouthPath = new THREE.Path();
320 smileyMouthPath.moveTo( 20, 40 );
321 smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
322 smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
323 smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
324 smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
326 smileyShape.holes.push( smileyMouthPath );
332 splinepts.push( new THREE.Vector2( 70, 20 ) );
333 splinepts.push( new THREE.Vector2( 80, 90 ) );
334 splinepts.push( new THREE.Vector2( - 30, 70 ) );
335 splinepts.push( new THREE.Vector2( 0, 0 ) );
337 var splineShape = new THREE.Shape();
338 splineShape.moveTo( 0, 0 );
339 splineShape.splineThru( splinepts );
341 var extrudeSettings = { amount: 8, bevelEnabled: true, bevelSegments: 2, steps: 2, bevelSize: 1, bevelThickness: 1 };
343 // addShape( shape, color, x, y, z, rx, ry,rz, s );
345 addShape( californiaShape, extrudeSettings, 0xf08000, -300, -100, 0, 0, 0, 0, 1 );
346 addShape( triangleShape, extrudeSettings, 0x8080f0, -180, 0, 0, 0, 0, 0, 1 );
347 addShape( roundedRectShape, extrudeSettings, 0x008000, -150, 150, 0, 0, 0, 0, 1 );
348 addShape( trackShape, extrudeSettings, 0x008080, 200, -100, 0, 0, 0, 0, 1 );
349 addShape( squareShape, extrudeSettings, 0x0040f0, 150, 100, 0, 0, 0, 0, 1 );
350 addShape( heartShape, extrudeSettings, 0xf00000, 60, 100, 0, 0, 0, Math.PI, 1 );
351 addShape( circleShape, extrudeSettings, 0x00f000, 120, 250, 0, 0, 0, 0, 1 );
352 addShape( fishShape, extrudeSettings, 0x404040, - 60, 200, 0, 0, 0, 0, 1 );
353 addShape( smileyShape, extrudeSettings, 0xf000f0, -200, 250, 0, 0, 0, Math.PI, 1 );
354 addShape( arcShape, extrudeSettings, 0x804000, 150, 0, 0, 0, 0, 0, 1 );
355 addShape( splineShape, extrudeSettings, 0x808080, - 50, -100, 0, 0, 0, 0, 1 );
357 addLineShape( arcShape.holes[ 0 ], 0x804000, 150, 0, 0, 0, 0, 0, 1 );
359 for ( var i = 0; i < smileyShape.holes.length; i += 1 ) {
361 addLineShape( smileyShape.holes[ i ], 0xf000f0, - 200, 250, 0, 0, 0, Math.PI, 1 );
367 renderer = new THREE.WebGLRenderer( { antialias: true } );
368 renderer.setPixelRatio( window.devicePixelRatio );
369 renderer.setSize( window.innerWidth, window.innerHeight );
370 container.appendChild( renderer.domElement );
373 container.appendChild( stats.dom );
375 document.addEventListener( 'mousedown', onDocumentMouseDown, false );
376 document.addEventListener( 'touchstart', onDocumentTouchStart, false );
377 document.addEventListener( 'touchmove', onDocumentTouchMove, false );
381 window.addEventListener( 'resize', onWindowResize, false );
385 function onWindowResize() {
387 camera.aspect = window.innerWidth / window.innerHeight;
388 camera.updateProjectionMatrix();
390 renderer.setSize( window.innerWidth, window.innerHeight );
396 function onDocumentMouseDown( event ) {
398 event.preventDefault();
400 document.addEventListener( 'mousemove', onDocumentMouseMove, false );
401 document.addEventListener( 'mouseup', onDocumentMouseUp, false );
402 document.addEventListener( 'mouseout', onDocumentMouseOut, false );
404 mouseXOnMouseDown = event.clientX - windowHalfX;
405 targetRotationOnMouseDown = targetRotation;
409 function onDocumentMouseMove( event ) {
411 mouseX = event.clientX - windowHalfX;
413 targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
417 function onDocumentMouseUp( event ) {
419 document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
420 document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
421 document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
425 function onDocumentMouseOut( event ) {
427 document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
428 document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
429 document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
433 function onDocumentTouchStart( event ) {
435 if ( event.touches.length == 1 ) {
437 event.preventDefault();
439 mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
440 targetRotationOnMouseDown = targetRotation;
446 function onDocumentTouchMove( event ) {
448 if ( event.touches.length == 1 ) {
450 event.preventDefault();
452 mouseX = event.touches[ 0 ].pageX - windowHalfX;
453 targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
463 requestAnimationFrame( animate );
472 group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
473 renderer.render( scene, camera );