2

I'm trying to create an object loader with Three.js but I noticed that the quality is way too low and used too much CPU at the same time.

When I use my version, the scene looks like this:

enter image description here

But when I use this website to load it, looks so much better and uses less CPU:

enter image description here

My JavaScript to load this object is:

var camera;
var scene;
var renderer;
var controls;
var container = document.getElementById('webgl');
var WIDTH = container.clientWidth;
var HEIGHT = container.clientHeight;
var ASPECT = WIDTH / HEIGHT;
var ANGLE = 45;
var container =  document.getElementById('webgl');

if (Detector.webgl) {
    main();
} else {
    var warning = Detector.getWebGLErrorMessage();
    document.getElementById('webgl').appendChild(warning);
}

function main(){  
    //Scene
    scene = new THREE.Scene();

    //Camera
    camera = new THREE.PerspectiveCamera(
        ANGLE, // field of view
        ASPECT, // aspect ratio
        10, // near clipping plane
        100000 // far clipping plane
    );

    camera.position.x = 500;
    camera.position.y = 200;
    camera.position.z = 500;
    camera.lookAt(new THREE.Vector3(100, 100, 100));

    //Renderer
    renderer = new THREE.WebGLRenderer();
    var ambientLight = getAmbientLigth(1);
    scene.add(ambientLight);
    scene.background = new THREE.Color( 0xc3c3c3 );
    renderer.setSize(WIDTH, HEIGHT);

    renderer.shadowMap.enabled = true;
    document.getElementById('webgl').appendChild(renderer.domElement);
    controls = new THREE.OrbitControls( camera, renderer.domElement );
    controls.maxPolarAngle = Math.PI/2;
    controls.enableKeys = true;
    loadObject();
    update(renderer, scene, camera, controls);
}

function getAmbientLigth(intensity, color) {
    color = color === undefined ? 'rgb(255, 255, 255)' : color;
    var light = new THREE.AmbientLight(color, intensity);
    return light;
}

function loadObject() {
    var mtlLoader = new THREE.MTLLoader();
    var objLoader = new THREE.OBJLoader();
    mtlLoader.setPath( 'objects/Blue_shed/' );
        mtlLoader.load('blueShed.mtl', function( materials ) {
        materials.isMultiMaterial = true;
        materials.preload();
        objLoader.setMaterials( materials );
        objLoader.setPath( 'objects/Blue_shed/' );
        objLoader.load( 'blueShed.obj', function ( object ) {
            object.name = 'cute-house';
            object.receiveShadow = true;
            object.castShadow = true;
            object.scale.set( 30, 30, 30);
            scene.add( object );
        } );
    });
}

function update(renderer, scene, camera, controls) {
    controls.update();
    renderer.render(scene, camera);
    requestAnimationFrame(function() {
        update(renderer, scene, camera, controls);
    });
}

I used renderer.setSize to increase the resolution of the renderer and that helped a little bit but still is not as good as in the second image, and still uses too much CPU.

Any ideas? Is there a setting or something that I'm not setting up correctly? I see that website uses a JSON loader, but I don't think that has something to do with this issue, but I mention it just in case.

Mithc
  • 851
  • 8
  • 23
  • I don't see any shadows in your one. – 2pha Oct 24 '17 at 01:35
  • Try with : renderer = new THREE.WebGLRenderer({ antialiasing: true }); , p.s. I see overlap vertices on door . Object need to be optimized for sure . – Nikola Lukic Oct 24 '17 at 08:57
  • @NikolaLukic Already tried `antialiasing: true` but I didn't see any change. That kind of optimization is done via the renderer or with another library? Like the JSON loader that the 3dviewer site uses? – Mithc Oct 24 '17 at 15:13
  • 2pha and Rabbid76, the shadows, and lights work fine but those didn't change the aliased look of the objects. Even after trying the setting that Nikola suggested. – Mithc Oct 24 '17 at 15:17
  • Did you try other Q/A about antialias like : https://stackoverflow.com/questions/20916396/three-js-antialiasing-not-working-on-chrome and https://stackoverflow.com/questions/17224795/antialiasing-not-working-in-three-js . – Nikola Lukic Oct 24 '17 at 16:29
  • Yeah, I saw those Q/A and tried those solutions but seems like whether I use `antialias` (as seen on the [official docs](https://threejs.org/docs/#api/renderers/WebGLRenderer.antialias)) or `antialiasing` the result is the same. – Mithc Oct 25 '17 at 14:25

0 Answers0