1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-25 04:42:01 +08:00
trois/src/meshes/Mesh.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-09-19 17:24:03 +08:00
import { watch } from 'vue';
2020-09-14 22:57:11 +08:00
import { Mesh } from 'three';
import { setFromProp } from '../tools.js';
export default {
inject: ['three', 'scene'],
2020-09-19 03:52:39 +08:00
emits: ['ready'],
2020-09-14 22:57:11 +08:00
props: {
2020-09-19 17:24:03 +08:00
materialId: String,
2020-09-14 22:57:11 +08:00
position: Object,
rotation: Object,
scale: Object,
2020-09-15 17:48:29 +08:00
castShadow: {
type: Boolean,
default: false,
},
receiveShadow: {
type: Boolean,
default: false,
},
2020-09-14 22:57:11 +08:00
},
mounted() {
2020-09-18 04:37:19 +08:00
if (this.geometry) this.initMesh();
2020-09-14 22:57:11 +08:00
},
2020-09-16 01:50:51 +08:00
unmounted() {
2020-09-18 04:37:19 +08:00
if (this.geometry) this.geometry.dispose();
},
methods: {
initMesh() {
2020-09-19 17:24:03 +08:00
if (!this.material && this.materialId) {
this.material = this.three.materials[this.materialId];
}
this.mesh = new Mesh(this.geometry, this.material);
this.updateMesh();
this.scene.add(this.mesh);
this.addWatchers();
this.$emit('ready');
},
addWatchers() {
['position', 'rotation', 'scale'].forEach(p => {
watch(() => this[p], () => {
setFromProp(this.mesh[p], this[p]);
}, { deep: true });
});
},
updateMesh() {
2020-09-18 04:37:19 +08:00
setFromProp(this.mesh.position, this.position);
setFromProp(this.mesh.rotation, this.rotation);
setFromProp(this.mesh.scale, this.scale);
this.mesh.castShadow = this.castShadow;
this.mesh.receiveShadow = this.receiveShadow;
},
2020-09-16 01:50:51 +08:00
},
2020-09-14 22:57:11 +08:00
render() {
return [];
},
};