1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 20:32:02 +08:00
trois/src/meshes/Mesh.js
2020-09-21 09:15:15 +02:00

64 lines
1.5 KiB
JavaScript

import { Mesh } from 'three';
import { watch } from 'vue';
import useBindProp from '../use/useBindProp.js';
export default {
inject: ['three', 'scene'],
emits: ['ready'],
props: {
materialId: String,
position: Object,
rotation: Object,
scale: Object,
castShadow: {
type: Boolean,
default: false,
},
receiveShadow: {
type: Boolean,
default: false,
},
},
mounted() {
if (this.geometry) this.initMesh();
},
unmounted() {
if (this.geometry) this.geometry.dispose();
if (this.mesh) this.scene.remove(this.mesh);
},
methods: {
initMesh() {
if (!this.material && this.materialId) {
this.material = this.three.materials[this.materialId];
}
this.mesh = new Mesh(this.geometry, this.material);
useBindProp(this, 'position', this.mesh.position);
useBindProp(this, 'rotation', this.mesh.rotation);
useBindProp(this, 'scale', this.mesh.scale);
['castShadow', 'receiveShadow'].forEach(p => {
this.mesh[p] = this[p];
watch(() => this[p], () => { this.mesh[p] = this[p]; });
});
watch(() => this.materialId, () => {
this.mesh.material = this.three.materials[this.materialId];
});
this.scene.add(this.mesh);
this.$emit('ready');
},
refreshGeometry() {
const oldGeo = this.geometry;
this.createGeometry();
this.mesh.geometry = this.geometry;
oldGeo.dispose();
},
},
render() {
return [];
},
__hmrId: 'Mesh',
};