canvas_geometry_subdivison.html 9.4 KB

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