I have two files, an ".obj" and a ".mtl", of a model that I am loading using the OBJMTLLoader
. The ".mtl" file specifies the texture to be applied to the model, and three.js successfully loads and renders the model with the applied texture.
However, I want to apply another texture onto the already loaded object. The first texture represents the surface material of the object, while the second texture is a drawing that I want to position at a specific location on the model.
So, my question is: how can I apply a second texture onto an already loaded and textured object?
I noticed that three.js creates an instance of THREE.Object3D
, which has a "children" array containing one instance of THREE.Mesh
.
When I try to apply a texture to this mesh (mesh.material.map = texture
), the initial texture is lost.
I checked out a similar question regarding multiple textures and JSONLoader, but did not find a suitable answer.
I also attempted to use
new THREE.MeshFaceMaterial( materials )
as recommended in another answer without success.
UPDATE:
Despite following @WestLangley's advice to utilize a multi-material object, I am still unable to render one material on top of another.
In my simple demo derived from three.js OBJLoader, I used this link as reference. I employed
THREE.SceneUtils.createMultiMaterialObject
by passing it the cloned geometry of the main mesh loaded from .obj along with two textures - one for the entire surface and another for the front surface of the model.
Unfortunately, this approach didn't yield the desired outcome. Even after adding checkboxes to toggle the visibility of corresponding materials, it was evident that both materials were present, yet I couldn't see the first one beneath the second.
The key aspects of the loading/rendering process are summarized below:
var texture = THREE.ImageUtils.loadTexture('fabric.jpg');
var texture2 = THREE.ImageUtils.loadTexture('table.jpg');
texture2.offset.set(-0.65, -2.5);
texture2.repeat.set(4, 4);
var loader = new THREE.OBJLoader();
loader.addEventListener( 'load', function ( event ) {
var mainMesh = event.content.children[0].children[0];
multiMaterialObject = THREE.SceneUtils.createMultiMaterialObject(
mainMesh.geometry.clone(), [
new THREE.MeshLambertMaterial({ map: texture2 }),
new THREE.MeshLambertMaterial({ map: texture })
]);
multiMaterialObject.position.y = -80;
scene.add(multiMaterialObject);
});
loader.load( 'male02.obj' );
UPDATE #2
At this stage, I am considering utilizing THREE.ShaderMaterial
to apply one texture over another. Though there are some examples demonstrating the usage of a single texture, I remain uncertain about how to display both in an overlaid state and how to precisely position the texture on a mesh.