2021-03-13 05:58:41 +08:00
|
|
|
import { ShaderMaterial } from 'three';
|
2021-03-12 22:55:30 +08:00
|
|
|
import { watch } from 'vue';
|
2021-03-12 22:29:10 +08:00
|
|
|
import { propsValues, defaultFragmentShader, defaultVertexShader } from '../tools.js';
|
|
|
|
|
2020-09-17 16:16:02 +08:00
|
|
|
export default {
|
2020-10-03 18:34:14 +08:00
|
|
|
inject: ['three', 'mesh'],
|
2020-09-17 16:16:02 +08:00
|
|
|
props: {
|
2021-03-13 05:58:41 +08:00
|
|
|
uniforms: { type: Object, default: () => {} },
|
2021-03-12 22:29:10 +08:00
|
|
|
vertexShader: { type: String, default: defaultVertexShader },
|
|
|
|
fragmentShader: { type: String, default: defaultFragmentShader },
|
2020-09-17 16:16:02 +08:00
|
|
|
},
|
2021-03-07 06:14:22 +08:00
|
|
|
created() {
|
2020-10-03 18:34:14 +08:00
|
|
|
this.createMaterial();
|
2021-03-13 05:58:41 +08:00
|
|
|
['vertexShader', 'fragmentShader'].forEach(p => {
|
|
|
|
watch(() => this[p], () => {
|
|
|
|
// recreate material if we change either shader
|
|
|
|
this.material.dispose();
|
|
|
|
this.createMaterial();
|
|
|
|
});
|
|
|
|
});
|
2020-09-17 16:16:02 +08:00
|
|
|
},
|
|
|
|
unmounted() {
|
|
|
|
this.material.dispose();
|
|
|
|
},
|
2021-03-12 22:29:10 +08:00
|
|
|
methods: {
|
|
|
|
createMaterial() {
|
|
|
|
this.material = new ShaderMaterial(propsValues(this.$props));
|
2021-03-13 05:58:41 +08:00
|
|
|
this.mesh.setMaterial(this.material);
|
2021-03-12 22:55:30 +08:00
|
|
|
},
|
2021-03-12 22:29:10 +08:00
|
|
|
},
|
2020-09-17 16:16:02 +08:00
|
|
|
render() {
|
|
|
|
return [];
|
|
|
|
},
|
2020-09-19 23:00:29 +08:00
|
|
|
__hmrId: 'ShaderMaterial',
|
2020-09-17 16:16:02 +08:00
|
|
|
};
|