From a7a4f157344f763623b71eb182957047a173f612 Mon Sep 17 00:00:00 2001 From: Kevin Levron Date: Sun, 18 Apr 2021 22:58:28 +0200 Subject: [PATCH] wip: lights --- src/lights/AmbientLight.js | 12 ------- src/lights/AmbientLight.ts | 11 +++++++ src/lights/DirectionalLight.js | 15 --------- src/lights/DirectionalLight.ts | 14 ++++++++ src/lights/HemisphereLight.js | 16 --------- src/lights/HemisphereLight.ts | 16 +++++++++ src/lights/Light.js | 47 --------------------------- src/lights/Light.ts | 59 ++++++++++++++++++++++++++++++++++ src/lights/PointLight.js | 22 ------------- src/lights/PointLight.ts | 15 +++++++++ src/lights/RectAreaLight.js | 32 ------------------ src/lights/RectAreaLight.ts | 33 +++++++++++++++++++ src/lights/SpotLight.js | 24 -------------- src/lights/SpotLight.ts | 27 ++++++++++++++++ src/lights/index.js | 6 ---- src/lights/index.ts | 6 ++++ 16 files changed, 181 insertions(+), 174 deletions(-) delete mode 100644 src/lights/AmbientLight.js create mode 100644 src/lights/AmbientLight.ts delete mode 100644 src/lights/DirectionalLight.js create mode 100644 src/lights/DirectionalLight.ts delete mode 100644 src/lights/HemisphereLight.js create mode 100644 src/lights/HemisphereLight.ts delete mode 100644 src/lights/Light.js create mode 100644 src/lights/Light.ts delete mode 100644 src/lights/PointLight.js create mode 100644 src/lights/PointLight.ts delete mode 100644 src/lights/RectAreaLight.js create mode 100644 src/lights/RectAreaLight.ts delete mode 100644 src/lights/SpotLight.js create mode 100644 src/lights/SpotLight.ts delete mode 100644 src/lights/index.js create mode 100644 src/lights/index.ts diff --git a/src/lights/AmbientLight.js b/src/lights/AmbientLight.js deleted file mode 100644 index 4f160cc..0000000 --- a/src/lights/AmbientLight.js +++ /dev/null @@ -1,12 +0,0 @@ -import { defineComponent } from 'vue'; -import { AmbientLight } from 'three'; -import Light from './Light.js'; - -export default defineComponent({ - extends: Light, - created() { - this.light = new AmbientLight(this.color, this.intensity); - this.initLight(); - }, - __hmrId: 'AmbientLight', -}); diff --git a/src/lights/AmbientLight.ts b/src/lights/AmbientLight.ts new file mode 100644 index 0000000..2697759 --- /dev/null +++ b/src/lights/AmbientLight.ts @@ -0,0 +1,11 @@ +import { defineComponent } from 'vue' +import { AmbientLight } from 'three' +import Light from './Light' + +export default defineComponent({ + extends: Light, + created() { + this.initLight(new AmbientLight(this.color, this.intensity)) + }, + __hmrId: 'AmbientLight', +}) diff --git a/src/lights/DirectionalLight.js b/src/lights/DirectionalLight.js deleted file mode 100644 index a738a56..0000000 --- a/src/lights/DirectionalLight.js +++ /dev/null @@ -1,15 +0,0 @@ -import { defineComponent } from 'vue'; -import { DirectionalLight } from 'three'; -import Light from './Light.js'; - -export default defineComponent({ - extends: Light, - props: { - target: Object, - }, - created() { - this.light = new DirectionalLight(this.color, this.intensity); - this.initLight(); - }, - __hmrId: 'DirectionalLight', -}); diff --git a/src/lights/DirectionalLight.ts b/src/lights/DirectionalLight.ts new file mode 100644 index 0000000..3f026e6 --- /dev/null +++ b/src/lights/DirectionalLight.ts @@ -0,0 +1,14 @@ +import { defineComponent } from 'vue' +import { DirectionalLight } from 'three' +import Light from './Light' + +export default defineComponent({ + extends: Light, + props: { + target: { type: Object, default: () => ({ x: 0, y: 0, z: 0 }) }, + }, + created() { + this.initLight(new DirectionalLight(this.color, this.intensity)) + }, + __hmrId: 'DirectionalLight', +}) diff --git a/src/lights/HemisphereLight.js b/src/lights/HemisphereLight.js deleted file mode 100644 index 1a7573a..0000000 --- a/src/lights/HemisphereLight.js +++ /dev/null @@ -1,16 +0,0 @@ -import { defineComponent, watch } from 'vue'; -import { HemisphereLight } from 'three'; -import Light from './Light.js'; - -export default defineComponent({ - extends: Light, - props: { - groundColor: { type: String, default: '#444444' }, - }, - created() { - this.light = new HemisphereLight(this.color, this.groundColor, this.intensity); - watch(() => this.groundColor, (value) => { this.light.groundColor.set(value); }); - this.initLight(); - }, - __hmrId: 'HemisphereLight', -}); diff --git a/src/lights/HemisphereLight.ts b/src/lights/HemisphereLight.ts new file mode 100644 index 0000000..f17269e --- /dev/null +++ b/src/lights/HemisphereLight.ts @@ -0,0 +1,16 @@ +import { defineComponent, watch } from 'vue' +import { HemisphereLight } from 'three' +import Light from './Light' + +export default defineComponent({ + extends: Light, + props: { + groundColor: { type: String, default: '#444444' }, + }, + created() { + const light = new HemisphereLight(this.color, this.groundColor, this.intensity) + watch(() => this.groundColor, (value) => { light.groundColor.set(value) }) + this.initLight(light) + }, + __hmrId: 'HemisphereLight', +}) diff --git a/src/lights/Light.js b/src/lights/Light.js deleted file mode 100644 index 5f5751d..0000000 --- a/src/lights/Light.js +++ /dev/null @@ -1,47 +0,0 @@ -import { defineComponent, watch } from 'vue'; -import Object3D from '../core/Object3D'; -import { bindProp, setFromProp } from '../tools'; - -export default defineComponent({ - extends: Object3D, - name: 'Light', - props: { - color: { type: String, default: '#ffffff' }, - intensity: { type: Number, default: 1 }, - castShadow: { type: Boolean, default: false }, - shadowMapSize: { type: Object, default: { x: 512, y: 512 } }, - shadowCamera: { type: Object, default: {} }, - }, - // can't use setup because it will not be used in sub components - // setup() {}, - unmounted() { - if (this.light.target) this.removeFromParent(this.light.target); - }, - methods: { - initLight() { - if (this.light.target) { - bindProp(this, 'target', this.light.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); - } - - ['color', 'intensity', 'castShadow'].forEach(p => { - watch(() => this[p], () => { - if (p === 'color') { - this.light.color.set(this.color); - } else { - this.light[p] = this[p]; - } - }); - }); - - this.initObject3D(this.light); - if (this.light.target) this.addToParent(this.light.target); - }, - }, - __hmrId: 'Light', -}); diff --git a/src/lights/Light.ts b/src/lights/Light.ts new file mode 100644 index 0000000..6227af0 --- /dev/null +++ b/src/lights/Light.ts @@ -0,0 +1,59 @@ +import { DirectionalLight, Light, SpotLight } from 'three' +import { defineComponent, watch } from 'vue' +import Object3D from '../core/Object3D' +import { bindProp, setFromProp } from '../tools' + +interface LightInterface { + light?: Light +} + +type LightWithTarget = SpotLight | DirectionalLight + +export default defineComponent({ + extends: Object3D, + name: 'Light', + props: { + color: { type: String, default: '#ffffff' }, + intensity: { type: Number, default: 1 }, + castShadow: { type: Boolean, default: false }, + shadowMapSize: { type: Object, default: () => ({ x: 512, y: 512 }) }, + shadowCamera: { type: Object, default: () => ({}) }, + }, + setup(): LightInterface { + return {} + }, + unmounted() { + const light = this.light as LightWithTarget + if (light && light.target) this.removeFromParent(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) + } + + ['color', 'intensity', 'castShadow'].forEach(p => { + watch(() => this[p], () => { + if (p === 'color') { + light.color.set(this.color) + } else { + light[p] = this[p] + } + }) + }) + + this.initObject3D(this.light) + if (lightWithTarget.target) this.addToParent(lightWithTarget.target) + }, + }, + __hmrId: 'Light', +}) diff --git a/src/lights/PointLight.js b/src/lights/PointLight.js deleted file mode 100644 index 532f717..0000000 --- a/src/lights/PointLight.js +++ /dev/null @@ -1,22 +0,0 @@ -import { defineComponent } from 'vue'; -import { PointLight } from 'three'; -import Light from './Light.js'; - -export default defineComponent({ - extends: Light, - props: { - distance: { - type: Number, - default: 0, - }, - decay: { - type: Number, - default: 1, - }, - }, - created() { - this.light = new PointLight(this.color, this.intensity, this.distance, this.decay); - this.initLight(); - }, - __hmrId: 'PointLight', -}); diff --git a/src/lights/PointLight.ts b/src/lights/PointLight.ts new file mode 100644 index 0000000..a79b121 --- /dev/null +++ b/src/lights/PointLight.ts @@ -0,0 +1,15 @@ +import { defineComponent } from 'vue' +import { PointLight } from 'three' +import Light from './Light' + +export default defineComponent({ + extends: Light, + props: { + distance: { type: Number, default: 0 }, + decay: { type: Number, default: 1 }, + }, + created() { + this.initLight(new PointLight(this.color, this.intensity, this.distance, this.decay)) + }, + __hmrId: 'PointLight', +}) diff --git a/src/lights/RectAreaLight.js b/src/lights/RectAreaLight.js deleted file mode 100644 index 4a00ab4..0000000 --- a/src/lights/RectAreaLight.js +++ /dev/null @@ -1,32 +0,0 @@ -import { defineComponent, watch } from 'vue'; -import { RectAreaLight } from 'three'; -import { RectAreaLightUniformsLib } from 'three/examples/jsm/lights/RectAreaLightUniformsLib.js'; -import { RectAreaLightHelper } from 'three/examples/jsm/helpers/RectAreaLightHelper.js'; -import Light from './Light.js'; - -export default defineComponent({ - extends: Light, - props: { - width: { type: Number, default: 10 }, - height: { type: Number, default: 10 }, - helper: Boolean, - }, - created() { - RectAreaLightUniformsLib.init(); - this.light = new RectAreaLight(this.color, this.intensity, this.width, this.height); - - ['width', 'height'].forEach(p => { - watch(() => this[p], () => { - this.light[p] = this[p]; - }); - }); - - if (this.helper) { - this.lightHelper = new RectAreaLightHelper(this.light); - this.light.add(this.lightHelper); - } - - this.initLight(); - }, - __hmrId: 'RectAreaLight', -}); diff --git a/src/lights/RectAreaLight.ts b/src/lights/RectAreaLight.ts new file mode 100644 index 0000000..12dc94e --- /dev/null +++ b/src/lights/RectAreaLight.ts @@ -0,0 +1,33 @@ +import { defineComponent, watch } from 'vue' +import { RectAreaLight } from 'three' +import { RectAreaLightUniformsLib } from 'three/examples/jsm/lights/RectAreaLightUniformsLib.js' +import { RectAreaLightHelper } from 'three/examples/jsm/helpers/RectAreaLightHelper.js' +import Light from './Light' + +export default defineComponent({ + extends: Light, + props: { + width: { type: Number, default: 10 }, + height: { type: Number, default: 10 }, + helper: Boolean, + }, + created() { + RectAreaLightUniformsLib.init() + const light = new RectAreaLight(this.color, this.intensity, this.width, this.height) + + const watchProps = ['width', 'height'] + watchProps.forEach(p => { + watch(() => this[p], () => { + light[p] = this[p] + }) + }) + + if (this.helper) { + const lightHelper = new RectAreaLightHelper(light) + light.add(lightHelper) + } + + this.initLight(light) + }, + __hmrId: 'RectAreaLight', +}) diff --git a/src/lights/SpotLight.js b/src/lights/SpotLight.js deleted file mode 100644 index 6013bad..0000000 --- a/src/lights/SpotLight.js +++ /dev/null @@ -1,24 +0,0 @@ -import { defineComponent, watch } from 'vue'; -import { SpotLight } from 'three'; -import Light from './Light.js'; - -export default defineComponent({ - extends: Light, - props: { - angle: { type: Number, default: Math.PI / 3 }, - decay: { type: Number, default: 1 }, - distance: { type: Number, default: 0 }, - penumbra: { type: Number, default: 0 }, - target: Object, - }, - created() { - this.light = new SpotLight(this.color, this.intensity, this.distance, this.angle, this.penumbra, this.decay); - ['angle', 'decay', 'distance', 'penumbra'].forEach(p => { - watch(() => this[p], () => { - this.light[p] = this[p]; - }); - }); - this.initLight(); - }, - __hmrId: 'SpotLight', -}); diff --git a/src/lights/SpotLight.ts b/src/lights/SpotLight.ts new file mode 100644 index 0000000..4a6b959 --- /dev/null +++ b/src/lights/SpotLight.ts @@ -0,0 +1,27 @@ +import { defineComponent, watch } from 'vue' +import { SpotLight } from 'three' +import Light from './Light' + +export default defineComponent({ + extends: Light, + props: { + angle: { type: Number, default: Math.PI / 3 }, + decay: { type: Number, default: 1 }, + distance: { type: Number, default: 0 }, + penumbra: { type: Number, default: 0 }, + target: Object, + }, + created() { + const light = new SpotLight(this.color, this.intensity, this.distance, this.angle, this.penumbra, this.decay) + + const watchProps = ['angle', 'decay', 'distance', 'penumbra'] + watchProps.forEach(p => { + watch(() => this[p], () => { + light[p] = this[p] + }) + }) + + this.initLight(light) + }, + __hmrId: 'SpotLight', +}) diff --git a/src/lights/index.js b/src/lights/index.js deleted file mode 100644 index 16e6df9..0000000 --- a/src/lights/index.js +++ /dev/null @@ -1,6 +0,0 @@ -export { default as AmbientLight } from './AmbientLight.js'; -export { default as DirectionalLight } from './DirectionalLight.js'; -export { default as HemisphereLight } from './HemisphereLight.js'; -export { default as PointLight } from './PointLight.js'; -export { default as RectAreaLight } from './RectAreaLight.js'; -export { default as SpotLight } from './SpotLight.js'; diff --git a/src/lights/index.ts b/src/lights/index.ts new file mode 100644 index 0000000..11cc7a1 --- /dev/null +++ b/src/lights/index.ts @@ -0,0 +1,6 @@ +export { default as AmbientLight } from './AmbientLight' +export { default as DirectionalLight } from './DirectionalLight' +export { default as HemisphereLight } from './HemisphereLight' +export { default as PointLight } from './PointLight' +export { default as RectAreaLight } from './RectAreaLight' +export { default as SpotLight } from './SpotLight'