canvas_geometry_subdivison.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry - cube</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. <script src="../build/Three.js"></script>
  18. <script src="js/RequestAnimationFrame.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script src="../src/core/Geometry.js"></script>
  21. <script src="../src/extras/geometries/CubeGeometry.js"></script>
  22. <script src="../src/extras/geometries/CylinderGeometry.js"></script>
  23. <script src="../src/extras/geometries/TorusGeometry.js"></script>
  24. <script src="../src/extras/geometries/SubdivisionGeometry.js"></script>
  25. <script src="fonts/helvetiker_regular.typeface.js"></script>
  26. <script>
  27. var container, stats;
  28. var camera, scene, renderer;
  29. var cube, plane;
  30. var targetRotation = 0;
  31. var targetRotationOnMouseDown = 0;
  32. var mouseX = 0;
  33. var mouseXOnMouseDown = 0;
  34. var windowHalfX = window.innerWidth / 2;
  35. var windowHalfY = window.innerHeight / 2;
  36. // Create subdivision geometry
  37. function createSubdivision(geometry, repeats) {
  38. repeats = (repeats === undefined ) ? 1 : repeats;
  39. var smooth = geometry;
  40. while (repeats--) {
  41. smooth = new THREE.SubdivisionGeometry(smooth);
  42. }
  43. return smooth;
  44. }
  45. init();
  46. animate();
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. var info = document.createElement( 'div' );
  51. info.style.position = 'absolute';
  52. info.style.top = '10px';
  53. info.style.width = '100%';
  54. info.style.textAlign = 'center';
  55. info.innerHTML = 'Drag to spin the cube';
  56. container.appendChild( info );
  57. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  58. camera.position.y = 150;
  59. camera.position.z = 500;
  60. camera.target.position.y = 150;
  61. scene = new THREE.Scene();
  62. // Cube
  63. var materials = [];
  64. for ( var i = 0; i < 6; i ++ ) {
  65. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
  66. }
  67. //geometry = new THREE.CubeGeometry( 200, 200, 200, 2, 2, 2, materials );
  68. geometry = new THREE.CubeGeometry( 200, 200, 200, 1, 1, 3, materials );
  69. //geometry = new THREE.TorusGeometry( 100, 60, 4, 8, Math.PI*2 );
  70. //geometry = new THREE.SphereGeometry( 200, 1, 1);
  71. //geometry = new THREE.CylinderGeometry( 50, 50, 200 );
  72. // geometry = new THREE.TextGeometry( '&', {
  73. // size: 200,
  74. // height: 50,
  75. // curveSegments: 1,
  76. // font: "helvetiker"
  77. //
  78. // });
  79. //PlaneGeometry not supported
  80. // quick fix for duplicated vertices
  81. // either mergeVertices or checkDupVertices(true) works
  82. geometry.mergeVertices();
  83. geometry.checkDupVertices( true );
  84. //console.log(geometry);
  85. smooth = createSubdivision(geometry, 2);
  86. var PI2 = Math.PI * 2;
  87. var program = function ( context ) {
  88. context.beginPath();
  89. context.arc( 0, 0, 1, 0, PI2, true );
  90. context.closePath();
  91. context.fill();
  92. }
  93. var drawText = function(i) {
  94. return function ( context ) {
  95. context.beginPath();
  96. context.scale(0.1,-0.1);
  97. context.fillText(i, 0,0);
  98. };
  99. }
  100. group = new THREE.Object3D();
  101. group.position.y = 150;
  102. scene.add( group );
  103. // Debug new Points
  104. // for ( var i = 0; i < smooth.vertices.length; i++ ) {
  105. //
  106. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  107. // particle.position = smooth.vertices[i].position;
  108. // var pos = smooth.vertices.position
  109. // particle.scale.x = particle.scale.y = 5;
  110. // group.add( particle );
  111. // }
  112. //Debug original points
  113. // for ( var i = 0; i < geometry.vertices.length; i++ ) {
  114. //
  115. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
  116. // particle.position = smooth.vertices[i].position;
  117. // var pos = geometry.vertices.position
  118. // particle.scale.x = particle.scale.y = 30;
  119. // group.add( particle );
  120. // }
  121. cube = new THREE.Mesh( smooth, new THREE.MeshBasicMaterial( { color: 0x405040, wireframe:true, opacity:0.8 } ) );
  122. //cube = new THREE.Mesh( smooth, new THREE.MeshFaceMaterial() );
  123. // material = new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
  124. // new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } );
  125. //
  126. cube.doubleSided = true;
  127. cube.position.y = 150;
  128. cube.overdraw = true;
  129. scene.add( cube );
  130. // Plane
  131. plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  132. plane.rotation.x = - 90 * ( Math.PI / 180 );
  133. plane.overdraw = true;
  134. scene.add( plane );
  135. renderer = new THREE.CanvasRenderer();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. container.appendChild( renderer.domElement );
  138. stats = new Stats();
  139. stats.domElement.style.position = 'absolute';
  140. stats.domElement.style.top = '0px';
  141. container.appendChild( stats.domElement );
  142. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  143. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  144. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  145. }
  146. //
  147. function onDocumentMouseDown( event ) {
  148. event.preventDefault();
  149. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  150. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  151. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  152. mouseXOnMouseDown = event.clientX - windowHalfX;
  153. targetRotationOnMouseDown = targetRotation;
  154. }
  155. function onDocumentMouseMove( event ) {
  156. mouseX = event.clientX - windowHalfX;
  157. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  158. }
  159. function onDocumentMouseUp( event ) {
  160. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  161. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  162. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  163. }
  164. function onDocumentMouseOut( event ) {
  165. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  166. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  167. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  168. }
  169. function onDocumentTouchStart( event ) {
  170. if ( event.touches.length == 1 ) {
  171. event.preventDefault();
  172. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  173. targetRotationOnMouseDown = targetRotation;
  174. }
  175. }
  176. function onDocumentTouchMove( event ) {
  177. if ( event.touches.length == 1 ) {
  178. event.preventDefault();
  179. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  180. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  181. }
  182. }
  183. //
  184. function animate() {
  185. requestAnimationFrame( animate );
  186. render();
  187. stats.update();
  188. }
  189. function render() {
  190. group.rotation.y = plane.rotation.z = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
  191. renderer.render( scene, camera );
  192. }
  193. </script>
  194. </body>
  195. </html>
粤ICP备19079148号