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

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-09-14 22:57:11 +08:00
import { Mesh } from 'three';
2020-09-20 02:29:53 +08:00
import { watch } from 'vue';
2020-09-19 18:16:16 +08:00
import useBindProp from '../use/useBindProp.js';
2020-09-14 22:57:11 +08:00
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();
if (this.mesh) this.scene.remove(this.mesh);
2020-09-18 04:37:19 +08:00
},
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);
2020-09-19 18:16:16 +08:00
useBindProp(this, 'position', this.mesh.position);
useBindProp(this, 'rotation', this.mesh.rotation);
useBindProp(this, 'scale', this.mesh.scale);
2020-09-18 04:37:19 +08:00
this.mesh.castShadow = this.castShadow;
this.mesh.receiveShadow = this.receiveShadow;
2020-09-20 02:29:53 +08:00
['castShadow', 'receiveShadow'].forEach(p => {
watch(() => this[p], () => { this.mesh[p] = this[p]; });
});
2020-09-19 18:16:16 +08:00
this.scene.add(this.mesh);
this.$emit('ready');
2020-09-18 04:37:19 +08:00
},
2020-09-20 03:42:33 +08:00
refreshGeometry() {
const oldGeo = this.geometry;
this.createGeometry();
this.mesh.geometry = this.geometry;
oldGeo.dispose();
},
2020-09-16 01:50:51 +08:00
},
2020-09-14 22:57:11 +08:00
render() {
return [];
},
2020-09-19 22:40:58 +08:00
__hmrId: 'Mesh',
2020-09-14 22:57:11 +08:00
};