diff --git a/src/materials/ShaderMaterial.js b/src/materials/ShaderMaterial.js index 8c2b2de..a4ed919 100644 --- a/src/materials/ShaderMaterial.js +++ b/src/materials/ShaderMaterial.js @@ -1,18 +1,23 @@ -import { ShaderMaterial } from 'three' +import { ShaderMaterial } from 'three'; import { watch } from 'vue'; import { propsValues, defaultFragmentShader, defaultVertexShader } from '../tools.js'; export default { inject: ['three', 'mesh'], props: { - uniforms: { type: Object, default: () => { } }, + uniforms: { type: Object, default: () => {} }, vertexShader: { type: String, default: defaultVertexShader }, fragmentShader: { type: String, default: defaultFragmentShader }, }, created() { this.createMaterial(); - this.mesh.setMaterial(this.material); - if (this.addWatchers) this.addWatchers(); + ['vertexShader', 'fragmentShader'].forEach(p => { + watch(() => this[p], () => { + // recreate material if we change either shader + this.material.dispose(); + this.createMaterial(); + }); + }); }, unmounted() { this.material.dispose(); @@ -20,22 +25,7 @@ export default { methods: { createMaterial() { this.material = new ShaderMaterial(propsValues(this.$props)); - }, - addWatchers() { - ['uniforms', 'vertexShader', 'fragmentShader'].forEach(p => { - watch(() => this[p], (value) => { - this.material[p] = value; - - if (p === 'vertexShader' || p === 'fragmentShader') { - // recreate material if we change either shader - this.material.dispose(); - this.createMaterial(); - } - }, - // only watch deep on uniforms - { deep: p === 'uniforms' } - ); - }); + this.mesh.setMaterial(this.material); }, }, render() {