webgl_geometry_subdivison.html 11 KB

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