Something strange when i apply same jpg to two differents .obj

Hi everyone, I’m new to three.js and I have this problem. I’m using the code below to apply a texture to an .obj object. On object 1 downloaded from the web by a friend of mine and exported from 3d studio max the texture is applied correctly. On object 2, purchased by him on the web and exported again with 3d studio max, there are these glitches. Can anyone help me and explain why? I imagine that the problem is with the object and its export but I don’t know 3d studio max and he doesn’t know three.js

function applyTextureToObjects(idelement, materialUrl, targetObjectNames = []) {

	var obj_material_size = parseFloat(jQuery("#" + idelement).attr("data-material-size"));
	var roughness = parseFloat(jQuery("#" + idelement).attr("data-material-roughness"));
	var metalness = parseFloat(jQuery("#" + idelement).attr("data-material-metalness"));

	// Carica la texture
	var textureLoader = new THREE.TextureLoader();

	if(typeof materialUrl === "undefined"){
		materialUrl = main_material;
		obj_material_size = main_size;
		roughness = main_roughness;
		metalness = main_metalness;
	}

	textureLoader.load(materialUrl, function (texture) {
		texture.offset.y += 1;
		texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
		texture.repeat.set(obj_material_size, obj_material_size);

		// Funzione per applicare la texture a un oggetto di tipo Mesh
		function applyTexture(targetObject) {
			if (targetObject instanceof THREE.Mesh) {

				var material = new THREE.MeshStandardMaterial({
					map: texture,                   // La texture applicata
					side: THREE.DoubleSide,         // Renderizza entrambi i lati del materiale
					roughness: roughness,                 // Valore massimo per un aspetto opaco
					metalness: metalness,                 // Valore minimo per evitare riflettività metallica
				});
				
				targetObject.material = material;
				targetObject.receiveShadow = true;
				targetObject.castShadow = true;
				targetObject.material.needsUpdate = true; // Aggiorna il materiale per riflettere il cambiamento
			}
		}

		// Funzione ricorsiva per applicare la texture a tutti i Mesh in un gruppo
		function applyTextureToGroup(group) {
			group.traverse(function (child) {
				applyTexture(child);
			});
		}

		// Se targetObjectNames è un array vuoto, applica la texture a tutti i Mesh e Group nella scena
		if (Array.isArray(targetObjectNames) && targetObjectNames.length === 0) {

			scene.traverse(function (object) {
				if (object instanceof THREE["Mesh"]) {
					applyTexture(object);
				}
				else if (object instanceof THREE["Group"]) {
					applyTextureToGroup(object);
				}
			});
		}
		else {
			// Se è una stringa o un array con nomi specificati
			var names = Array.isArray(targetObjectNames) ? targetObjectNames : [targetObjectNames];
			names.forEach(function (name) {
				var targetObject = scene.getObjectByName(name);
				if (targetObject) {
					// Se l'oggetto target è trovato e è di tipo Group, applica la texture a tutti i Mesh nel gruppo
					if (targetObject instanceof THREE.Group) {
						applyTextureToGroup(targetObject);
					} else {
						// Se l'oggetto target non è un Group, applica la texture direttamente
						applyTexture(targetObject);
					}
				}
			});
		}
	});
}


Maybe you should try switching to GLTF format all together if you can. It might make it easier than working with OBJ.

For OBJ models, this is a text file so you should be able to open it in text editors, like Notepad in Windows. There should be bunch of lines, some starting with v and some with vt or vn or f … etc.

Then maybe check the official OBJ Loader to see how it processes each of these lines.

Your 2 obj models are probably different and the strange effect you get is probably from UV coordinates (I guess).

1 Like

ok thanks, we’ll check UV coordinates. What about “easier than working with OBJ” ? do you mean exporting or into three.js? because in three.js i use the code above and i think it’s not so a complex code

editing files: i see
0 polygons - 67591 triangles for the correct one
85705 polygons - 2579 triangles for the wrong one

i see all lines but there are too many line to try to get “the bug”

GLTF is probably the most current and best defined format within three.js and with most features. The other formats don’t get as frequently updated either.

There is probably a bunch of topics in this forum where somebody might have suggested to a user to switch from OBJ or FBX or some other format to GLTF workflow.

OBJ format in general seems to be obsolete and not being officially maintained by anyone any longer - just check the Wikipedia page.

1 Like

Also, you should not try to debug lines in your models since they are completely different (even though they look alike). It is not like you can just slip a shirt properly onto any torso model you can get from the internet.

If 3d studio max software cannot map the texture properly then maybe try using Blender software.

hi, i modified my code to use GLTF and it’s loaded in the canvas but now the object is all black and i can’t apply textures. The code i use to apply texture is the one i posted above. Is it correct or do i miss something using GLTF format? or it’s the object with some problems? i tried also with this Flower.glb and it’s black too

Just in case, if you are using React to program all this then I cannot make any proper suggestions since I am not a React user.

GLB files are binary files that contain the model and textures - so whatever software you use to export the model to GLB format then you need to tell it to include textures. Everything should be applied in that software. Maybe try using the official three.js editor first.

Also, there should be some light in the scene, check the attached picture of that Flower.glb file when loaded in my GLTF Viewer.

hi, no i’m not using react. I tried to use another example Parrot.glb and here i can apply textures. I need to apply custom textures (i’m creating a woocommerce shirt configurator, so textures are the shirt fabrics) and i can’t insert theme in the glb

hi, we resolved with fbx format :slight_smile:

The official editor will allow you to change textures in almost any slot of the material, you can use it to test your model and textures. Try to get familiar with using the editor so you can see what it can do. You can even load your OBJ model there for a test.

You should be able to do the same in the code, where you can dispose of a current texture and insert a new texture. If you will be using only a single texture, like material.map, then you can even dispose of a texture and then dispose the material, afterwards just create the same type of material and add a new texture to it.

Cool, then stick with FBX format.