Skip to content

Commit 44a0980

Browse files
authored
Merge pull request #46 from AryehMischel/update-compatibility-with-threejs
Update compatibility with threejs
2 parents e4def4c + d1d1da7 commit 44a0980

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ AFRAME.registerComponent("cubemap", {
3838

3939
// A Cubemap can be rendered as a mesh composed of a BoxBufferGeometry and
4040
// ShaderMaterial. EdgeLength will scale the mesh
41-
this.geometry = new THREE.BoxBufferGeometry(1, 1, 1);
41+
this.geometry = new THREE.BoxGeometry(1, 1, 1);
4242

4343
// Now for the ShaderMaterial.
4444
const shader = THREE.ShaderLib["cube"];
@@ -53,16 +53,26 @@ AFRAME.registerComponent("cubemap", {
5353
side: THREE.BackSide,
5454
transparent: true
5555
}).clone();
56+
57+
// Starting in Three.js v146, `envMap` changed to `tCube`.
58+
// This variable helps us keep track of the name across Three.js versions.
59+
// https://github.com/mrdoob/three.js/wiki/Migration-Guide#145--146
60+
this.envMapUniformName = this.material.uniforms["envMap"] ? "envMap" : "tCube";
61+
62+
5663
// Threejs seems to have removed the 'tCube' uniform.
5764
// Workaround from: https://stackoverflow.com/a/59454999/6591491
65+
5866
Object.defineProperty(this.material, "envMap", {
5967
get: function () {
60-
return this.uniforms.envMap.value;
68+
return this.uniforms.envMap ? this.uniforms.envMap.value : this.uniforms.tCube.value;
6169
},
6270
});
71+
6372
// A dummy texture is needed (otherwise the shader will be invalid and spew
6473
// a million errors)
65-
this.material.uniforms["envMap"].value = new THREE.Texture();
74+
this.material.uniforms[this.envMapUniformName].value = new THREE.Texture();
75+
6676
this.loader = new THREE.CubeTextureLoader();
6777

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

137147
// 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;
148+
const oldTexture = this.material.uniforms[this.envMapUniformName].value;
149+
this.material.uniforms[this.envMapUniformName].value = texture;
140150
if (oldTexture) {
141151
oldTexture.dispose();
142152
}
@@ -153,8 +163,8 @@ AFRAME.registerComponent("cubemap", {
153163
*/
154164
remove: function () {
155165
this.geometry.dispose();
156-
this.material.uniforms["envMap"].value.dispose();
166+
this.material.uniforms[this.envMapUniformName].value.dispose();
157167
this.material.dispose();
158168
this.el.removeObject3D("cubemap");
159169
},
160-
});
170+
});

0 commit comments

Comments
 (0)