3

I am trying to load the 3dsmax .obj and .mtl file in browser using three.js.

I am using this tutorial and code..

The obj model is loading successfully but it looks dark and not the way it should be.

enter image description here

what should i do so there is no black spots in the object.

How should i add lights from all sides?

Here's the script -

<script>

    var lesson6 = {
     scene: null,
     camera: null,
      renderer: null,
     container: null,
    controls: null,
    clock: null,
   stats: null,

   init: function() { // Initialization

// create main scene
this.scene = new THREE.Scene();
this.scene.fog = new THREE.FogExp2(0xcce0ff, 0.0003);

var SCREEN_WIDTH = window.innerWidth,
    SCREEN_HEIGHT = window.innerHeight;

// prepare camera
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 1, FAR = 2000;
this.camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
this.scene.add(this.camera);
this.camera.position.set(0, 100, 300);
this.camera.lookAt(new THREE.Vector3(0,0,0));

// prepare renderer
this.renderer = new THREE.WebGLRenderer({ antialias:true });
this.renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
this.renderer.setClearColor(this.scene.fog.color);
this.renderer.shadowMapEnabled = true;
this.renderer.shadowMapSoft = true;

// prepare container
this.container = document.createElement('div');
document.body.appendChild(this.container);
this.container.appendChild(this.renderer.domElement);

// events
THREEx.WindowResize(this.renderer, this.camera);

// prepare controls (OrbitControls)
this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
this.controls.target = new THREE.Vector3(0, 0, 0);
this.controls.maxDistance = 2000;

// prepare clock
this.clock = new THREE.Clock();

// prepare stats
this.stats = new Stats();
this.stats.domElement.style.position = 'absolute';
this.stats.domElement.style.left = '50px';
this.stats.domElement.style.bottom = '50px';
this.stats.domElement.style.zIndex = 1;
this.container.appendChild( this.stats.domElement );

// add spot light
  var spLight = new THREE.SpotLight(0xffffff, 1.75, 2000, Math.PI / 3);

spLight.castShadow = true;

spLight.position.set(-200, 500, -150);

this.scene.add(spLight);



// add simple ground
var ground = new THREE.Mesh( new THREE.PlaneGeometry(200, 200, 10, 10), new      THREE.MeshLambertMaterial({color:0x999999}) );
 ground.receiveShadow = true;
 ground.position.set(0, 0, 0);
  ground.rotation.x = -Math.PI / 2;
this.scene.add(ground);

   // load a model
   this.loadModel();
  },
   loadModel: function() {
 // prepare loader and load the model
  var oLoader = new THREE.OBJMTLLoader();
  oLoader.load('1.obj', '1.mtl', function(object) {

 object.position.x = 80;
 object.position.y = 0;
 object.position.z = 30;
 object.scale.set(10,10,10);
  lesson6.scene.add(object);
  });
  }
 };

 // Animate the scene
 function animate() {
  requestAnimationFrame(animate);
 render();
 update();
 }

// Update controls and stats
function update() {
 lesson6.controls.update(lesson6.clock.getDelta());
 lesson6.stats.update();
 }

// Render the scene
 function render() {
if (lesson6.renderer) {
 lesson6.renderer.render(lesson6.scene, lesson6.camera);
}
}

// Initialize lesson on page load
function initializeLesson() {
 lesson6.init();
 animate();
}

 if (window.addEventListener)
window.addEventListener('load', initializeLesson, false);
else if (window.attachEvent)
  window.attachEvent('onload', initializeLesson);
else window.onload = initializeLesson;

</script>

Default obj and mtl file preview- [![enter image description here][2]][2]
gman
  • 100,619
  • 31
  • 269
  • 393
user2897282
  • 81
  • 1
  • 5
  • 14

1 Answers1

2

Refer: three.js load obj/mtl renders black

OBJMTLLoader has been removed since r74. You should use MTLLoader and OBJLoader.

var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath('obj/');
mtlLoader.load('1.mtl', function(materials){
    materials.preload();
    //OBJ LOADER
    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials(materials);
    objLoader.setPath('obj/');
    objLoader.load('1.obj', function(object){
        objModel = object;
        scene.add(objModel);
    });
});

Also try adding ambient light to the scene to check whether the materials have been loaded properly.

var ambientLight = new THREE.AmbientLight('#fff');
scene.add(ambientLight);
Community
  • 1
  • 1
Safal Pillai
  • 1,567
  • 1
  • 16
  • 31
  • I added OBJLoader and added this code but its not loading the object. I am fairly new to this. I just want to load the object to be seen as it is in the browser. Is there some good viewer where i can add my .obj and .mtl and it loads with proper lighting? – user2897282 Sep 21 '16 at 07:14
  • TypeError: mtlLoader.setPath is not a function – user2897282 Sep 21 '16 at 07:50
  • I am guessing you aren't loading MTLLoader.js in your file. – Safal Pillai Sep 21 '16 at 07:52
  • ` ` yup i added it. and all this files. – user2897282 Sep 21 '16 at 07:53
  • I used the same code as above and it worked fine for me. Check whether the paths are correct or if you are getting any CORS error. – Safal Pillai Sep 21 '16 at 08:07
  • All paths are correct. I think there is some clash between old tutorial and new addition of function. Can you share your script of loading the obj. – user2897282 Sep 21 '16 at 09:32
  • You mean OBJLoader.js? Also try using the latest stable release - three.js r80. – Safal Pillai Sep 21 '16 at 09:41
  • no, i mean the html with script that loads the obj into the browser. Like the script i posted. – user2897282 Sep 21 '16 at 11:20
  • Thanks man. i added new three.js and other script files that is in your code , added your code, added mouse pointer png but got this error - `THREE.WebGLRenderer 76three.js:24764:2 OBJLoader: timer startedOBJLoader.js:408 OBJLoader: 232.3msOBJLoader.js:712 OBJLoader: timer startedOBJLoader.js:408 OBJLoader: 233.21msOBJLoader.js:712 second object model loadedin.html:105:5 TypeError: mouseInfo is undefined render() in.html:142 renderCheck()` – user2897282 Sep 21 '16 at 12:42