misc_controls_trackball.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - trackball controls</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: #ccc;
  11. color: #000;
  12. }
  13. a {
  14. color: #f00;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - trackball controls<br />
  21. MOVE mouse &amp; press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
  22. </div>
  23. <script type="module">
  24. import {
  25. AmbientLight,
  26. Color,
  27. CylinderBufferGeometry,
  28. DirectionalLight,
  29. FogExp2,
  30. Mesh,
  31. MeshPhongMaterial,
  32. PerspectiveCamera,
  33. Scene,
  34. WebGLRenderer
  35. } from "../build/three.module.js";
  36. import Stats from './jsm/libs/stats.module.js';
  37. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  38. var camera, controls, scene, renderer, stats;
  39. init();
  40. animate();
  41. function init() {
  42. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.position.z = 500;
  44. // world
  45. scene = new Scene();
  46. scene.background = new Color( 0xcccccc );
  47. scene.fog = new FogExp2( 0xcccccc, 0.002 );
  48. var geometry = new CylinderBufferGeometry( 0, 10, 30, 4, 1 );
  49. var material = new MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  50. for ( var i = 0; i < 500; i ++ ) {
  51. var mesh = new Mesh( geometry, material );
  52. mesh.position.x = ( Math.random() - 0.5 ) * 1000;
  53. mesh.position.y = ( Math.random() - 0.5 ) * 1000;
  54. mesh.position.z = ( Math.random() - 0.5 ) * 1000;
  55. mesh.updateMatrix();
  56. mesh.matrixAutoUpdate = false;
  57. scene.add( mesh );
  58. }
  59. // lights
  60. var light = new DirectionalLight( 0xffffff );
  61. light.position.set( 1, 1, 1 );
  62. scene.add( light );
  63. var light = new DirectionalLight( 0x002288 );
  64. light.position.set( - 1, - 1, - 1 );
  65. scene.add( light );
  66. var light = new AmbientLight( 0x222222 );
  67. scene.add( light );
  68. // renderer
  69. renderer = new WebGLRenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. document.body.appendChild( renderer.domElement );
  73. controls = new TrackballControls( camera, renderer.domElement );
  74. controls.rotateSpeed = 1.0;
  75. controls.zoomSpeed = 1.2;
  76. controls.panSpeed = 0.8;
  77. controls.noZoom = false;
  78. controls.noPan = false;
  79. controls.staticMoving = true;
  80. controls.dynamicDampingFactor = 0.3;
  81. controls.keys = [ 65, 83, 68 ];
  82. controls.addEventListener( 'change', render );
  83. stats = new Stats();
  84. document.body.appendChild( stats.dom );
  85. //
  86. window.addEventListener( 'resize', onWindowResize, false );
  87. //
  88. render();
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. controls.handleResize();
  95. render();
  96. }
  97. function animate() {
  98. requestAnimationFrame( animate );
  99. controls.update();
  100. stats.update();
  101. }
  102. function render() {
  103. renderer.render( scene, camera );
  104. }
  105. </script>
  106. </body>
  107. </html>