webgl_geometry_extrude.html 6.8 KB

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