myown.obj.zip (2.5 MB) Hi, I want to play the attached obj file using threejs, what I did is to modify the webgl_loader_obj.html directly by pointing to this obj, using below:
But when I refresh the page, the whole page is empty.
I am very new at using threejs, and do not know how to debug and find error, I don’t see any error in the console either.
can you please help to tell me the error here?
Thanks in advance
import * as THREE from '../build/three.module.js';
import { OBJLoader } from './jsm/loaders/OBJLoader.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js';
var container;
var camera, scene, renderer;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 5;
// 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 loader = new OBJLoader();
loader.load( 'models/obj/myown.obj', function ( obj ) {
// center asset
var box = new THREE.Box3().setFromObject( obj );
var center = new THREE.Vector3();
box.getCenter( center );
obj.position.sub( center );
// add to scene
scene.add( obj );
} );
//
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
var controls = new OrbitControls( camera, renderer.domElement );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
renderer.render( scene, camera );
}
The original OBJ model has a far bigger scale than your book. The above code has optimized camera settings for your use case. It also uses OrbitControls which is more appropriate for a model viewer.
However, the colors of your book are messed up because there is a bug in OBJLoader. I’m going to fix this and link the PR here.
I can’t see something obvious in your code. Any chances to share a live example with your code? It’s also a good option to share a repository so I can debug the code on my computer.
BTW: Any reasons for setting preserveDrawingBuffer to true. Unless you don’t do stuff like synchronous drawing buffer access (e.g. via toDataURL()), it’s better to not set this flag to true. Otherwise you might have significant performance loss on some platforms.
when you first run, you can see the obj is loaded, but if you use the commented out code, then the book does not appear until you click the canvas. I think something is wrong in my code, but I do not know where.
Also for the current code, if you zoom the obj, the book is cut off from half, no idea why.
Hey man,
It’s my first time creating account here and it’s all because of you. I’ve just started learning threejs and been struggling with loading stl file for a while. Some of the lightning codes in the script above helps me while I couldn’t find my model on the canvas.