canvas_geometry_subdivison.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. // Create new object by parameters
  46. var createSomething = function(klass, args) {
  47. var F = function(klass, args) {
  48. return klass.apply(this, args);
  49. }
  50. F.prototype = klass.prototype;
  51. return new F(klass, args);
  52. };
  53. init();
  54. animate();
  55. var info;
  56. function init() {
  57. container = document.createElement( 'div' );
  58. document.body.appendChild( container );
  59. info = document.createElement( 'div' );
  60. info.style.position = 'absolute';
  61. info.style.top = '10px';
  62. info.style.width = '100%';
  63. info.style.textAlign = 'center';
  64. info.innerHTML = 'Drag to spin the geometry ';
  65. container.appendChild( info );
  66. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  67. camera.position.y = 150;
  68. camera.position.z = 500;
  69. camera.target.position.y = 150;
  70. scene = new THREE.Scene();
  71. // Cube
  72. var materials = [];
  73. for ( var i = 0; i < 6; i ++ ) {
  74. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
  75. }
  76. var createGeometry = function(i, subdivisions) {
  77. var params = geometriesParams[i];
  78. info.innerHTML = 'Drag to spin the geometry THREE.' + params.type
  79. + ' with ' + subdivisions + ' subdivisions'; //+ params.args;
  80. geometry = createSomething(THREE[params.type], params.args);
  81. geometry.mergeVertices();
  82. smooth = createSubdivision(geometry, subdivisions);
  83. return smooth;
  84. };
  85. var geometriesParams = [
  86. {type: 'CubeGeometry', args: [ 200, 200, 200, 2, 2, 2, materials ] },
  87. {type: 'TorusGeometry', args: [ 100, 60, 4, 8, Math.PI*2 ] },
  88. {type: 'SphereGeometry', args: [ 200, 1, 1 ] },
  89. {type: 'CylinderGeometry', args: [ 25, 75, 200, 8, 3 ] },
  90. {type: 'TextGeometry', args: ['&', {
  91. size: 200,
  92. height: 50,
  93. curveSegments: 1,
  94. font: "helvetiker"
  95. }]},
  96. ];
  97. smooth = createGeometry(3, 2);
  98. //geometry = new THREE.CubeGeometry( 200, 200, 200, 2, 2, 2, materials );
  99. // make sure mergeVertices(); is run to fix quick duplicated vertices
  100. // geometry.mergeVertices();
  101. // smooth = createSubdivision(geometry, 2);
  102. group = new THREE.Object3D();
  103. group.position.y = 150;
  104. scene.add( group );
  105. // Debug new Points
  106. // var PI2 = Math.PI * 2;
  107. // var program = function ( context ) {
  108. //
  109. // context.beginPath();
  110. // context.arc( 0, 0, 1, 0, PI2, true );
  111. // context.closePath();
  112. // context.fill();
  113. //
  114. // }
  115. //
  116. // for ( var i = 0; i < smooth.vertices.length; i++ ) {
  117. //
  118. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  119. // particle.position = smooth.vertices[i].position;
  120. // var pos = smooth.vertices.position
  121. // particle.scale.x = particle.scale.y = 5;
  122. // group.add( particle );
  123. // }
  124. //Debug original points
  125. // var drawText = function(i) {
  126. //
  127. // return function ( context ) {
  128. //
  129. // context.beginPath();
  130. // context.scale(0.1,-0.1);
  131. //
  132. // context.fillText(i, 0,0);
  133. //
  134. // };
  135. //
  136. // }
  137. // for ( var i = 0; i < geometry.vertices.length; i++ ) {
  138. //
  139. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
  140. // particle.position = smooth.vertices[i].position;
  141. // var pos = geometry.vertices.position
  142. // particle.scale.x = particle.scale.y = 30;
  143. // group.add( particle );
  144. // }
  145. var meshmaterials = [
  146. new THREE.MeshBasicMaterial( { color: 0x405040, wireframe:true, opacity:0.8 } )
  147. ];
  148. // new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  149. // new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )
  150. // new THREE.MeshFaceMaterial()
  151. // new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
  152. // new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } );
  153. cube = new THREE.Mesh( smooth, meshmaterials );
  154. // cube.scale.x = 200;
  155. // cube.scale.y = 200;
  156. // cube.scale.z = 200;
  157. cube.doubleSided = true;
  158. cube.position.y = 150;
  159. cube.overdraw = true;
  160. scene.add( cube );
  161. // Plane
  162. plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  163. plane.rotation.x = - 90 * ( Math.PI / 180 );
  164. plane.overdraw = true;
  165. scene.add( plane );
  166. renderer = new THREE.CanvasRenderer(); // WebGLRenderer CanvasRenderer
  167. renderer.setSize( window.innerWidth, window.innerHeight );
  168. container.appendChild( renderer.domElement );
  169. stats = new Stats();
  170. stats.domElement.style.position = 'absolute';
  171. stats.domElement.style.top = '0px';
  172. container.appendChild( stats.domElement );
  173. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  174. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  175. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  176. }
  177. //
  178. function onDocumentMouseDown( event ) {
  179. event.preventDefault();
  180. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  181. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  182. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  183. mouseXOnMouseDown = event.clientX - windowHalfX;
  184. targetRotationOnMouseDown = targetRotation;
  185. }
  186. function onDocumentMouseMove( event ) {
  187. mouseX = event.clientX - windowHalfX;
  188. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  189. }
  190. function onDocumentMouseUp( event ) {
  191. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  192. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  193. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  194. }
  195. function onDocumentMouseOut( event ) {
  196. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  197. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  198. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  199. }
  200. function onDocumentTouchStart( event ) {
  201. if ( event.touches.length == 1 ) {
  202. event.preventDefault();
  203. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  204. targetRotationOnMouseDown = targetRotation;
  205. }
  206. }
  207. function onDocumentTouchMove( event ) {
  208. if ( event.touches.length == 1 ) {
  209. event.preventDefault();
  210. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  211. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  212. }
  213. }
  214. //
  215. function animate() {
  216. requestAnimationFrame( animate );
  217. render();
  218. stats.update();
  219. }
  220. function render() {
  221. group.rotation.y = plane.rotation.z = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
  222. renderer.render( scene, camera );
  223. }
  224. </script>
  225. </body>
  226. </html>
粤ICP备19079148号