webgl_geometry_extrude.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - extrusion materials</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  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 src="../build/Three.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, scene, renderer;
  23. var text, plane;
  24. var targetRotation = 0;
  25. var targetRotationOnMouseDown = 0;
  26. var mouseX = 0;
  27. var mouseXOnMouseDown = 0;
  28. var windowHalfX = window.innerWidth / 2;
  29. var windowHalfY = window.innerHeight / 2;
  30. init();
  31. animate();
  32. function loadTexture( path ) {
  33. var image = new Image();
  34. image.onload = function () { texture.needsUpdate = true; };
  35. image.src = path;
  36. var texture = new THREE.Texture( image, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter );
  37. return new THREE.MeshPhongMaterial( { map: texture, color: 0x00aaff, shininess: 100 } );
  38. }
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. var texturedMaterial = loadTexture( 'textures/water.jpg' );
  43. var info = document.createElement( 'div' );
  44. info.style.position = 'absolute';
  45. info.style.top = '10px';
  46. info.style.width = '100%';
  47. info.style.textAlign = 'center';
  48. info.innerHTML = 'Procedurally generated 3D shapes with different materials on extrusions<br/>Drag to spin';
  49. container.appendChild( info );
  50. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1000 );
  51. camera.position.set( 0, 0, 300 );
  52. scene = new THREE.Scene();
  53. scene.add( camera );
  54. var light = new THREE.PointLight( 0xffffff );
  55. light.position.set( 0, 0, 100 );
  56. scene.add( light );
  57. parent = new THREE.Object3D();
  58. parent.position.y = 0;
  59. scene.add( parent );
  60. var extrudeSettings = { amount: 10, bevelEnabled: true, bevelSegments: 3, steps: 4, bevelThickness: 8, material: 0, extrudeMaterial: 1 };
  61. var colorMaterials = [ new THREE.MeshPhongMaterial( { color: 0xff5500, shininess: 100 } ), new THREE.MeshPhongMaterial( { color: 0xff0000, shininess: 100 } ) ];
  62. var imageAndColorMaterials = [ texturedMaterial, new THREE.MeshPhongMaterial( { color: 0x00aaff, shininess: 100 } ) ];
  63. var pillShape1 = new THREE.Shape();
  64. pillShape1.moveTo( 30, 80 );
  65. pillShape1.lineTo( 100, 80 );
  66. pillShape1.quadraticCurveTo( 130, 90, 100, 100 );
  67. pillShape1.lineTo( 30, 100 );
  68. pillShape1.quadraticCurveTo( 0, 90, 30, 80 );
  69. var pillGeometry1 = pillShape1.extrude( extrudeSettings );
  70. pillGeometry1.materials = colorMaterials;
  71. pillGeometry1.computeVertexNormals();
  72. THREE.GeometryUtils.center( pillGeometry1 );
  73. var pillMesh1 = new THREE.Mesh( pillGeometry1, new THREE.MeshFaceMaterial() );
  74. pillMesh1.position.y = -25;
  75. pillMesh1.scale.x = 0.5;
  76. parent.add ( pillMesh1 );
  77. var pillShape2 = new THREE.Shape();
  78. pillShape2.moveTo( 30, 80 );
  79. pillShape2.lineTo( 100, 80 );
  80. pillShape2.quadraticCurveTo( 130, 90, 100, 100 );
  81. pillShape2.lineTo( 30, 100 );
  82. pillShape2.quadraticCurveTo( 0, 90, 30, 80 );
  83. var pillGeometry2 = pillShape2.extrude( extrudeSettings );
  84. pillGeometry2.materials = imageAndColorMaterials;
  85. pillGeometry2.computeVertexNormals();
  86. THREE.GeometryUtils.center( pillGeometry2 );
  87. var pillMesh2 = new THREE.Mesh( pillGeometry2, new THREE.MeshFaceMaterial() );
  88. pillMesh2.position.y = 25;
  89. pillMesh2.scale.x = 0.5;
  90. parent.add ( pillMesh2 );
  91. renderer = new THREE.WebGLRenderer( { antialias: true } );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. container.appendChild( renderer.domElement );
  94. stats = new Stats();
  95. stats.domElement.style.position = 'absolute';
  96. stats.domElement.style.top = '0px';
  97. container.appendChild( stats.domElement );
  98. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  99. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  100. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  101. }
  102. //
  103. function onDocumentMouseDown( event ) {
  104. event.preventDefault();
  105. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  106. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  107. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  108. mouseXOnMouseDown = event.clientX - windowHalfX;
  109. targetRotationOnMouseDown = targetRotation;
  110. }
  111. function onDocumentMouseMove( event ) {
  112. mouseX = event.clientX - windowHalfX;
  113. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  114. }
  115. function onDocumentMouseUp( event ) {
  116. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  117. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  118. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  119. }
  120. function onDocumentMouseOut( event ) {
  121. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  122. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  123. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  124. }
  125. function onDocumentTouchStart( event ) {
  126. if ( event.touches.length == 1 ) {
  127. event.preventDefault();
  128. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  129. targetRotationOnMouseDown = targetRotation;
  130. }
  131. }
  132. function onDocumentTouchMove( event ) {
  133. if ( event.touches.length == 1 ) {
  134. event.preventDefault();
  135. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  136. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  137. }
  138. }
  139. //
  140. function animate() {
  141. requestAnimationFrame( animate );
  142. render();
  143. stats.update();
  144. }
  145. function render() {
  146. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  147. renderer.render( scene, camera );
  148. }
  149. </script>
  150. </body>
  151. </html>