From 65eb3d945013940a91b3bff8d93c72581dc75729 Mon Sep 17 00:00:00 2001 From: Kevin Levron Date: Mon, 19 Apr 2021 19:57:10 +0200 Subject: [PATCH] wip: lights --- src/lights/Light.ts | 42 +++++++++++++++++++------------------ src/lights/RectAreaLight.ts | 5 ++--- src/lights/SpotLight.ts | 5 ++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/lights/Light.ts b/src/lights/Light.ts index 6227af0..89192e3 100644 --- a/src/lights/Light.ts +++ b/src/lights/Light.ts @@ -1,14 +1,12 @@ -import { DirectionalLight, Light, SpotLight } from 'three' +import { DirectionalLight, Light, LightShadow, SpotLight } from 'three' import { defineComponent, watch } from 'vue' import Object3D from '../core/Object3D' import { bindProp, setFromProp } from '../tools' -interface LightInterface { +interface LightSetupInterface { light?: Light } -type LightWithTarget = SpotLight | DirectionalLight - export default defineComponent({ extends: Object3D, name: 'Light', @@ -19,40 +17,44 @@ export default defineComponent({ shadowMapSize: { type: Object, default: () => ({ x: 512, y: 512 }) }, shadowCamera: { type: Object, default: () => ({}) }, }, - setup(): LightInterface { + setup(): LightSetupInterface { return {} }, unmounted() { - const light = this.light as LightWithTarget - if (light && light.target) this.removeFromParent(light.target) + if (this.light instanceof SpotLight || this.light instanceof DirectionalLight) { + this.removeFromParent(this.light.target) + } }, methods: { initLight(light: Light) { this.light = light - const lightWithTarget = light as LightWithTarget - if (lightWithTarget.target) { - bindProp(this, 'target', lightWithTarget.target, 'position') - } - - if (this.light?.shadow) { - this.light.castShadow = this.castShadow - setFromProp(this.light.shadow.mapSize, this.shadowMapSize) - setFromProp(this.light.shadow.camera, this.shadowCamera) + if (light instanceof LightShadow) { + light.castShadow = this.castShadow + // @ts-ignore + setFromProp(light.shadow.mapSize, this.shadowMapSize) + // @ts-ignore + setFromProp(light.shadow.camera, this.shadowCamera) } ['color', 'intensity', 'castShadow'].forEach(p => { - watch(() => this[p], () => { + // @ts-ignore + watch(() => this[p], (value) => { if (p === 'color') { - light.color.set(this.color) + light.color.set(value) } else { + // @ts-ignore light[p] = this[p] } }) }) - this.initObject3D(this.light) - if (lightWithTarget.target) this.addToParent(lightWithTarget.target) + this.initObject3D(light) + + if (light instanceof SpotLight || light instanceof DirectionalLight) { + bindProp(this, 'target', light.target, 'position') + this.addToParent(light.target) + } }, }, __hmrId: 'Light', diff --git a/src/lights/RectAreaLight.ts b/src/lights/RectAreaLight.ts index 12dc94e..5347b78 100644 --- a/src/lights/RectAreaLight.ts +++ b/src/lights/RectAreaLight.ts @@ -17,9 +17,8 @@ export default defineComponent({ const watchProps = ['width', 'height'] watchProps.forEach(p => { - watch(() => this[p], () => { - light[p] = this[p] - }) + // @ts-ignore + watch(() => this[p], () => { light[p] = this[p] }) }) if (this.helper) { diff --git a/src/lights/SpotLight.ts b/src/lights/SpotLight.ts index 4a6b959..e24475a 100644 --- a/src/lights/SpotLight.ts +++ b/src/lights/SpotLight.ts @@ -16,9 +16,8 @@ export default defineComponent({ const watchProps = ['angle', 'decay', 'distance', 'penumbra'] watchProps.forEach(p => { - watch(() => this[p], () => { - light[p] = this[p] - }) + // @ts-ignore + watch(() => this[p], () => { light[p] = this[p] }) }) this.initLight(light)