Texture-assignment fails after first call of "renderer.render()" #182

Closed
domme opened this Issue Apr 20, 2011 · 3 comments

2 participants

@domme

Hi,

I've encountered a weird problem when trying to assign a texture to a material of a mesh after it's creation and after the renderer.render() was called the first time.

Here are the two versions of what I thing should work BOTH:

//Works fine:
function render()
{
       if( firstTime )
       {
                meshes[ 0 ].materials[ 0 ].map = THREE.ImageUtils.loadTexture( "disturb.jpg", new THREE.UVMapping() );
                firstTime = false;
        }
        renderer.render( scene, camera );
}

//doesn't work:
function render()
{ 
       renderer.render( scene, camera );
       if( firstTime )
       {
                meshes[ 0 ].materials[ 0 ].map = THREE.ImageUtils.loadTexture( "disturb.jpg", new THREE.UVMapping() );
                firstTime = false;
        }
}

Obviously the renderer (WebGLRenderer in this case) does some kind of texture-caching and maybe doesn't account for new textures after the first gathering of all textures in the scene.
If so: Is there a "correct" way to implement texture-changes during runtime?

@pyrotechnick

I believe what you're looking for is THREE::Texture::needsUpdate

Definition - https://github.com/mrdoob/three.js/blob/master/src/materials/Texture.js#L19

Usage - https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLRenderer.js#L4585

The webgl_materials_video example is one of the best references I can provide you with since it involves updating textures each frame; that is, well after the renderer has been initialised.

https://github.com/mrdoob/three.js/blob/master/examples/webgl_materials_video.html#L280

Let me know if you're still struggling after digesting what you find at the links above otherwise post a video or link of it working :)

@domme

Texture.needsUpdate = true is already being set in the loadTexture() function itself (or more correctly: in the callback function):

https://github.com/mrdoob/three.js/blob/master/src/extras/ImageUtils.js#L12

but manually setting this flag after loading (just to be save) also doesn't solve the issue...

I will have a look at the example you proposed - sounds like a good place to investigate further ;)

@domme

Ok... it seems the trick is to first assign a dummy-texture to the material.
If a material's mesh-field is null during the first render()-command, subsequent texture-assignments won't work.

@domme domme closed this Apr 20, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment