webgl_geometry_subdivison.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/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>
  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: [ 1 ], scale: 100, 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,0,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. });
  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. } );
  86. }
  87. var info;
  88. var subdivisions = 2;
  89. var geometryIndex = 0;
  90. // start scene
  91. init();
  92. animate();
  93. function nextSubdivision( x ) {
  94. subdivisions = Math.max( 0, subdivisions + x );
  95. addStuff();
  96. }
  97. function nextGeometry() {
  98. geometryIndex ++;
  99. if ( geometryIndex > geometriesParams.length - 1 ) {
  100. geometryIndex = 0;
  101. }
  102. addStuff();
  103. }
  104. function addStuff() {
  105. if ( cube ) {
  106. scene.remove( group );
  107. scene.remove( cube );
  108. }
  109. var modifier = new THREE.SubdivisionModifier( subdivisions );
  110. var params = geometriesParams[ geometryIndex ];
  111. geometry = createSomething( THREE[ params.type ], params.args );
  112. // Scale Geometry
  113. if (params.scale) {
  114. geometry.applyMatrix( new THREE.Matrix4().setScale( params.scale, params.scale, params.scale ) );
  115. }
  116. // Cloning original geometry for debuging
  117. smooth = THREE.GeometryUtils.clone( geometry );
  118. // mergeVertices(); is run in case of duplicated vertices
  119. smooth.mergeVertices();
  120. modifier.modify( smooth );
  121. info.innerHTML = 'Drag to spin THREE.' + params.type +
  122. '<br/><br/>Subdivisions: ' + modifier.subdivisions +
  123. ' <a href="#" onclick="nextSubdivision(1); return false;">more</a>/<a href="#" onclick="nextSubdivision(-1); return false;">less</a>' +
  124. '<br>Geometry: <a href="#" onclick="nextGeometry();return false;">next</a>' +
  125. '<br/><br>Vertices count: before ' + geometry.vertices.length + ' after ' + smooth.vertices.length +
  126. '<br>Face count: before ' + geometry.faces.length + ' after ' + smooth.faces.length
  127. ; //+ params.args;
  128. var faceABCD = "abcd";
  129. var color, f, p, n, vertexIndex;
  130. for ( i = 0; i < smooth.faces.length; i ++ ) {
  131. f = smooth.faces[ i ];
  132. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  133. for( var j = 0; j < n; j++ ) {
  134. vertexIndex = f[ faceABCD.charAt( j ) ];
  135. p = smooth.vertices[ vertexIndex ].position;
  136. color = new THREE.Color( 0xffffff );
  137. color.setHSV( ( p.y ) / 200 + 0.5, 1.0, 1.0 );
  138. f.vertexColors[ j ] = color;
  139. }
  140. }
  141. group = new THREE.Object3D();
  142. group.add( new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0xfefefe, wireframe: true, opacity: 0.5 } ) ) );
  143. scene.add( group );
  144. var debugNewPoints = false;
  145. var debugOldPoints = false;
  146. // Debug new Points
  147. if (debugNewPoints) {
  148. var PI2 = Math.PI * 2;
  149. var program = function ( context ) {
  150. context.beginPath();
  151. context.arc( 0, 0, 1, 0, PI2, true );
  152. context.closePath();
  153. context.fill();
  154. }
  155. for ( var i = 0; i < smooth.vertices.length; i++ ) {
  156. particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  157. particle.position = smooth.vertices[i].position;
  158. var pos = smooth.vertices.position
  159. particle.scale.x = particle.scale.y = 5;
  160. group.add( particle );
  161. }
  162. }
  163. //Debug original points
  164. if (debugOldPoints) {
  165. var drawText = function(i) {
  166. return function ( context ) {
  167. context.beginPath();
  168. context.scale(0.1,-0.1);
  169. context.fillText(i, 0,0);
  170. };
  171. }
  172. for ( var i = 0; i < geometry.vertices.length; i++ ) {
  173. particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
  174. particle.position = smooth.vertices[i].position;
  175. var pos = geometry.vertices.position
  176. particle.scale.x = particle.scale.y = 30;
  177. group.add( particle );
  178. }
  179. }
  180. var meshmaterials = [
  181. // new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )
  182. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  183. new THREE.MeshBasicMaterial( { color: 0x405040, wireframe: true, opacity: 0.8, transparent: true } )
  184. ];
  185. // new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  186. // new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )
  187. // new THREE.MeshFaceMaterial()
  188. // new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
  189. // new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } );
  190. cube = THREE.SceneUtils.createMultiMaterialObject( smooth, meshmaterials );
  191. var meshScale = params.meshScale ? params.meshScale : 1;
  192. cube.scale.x = meshScale;
  193. cube.scale.y = meshScale;
  194. cube.scale.z = meshScale;
  195. scene.add( cube );
  196. group.scale.copy( cube.scale );
  197. }
  198. function init() {
  199. container = document.createElement( 'div' );
  200. document.body.appendChild( container );
  201. info = document.createElement( 'div' );
  202. info.style.position = 'absolute';
  203. info.style.top = '10px';
  204. info.style.width = '100%';
  205. info.style.textAlign = 'center';
  206. info.innerHTML = 'Drag to spin the geometry ';
  207. container.appendChild( info );
  208. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  209. camera.position.z = 500;
  210. scene = new THREE.Scene();
  211. var light = new THREE.PointLight( 0xffffff, 1.5 );
  212. light.position.set( 1000, 1000, 2000 );
  213. scene.add( light );
  214. addStuff();
  215. renderer = new THREE.WebGLRenderer( { antialias: true } ); // WebGLRenderer CanvasRenderer
  216. renderer.setSize( window.innerWidth, window.innerHeight );
  217. container.appendChild( renderer.domElement );
  218. stats = new Stats();
  219. stats.domElement.style.position = 'absolute';
  220. stats.domElement.style.top = '0px';
  221. container.appendChild( stats.domElement );
  222. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  223. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  224. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  225. }
  226. //
  227. function onDocumentMouseDown( event ) {
  228. event.preventDefault();
  229. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  230. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  231. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  232. mouseXOnMouseDown = event.clientX - windowHalfX;
  233. mouseYOnMouseDown = event.clientY - windowHalfY;
  234. targetYRotationOnMouseDown = targetYRotation;
  235. targetXRotationOnMouseDown = targetXRotation;
  236. }
  237. function onDocumentMouseMove( event ) {
  238. mouseX = event.clientX - windowHalfX;
  239. mouseY = event.clientY - windowHalfY;
  240. targetYRotation = targetYRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  241. targetXRotation = targetXRotationOnMouseDown + ( mouseY - mouseYOnMouseDown ) * 0.02;
  242. }
  243. function onDocumentMouseUp( event ) {
  244. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  245. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  246. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  247. }
  248. function onDocumentMouseOut( event ) {
  249. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  250. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  251. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  252. }
  253. function onDocumentTouchStart( event ) {
  254. if ( event.touches.length == 1 ) {
  255. event.preventDefault();
  256. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  257. targetRotationOnMouseDown = targetRotation;
  258. }
  259. }
  260. function onDocumentTouchMove( event ) {
  261. if ( event.touches.length == 1 ) {
  262. event.preventDefault();
  263. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  264. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  265. }
  266. }
  267. //
  268. function animate() {
  269. requestAnimationFrame( animate );
  270. render();
  271. stats.update();
  272. }
  273. function render() {
  274. group.rotation.x = cube.rotation.x += ( targetXRotation - cube.rotation.x ) * 0.05;
  275. group.rotation.y = cube.rotation.y += ( targetYRotation - cube.rotation.y ) * 0.05;
  276. renderer.render( scene, camera );
  277. }
  278. </script>
  279. </body>
  280. </html>
粤ICP备19079148号