demo案例
TrackballControls是Three.js中的一个相机控件,它允许用户通过鼠标拖拽、滚轮缩放以及键盘移动相机,实现类似于球形的相机旋转操作。这个控件可以用于3D场景中,以提供更好的用户体验。以下是对TrackballControls的入参、出参、方法和属性的详细讲解:
入参:
TrackballControls的构造函数通常接受两个参数:
camera
:这是用于渲染场景的摄像机对象。TrackballControls将控制这个摄像机的移动和旋转。domElement
:这是一个HTMLDOMElement对象,用于监听鼠标/触摸事件。这通常是一个canvas元素,用户可以在上面进行交互操作。
出参:
TrackballControls本身并没有直接的返回值(出参)。它主要的作用是通过改变摄像机的位置、旋转和缩放,从而改变渲染的场景视图。用户与TrackballControls的交互会直接影响摄像机的状态,进而改变渲染结果。
方法:
TrackballControls提供了一些方法用于控制其行为:
update()
:这个方法需要在循环函数中不断调用,以更新控件的状态。它根据用户的交互操作(如鼠标拖拽、滚轮滚动等)来更新摄像机的位置、旋转和缩放。handleResize()
:当渲染窗口的大小发生变化时,可以调用这个方法来重新计算控件的布局和尺寸。
此外,TrackballControls还定义了一些事件处理函数,如change
、start
和end
,这些函数会在摄像机被变换、交互被初始化以及交互完成后被触发。
属性:
TrackballControls有一些属性可以用于配置其行为:
domElement
:如前所述,这是一个HTMLDOMElement对象,用于监听鼠标/触摸事件。enabled
:一个布尔值,用于启用或禁用控件。当设为false
时,控件将不再响应用户的交互操作。rotateSpeed
、zoomSpeed
、panSpeed
:这些属性用于控制旋转、缩放和平移的速度。noRotate
、noZoom
、noPan
:这些属性用于禁用特定的交互操作,如旋转、缩放或平移。
全部源码
<!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="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
let perspectiveCamera, orthographicCamera, controls, scene, renderer, stats;
const params = {
orthographicCamera: false
};
const frustumSize = 400;
init();
animate();
function init() {
const 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 );
const geometry = new THREE.ConeGeometry( 10, 30, 4, 1 );
const material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
for ( let i = 0; i < 500; i ++ ) {
const 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
const dirLight1 = new THREE.DirectionalLight( 0xffffff, 3 );
dirLight1.position.set( 1, 1, 1 );
scene.add( dirLight1 );
const dirLight2 = new THREE.DirectionalLight( 0x002288, 3 );
dirLight2.position.set( - 1, - 1, - 1 );
scene.add( dirLight2 );
const ambientLight = new THREE.AmbientLight( 0x555555 );
scene.add( ambientLight );
// 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 );
//
const gui = new GUI();
gui.add( params, 'orthographicCamera' ).name( 'use orthographic' ).onChange( function ( value ) {
controls.dispose();
createControls( value ? orthographicCamera : perspectiveCamera );
} );
//
window.addEventListener( 'resize', onWindowResize );
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 = [ 'KeyA', 'KeyS', 'KeyD' ];
}
function onWindowResize() {
const 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() {
const camera = ( params.orthographicCamera ) ? orthographicCamera : perspectiveCamera;
renderer.render( scene, camera );
}
</script>
</body>
</html>
本内容来源于小豆包,想要更多内容请跳转小豆包 》