misc_lookat.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. <!-- Import maps polyfill -->
  21. <!-- Remove this when import maps will be widely supported -->
  22. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.module.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import Stats from 'three/addons/libs/stats.module.js';
  34. let camera, scene, renderer, stats;
  35. let sphere;
  36. let mouseX = 0, mouseY = 0;
  37. let windowHalfX = window.innerWidth / 2;
  38. let windowHalfY = window.innerHeight / 2;
  39. document.addEventListener( 'mousemove', onDocumentMouseMove );
  40. init();
  41. animate();
  42. function init() {
  43. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 15000 );
  44. camera.position.z = 3200;
  45. scene = new THREE.Scene();
  46. scene.background = new THREE.Color( 0xffffff );
  47. sphere = new THREE.Mesh( new THREE.SphereGeometry( 100, 20, 20 ), new THREE.MeshNormalMaterial() );
  48. scene.add( sphere );
  49. const geometry = new THREE.CylinderGeometry( 0, 10, 100, 12 );
  50. geometry.rotateX( Math.PI / 2 );
  51. const material = new THREE.MeshNormalMaterial();
  52. for ( let i = 0; i < 1000; i ++ ) {
  53. const mesh = new THREE.Mesh( geometry, material );
  54. mesh.position.x = Math.random() * 4000 - 2000;
  55. mesh.position.y = Math.random() * 4000 - 2000;
  56. mesh.position.z = Math.random() * 4000 - 2000;
  57. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 4 + 2;
  58. scene.add( mesh );
  59. }
  60. renderer = new THREE.WebGLRenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. document.body.appendChild( renderer.domElement );
  64. stats = new Stats();
  65. document.body.appendChild( stats.dom );
  66. //
  67. window.addEventListener( 'resize', onWindowResize );
  68. }
  69. function onWindowResize() {
  70. windowHalfX = window.innerWidth / 2;
  71. windowHalfY = window.innerHeight / 2;
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. function onDocumentMouseMove( event ) {
  77. mouseX = ( event.clientX - windowHalfX ) * 10;
  78. mouseY = ( event.clientY - windowHalfY ) * 10;
  79. }
  80. //
  81. function animate() {
  82. requestAnimationFrame( animate );
  83. render();
  84. stats.update();
  85. }
  86. function render() {
  87. const time = Date.now() * 0.0005;
  88. sphere.position.x = Math.sin( time * 0.7 ) * 2000;
  89. sphere.position.y = Math.cos( time * 0.5 ) * 2000;
  90. sphere.position.z = Math.cos( time * 0.3 ) * 2000;
  91. for ( let i = 1, l = scene.children.length; i < l; i ++ ) {
  92. scene.children[ i ].lookAt( sphere.position );
  93. }
  94. camera.position.x += ( mouseX - camera.position.x ) * .05;
  95. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  96. camera.lookAt( scene.position );
  97. renderer.render( scene, camera );
  98. }
  99. </script>
  100. </body>
  101. </html>