From 3f40a59b73d078ea251f553265f0943af313b536 Mon Sep 17 00:00:00 2001 From: Sander Moolin Date: Mon, 15 Mar 2021 00:33:27 -0400 Subject: [PATCH] Shader texture proof of concept --- src/materials/ShaderMaterial.js | 7 ++++++- src/materials/Texture.js | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/materials/ShaderMaterial.js b/src/materials/ShaderMaterial.js index 5222d94..1677b10 100644 --- a/src/materials/ShaderMaterial.js +++ b/src/materials/ShaderMaterial.js @@ -9,6 +9,11 @@ export default { vertexShader: { type: String, default: defaultVertexShader }, fragmentShader: { type: String, default: defaultFragmentShader }, }, + provide() { + return { + material: this, + }; + }, created() { this.createMaterial(); ['vertexShader', 'fragmentShader'].forEach(p => { @@ -29,7 +34,7 @@ export default { }, }, render() { - return []; + return this.$slots.default ? this.$slots.default() : []; }, __hmrId: 'ShaderMaterial', }; diff --git a/src/materials/Texture.js b/src/materials/Texture.js index 9dd0ff3..9de7d00 100644 --- a/src/materials/Texture.js +++ b/src/materials/Texture.js @@ -25,7 +25,7 @@ export default { watch(() => this.src, this.refreshTexture); }, unmounted() { - this.material.setTexture(null, this.id); + if (this.material && this.material.setTexture) this.material.setTexture(null, this.id); this.texture.dispose(); }, methods: { @@ -38,7 +38,11 @@ export default { }, refreshTexture() { this.createTexture(); - this.material.setTexture(this.texture, this.id); + if (this.material && this.material.setTexture) { this.material.setTexture(this.texture, this.id); } + else if (this.material && this.material.material.type === "ShaderMaterial") { + const id = this.id === 'map' ? this.src.replace(/\..*/, '') : this.id; + this.material.uniforms[id] = { value: this.texture }; + } }, onLoaded() { if (this.onLoad) this.onLoad();