Skip to content

Commit 5985832

Browse files
committed
Created a variable " this.envMapUniformName" to keep track of proper name for uniform across three js versions
I added a simple variable to keep track of the uniform's name across three js/A-Frame versions. This seemed like a quick and easy solution to me. I did some manual testing and I didn't encounter any problems. Everything should now work as intended in A-Frame versions 1.4.0 + : )
1 parent e908d8c commit 5985832

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,27 @@ AFRAME.registerComponent("cubemap", {
5353
side: THREE.BackSide,
5454
transparent: true
5555
}).clone();
56+
57+
//https://github.com/mrdoob/three.js/wiki/Migration-Guide#145--146
58+
//they changed the name of the uniform from tCube to envMap, this variable helps us keep track of the name across three js versions
59+
this.envMapUniformName = this.material.uniforms["envMap"] ? "envMap" : "tCube";
60+
61+
5662
// Threejs seems to have removed the 'tCube' uniform.
5763
// Workaround from: https://stackoverflow.com/a/59454999/6591491
64+
5865
Object.defineProperty(this.material, "envMap", {
5966
get: function () {
60-
return this.uniforms.envMap.value;
67+
return this.uniforms.envMap ? this.uniforms.envMap.value : this.uniforms.tCube.value;
6168
},
6269
});
70+
6371
// A dummy texture is needed (otherwise the shader will be invalid and spew
6472
// a million errors)
65-
this.material.uniforms["envMap"].value = new THREE.Texture();
73+
74+
// this.material.uniforms["envMap"].value = new THREE.Texture();
75+
this.material.uniforms[this.envMapUniformName].value = new THREE.Texture();
76+
6677
this.loader = new THREE.CubeTextureLoader();
6778

6879
// We can create the mesh now and update the material with a texture later on
@@ -135,8 +146,8 @@ AFRAME.registerComponent("cubemap", {
135146
rendererSystem.applyColorCorrection(texture);
136147

137148
// Apply cubemap texture to shader uniforms and dispose of the old texture.
138-
const oldTexture = this.material.uniforms["envMap"].value;
139-
this.material.uniforms["envMap"].value = texture;
149+
const oldTexture = this.material.uniforms[this.envMapUniformName].value;
150+
this.material.uniforms[this.envMapUniformName].value = texture;
140151
if (oldTexture) {
141152
oldTexture.dispose();
142153
}
@@ -153,8 +164,8 @@ AFRAME.registerComponent("cubemap", {
153164
*/
154165
remove: function () {
155166
this.geometry.dispose();
156-
this.material.uniforms["envMap"].value.dispose();
167+
this.material.uniforms[this.envMapUniformName].value.dispose();
157168
this.material.dispose();
158169
this.el.removeObject3D("cubemap");
159170
},
160-
});
171+
});

0 commit comments

Comments
 (0)