webgl_interactive_cubes_tween.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - interactive cubes</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. font-family: Monospace;
  9. background-color: #f0f0f0;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <script type="text/javascript" src="../build/Three.js"></script>
  17. <script type="text/javascript" src="js/Tween.js"></script>
  18. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  19. <script type="text/javascript" src="js/Stats.js"></script>
  20. <script type="text/javascript">
  21. var container, stats;
  22. var camera, scene, projector, renderer;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. var info = document.createElement( 'div' );
  29. info.style.position = 'absolute';
  30. info.style.top = '10px';
  31. info.style.width = '100%';
  32. info.style.textAlign = 'center';
  33. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> webgl - interactive cubes';
  34. container.appendChild( info );
  35. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  36. camera.position.y = 300;
  37. camera.position.z = 500;
  38. scene = new THREE.Scene();
  39. var light = new THREE.DirectionalLight( 0xffffff, 2 );
  40. light.position.x = 1;
  41. light.position.y = 1;
  42. light.position.z = 1;
  43. light.position.normalize();
  44. scene.addLight( light );
  45. var light = new THREE.DirectionalLight( 0xffffff );
  46. light.position.x = - 1;
  47. light.position.y = - 1;
  48. light.position.z = - 1;
  49. light.position.normalize();
  50. scene.addLight( light );
  51. var geometry = new THREE.Cube( 100, 100, 100 );
  52. for ( var i = 0; i < 20; i ++ ) {
  53. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  54. object.position.x = Math.random() * 800 - 400;
  55. object.position.y = Math.random() * 800 - 400;
  56. object.position.z = Math.random() * 800 - 400;
  57. object.scale.x = Math.random() * 2 + 1;
  58. object.scale.y = Math.random() * 2 + 1;
  59. object.scale.z = Math.random() * 2 + 1;
  60. object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
  61. object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
  62. object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
  63. scene.addObject( object );
  64. }
  65. projector = new THREE.Projector();
  66. renderer = new THREE.WebGLRenderer();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. container.appendChild(renderer.domElement);
  69. stats = new Stats();
  70. stats.domElement.style.position = 'absolute';
  71. stats.domElement.style.top = '0px';
  72. container.appendChild( stats.domElement );
  73. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  74. }
  75. function onDocumentMouseDown( event ) {
  76. event.preventDefault();
  77. var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
  78. projector.unprojectVector( vector, camera );
  79. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  80. var intersects = ray.intersectScene( scene );
  81. if ( intersects.length > 0 ) {
  82. new TWEEN.Tween( intersects[ 0 ].object.position ).to( {
  83. x: Math.random() * 800 - 400,
  84. y: Math.random() * 800 - 400,
  85. z: Math.random() * 800 - 400 }, 2000 )
  86. .easing( TWEEN.Easing.Elastic.EaseOut).start();
  87. new TWEEN.Tween( intersects[ 0 ].object.rotation ).to( {
  88. x: ( Math.random() * 360 ) * Math.PI / 180,
  89. y: ( Math.random() * 360 ) * Math.PI / 180,
  90. z: ( Math.random() * 360 ) * Math.PI / 180 }, 2000 )
  91. .easing( TWEEN.Easing.Elastic.EaseOut).start();
  92. }
  93. /*
  94. // Parse all the faces
  95. for ( var i in intersects ) {
  96. intersects[ i ].face.material[ 0 ].color.setHex( Math.random() * 0xffffff | 0x80000000 );
  97. }
  98. */
  99. }
  100. //
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. render();
  104. stats.update();
  105. }
  106. var radius = 600;
  107. var theta = 0;
  108. function render() {
  109. TWEEN.update();
  110. theta += 0.2;
  111. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  112. camera.position.y = radius * Math.sin( theta * Math.PI / 360 );
  113. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>
粤ICP备19079148号