1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-27 22:12:01 +08:00

wip: lights

This commit is contained in:
Kevin Levron 2021-04-19 19:57:10 +02:00
parent 8adb685c56
commit 65eb3d9450
3 changed files with 26 additions and 26 deletions

View File

@ -1,14 +1,12 @@
import { DirectionalLight, Light, SpotLight } from 'three' import { DirectionalLight, Light, LightShadow, SpotLight } from 'three'
import { defineComponent, watch } from 'vue' import { defineComponent, watch } from 'vue'
import Object3D from '../core/Object3D' import Object3D from '../core/Object3D'
import { bindProp, setFromProp } from '../tools' import { bindProp, setFromProp } from '../tools'
interface LightInterface { interface LightSetupInterface {
light?: Light light?: Light
} }
type LightWithTarget = SpotLight | DirectionalLight
export default defineComponent({ export default defineComponent({
extends: Object3D, extends: Object3D,
name: 'Light', name: 'Light',
@ -19,40 +17,44 @@ export default defineComponent({
shadowMapSize: { type: Object, default: () => ({ x: 512, y: 512 }) }, shadowMapSize: { type: Object, default: () => ({ x: 512, y: 512 }) },
shadowCamera: { type: Object, default: () => ({}) }, shadowCamera: { type: Object, default: () => ({}) },
}, },
setup(): LightInterface { setup(): LightSetupInterface {
return {} return {}
}, },
unmounted() { unmounted() {
const light = this.light as LightWithTarget if (this.light instanceof SpotLight || this.light instanceof DirectionalLight) {
if (light && light.target) this.removeFromParent(light.target) this.removeFromParent(this.light.target)
}
}, },
methods: { methods: {
initLight(light: Light) { initLight(light: Light) {
this.light = light this.light = light
const lightWithTarget = light as LightWithTarget if (light instanceof LightShadow) {
if (lightWithTarget.target) { light.castShadow = this.castShadow
bindProp(this, 'target', lightWithTarget.target, 'position') // @ts-ignore
} setFromProp(light.shadow.mapSize, this.shadowMapSize)
// @ts-ignore
if (this.light?.shadow) { setFromProp(light.shadow.camera, this.shadowCamera)
this.light.castShadow = this.castShadow
setFromProp(this.light.shadow.mapSize, this.shadowMapSize)
setFromProp(this.light.shadow.camera, this.shadowCamera)
} }
['color', 'intensity', 'castShadow'].forEach(p => { ['color', 'intensity', 'castShadow'].forEach(p => {
watch(() => this[p], () => { // @ts-ignore
watch(() => this[p], (value) => {
if (p === 'color') { if (p === 'color') {
light.color.set(this.color) light.color.set(value)
} else { } else {
// @ts-ignore
light[p] = this[p] light[p] = this[p]
} }
}) })
}) })
this.initObject3D(this.light) this.initObject3D(light)
if (lightWithTarget.target) this.addToParent(lightWithTarget.target)
if (light instanceof SpotLight || light instanceof DirectionalLight) {
bindProp(this, 'target', light.target, 'position')
this.addToParent(light.target)
}
}, },
}, },
__hmrId: 'Light', __hmrId: 'Light',

View File

@ -17,9 +17,8 @@ export default defineComponent({
const watchProps = ['width', 'height'] const watchProps = ['width', 'height']
watchProps.forEach(p => { watchProps.forEach(p => {
watch(() => this[p], () => { // @ts-ignore
light[p] = this[p] watch(() => this[p], () => { light[p] = this[p] })
})
}) })
if (this.helper) { if (this.helper) {

View File

@ -16,9 +16,8 @@ export default defineComponent({
const watchProps = ['angle', 'decay', 'distance', 'penumbra'] const watchProps = ['angle', 'decay', 'distance', 'penumbra']
watchProps.forEach(p => { watchProps.forEach(p => {
watch(() => this[p], () => { // @ts-ignore
light[p] = this[p] watch(() => this[p], () => { light[p] = this[p] })
})
}) })
this.initLight(light) this.initLight(light)