MTL Material Not Loading on OBJ

Attempting to load OBJ + MTL of the Earth (not my model). I am using the template provided in the examples. OBJ (sphere) will load, but it is grey with no material/texture. The blender file will load the material properly. I have looked at other solutions such as Loading models problem (OBJ + MTL or FBX) to no avail. Provided HTML below.

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - OBJLoader + MTLLoader</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #000;
				color: #fff;
				margin: 0px;
				overflow: hidden;
			}
			#info {
				color: #fff;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				z-index: 100;
				display:block;
			}
			#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
		</style>
	</head>

	<body>
		<div id="info">
		<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - OBJLoader + MTLLoader
		</div>

		<script src="Resources/three.min.js"></script>

		<script src="Resources/DDSLoader.js"></script>
		<script src="Resources/MTLLoader.js"></script>
		<script src="Resources/OBJLoader.js"></script>

		<script src="Resources/Detector.js"></script>
		<script src="Resources/stats.min.js"></script>

		<script>

			var container, stats;

			var camera, scene, renderer;

			var mouseX = 0, mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;


			init();
			animate();


			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.z = 50;

				// scene

				scene = new THREE.Scene();

				var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
				scene.add( ambientLight );

				var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
				camera.add( pointLight );
				scene.add( camera );

				// model

				var onProgress = function ( xhr ) {
					if ( xhr.lengthComputable ) {
						var percentComplete = xhr.loaded / xhr.total * 100;
						console.log( Math.round(percentComplete, 2) + '% downloaded' );
					}
				};

				var onError = function ( xhr ) { };

				THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );

				var mtlLoader = new THREE.MTLLoader();
				mtlLoader.setPath( 'Resources/Earth/' );
				mtlLoader.load( 'earth.mtl', function( materials ) {

					materials.preload();

					var objLoader = new THREE.OBJLoader();
					objLoader.setMaterials( materials );
					objLoader.setPath( 'Resources/Earth/' );
					objLoader.load( 'earth.obj', function ( object ) {

						object.position.y = 0;
						scene.add( object );

					}, onProgress, onError );

				});

				//

				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );

				document.addEventListener( 'mousemove', onDocumentMouseMove, false );

				//

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				windowHalfX = window.innerWidth / 2;
				windowHalfY = window.innerHeight / 2;

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function onDocumentMouseMove( event ) {

				mouseX = ( event.clientX - windowHalfX ) / 2;
				mouseY = ( event.clientY - windowHalfY ) / 2;

			}

			//

			function animate() {

				requestAnimationFrame( animate );
				render();

			}

			function render() {

				camera.position.x += ( mouseX - camera.position.x ) * .05;
				camera.position.y += ( - mouseY - camera.position.y ) * .05;

				camera.lookAt( scene.position );

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>

I have a feeling there is an issue within the MTL file itself which I will post below.

# Blender MTL File: 'earth.blend'
# Material Count: 3

newmtl Atmo
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

newmtl Clouds
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

newmtl Earth
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

It seems there should be a file path somewhere linking to the actual images, not 100% sure though as I am new to this.

Are you get any errors in the browser console?

Are you running this using a server, or just opening the HTML file in the browser? If it’s the latter, have a look at

https://threejs.org/docs/#manual/introduction/How-to-run-things-locally

1 Like

No errors in the console. I am running from a brackets local server (I think? based on https://github.com/adobe/brackets/wiki/Live-Preview-Overview)(sorry ultra web-dev noob) via live preview. Will try uploading to actual apache server and see if there is any progress.

Try uploading it here: https://blackthread.io/gltf-converter/

Upload the obj, mtl and any textures at the same time, if the files are error free it should show up in the preview window.

1 Like

Uploaded all and a white sphere was returned in the preview. Also uploaded to apache server and am getting the same result. Taking it as an error in the MTL file.

EDIT1: This is confirmed, loaded a known working OBJ and MTL which worked. Looking into how to map the MTL to the OBJ properly.

EDIT2: As an update, ditched loading the OBJ and MTL all together as I was having too many difficulties, ended up just creating the earth in the script itself.

1 Like