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

improve hmr

This commit is contained in:
Kevin Levron 2020-09-19 18:54:50 +02:00
parent 9250eca910
commit fd4807a6bb
6 changed files with 49 additions and 11 deletions

View File

@ -22,8 +22,10 @@ Thanks to VueJS/ViteJS, **TroisJS use watchers and HMR to update ThreeJS objects
- [ ] HMR - [ ] HMR
- [x] PerspectiveCamera : aspect, far, fov, near, position - [x] PerspectiveCamera : aspect, far, fov, near, position
- [x] Light : position - [x] Light : castShadow, color, intensity, position, target
- [x] SpotLight : angle, decay, distance, penumbra
- [x] Material : color, depthTest, depthWrite, fog, opacity, transparent - [x] Material : color, depthTest, depthWrite, fog, opacity, transparent
- [x] StandardMaterial : emissive, emissiveIntensity, metalness, roughness
- [x] Mesh : position, rotation, scale - [x] Mesh : position, rotation, scale
- [ ] ... - [ ] ...

View File

@ -1,5 +1,5 @@
import { PerspectiveCamera, Vector3 } from 'three'; import { PerspectiveCamera, Vector3 } from 'three';
import { toRef, watch } from 'vue'; import { watch } from 'vue';
import useBindProp from '../use/useBindProp.js'; import useBindProp from '../use/useBindProp.js';
export default { export default {

View File

@ -3,6 +3,9 @@ import Light from './Light.js';
export default { export default {
extends: Light, extends: Light,
props: {
target: Object,
},
created() { created() {
this.light = new DirectionalLight(this.color, this.intensity); this.light = new DirectionalLight(this.color, this.intensity);
}, },

View File

@ -1,3 +1,5 @@
import { Color } from 'three';
import { watch } from 'vue';
import { setFromProp } from '../tools.js'; import { setFromProp } from '../tools.js';
import useBindProp from '../use/useBindProp.js'; import useBindProp from '../use/useBindProp.js';
@ -22,11 +24,25 @@ export default {
mounted() { mounted() {
useBindProp(this, 'position', this.light.position); useBindProp(this, 'position', this.light.position);
if (this.light.target) {
useBindProp(this, 'target', this.light.target.position);
}
if (this.light.shadow) { if (this.light.shadow) {
this.light.castShadow = this.castShadow; this.light.castShadow = this.castShadow;
setFromProp(this.light.shadow.mapSize, this.shadowMapSize); setFromProp(this.light.shadow.mapSize, this.shadowMapSize);
} }
['color', 'intensity', 'castShadow'].forEach(p => {
watch(() => this[p], () => {
if (p === 'color') {
this.light.color = new Color(this.color);
} else {
this.light[p] = this[p];
}
});
});
this.scene.add(this.light); this.scene.add(this.light);
if (this.light.target) this.scene.add(this.light.target); if (this.light.target) this.scene.add(this.light.target);
}, },

View File

@ -1,28 +1,35 @@
import { SpotLight } from 'three'; import { SpotLight } from 'three';
import { watch } from 'vue';
import Light from './Light.js'; import Light from './Light.js';
export default { export default {
extends: Light, extends: Light,
props: { props: {
distance: {
type: Number,
default: 0,
},
angle: { angle: {
type: Number, type: Number,
default: Math.PI / 3, default: Math.PI / 3,
}, },
penumbra: {
type: Number,
default: 0,
},
decay: { decay: {
type: Number, type: Number,
default: 1, default: 1,
}, },
distance: {
type: Number,
default: 0,
},
penumbra: {
type: Number,
default: 0,
},
target: Object,
}, },
created() { created() {
this.light = new SpotLight(this.color, this.intensity, this.distance, this.angle, this.penumbra, this.decay); 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];
});
});
}, },
__hmrId: 'SpotLight', __hmrId: 'SpotLight',
}; };

View File

@ -1,4 +1,5 @@
import { MeshStandardMaterial } from 'three'; import { Color, MeshStandardMaterial } from 'three';
import { watch } from 'vue';
import Material from './Material'; import Material from './Material';
export default { export default {
@ -23,6 +24,15 @@ export default {
}, },
created() { created() {
this.material = new MeshStandardMaterial(this.propsValues()); this.material = new MeshStandardMaterial(this.propsValues());
['emissive', 'emissiveIntensity', 'metalness', 'roughness'].forEach(p => {
watch(() => this[p], () => {
if (p === 'emissive') {
this.material.emissive = new Color(this.emissive);
} else {
this.material[p] = this[p];
}
});
});
}, },
__hmrId: 'StandardMaterial', __hmrId: 'StandardMaterial',
}; };