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

47 lines
1.4 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 { OrthographicCamera } from 'three'
import { bindProp } from '../tools'
import Camera 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-10-08 05:38:39 +08:00
export default defineComponent({
extends: Camera,
2021-03-07 20:22:54 +08:00
name: 'OrthographicCamera',
2020-10-08 05:38:39 +08:00
props: {
left: { type: Number, default: -1 },
right: { type: Number, default: 1 },
top: { type: Number, default: 1 },
bottom: { type: Number, default: -1 },
near: { type: Number, default: 0.1 },
far: { type: Number, default: 2000 },
zoom: { type: Number, default: 1 },
2021-04-24 05:29:56 +08:00
position: { type: Object as PropType<Vector3PropInterface>, default: () => ({ x: 0, y: 0, z: 0 }) },
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-16 09:19:50 +08:00
const camera = new OrthographicCamera(props.left, props.right, props.top, props.bottom, props.near, props.far)
2021-04-25 03:45:57 +08:00
renderer.camera = camera
2021-04-19 04:27:53 +08:00
2021-04-22 06:32:46 +08:00
bindProp(props, 'position', camera)
2020-10-08 05:38:39 +08:00
2021-04-16 10:12:41 +08:00
const watchProps = ['left', 'right', 'top', 'bottom', 'near', 'far', 'zoom']
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-10-08 05:38:39 +08:00
2021-04-25 03:45:57 +08:00
return { renderer, camera }
2020-10-08 05:38:39 +08:00
},
2021-03-07 22:14:34 +08:00
__hmrId: 'OrthographicCamera',
2021-04-16 09:19:50 +08:00
})