webgl_geometry_subdivison.html 11 KB

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