bindTexture: Texture previously bound to TEXTURE_2D cannot be bound now to TEXTURE_CUBE_MAP

I’m creating a custom PBRShader and I’m looking to use the output of PMREMGenerator to do the IBL. My uniforms are set up as follows:

const uniforms = THREE.UniformsUtils.merge([
	THREE.UniformsLib['normalmap'],
	THREE.UniformsLib['lights'],
	{
		normalScale: { value: new THREE.Vector2(1, 1) },
		albedoFactor: { value: new THREE.Vector3(1, 1, 1) },
		albedoMap: { value: null },
		metallicFactor: { value: 1.0 },
		roughnessFactor: { value: 1.0 },
		metallicRoughnessMap: { value: null },
		eye: { value: new THREE.Vector3() },
		radianceMap: { value: null },
		// I'm not sure where this should be defined/updated
		receiveShadow: { value: true },
	}
])

I load the HDR for an equirectangular map using RGBELoader and PMREM generator.

const pmremGenerator = new PMREMGenerator(this.viewer.renderer);

            let hdrCubeRenderTarget: THREE.WebGLRenderTarget;
            const hdrCubeMap = new RGBELoader()
                .setDataType(THREE.UnsignedByteType)
                .load("http://localhost:8080/studio-low.hdr", texture => {
                    console.log("HDR Loaded")
                    hdrCubeRenderTarget = pmremGenerator.fromEquirectangular(texture);
                    hdrCubeMap.magFilter = THREE.LinearFilter;
                    hdrCubeMap.needsUpdate = true;
                    console.log(hdrCubeRenderTarget);

                    this.viewer!.scene.environment = hdrCubeRenderTarget.texture;
                    this.viewer!.scene.background = hdrCubeRenderTarget.texture;
                    console.log("Binding HDR")
                    m.uniforms.radianceMap.value = hdrCubeRenderTarget.texture;

                    texture.dispose()
                }, progress => {
                    console.log(progress)
                }, error => {
                    console.error(error)
                })

            pmremGenerator.compileEquirectangularShader();

When it goes to render I get bindTexture: Texture previously bound to TEXTURE_2D cannot be bound now to TEXTURE_CUBE_MAP. I haven’t bound any textures to radianceMap, it’s value is just set to null by default. I’m not sure what I’m doing wrong. Any help would be greatly appreciated.

Not sure I understand since you are doing this:

Is the output of PMREMGenerator a 2D texture?

EDIT: it looks like it’s a cubemap layed out as a 2D texture. How do I sample this properly?

I figured it out. For those who want to know you can add this code to your fragment shader:

#ifndef inputEncoding
#define inputEncoding 2
#endif

vec4 inputTexelToLinear( vec4 value ) {
	if ( inputEncoding == 0 ) {
		return value;
	} else if ( inputEncoding == 1 ) {
		return sRGBToLinear( value );
	} else if ( inputEncoding == 2 ) {
		return RGBEToLinear( value );
	} else if ( inputEncoding == 3 ) {
		return RGBMToLinear( value, 7.0 );
	} else if ( inputEncoding == 4 ) {
		return RGBMToLinear( value, 16.0 );
	} else if ( inputEncoding == 5 ) {
		return RGBDToLinear( value, 256.0 );
	} else {
		return GammaToLinear( value, 2.2 );
	}
}
vec4 envMapTexelToLinear( vec4 color ) {
	return inputTexelToLinear( color );
}

#define ENVMAP_TYPE_CUBE_UV
#include <cube_uv_reflection_fragment>

This will give you access to textureCubeUV which you can use to sample the texture generated by PMREMGenerator. You may need to #include <common> but I’m not sure.