From 3f40a59b73d078ea251f553265f0943af313b536 Mon Sep 17 00:00:00 2001 From: Sander Moolin Date: Mon, 15 Mar 2021 00:33:27 -0400 Subject: [PATCH 1/3] 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(); From 99b888b27bb6b27eb171029fab23dc31214583d8 Mon Sep 17 00:00:00 2001 From: Sander Moolin Date: Mon, 15 Mar 2021 11:45:08 -0400 Subject: [PATCH 2/3] - Remove ID from texture, replace with name - Add uniform prop to texture, require use in shaders --- src/materials/Texture.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/materials/Texture.js b/src/materials/Texture.js index 9de7d00..7797089 100644 --- a/src/materials/Texture.js +++ b/src/materials/Texture.js @@ -6,7 +6,8 @@ export default { inject: ['material'], emits: ['loaded'], props: { - id: { type: String, default: 'map' }, + name: { type: String, default: 'map' }, + uniform: { type: String, default: null }, src: String, onLoad: Function, onProgress: Function, @@ -25,7 +26,7 @@ export default { watch(() => this.src, this.refreshTexture); }, unmounted() { - if (this.material && this.material.setTexture) this.material.setTexture(null, this.id); + if (this.material && this.material.setTexture) this.material.setTexture(null, this.name); this.texture.dispose(); }, methods: { @@ -38,10 +39,16 @@ export default { }, refreshTexture() { this.createTexture(); - if (this.material && this.material.setTexture) { this.material.setTexture(this.texture, this.id); } + // handle standard material + if (this.material && this.material.setTexture) { this.material.setTexture(this.texture, this.name); } + // handle shader material 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 }; + // require a `uniform` prop so we know what to call the uniform + if (!this.uniform) { + console.warn('"uniform" prop required to use texture in a shader.') + return + } + this.material.uniforms[this.uniform] = { value: this.texture }; } }, onLoaded() { From e31900d467f5dcdd8c0de40ff567754d7931df9d Mon Sep 17 00:00:00 2001 From: Sander Moolin Date: Mon, 15 Mar 2021 11:46:09 -0400 Subject: [PATCH 3/3] Change `id` to `name` in CubeTexture --- src/materials/CubeTexture.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/materials/CubeTexture.js b/src/materials/CubeTexture.js index 678a4b2..31ddd0e 100644 --- a/src/materials/CubeTexture.js +++ b/src/materials/CubeTexture.js @@ -13,7 +13,7 @@ export default { onLoad: Function, onProgress: Function, onError: Function, - id: { type: String, default: 'envMap' }, + name: { type: String, default: 'envMap' }, refraction: Boolean, // todo: remove ? refractionRatio: { type: Number, default: 0.98 }, @@ -24,7 +24,7 @@ export default { watch(() => this.urls, this.refreshTexture); }, unmounted() { - this.material.setTexture(null, this.id); + this.material.setTexture(null, this.name); this.texture.dispose(); }, methods: { @@ -35,7 +35,7 @@ export default { }, refreshTexture() { this.createTexture(); - this.material.setTexture(this.texture, this.id); + this.material.setTexture(this.texture, this.name); if (this.refraction) { this.texture.mapping = CubeRefractionMapping; this.material.setProp('refractionRatio', this.refractionRatio);