1

PREMISE:

I'm trying to create a static scene that has a fixed image background and a geometry in front of it. Since the scene is static, I don't need and envMap.

I created two scenes and cameras (one for the background and one for the geometries) as suggested in this SO question, and this demo, and updated the procedure to consider that THREE.ImageUtils.loadTexture() is deprecated. Here is the working code:

// load the background texture
var loader = new THREE.TextureLoader();
texture = loader.load('path_to_image.jpg');
var backgroundMesh = new THREE.Mesh(
    new THREE.PlaneGeometry(2, 2, 0),
    new THREE.MeshBasicMaterial({
        map: texture
    })
);
backgroundMesh.material.depthTest = false;
backgroundMesh.material.depthWrite = false;

// create your background scene
backgroundScene = new THREE.Scene();
backgroundCamera = new THREE.Camera();
backgroundScene.add( backgroundCamera );
backgroundScene.add( backgroundMesh );

The variables backgroundScene and backgroundCamera are global and the following procedure is called inside the init() function. All scenes and cameras are later rendered using:

renderer.autoClear = false;
renderer.clear();
renderer.render( backgroundScene , backgroundCamera);
renderer.render(scene, camera);

PROBLEM:

I implemented an event listener that is supposed to change the background image and geometry when a button is pressed, however this is not working.

I thought that loading the new texture and changing the material property of the backgroundScene variable, clearing the renderer and rendering the scene again would do the job. Here is the code:

var loader = new THREE.TextureLoader();
var texture = loader.load('path_to_new_image.jpg');
console.debug(texture);
console.debug(backgroundScene.children[1].material.map);
backgroundScene.children[1].material.map = texture;
console.debug(backgroundScene.children[1].material.map);

renderer.clear();
renderer.render( backgroundScene , backgroundCamera );
renderer.render(scene, camera);

The console.debug() show me that the new texture is actually loaded and the backgroundScene material is changed accordingly.

However, while the geometries are rendered fine I am left with a blank background and get the following error: [.Offscreen-For-WebGL-0x364ad7e56700]RENDER WARNING: there is no texture bound to the unit 0.

Any ideas of what is going on? Thanks for your help!

Community
  • 1
  • 1
Matteo
  • 7,924
  • 24
  • 84
  • 129

1 Answers1

1

you will need to call object.material.needsUpdate = true; for the change to take effect (see here). When the map-property of the material is changed, three.js needs to re-setup the texture-binding, which is skipped unless the needsUpdate-flag is set.

Alternatively, if you just change the material.map.image-property it should work without that extra-step.

xxx
  • 1,153
  • 1
  • 11
  • 23
Martin Schuhfuß
  • 6,814
  • 1
  • 36
  • 44