webgl_geometry_subdivision.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - Subdivisions with Catmull-Clark</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.min.js"></script>
  18. <script src="js/modifiers/SubdivisionModifier.js"></script>
  19. <script src="js/libs/stats.min.js"></script>
  20. <script src="fonts/helvetiker_regular.typeface.js"></script>
  21. <script>
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var cube, plane;
  25. var targetYRotation = targetXRotation = 0;
  26. var targetYRotationOnMouseDown = targetXRotationOnMouseDown = 0;
  27. var mouseX = 0, mouseY = 0;
  28. var mouseXOnMouseDown = 0;
  29. var windowHalfX = window.innerWidth / 2;
  30. var windowHalfY = window.innerHeight / 2;
  31. // Create new object by parameters
  32. var createSomething = function( klass, args ) {
  33. var F = function( klass, args ) {
  34. return klass.apply( this, args );
  35. }
  36. F.prototype = klass.prototype;
  37. return new F( klass, args );
  38. };
  39. // Cube
  40. var materials = [];
  41. for ( var i = 0; i < 6; i ++ ) {
  42. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
  43. }
  44. var geometriesParams = [
  45. { type: 'CubeGeometry', args: [ 200, 200, 200, 2, 2, 2, materials ] },
  46. { type: 'TorusGeometry', args: [ 100, 60, 4, 8, Math.PI*2 ] },
  47. { type: 'TorusKnotGeometry', args: [ ], scale:0.25, meshScale:4 },
  48. { type: 'SphereGeometry', args: [ 100, 3, 3 ], meshScale:2 },
  49. { type: 'IcosahedronGeometry', args: [ 100, 1 ], meshScale:2 },
  50. { type: 'CylinderGeometry', args: [ 25, 75, 200, 8, 3 ]} ,
  51. { type: 'OctahedronGeometry', args: [200, 0], meshScale:2 },
  52. { type: 'LatheGeometry', args: [ [
  53. new THREE.Vector3(0,0,0),
  54. new THREE.Vector3(0,50,50),
  55. new THREE.Vector3(0,10,100),
  56. new THREE.Vector3(0,50,150),
  57. new THREE.Vector3(0,0,200) ] ]},
  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. ];
  66. var loader = new THREE.JSONLoader();
  67. loader.load( 'obj/WaltHeadLo.js', function ( geometry ) {
  68. geometriesParams.push({type: 'WaltHead', args: [ ], meshScale: 6 });
  69. THREE.WaltHead = function() {
  70. return geometry.clone();
  71. };
  72. updateInfo()
  73. });
  74. var loader2 = new THREE.JSONLoader();
  75. loader2.load( 'obj/Suzanne.js', function ( geometry ) {
  76. geometriesParams.push({type: 'Suzanne', args: [ ], scale: 100, meshScale:2 });
  77. THREE.Suzanne = function() {
  78. return geometry.clone();
  79. };
  80. updateInfo()
  81. } );
  82. var info;
  83. var subdivisions = 2;
  84. var geometryIndex = 0;
  85. // start scene
  86. init();
  87. animate();
  88. function nextSubdivision( x ) {
  89. subdivisions = Math.max( 0, subdivisions + x );
  90. addStuff();
  91. }
  92. function nextGeometry() {
  93. geometryIndex ++;
  94. if ( geometryIndex > geometriesParams.length - 1 ) {
  95. geometryIndex = 0;
  96. }
  97. addStuff();
  98. }
  99. function switchGeometry(i) {
  100. geometryIndex = i;
  101. addStuff();
  102. }
  103. function updateInfo() {
  104. var params = geometriesParams[ geometryIndex ];
  105. var dropdown = '<select id="dropdown" onchange="switchGeometry(this.value)">';
  106. for ( i = 0; i < geometriesParams.length; i ++ ) {
  107. dropdown += '<option value="' + i + '"';
  108. dropdown += (geometryIndex == i) ? ' selected' : '';
  109. dropdown += '>' + geometriesParams[i].type + '</option>';
  110. }
  111. dropdown += '</select>';
  112. info.innerHTML = 'Drag to spin THREE.' + params.type +
  113. '<br><br>Subdivisions: ' + subdivisions +
  114. ' <a href="#" onclick="nextSubdivision(1); return false;">more</a>/<a href="#" onclick="nextSubdivision(-1); return false;">less</a>' +
  115. '<br>Geometry: ' + dropdown + ' <a href="#" onclick="nextGeometry();return false;">next</a>' +
  116. '<br><br>Vertices count: before ' + geometry.vertices.length + ' after ' + smooth.vertices.length +
  117. '<br>Face count: before ' + geometry.faces.length + ' after ' + smooth.faces.length
  118. ; //+ params.args;
  119. }
  120. function addStuff() {
  121. if ( cube ) {
  122. scene.remove( group );
  123. scene.remove( cube );
  124. }
  125. var modifier = new THREE.SubdivisionModifier( subdivisions );
  126. var params = geometriesParams[ geometryIndex ];
  127. geometry = createSomething( THREE[ params.type ], params.args );
  128. // Scale Geometry
  129. if ( params.scale ) {
  130. geometry.applyMatrix( new THREE.Matrix4().makeScale( params.scale, params.scale, params.scale ) );
  131. }
  132. // Cloning original geometry for debuging
  133. smooth = geometry.clone();
  134. // mergeVertices(); is run in case of duplicated vertices
  135. smooth.mergeVertices();
  136. smooth.computeCentroids();
  137. smooth.computeFaceNormals();
  138. smooth.computeVertexNormals();
  139. modifier.modify( smooth );
  140. updateInfo();
  141. var faceABCD = "abcd";
  142. var color, f, p, n, vertexIndex;
  143. for ( i = 0; i < smooth.faces.length; i ++ ) {
  144. f = smooth.faces[ i ];
  145. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  146. for( var j = 0; j < n; j++ ) {
  147. vertexIndex = f[ faceABCD.charAt( j ) ];
  148. p = smooth.vertices[ vertexIndex ];
  149. color = new THREE.Color( 0xffffff );
  150. color.setHSL( ( p.y ) / 200 + 0.5, 1.0, 0.5 );
  151. f.vertexColors[ j ] = color;
  152. }
  153. }
  154. group = new THREE.Object3D();
  155. group.add( new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0xfefefe, wireframe: true, opacity: 0.5 } ) ) );
  156. scene.add( group );
  157. var debugNewPoints = false;
  158. var debugOldPoints = false;
  159. // Debug new Points
  160. if (debugNewPoints) {
  161. var PI2 = Math.PI * 2;
  162. var program = function ( context ) {
  163. context.beginPath();
  164. context.arc( 0, 0, 1, 0, PI2, true );
  165. context.closePath();
  166. context.fill();
  167. }
  168. for ( var i = 0; i < smooth.vertices.length; i++ ) {
  169. particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  170. particle.position = smooth.vertices[ i ];
  171. var pos = smooth.vertices.position
  172. particle.scale.x = particle.scale.y = 5;
  173. group.add( particle );
  174. }
  175. }
  176. //Debug original points
  177. if (debugOldPoints) {
  178. var drawText = function(i) {
  179. return function ( context ) {
  180. context.beginPath();
  181. context.scale(0.1,-0.1);
  182. context.fillText(i, 0,0);
  183. };
  184. }
  185. for ( var i = 0; i < geometry.vertices.length; i++ ) {
  186. particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
  187. particle.position = smooth.vertices[ i ];
  188. var pos = geometry.vertices.position
  189. particle.scale.x = particle.scale.y = 30;
  190. group.add( particle );
  191. }
  192. }
  193. var meshmaterials = [
  194. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  195. new THREE.MeshBasicMaterial( { color: 0x405040, wireframe: true, opacity: 0.8, transparent: true } )
  196. ];
  197. cube = THREE.SceneUtils.createMultiMaterialObject( smooth, meshmaterials );
  198. var meshScale = params.meshScale ? params.meshScale : 1;
  199. cube.scale.x = meshScale;
  200. cube.scale.y = meshScale;
  201. cube.scale.z = meshScale;
  202. scene.add( cube );
  203. group.scale.copy( cube.scale );
  204. }
  205. function init() {
  206. container = document.createElement( 'div' );
  207. document.body.appendChild( container );
  208. info = document.createElement( 'div' );
  209. info.style.position = 'absolute';
  210. info.style.top = '10px';
  211. info.style.width = '100%';
  212. info.style.textAlign = 'center';
  213. info.innerHTML = 'Drag to spin the geometry ';
  214. container.appendChild( info );
  215. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  216. camera.position.z = 500;
  217. scene = new THREE.Scene();
  218. var light = new THREE.PointLight( 0xffffff, 1.5 );
  219. light.position.set( 1000, 1000, 2000 );
  220. scene.add( light );
  221. addStuff();
  222. renderer = new THREE.WebGLRenderer( { antialias: true } ); // WebGLRenderer CanvasRenderer
  223. renderer.setSize( window.innerWidth, window.innerHeight );
  224. container.appendChild( renderer.domElement );
  225. stats = new Stats();
  226. stats.domElement.style.position = 'absolute';
  227. stats.domElement.style.top = '0px';
  228. container.appendChild( stats.domElement );
  229. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  230. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  231. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  232. //
  233. window.addEventListener( 'resize', onWindowResize, false );
  234. }
  235. function onWindowResize() {
  236. windowHalfX = window.innerWidth / 2;
  237. windowHalfY = window.innerHeight / 2;
  238. camera.aspect = window.innerWidth / window.innerHeight;
  239. camera.updateProjectionMatrix();
  240. renderer.setSize( window.innerWidth, window.innerHeight );
  241. }
  242. //
  243. function onDocumentMouseDown( event ) {
  244. //event.preventDefault();
  245. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  246. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  247. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  248. mouseXOnMouseDown = event.clientX - windowHalfX;
  249. mouseYOnMouseDown = event.clientY - windowHalfY;
  250. targetYRotationOnMouseDown = targetYRotation;
  251. targetXRotationOnMouseDown = targetXRotation;
  252. }
  253. function onDocumentMouseMove( event ) {
  254. mouseX = event.clientX - windowHalfX;
  255. mouseY = event.clientY - windowHalfY;
  256. targetYRotation = targetYRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  257. targetXRotation = targetXRotationOnMouseDown + ( mouseY - mouseYOnMouseDown ) * 0.02;
  258. }
  259. function onDocumentMouseUp( event ) {
  260. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  261. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  262. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  263. }
  264. function onDocumentMouseOut( event ) {
  265. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  266. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  267. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  268. }
  269. function onDocumentTouchStart( event ) {
  270. if ( event.touches.length == 1 ) {
  271. event.preventDefault();
  272. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  273. targetRotationOnMouseDown = targetRotation;
  274. }
  275. }
  276. function onDocumentTouchMove( event ) {
  277. if ( event.touches.length == 1 ) {
  278. event.preventDefault();
  279. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  280. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  281. }
  282. }
  283. //
  284. function animate() {
  285. requestAnimationFrame( animate );
  286. render();
  287. stats.update();
  288. }
  289. function render() {
  290. group.rotation.x = cube.rotation.x += ( targetXRotation - cube.rotation.x ) * 0.05;
  291. group.rotation.y = cube.rotation.y += ( targetYRotation - cube.rotation.y ) * 0.05;
  292. renderer.render( scene, camera );
  293. }
  294. </script>
  295. </body>
  296. </html>