canvas_geometry_subdivison.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry - cube</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>
  23. var container, stats;
  24. var camera, scene, renderer;
  25. var cube, plane;
  26. var targetRotation = 0;
  27. var targetRotationOnMouseDown = 0;
  28. var mouseX = 0;
  29. var mouseXOnMouseDown = 0;
  30. var windowHalfX = window.innerWidth / 2;
  31. var windowHalfY = window.innerHeight / 2;
  32. // Create subdivision geometry
  33. function createSubdivision(geometry, repeats) {
  34. repeats = (repeats === undefined ) ? 1 : repeats;
  35. var smooth = geometry;
  36. while (repeats--) {
  37. smooth = new THREE.SubdivisionGeometry(smooth);
  38. }
  39. return smooth;
  40. }
  41. init();
  42. animate();
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. var info = document.createElement( 'div' );
  47. info.style.position = 'absolute';
  48. info.style.top = '10px';
  49. info.style.width = '100%';
  50. info.style.textAlign = 'center';
  51. info.innerHTML = 'Drag to spin the cube';
  52. container.appendChild( info );
  53. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  54. camera.position.y = 150;
  55. camera.position.z = 500;
  56. camera.target.position.y = 150;
  57. scene = new THREE.Scene();
  58. // Cube
  59. var materials = [];
  60. for ( var i = 0; i < 6; i ++ ) {
  61. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: true } ) ] );
  62. }
  63. geometry = new THREE.CubeGeometry( 200, 200, 200, 1, 1, 1, materials );
  64. smooth = createSubdivision(geometry, 2);
  65. var PI2 = Math.PI * 2;
  66. var program = function ( context ) {
  67. context.beginPath();
  68. context.arc( 0, 0, 1, 0, PI2, true );
  69. context.closePath();
  70. context.fill();
  71. }
  72. group = new THREE.Object3D();
  73. group.position.y = 150;
  74. scene.add( group );
  75. for ( var i = 0; i < smooth.vertices.length; i++ ) {
  76. particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  77. particle.position = smooth.vertices[i].position;
  78. var pos = smooth.vertices.position
  79. particle.scale.x = particle.scale.y = 5;
  80. group.add( particle );
  81. }
  82. cube = new THREE.Mesh( smooth, new THREE.MeshBasicMaterial( { color: 0x405040, wireframe:true, opacity:0.8 } ) ); //new THREE.MeshFaceMaterial()
  83. cube.doubleSided = true;
  84. cube.position.y = 150;
  85. cube.overdraw = true;
  86. scene.add( cube );
  87. // Plane
  88. plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  89. plane.rotation.x = - 90 * ( Math.PI / 180 );
  90. plane.overdraw = true;
  91. scene.add( plane );
  92. renderer = new THREE.CanvasRenderer();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. container.appendChild( renderer.domElement );
  95. stats = new Stats();
  96. stats.domElement.style.position = 'absolute';
  97. stats.domElement.style.top = '0px';
  98. container.appendChild( stats.domElement );
  99. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  100. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  101. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  102. }
  103. //
  104. function onDocumentMouseDown( event ) {
  105. event.preventDefault();
  106. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  107. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  108. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  109. mouseXOnMouseDown = event.clientX - windowHalfX;
  110. targetRotationOnMouseDown = targetRotation;
  111. }
  112. function onDocumentMouseMove( event ) {
  113. mouseX = event.clientX - windowHalfX;
  114. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  115. }
  116. function onDocumentMouseUp( event ) {
  117. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  118. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  119. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  120. }
  121. function onDocumentMouseOut( event ) {
  122. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  123. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  124. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  125. }
  126. function onDocumentTouchStart( event ) {
  127. if ( event.touches.length == 1 ) {
  128. event.preventDefault();
  129. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  130. targetRotationOnMouseDown = targetRotation;
  131. }
  132. }
  133. function onDocumentTouchMove( event ) {
  134. if ( event.touches.length == 1 ) {
  135. event.preventDefault();
  136. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  137. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  138. }
  139. }
  140. //
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. render();
  144. stats.update();
  145. }
  146. function render() {
  147. group.rotation.y = plane.rotation.z = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
  148. renderer.render( scene, camera );
  149. }
  150. </script>
  151. </body>
  152. </html>
粤ICP备19079148号