hRdCoder hRdCoder -4 years ago 125
Javascript Question

Need help loading an OBJ and MTL file in Three.js

I'm experiencing an odd issue while trying to load an OBJ and MTL file in Three.js using it's OBJMTLLoader() function and just can't seem to figure it out, or find a solution on Google. I have my index.html file inside of a folder, and also in that same folder is my js folder, which includes all external js files that I wrote for my project along with the three.js r68 library. Also in my root project folder is a folder named 'obj', which includes all of my obj files and their corresponding mtl files.

I initially started my project under a Windows system and was able to load everything just fine, no problem at all, but now I switched to my personal Linux system and I started seeing this problem and am unable to load any of my obj/mtl files any longer.

The error I am receiving is 'Uncaught TypeError: undefined is not a function' and I've tried printing out my 'loader' variable and it simply shows me in the console that it's an object of type OBJMTLLoader. Any thoughts on this?

EDIT: The first file is js/main.js, and the next one is index.html.

const WIDTH = 1200;
const HEIGHT = 700;
const VIEW_ANGLE = 45;
const NEAR = 0.1;
const FAR = 10000;

var renderer,

var pointLight, pointLight2;
var xrot = 0.0025,
yrot = 0.0025;

$(document).ready(function() {

// setup renderer, camera, perspective, etc.


// lights
pointLight = new THREE.PointLight(0xffffff);
pointLight.position.x = -10;
pointLight.position.y = 50;
pointLight.position.z = 130;


pointLight2 = new THREE.PointLight(0xffffff);
pointLight2.position.x = 0;
pointLight2.position.y = 50;
pointLight2.position.z = 500;


var render = function() {

$(scene.children).each(function() {
if (this !== camera && this !== pointLight) {
this.rotation.x = xrot;
this.rotation.y += yrot;

renderer.render(scene, camera);


function initScene() {

renderer = new THREE.WebGLRenderer({
antialias: true
camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT_RATIO, NEAR, FAR);
scene = new THREE.Scene();

camera.position.z = 250;

renderer.setSize(WIDTH, HEIGHT);


function getLoadedFile(evt) {
var fileList =;
var filename = fileList[0].name;

if (filename.substring(filename.length - 4, filename.length) == '.obj') {
var obj = filename;
var mtl = filename.substring(0, filename.length - 4) + '.mtl';

console.log('adding ' + filename + ' to scene.');
loadOBJMTLModel(obj, mtl);

function loadOBJMTLModel(obj, mtl) {

var loader = new THREE.OBJMTLLoader();
loader.load('obj/' + obj, 'obj/' + mtl, function(object) {

<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<!DOCTYPE html>

<meta charset="utf-8">
<title>OBJ Model Previewer</title>
body {
background-color: lightgrey;

div#container {
width: 1200px;
height: 700px;
border: 1px solid grey;
<!-- <script src="js/main.js"></script> //-->

<h3>Choose file to load</h3>
<input id="loadfile" type="file" />
<br />
<div id="container"></div>
<br />
<span></span><br />
<span></span><br />
<br />
<span>Y Rotation: </span><input type="range" min="0.0" max="0.1" step="0.001" />
<span id="yrot"></span>


Answer Source

As requested.

OBJMTLLoader is deprecated. you must use MTLLoader and OBJLoader together. You can view a traditional example here:

There have also been numerous updates to objloader2.js available @

For a little more digging, see my previous answer for automated MTL loading here: Is there a way to load a mtl using the path in the obj file?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download