PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/webgl_geometry_shapes.html

https://github.com/wttsang/three.js
HTML | 477 lines | 339 code | 138 blank | 0 comment | 0 complexity | 1276e1aa7f7fbeba295d71156f1f2be9 MD5 | raw file
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry - shapes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  7. <style type="text/css">
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <canvas id="debug" style="position:absolute; left:100px"></canvas>
  18. <script type="text/javascript" src="../build/Three.js"></script>
  19. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  20. <script type="text/javascript" src="js/Stats.js"></script>
  21. <script type="text/javascript">
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var text, plane;
  25. var targetRotation = 0;
  26. var targetRotationOnMouseDown = 0;
  27. var mouseX = 0;
  28. var mouseXOnMouseDown = 0;
  29. var windowHalfX = window.innerWidth / 2;
  30. var windowHalfY = window.innerHeight / 2;
  31. init();
  32. animate();
  33. function init() {
  34. container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. var info = document.createElement( 'div' );
  37. info.style.position = 'absolute';
  38. info.style.top = '10px';
  39. info.style.width = '100%';
  40. info.style.textAlign = 'center';
  41. info.innerHTML = 'Simple procedurally generated 3D shapes example by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Drag to spin';
  42. container.appendChild( info );
  43. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.y = 150;
  45. camera.position.z = 500;
  46. camera.target.position.y = 150;
  47. scene = new THREE.Scene();
  48. var light = new THREE.DirectionalLight( 0xffffff );
  49. light.position.set( 0, 0, 1 );
  50. light.position.normalize();
  51. scene.addLight( light );
  52. parent = new THREE.Object3D();
  53. parent.position.y = 50;
  54. scene.addChild( parent );
  55. function addGeometry( geometry, points, spacedPoints, color, x, y, z, rx, ry, rz, s ) {
  56. // 3d shape
  57. var mesh = new THREE.Mesh( geometry, [ new THREE.MeshLambertMaterial( { color: color } ), new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true } ) ] );
  58. mesh.position.set( x, y, z - 75 );
  59. mesh.rotation.set( rx, ry, rz );
  60. mesh.scale.set( s, s, s );
  61. parent.addChild( mesh );
  62. // solid line
  63. var line = new THREE.Line( points, new THREE.LineBasicMaterial( { color: color, linewidth: 2 } ) );
  64. line.position.set( x, y, z + 25 );
  65. line.rotation.set( rx, ry, rz );
  66. line.scale.set( s, s, s );
  67. parent.addChild( line );
  68. // transparent line from real points
  69. var line = new THREE.Line( points, new THREE.LineBasicMaterial( { color: color, opacity: 0.5 } ) );
  70. line.position.set( x, y, z + 75 );
  71. line.rotation.set( rx, ry, rz );
  72. line.scale.set( s, s, s );
  73. parent.addChild( line );
  74. // vertices from real points
  75. var pgeo = THREE.GeometryUtils.clone( points );
  76. var particles = new THREE.ParticleSystem( pgeo, new THREE.ParticleBasicMaterial( { color: color, size: 2, opacity: 0.75 } ) );
  77. particles.position.set( x, y, z + 75 );
  78. particles.rotation.set( rx, ry, rz );
  79. particles.scale.set( s, s, s );
  80. parent.addChild( particles );
  81. // transparent line from equidistance sampled points
  82. var line = new THREE.Line( spacedPoints, new THREE.LineBasicMaterial( { color: color, opacity: 0.2 } ) );
  83. line.position.set( x, y, z + 100 );
  84. line.rotation.set( rx, ry, rz );
  85. line.scale.set( s, s, s );
  86. parent.addChild( line );
  87. // equidistance sampled points
  88. var pgeo = THREE.GeometryUtils.clone( spacedPoints );
  89. var particles2 = new THREE.ParticleSystem( pgeo, new THREE.ParticleBasicMaterial( { color: color, size: 2, opacity: 0.5 } ) );
  90. particles2.position.set( x, y, z + 100 );
  91. particles2.rotation.set( rx, ry, rz );
  92. particles2.scale.set( s, s, s );
  93. parent.addChild( particles2 );
  94. }
  95. var extrudeSettings = { amount: 20, bevelEnabled: true, bevelSegments: 2, steps: 2 }; // bevelSegments: 2, steps: 2 , bevelSegments: 5, bevelSize: 8, bevelThickness:5,
  96. // California
  97. var californiaPts = [];
  98. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  99. californiaPts.push( new THREE.Vector2 ( 450, 300 ) );
  100. californiaPts.push( new THREE.Vector2 ( 392, 392 ) );
  101. californiaPts.push( new THREE.Vector2 ( 266, 438 ) );
  102. californiaPts.push( new THREE.Vector2 ( 190, 570 ) );
  103. californiaPts.push( new THREE.Vector2 ( 190, 600 ) );
  104. californiaPts.push( new THREE.Vector2 ( 160, 620 ) );
  105. californiaPts.push( new THREE.Vector2 ( 160, 650 ) );
  106. californiaPts.push( new THREE.Vector2 ( 180, 640 ) );
  107. californiaPts.push( new THREE.Vector2 ( 165, 680 ) );
  108. californiaPts.push( new THREE.Vector2 ( 150, 670 ) );
  109. californiaPts.push( new THREE.Vector2 ( 90, 737 ) );
  110. californiaPts.push( new THREE.Vector2 ( 80, 795 ) );
  111. californiaPts.push( new THREE.Vector2 ( 50, 835 ) );
  112. californiaPts.push( new THREE.Vector2 ( 64, 870 ) );
  113. californiaPts.push( new THREE.Vector2 ( 60, 945 ) );
  114. californiaPts.push( new THREE.Vector2 ( 300, 945 ) );
  115. californiaPts.push( new THREE.Vector2 ( 300, 743 ) );
  116. californiaPts.push( new THREE.Vector2 ( 600, 473 ) );
  117. californiaPts.push( new THREE.Vector2 ( 626, 425 ) );
  118. californiaPts.push( new THREE.Vector2 ( 600, 370 ) );
  119. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  120. var californiaShape = new THREE.Shape( californiaPts );
  121. var california3d = new THREE.ExtrudeGeometry( californiaShape, { amount: 20 } );
  122. var californiaPoints = californiaShape.createPointsGeometry();
  123. var californiaSpacedPoints = californiaShape.createSpacedPointsGeometry( 100 );
  124. // Triangle
  125. var triangleShape = new THREE.Shape();
  126. triangleShape.moveTo( 80, 20 );
  127. triangleShape.lineTo( 40, 80 );
  128. triangleShape.lineTo( 120, 80 );
  129. triangleShape.lineTo( 80, 20 ); // close path
  130. var triangle3d = triangleShape.extrude( extrudeSettings );
  131. var trianglePoints = triangleShape.createPointsGeometry();
  132. var triangleSpacedPoints = triangleShape.createSpacedPointsGeometry();
  133. // Heart
  134. var x = 0, y = 0;
  135. var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
  136. heartShape.moveTo( x + 25, y + 25 );
  137. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  138. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35,x - 30,y + 35 );
  139. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  140. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  141. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  142. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  143. var heart3d = heartShape.extrude( extrudeSettings );
  144. var heartPoints = heartShape.createPointsGeometry();
  145. var heartSpacedPoints = heartShape.createSpacedPointsGeometry();
  146. //heartShape.debug( document.getElementById("debug") );
  147. // Square
  148. var sqLength = 80;
  149. var squareShape = new THREE.Shape();
  150. squareShape.moveTo( 0,0 );
  151. squareShape.lineTo( 0, sqLength );
  152. squareShape.lineTo( sqLength, sqLength );
  153. squareShape.lineTo( sqLength, 0 );
  154. squareShape.lineTo( 0, 0 );
  155. var square3d = squareShape.extrude( extrudeSettings );
  156. var squarePoints = squareShape.createPointsGeometry();
  157. var squareSpacedPoints = squareShape.createSpacedPointsGeometry();
  158. // Rectangle
  159. var rectLength = 120, rectWidth = 40;
  160. var rectShape = new THREE.Shape();
  161. rectShape.moveTo( 0,0 );
  162. rectShape.lineTo( 0, rectWidth );
  163. rectShape.lineTo( rectLength, rectWidth );
  164. rectShape.lineTo( rectLength, 0 );
  165. rectShape.lineTo( 0, 0 );
  166. var rect3d = rectShape.extrude( extrudeSettings );
  167. var rectPoints = rectShape.createPointsGeometry();
  168. var rectSpacedPoints = rectShape.createSpacedPointsGeometry();
  169. // Rounded rectangle
  170. var roundedRectShape = new THREE.Shape();
  171. roundedRect( roundedRectShape, 0, 0, 50, 50, 20 );
  172. var roundedRect3d = roundedRectShape.extrude( extrudeSettings );
  173. var roundedRectPoints = roundedRectShape.createPointsGeometry();
  174. var roundedRectSpacedPoints = roundedRectShape.createSpacedPointsGeometry();
  175. function roundedRect( ctx, x, y, width, height, radius ){
  176. ctx.moveTo( x, y + radius );
  177. ctx.lineTo( x, y + height - radius );
  178. ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
  179. ctx.lineTo( x + width - radius, y + height) ;
  180. ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
  181. ctx.lineTo( x + width, y + radius );
  182. ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
  183. ctx.lineTo( x + radius, y );
  184. ctx.quadraticCurveTo( x, y, x, y + radius );
  185. }
  186. // Circle
  187. var circleRadius = 40;
  188. var circleShape = new THREE.Shape();
  189. circleShape.moveTo( 0, circleRadius );
  190. circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
  191. circleShape.quadraticCurveTo( circleRadius, -circleRadius, 0, -circleRadius );
  192. circleShape.quadraticCurveTo( -circleRadius, -circleRadius, -circleRadius, 0 );
  193. circleShape.quadraticCurveTo( -circleRadius, circleRadius, 0, circleRadius );
  194. var circle3d = circleShape.extrude( extrudeSettings );
  195. var circlePoints = circleShape.createPointsGeometry();
  196. var circleSpacedPoints = circleShape.createSpacedPointsGeometry();
  197. // Fish
  198. x = y = 0;
  199. var fishShape = new THREE.Shape();
  200. fishShape.moveTo(x,y);
  201. fishShape.quadraticCurveTo(x + 50, y - 80, x + 90, y - 10);
  202. fishShape.quadraticCurveTo(x + 100, y - 10, x + 115, y - 40);
  203. fishShape.quadraticCurveTo(x + 115, y, x + 115, y + 40);
  204. fishShape.quadraticCurveTo(x + 100, y + 10, x + 90, y + 10);
  205. fishShape.quadraticCurveTo(x + 50, y + 80, x, y);
  206. var fish3d = fishShape.extrude( extrudeSettings );
  207. var fishPoints = fishShape.createPointsGeometry();
  208. var fishSpacedPoints = fishShape.createSpacedPointsGeometry();
  209. // Arc circle
  210. var arcShape = new THREE.Shape();
  211. arcShape.moveTo( 0, 0 );
  212. arcShape.arc( 10, 10, 40, 0, Math.PI*2, false );
  213. var holePath = new THREE.Path();
  214. holePath.moveTo( 0, 0 );
  215. holePath.arc( 10, 10, 10, 0, Math.PI*2, true );
  216. arcShape.holes.push( holePath );
  217. var arc3d = arcShape.extrude( extrudeSettings );
  218. var arcPoints = arcShape.createPointsGeometry();
  219. var arcSpacedPoints = arcShape.createSpacedPointsGeometry();
  220. // Smiley
  221. var smileyShape = new THREE.Shape();
  222. smileyShape.moveTo( 0, 0 );
  223. smileyShape.arc( 40, 40, 40, 0, Math.PI*2, false );
  224. var smileyEye1Path = new THREE.Path();
  225. smileyEye1Path.moveTo( 0, 0 );
  226. smileyEye1Path.arc( 25, 20, 10, 0, Math.PI*2, true );
  227. smileyShape.holes.push( smileyEye1Path );
  228. var smileyEye2Path = new THREE.Path();
  229. smileyEye2Path.moveTo( 0, 0 );
  230. smileyEye2Path.arc( 55, 20, 10, 0, Math.PI*2, true );
  231. smileyShape.holes.push( smileyEye2Path );
  232. var smileyMouthPath = new THREE.Path();
  233. // ugly box mouth
  234. // smileyMouthPath.moveTo( 20, 40 );
  235. // smileyMouthPath.lineTo( 60, 40 );
  236. // smileyMouthPath.lineTo( 60, 60 );
  237. // smileyMouthPath.lineTo( 20, 60 );
  238. // smileyMouthPath.lineTo( 20, 40 );
  239. smileyMouthPath.moveTo( 20, 40 );
  240. smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
  241. smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
  242. smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
  243. smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
  244. smileyShape.holes.push( smileyMouthPath );
  245. var smiley3d = smileyShape.extrude( extrudeSettings );
  246. var smileyPoints = smileyShape.createPointsGeometry();
  247. var smileySpacedPoints = smileyShape.createSpacedPointsGeometry();
  248. // Spline shape + path extrusion
  249. var splinepts = [];
  250. splinepts.push( new THREE.Vector2 ( 350, 100 ) );
  251. splinepts.push( new THREE.Vector2 ( 400, 450 ) );
  252. splinepts.push( new THREE.Vector2 ( -140, 350 ) );
  253. splinepts.push( new THREE.Vector2 ( 0, 0 ) );
  254. var splineShape = new THREE.Shape( );
  255. splineShape.moveTo( 0, 0 );
  256. splineShape.splineThru( splinepts );
  257. //splineShape.debug( document.getElementById("debug") );
  258. var extrudePath = new THREE.Path();
  259. extrudePath.moveTo( 0, 0 );
  260. extrudePath.lineTo( 10, 10 );
  261. extrudePath.quadraticCurveTo( 80, 60, 160, 10 );
  262. extrudePath.quadraticCurveTo( 240, -40, 320, 10 );
  263. extrudeSettings.extrudePath = extrudePath;
  264. extrudeSettings.bevelEnabled = false;
  265. var splineShape3d = splineShape.extrude( extrudeSettings );
  266. var splinePoints = splineShape.createPointsGeometry( );
  267. var splineSpacedPoints = splineShape.createSpacedPointsGeometry( );
  268. addGeometry( california3d, californiaPoints, californiaSpacedPoints, 0xffaa00, -300, -100, 0, 0, 0, 0, 0.25 );
  269. addGeometry( triangle3d, trianglePoints, triangleSpacedPoints, 0xffee00, -180, 0, 0, 0, 0, 0, 1 );
  270. addGeometry( roundedRect3d, roundedRectPoints, roundedRectSpacedPoints, 0x005500, -150, 150, 0, 0, 0, 0, 1 );
  271. addGeometry( square3d, squarePoints, squareSpacedPoints, 0x0055ff, 150, 100, 0, 0, 0, 0, 1 );
  272. addGeometry( heart3d, heartPoints, heartSpacedPoints, 0xff1100, 0, 100, 0, 3.14, 0, 0, 1 );
  273. addGeometry( circle3d, circlePoints, circleSpacedPoints, 0x00ff11, 120, 250, 0, 0, 0, 0, 1 );
  274. addGeometry( fish3d, fishPoints, fishSpacedPoints, 0x222222, -60, 200, 0, 0, 0, 0, 1 );
  275. addGeometry( splineShape3d, splinePoints, splineSpacedPoints, 0x888888, -50, -100, -50, 0, 0, 0, 0.2 );
  276. addGeometry( arc3d, arcPoints, arcSpacedPoints, 0xbb4422, 150, 0, 0, 0, 0, 0, 1 );
  277. addGeometry( smiley3d, smileyPoints, smileySpacedPoints, 0xee00ff, -270, 250, 0, Math.PI, 0, 0, 1 );
  278. //
  279. renderer = new THREE.WebGLRenderer( { antialias: true } );
  280. renderer.setSize( window.innerWidth, window.innerHeight );
  281. container.appendChild( renderer.domElement );
  282. stats = new Stats();
  283. stats.domElement.style.position = 'absolute';
  284. stats.domElement.style.top = '0px';
  285. container.appendChild( stats.domElement );
  286. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  287. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  288. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  289. }
  290. //
  291. function onDocumentMouseDown( event ) {
  292. event.preventDefault();
  293. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  294. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  295. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  296. mouseXOnMouseDown = event.clientX - windowHalfX;
  297. targetRotationOnMouseDown = targetRotation;
  298. }
  299. function onDocumentMouseMove( event ) {
  300. mouseX = event.clientX - windowHalfX;
  301. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  302. }
  303. function onDocumentMouseUp( event ) {
  304. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  305. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  306. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  307. }
  308. function onDocumentMouseOut( event ) {
  309. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  310. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  311. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  312. }
  313. function onDocumentTouchStart( event ) {
  314. if ( event.touches.length == 1 ) {
  315. event.preventDefault();
  316. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  317. targetRotationOnMouseDown = targetRotation;
  318. }
  319. }
  320. function onDocumentTouchMove( event ) {
  321. if ( event.touches.length == 1 ) {
  322. event.preventDefault();
  323. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  324. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  325. }
  326. }
  327. //
  328. function animate() {
  329. requestAnimationFrame( animate );
  330. render();
  331. stats.update();
  332. }
  333. function render() {
  334. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  335. renderer.render( scene, camera );
  336. }
  337. </script>
  338. </body>
  339. </html>