123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - trackball controls</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #ccc;
- color: #000;
- }
- a {
- color: #f00;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - trackball controls<br />
- MOVE mouse & press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { TrackballControls } from './jsm/controls/TrackballControls.js';
- var perspectiveCamera, orthographicCamera, controls, scene, renderer, stats;
- var params = {
- orthographicCamera: false
- };
- var frustumSize = 400;
- init();
- animate();
- function init() {
- var aspect = window.innerWidth / window.innerHeight;
- perspectiveCamera = new THREE.PerspectiveCamera( 60, aspect, 1, 1000 );
- perspectiveCamera.position.z = 500;
- orthographicCamera = new THREE.OrthographicCamera( frustumSize * aspect / - 2, frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 1, 1000 );
- orthographicCamera.position.z = 500;
- // world
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xcccccc );
- scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
- var geometry = new THREE.CylinderBufferGeometry( 0, 10, 30, 4, 1 );
- var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
- for ( var i = 0; i < 500; i ++ ) {
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = ( Math.random() - 0.5 ) * 1000;
- mesh.position.y = ( Math.random() - 0.5 ) * 1000;
- mesh.position.z = ( Math.random() - 0.5 ) * 1000;
- mesh.updateMatrix();
- mesh.matrixAutoUpdate = false;
- scene.add( mesh );
- }
- // lights
- var light = new THREE.DirectionalLight( 0xffffff );
- light.position.set( 1, 1, 1 );
- scene.add( light );
- var light = new THREE.DirectionalLight( 0x002288 );
- light.position.set( - 1, - 1, - 1 );
- scene.add( light );
- var light = new THREE.AmbientLight( 0x222222 );
- scene.add( light );
- // renderer
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- stats = new Stats();
- document.body.appendChild( stats.dom );
- //
- var gui = new GUI();
- gui.add( params, 'orthographicCamera' ).name( 'use orthographic' ).onChange( function ( value ) {
- controls.dispose();
- createControls( value ? orthographicCamera : perspectiveCamera );
- } );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- createControls( perspectiveCamera );
- }
- function createControls( camera ) {
- controls = new TrackballControls( camera, renderer.domElement );
- controls.rotateSpeed = 1.0;
- controls.zoomSpeed = 1.2;
- controls.panSpeed = 0.8;
- controls.keys = [ 65, 83, 68 ];
- }
- function onWindowResize() {
- var aspect = window.innerWidth / window.innerHeight;
- perspectiveCamera.aspect = aspect;
- perspectiveCamera.updateProjectionMatrix();
- orthographicCamera.left = - frustumSize * aspect / 2;
- orthographicCamera.right = frustumSize * aspect / 2;
- orthographicCamera.top = frustumSize / 2;
- orthographicCamera.bottom = - frustumSize / 2;
- orthographicCamera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- controls.handleResize();
- }
- function animate() {
- requestAnimationFrame( animate );
- controls.update();
- stats.update();
- render();
- }
- function render() {
- var camera = ( params.orthographicCamera ) ? orthographicCamera : perspectiveCamera;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|