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