webgl_geometry_subdivison.html 10 KB

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