1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 12:22:03 +08:00
trois/src/core/PerspectiveCamera.ts

47 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-04-25 03:45:57 +08:00
import { defineComponent, inject, PropType, watch } from 'vue'
2021-04-16 09:19:50 +08:00
import { PerspectiveCamera } from 'three'
2021-05-06 03:40:23 +08:00
import { bindObjectProp, bindProp } from '../tools'
import Camera, { cameraSetProp } from './Camera'
2021-04-24 05:29:56 +08:00
import { Vector3PropInterface } from './Object3D'
2021-04-25 03:45:57 +08:00
import { RendererInjectionKey } from './Renderer'
2020-09-14 22:57:11 +08:00
export default defineComponent({
extends: Camera,
2021-03-07 20:22:54 +08:00
name: 'PerspectiveCamera',
2020-09-14 22:57:11 +08:00
props: {
2020-10-08 05:38:39 +08:00
aspect: { type: Number, default: 1 },
far: { type: Number, default: 2000 },
fov: { type: Number, default: 50 },
near: { type: Number, default: 0.1 },
2021-04-24 05:29:56 +08:00
position: { type: Object as PropType<Vector3PropInterface>, default: () => ({ x: 0, y: 0, z: 0 }) },
lookAt: { type: Object as PropType<Vector3PropInterface>, default: null },
2020-09-18 22:28:17 +08:00
},
2021-04-16 09:19:50 +08:00
setup(props) {
2021-04-25 03:45:57 +08:00
const renderer = inject(RendererInjectionKey)
if (!renderer) {
console.error('Renderer not found')
return
}
2021-04-19 04:27:53 +08:00
const camera = new PerspectiveCamera(props.fov, props.aspect, props.near, props.far)
2021-04-25 03:45:57 +08:00
renderer.camera = camera
2021-04-19 04:27:53 +08:00
bindProp(props, 'position', camera)
2020-09-19 23:52:02 +08:00
2021-04-24 05:29:56 +08:00
if (props.lookAt) camera.lookAt(props.lookAt.x ?? 0, props.lookAt.y, props.lookAt.z)
watch(() => props.lookAt, (v) => { camera.lookAt(v.x ?? 0, v.y, v.z) }, { deep: true })
2021-03-05 16:10:20 +08:00
2021-05-06 03:40:23 +08:00
bindObjectProp(props, 'props', camera, true, cameraSetProp);
['aspect', 'far', 'fov', 'near'].forEach(p => {
2021-04-19 07:53:25 +08:00
// @ts-ignore
2021-04-19 04:27:53 +08:00
watch(() => props[p], (value) => {
2021-05-06 03:40:23 +08:00
cameraSetProp(camera, p, value)
2021-04-16 09:19:50 +08:00
})
})
2020-09-19 23:52:02 +08:00
2021-04-25 03:45:57 +08:00
return { renderer, camera }
2020-09-14 22:57:11 +08:00
},
2021-03-07 22:14:34 +08:00
__hmrId: 'PerspectiveCamera',
2021-04-16 09:19:50 +08:00
})