misc_lookat.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js misc - lookAt</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08b;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Object3D.lookAt() example</div>
  20. <script type="module">
  21. import * as THREE from '../build/three.module.js';
  22. import Stats from './jsm/libs/stats.module.js';
  23. var camera, scene, renderer, stats;
  24. var sphere;
  25. var mouseX = 0, mouseY = 0;
  26. var windowHalfX = window.innerWidth / 2;
  27. var windowHalfY = window.innerHeight / 2;
  28. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  29. init();
  30. animate();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 15000 );
  33. camera.position.z = 3200;
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0xffffff );
  36. sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 100, 20, 20 ), new THREE.MeshNormalMaterial() );
  37. scene.add( sphere );
  38. var geometry = new THREE.CylinderBufferGeometry( 0, 10, 100, 12 );
  39. geometry.rotateX( Math.PI / 2 );
  40. var material = new THREE.MeshNormalMaterial();
  41. for ( var i = 0; i < 1000; i ++ ) {
  42. var mesh = new THREE.Mesh( geometry, material );
  43. mesh.position.x = Math.random() * 4000 - 2000;
  44. mesh.position.y = Math.random() * 4000 - 2000;
  45. mesh.position.z = Math.random() * 4000 - 2000;
  46. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 4 + 2;
  47. scene.add( mesh );
  48. }
  49. renderer = new THREE.WebGLRenderer( { antialias: true } );
  50. renderer.setPixelRatio( window.devicePixelRatio );
  51. renderer.setSize( window.innerWidth, window.innerHeight );
  52. document.body.appendChild( renderer.domElement );
  53. stats = new Stats();
  54. document.body.appendChild( stats.dom );
  55. //
  56. window.addEventListener( 'resize', onWindowResize, false );
  57. }
  58. function onWindowResize() {
  59. windowHalfX = window.innerWidth / 2;
  60. windowHalfY = window.innerHeight / 2;
  61. camera.aspect = window.innerWidth / window.innerHeight;
  62. camera.updateProjectionMatrix();
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. }
  65. function onDocumentMouseMove( event ) {
  66. mouseX = ( event.clientX - windowHalfX ) * 10;
  67. mouseY = ( event.clientY - windowHalfY ) * 10;
  68. }
  69. //
  70. function animate() {
  71. requestAnimationFrame( animate );
  72. render();
  73. stats.update();
  74. }
  75. function render() {
  76. var time = Date.now() * 0.0005;
  77. sphere.position.x = Math.sin( time * 0.7 ) * 2000;
  78. sphere.position.y = Math.cos( time * 0.5 ) * 2000;
  79. sphere.position.z = Math.cos( time * 0.3 ) * 2000;
  80. for ( var i = 1, l = scene.children.length; i < l; i ++ ) {
  81. scene.children[ i ].lookAt( sphere.position );
  82. }
  83. camera.position.x += ( mouseX - camera.position.x ) * .05;
  84. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  85. camera.lookAt( scene.position );
  86. renderer.render( scene, camera );
  87. }
  88. </script>
  89. </body>
  90. </html>