控制器

<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当然要封装了OrbitControlsopen in new window。使用方法已经在例子中,可以跳转演示open in new window

参数说明

暂未做翻译,详情字段可以对比ThreeJS的中文文档说明,详情跳转open in new window

PropDescriptionDefault
makeDefaultIf true, the controls will be set as the default controls for the scene.false
cameraThe camera to control.undefined
domElementThe dom element to listen to.undefined
targetThe target to orbit around.undefined
enableDampingIf true, the controls will use damping (inertia), which can be used to give a sense of weight to the controls.false
dampingFactorThe damping inertia used if .enableDamping is set to true.0.05
autoRotateSet to true to automatically rotate around the target.false
autoRotateSpeedHow fast to rotate around the target if .autoRotate is true.2
enablePanWhether to enable panning.true
keyPanSpeedHow fast to pan the camera when the keyboard is used. Default is 7.0 pixels per keypress.7.0
keysThis object contains references to the keycodes for controlling camera panning. Default is the 4 arrow keys.{ LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' }
maxAzimuthAngleHow 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
minAzimuthAngleHow 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
maxPolarAngleHow far you can orbit vertically, upper limit. Range is 0 to Math.PI radians, and default is Math.PI.Math.PI
minPolarAngleHow far you can orbit vertically, lower limit. Range is 0 to Math.PI radians, and default is 0.0
minDistanceThe minimum distance of the camera to the target. Default is 0.0
maxDistanceThe maximum distance of the camera to the target. Default is Infinity.Infinity
minZoomThe minimum field of view angle, in radians. Default is 0.0
maxZoomThe maximum field of view angle, in radians. ( OrthographicCamera only ). Default is Infinity.Infinity
touchesThis object contains references to the touch actions used by the controls.{ ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN }
--
enableZoomWhether to enable zooming.true
zoomSpeedHow fast to zoom in and out. Default is 1.1
enableRotateWhether to enable rotating.true
rotateSpeedHow fast to rotate around the target. Default is 1.

绑定事件

<OrbitControls @change="onChange" @start="onStart" @end="onEnd" />
EventDescription
startDispatched when the control starts to change.
changeDispatched when the control changes.
endDispatched when the control ends to change.

摄像头控制器(CameraControls)

CameraControls是一种类似于OrbitControls的相机控制器,但支持平滑过渡和更多功能。查看演示open in new window

<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对应章节open in new window

变换控制器(TransformControls)

该类可提供一种类似于在数字内容创建工具(例如 Blender)中对模型进行交互的方式,来在 3D 空间中变换物体。 和其他控制器不同的是,变换控制器不倾向于对场景摄像机的变换进行改变。

TresJS当然要封装了TransformControlsopen in new window。使用方法已经在例子中,可以跳转演示open in new window

<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的中文文档说明,详情跳转open in new window

PropDescriptionDefault
objectThe instance Object3Dopen in new window to control.null
modeThe mode of the controls. Can be translate, rotate or scale.translate
enabledIf true, the controls will be enabled.true
axisThe axis to use for the controls. Can be X, Y, Z, XY, YZ, XZ, XYZ.XYZ
spaceThe space to use for the controls. Can be local or world.local
sizeThe size of the controls.1
translationSnapThe distance to snap to when translating. (World units)null
rotationSnapThe angle to snap to when rotating. (Radians)null
scaleSnapThe scale to snap to when scaling.null
showXIf true, the X-axis helper will be shown.true
showYIf true, the Y-axis helper will be shown.true
showZIf true, the Z-axis helper will be shown.true

绑定事件

EventDescription
draggingFired when the user starts or stops dragging the controls.
changeFired when the user changes the controls.
mouseDownFired when the user clicks on the controls.
mouseUpFired when the user releases the mouse button on the controls.
objectChangeFired when the user changes the object.
<TransformControls
  @dragging="(e) => {}"
  @change="(e) => {}"
  @mouseDown="(e) => {}"
  @mouseUp="(e) => {}"
  @objectChange="(e) => {}"
/>