Good morning,
I’m learning Three.js and for exercise I was trying to animate some random 3D models. I downloaded them as gltf files from sketchfab. Once loaded, some display correctly, but some other seem to have texture issues, like if they’re sort of misplaced or missing.
As I can see all of them correctly on gltfViewer, I suppose the problem is not in the models, but in how I load them.
I tried adding lights or moving the camera, but nothing seems to work. Also, I have no errors in console, so I don’t know what I’m missing.
This is the code fragment I use to load (it works for some)
const canvas = document.querySelector("canvas");
const renderer = new THREE.WebGLRenderer({ canvas });
renderer.outputEncoding = THREE.sRGBEncoding;
const fov = 75;
const aspect = 2;
const near = 0.1;
const far = 500;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(-130, 50, 150);
const controls = new OrbitControls(camera, canvas);
controls.target.set(0, 5, 0);
const scene = new THREE.Scene();
scene.background = new THREE.Color(MODEL_CONSTS.lightBlue);
const axesHelper = new THREE.AxesHelper(100);
axesHelper.visible = true;
scene.add(axesHelper);
{
const gltfLoader = new GLTFLoader();
const url = `${CONSTS.resourcesRelativePath}/${CONSTS.modelsFolder}/${MODEL_CONSTS.gltfFolderName}/${MODEL_CONSTS.gltfFileName}${CONSTS.gltfFormat}`;
gltfLoader.load(url, (gltf) => {
const root = gltf.scene;
scene.add(root);
});
}
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
function render(time) {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
time *= 0.001;
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
renderer.render(scene, camera);
requestId = requestAnimationFrame(render);
}
requestId = requestAnimationFrame(render);
Here are some of the results
I logged nodes:
Here the links to sketchfab models: