2021-04-16 09:19:50 +08:00
|
|
|
import { defineComponent, watch } from 'vue'
|
|
|
|
import { PerspectiveCamera } from 'three'
|
|
|
|
import { bindProp } from '../tools'
|
|
|
|
import Camera from './Camera'
|
2020-09-14 22:57:11 +08:00
|
|
|
|
2021-04-04 04:19:57 +08:00
|
|
|
export default defineComponent({
|
2021-03-22 21:30:25 +08:00
|
|
|
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-16 09:19:50 +08:00
|
|
|
position: { type: Object, default: () => ({ x: 0, y: 0, z: 0 }) },
|
2021-03-07 18:45:25 +08:00
|
|
|
lookAt: { type: Object, default: null },
|
2020-09-18 22:28:17 +08:00
|
|
|
},
|
2021-04-16 09:19:50 +08:00
|
|
|
setup(props) {
|
2021-04-19 04:27:53 +08:00
|
|
|
const camera = new PerspectiveCamera(props.fov, props.aspect, props.near, props.far)
|
|
|
|
|
|
|
|
bindProp(props, 'position', camera)
|
2020-09-19 23:52:02 +08:00
|
|
|
|
2021-04-16 09:19:50 +08:00
|
|
|
// TODO : fix lookAt x
|
2021-04-19 04:27:53 +08:00
|
|
|
if (props.lookAt) camera.lookAt(props.lookAt.x, props.lookAt.y, props.lookAt.z)
|
|
|
|
watch(() => props.lookAt, (v) => { camera.lookAt(v.x, v.y, v.z) }, { deep: true })
|
2021-03-05 16:10:20 +08:00
|
|
|
|
2021-04-16 09:19:50 +08:00
|
|
|
const watchProps = ['aspect', 'far', 'fov', 'near']
|
|
|
|
watchProps.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-04-19 07:53:25 +08:00
|
|
|
// @ts-ignore
|
2021-04-19 04:27:53 +08:00
|
|
|
camera[p] = value
|
|
|
|
camera.updateProjectionMatrix()
|
2021-04-16 09:19:50 +08:00
|
|
|
})
|
|
|
|
})
|
2020-09-19 23:52:02 +08:00
|
|
|
|
2021-04-19 04:27:53 +08:00
|
|
|
return { camera }
|
|
|
|
},
|
|
|
|
created() {
|
2021-04-16 09:19:50 +08:00
|
|
|
this.three.camera = this.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
|
|
|
})
|