控制器
<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { OrbitControls } from '@tresjs/cientos'
</script>
<template>
<TresCanvas clearColor='#202020'>
<TresPerspectiveCamera :position="[3, 3, 3]" />
<OrbitControls enableDamping />
<TresGridHelper />
<TresAmbientLight />
</TresCanvas>
</template>
如上OrbitControls例子,所谓控制器,其可以在各种场景下,找到对应种类的控制器,完成功能需求。 TresJS 提供了:OrbitControls、CameraControls、TransformControls、PointerLockControls、KeyboardControls、ScrollControls、MapControls。
轨道控制器(OrbitControls)
OrbitControls 是一个摄影机控制器,允许您围绕目标进行动态观察。是threeJS最常用的控制器。 TresJS当然要封装了OrbitControls。使用方法已经在例子中,可以跳转演示
参数说明
暂未做翻译,详情字段可以对比ThreeJS的中文文档说明,详情跳转
| Prop | Description | Default |
|---|---|---|
| makeDefault | If true, the controls will be set as the default controls for the scene. | false |
| camera | The camera to control. | undefined |
| domElement | The dom element to listen to. | undefined |
| target | The target to orbit around. | undefined |
| enableDamping | If true, the controls will use damping (inertia), which can be used to give a sense of weight to the controls. | false |
| dampingFactor | The damping inertia used if .enableDamping is set to true. | 0.05 |
| autoRotate | Set to true to automatically rotate around the target. | false |
| autoRotateSpeed | How fast to rotate around the target if .autoRotate is true. | 2 |
| enablePan | Whether to enable panning. | true |
| keyPanSpeed | How fast to pan the camera when the keyboard is used. Default is 7.0 pixels per keypress. | 7.0 |
| keys | This object contains references to the keycodes for controlling camera panning. Default is the 4 arrow keys. | { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' } |
| maxAzimuthAngle | How far you can orbit horizontally, upper limit. If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI ). Default is Infinity. | Infinity |
| minAzimuthAngle | How far you can orbit horizontally, lower limit. If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI ). Default is - Infinity. | -Infinity |
| maxPolarAngle | How far you can orbit vertically, upper limit. Range is 0 to Math.PI radians, and default is Math.PI. | Math.PI |
| minPolarAngle | How far you can orbit vertically, lower limit. Range is 0 to Math.PI radians, and default is 0. | 0 |
| minDistance | The minimum distance of the camera to the target. Default is 0. | 0 |
| maxDistance | The maximum distance of the camera to the target. Default is Infinity. | Infinity |
| minZoom | The minimum field of view angle, in radians. Default is 0. | 0 |
| maxZoom | The maximum field of view angle, in radians. ( OrthographicCamera only ). Default is Infinity. | Infinity |
| touches | This object contains references to the touch actions used by the controls. | { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN } |
| - | - | |
| enableZoom | Whether to enable zooming. | true |
| zoomSpeed | How fast to zoom in and out. Default is 1. | 1 |
| enableRotate | Whether to enable rotating. | true |
| rotateSpeed | How fast to rotate around the target. Default is 1. |
绑定事件
<OrbitControls @change="onChange" @start="onStart" @end="onEnd" />
| Event | Description |
|---|---|
| start | Dispatched when the control starts to change. |
| change | Dispatched when the control changes. |
| end | Dispatched when the control ends to change. |
摄像头控制器(CameraControls)
CameraControls是一种类似于OrbitControls的相机控制器,但支持平滑过渡和更多功能。查看演示
<script setup lang="ts">
import { reactive } from 'vue'
import { TresCanvas } from '@tresjs/core'
import { CameraControls, Box } from '@tresjs/cientos'
const controlsState = reactive({
minDistance: 0,
maxDistance: 100,
})
</script>
<template>
<TresCanvas clear-color="#202020">
<TresPerspectiveCamera :position="[5, 5, 5]" />
<CameraControls v-bind="controlsState" make-default />
<TresGridHelper :position="[0, -1, 0]" />
<Box :scale="2">
<TresMeshToonMaterial color="orange" />
</Box>
<TresAmbientLight />
<TresDirectionalLight :position="[0, 2, 4]" />
</TresCanvas>
</template>
具体用法和参数详见:TresJS对应章节
变换控制器(TransformControls)
该类可提供一种类似于在数字内容创建工具(例如 Blender)中对模型进行交互的方式,来在 3D 空间中变换物体。 和其他控制器不同的是,变换控制器不倾向于对场景摄像机的变换进行改变。
TresJS当然要封装了TransformControls。使用方法已经在例子中,可以跳转演示
<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { OrbitControls, TransformControls } from '@tresjs/cientos'
import { ref, shallowReactive } from 'vue'
const boxRef = ref()
const controlsState = shallowReactive({
mode: 'translate',
enabled: true,
})
</script>
<template>
<TresCanvas clear-color="#202020">
<TresPerspectiveCamera :position="[11, 11, 11]" :fov="45" :near="0.1" :far="1000" />
<OrbitControls make-default />
<TransformControls :object="boxRef" v-bind="transformState" :mode="controlsState.mode" />
<TresMesh ref="boxRef" :position="[0, 4, 0]" cast-shadow>
<TresBoxGeometry :args="[1.5, 1.5, 1.5]" />
<TresMeshToonMaterial color="#FBB03B" />
</TresMesh>
<TresMesh :rotation="[-Math.PI / 2, 0, 0]" receive-shadow>
<TresPlaneGeometry :args="[10, 10, 10, 10]" />
<TresMeshToonMaterial />
</TresMesh>
<TresAmbientLight :intensity="0.5" />
<TresDirectionalLight :position="[0, 8, 4]" :intensity="1.5" cast-shadow />
</TresCanvas>
</template>
模式挂载
<!-- 改变位置 -->
<TransformControls mode="translate" :object="sphereRef" />
<!-- 改变旋转 -->
<TransformControls mode="rotate" :object="sphereRef" />
<!-- 改变大小 -->
<TransformControls mode="scale" :object="sphereRef" />
参数说明
暂未做翻译,详情字段可以对比ThreeJS的中文文档说明,详情跳转
| Prop | Description | Default |
|---|---|---|
| object | The instance Object3D to control. | null |
| mode | The mode of the controls. Can be translate, rotate or scale. | translate |
| enabled | If true, the controls will be enabled. | true |
| axis | The axis to use for the controls. Can be X, Y, Z, XY, YZ, XZ, XYZ. | XYZ |
| space | The space to use for the controls. Can be local or world. | local |
| size | The size of the controls. | 1 |
| translationSnap | The distance to snap to when translating. (World units) | null |
| rotationSnap | The angle to snap to when rotating. (Radians) | null |
| scaleSnap | The scale to snap to when scaling. | null |
| showX | If true, the X-axis helper will be shown. | true |
| showY | If true, the Y-axis helper will be shown. | true |
| showZ | If true, the Z-axis helper will be shown. | true |
绑定事件
| Event | Description |
|---|---|
| dragging | Fired when the user starts or stops dragging the controls. |
| change | Fired when the user changes the controls. |
| mouseDown | Fired when the user clicks on the controls. |
| mouseUp | Fired when the user releases the mouse button on the controls. |
| objectChange | Fired when the user changes the object. |
<TransformControls
@dragging="(e) => {}"
@change="(e) => {}"
@mouseDown="(e) => {}"
@mouseUp="(e) => {}"
@objectChange="(e) => {}"
/>
