Changing Materials at Runtime
Hi guys
I have been having a lot of trouble with materials lately. I checked the forums on this subject and tried everything that was suggested but I'm still not having any luck.
Basically I'm building a game editor and trying to change the material of a mesh in the scene. When the mesh is loaded, I apply a default Phong Material to it, defined as such:
this.defaultMaterial = new THREE.MeshPhongMaterial( { ambient: 0x555555,
color: 0x555555, specular: 0xffffff, shininess: 50, shading: THREE.SmoothShading } );
and implemented like so: ... var content = new THREE.Mesh( geometry, this.defaultMaterial ); content.scale.set( 1, 1, 1); ...
Later I create a new Material (which can be any of the other standard material types) and apply the newly created material to the mesh like so:
//First remove the old mesh from the scene...
var mesh = meshNode.children[0];
meshNode.remove( mesh );
//Now create a new mesh, and add it to the node using the same geometry but a new material
if ( assetMaterial ) //The newly created material
meshNode.add( new THREE.Mesh( mesh.geometry, assetMaterial ) );
else
meshNode.add( new THREE.Mesh( mesh.geometry, this.defaultMaterial ) );
At first I wanted to just reset the material of the Mesh ( mesh.material = assetMaterial ). But that didn't work and was throwing all kinds of errors. So after some digging around it seemed that you could not change materials like that, and that the materials were linked to the mesh and or its geometry. This led me to the above code, which would remove the previous mesh from the scene and create a new one with the new material passed into it.
This works most of the time - however when the new material being added has a texture map assigned to it, the mesh disappears from the scene and I get the following error:
"GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 2 "
But annoyingly, if I create the default material and pass in a map property, with a hard coded texture file it works fine. ie...:
var texture = var texture = THREE.ImageUtils.loadTexture( "http://localhost/animate-online/Animate/bin/uploads/p2/Map-COL.jpg" );
this.defaultMaterial = new THREE.MeshPhongMaterial( { map: texture, ambient: 0x555555,
color: 0x555555, specular: 0xffffff, shininess: 50, shading: THREE.SmoothShading } );
I'm hoping I'm just doing this wrong, but I would greatly appreciate any information you might be able to shed on this.
Thanks
We ran into this issue with Editor:
http://mrdoob.github.com/three.js/editor/
It's about changing requirements for geometry attribute buffers when changing the material (geometry doesn't get UVs if object was rendered first with a material without a texture).
So you need to update geometry buffers when structure of the material changes (e.g. no texture => texture, while changing one texture to another is ok).
Have a look at how handling of this is implemented in Editor:
https://github.com/mrdoob/three.js/blob/dev/editor/js/ui/Sidebar.Material.js#L277
(you must use dev branch, this is a new feature, live version of Editor uses dummy texture instead)
Ah ha! I thought that might be the case :) I'll download the dev and give it a try - but that definitely sounds like what was going wrong. I'll give it a try and let you know. Thanks Alteredq!
Yup that was it. After adding the
selected.geometry.buffersNeedUpdate = true;
selected.geometry.uvsNeedUpdate = true;
it worked like a dream.
Thanks Alteredq :)