From 802b299e77d9fafdf639a2e41d7bed562e39f4cf Mon Sep 17 00:00:00 2001 From: Kevin Levron Date: Wed, 5 May 2021 21:35:11 +0200 Subject: [PATCH] add camera props --- src/core/Camera.ts | 9 +++++++++ src/core/OrthographicCamera.ts | 12 +++++------- src/core/PerspectiveCamera.ts | 13 ++++++------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/core/Camera.ts b/src/core/Camera.ts index e6df469..129956e 100644 --- a/src/core/Camera.ts +++ b/src/core/Camera.ts @@ -12,6 +12,10 @@ export default defineComponent({ // TODO: eventually extend Object3D // extends: Object3D, + props: { + props: { type: Object, default: () => ({}) }, + }, + // inject: { renderer: RendererInjectionKey as symbol }, // setup(): CameraSetupInterface { @@ -22,3 +26,8 @@ export default defineComponent({ return this.$slots.default ? this.$slots.default() : [] }, }) + +export function cameraSetProp(camera: any, key: string, value: any, updateProjectionMatrix = true) { + camera[key] = value + if (updateProjectionMatrix) camera.updateProjectionMatrix() +} diff --git a/src/core/OrthographicCamera.ts b/src/core/OrthographicCamera.ts index 783eaa8..6034162 100644 --- a/src/core/OrthographicCamera.ts +++ b/src/core/OrthographicCamera.ts @@ -1,7 +1,7 @@ import { defineComponent, inject, PropType, watch } from 'vue' import { OrthographicCamera } from 'three' -import { bindProp } from '../tools' -import Camera from './Camera' +import { bindObjectProp, bindProp } from '../tools' +import Camera, { cameraSetProp } from './Camera' import { Vector3PropInterface } from './Object3D' import { RendererInjectionKey } from './Renderer' @@ -29,14 +29,12 @@ export default defineComponent({ renderer.camera = camera bindProp(props, 'position', camera) + bindObjectProp(props, 'props', camera, true, cameraSetProp); - const watchProps = ['left', 'right', 'top', 'bottom', 'near', 'far', 'zoom'] - watchProps.forEach(p => { + ['left', 'right', 'top', 'bottom', 'near', 'far', 'zoom'].forEach(p => { // @ts-ignore watch(() => props[p], (value) => { - // @ts-ignore - camera[p] = value - camera.updateProjectionMatrix() + cameraSetProp(camera, p, value) }) }) diff --git a/src/core/PerspectiveCamera.ts b/src/core/PerspectiveCamera.ts index 5147a3f..ac9001b 100644 --- a/src/core/PerspectiveCamera.ts +++ b/src/core/PerspectiveCamera.ts @@ -1,7 +1,7 @@ import { defineComponent, inject, PropType, watch } from 'vue' import { PerspectiveCamera } from 'three' -import { bindProp } from '../tools' -import Camera from './Camera' +import { bindObjectProp, bindProp } from '../tools' +import Camera, { cameraSetProp } from './Camera' import { Vector3PropInterface } from './Object3D' import { RendererInjectionKey } from './Renderer' @@ -31,13 +31,12 @@ export default defineComponent({ 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 }) - const watchProps = ['aspect', 'far', 'fov', 'near'] - watchProps.forEach(p => { + bindObjectProp(props, 'props', camera, true, cameraSetProp); + + ['aspect', 'far', 'fov', 'near'].forEach(p => { // @ts-ignore watch(() => props[p], (value) => { - // @ts-ignore - camera[p] = value - camera.updateProjectionMatrix() + cameraSetProp(camera, p, value) }) })