webgl_geometry_subdivison.html 11 KB

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