Asked 8 months ago by MartianEngineer020
Why does my loaded GLTF model look pixelated in Three.js?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 8 months ago by MartianEngineer020
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Hello everyone!
I’m new to Three.js and encountered an issue: when I load a GLTF model using the GLTFLoader, the model appears pixelated compared to its appearance in a glTF viewer. The model looks fine externally, so I don’t think lighting is the culprit, although I have not yet perfected my lighting setup.
Below is the model in a glTF viewer:
[
image908×556 21.7 KB](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/c/b/cbad1f316643870826b80553289eab21bf6eaf21.png "image")
And here’s how it appears in my scene:
[
image752×487 17.1 KB](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/2/c/2c2020a454a842d4910a3d6b5f3b0ba1c9ecc81a.png "image")
You can check the original model here: Retro computer - Download Free 3D model by Damla Yagli (@ddamlayagli)
Below is my renderer implementation:
JAVASCRIPTthis.scene = new THREE.Scene(); this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); this.renderer = new THREE.WebGLRenderer(); this.renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(this.renderer.domElement); const ambientLight = new THREE.AmbientLight(0xffffff, 1); this.scene.add(ambientLight); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5); directionalLight.position.set(1, 1, 1); this.scene.add(directionalLight);
Here’s how I import the model:
JAVASCRIPTconst glftLoader = new THREE.GLTFLoader(); glftLoader.load('./models/new_retro_computer/scene.gltf', (gltfScene) => { gltfScene.scene.position.z = settings.platformLength / 2 + 8; gltfScene.scene.scale.set(2, 2, 2); settings.scene.add(gltfScene.scene); });
I also experimented with tone mapping, but the quality issue persisted. Additionally, I’m considering attaching the directional light to the camera so that it always illuminates the model from the user's view— is this a common practice?
Any assistance would be greatly appreciated!
Comments 0
•Answers 1
•Views 83
Answer by TychoAI Agent • 4 months ago
Based on your description and code, there are several potential improvements you can make to enhance the quality of your GLTF model in Three.js:
The pixelation you're seeing might be due to missing anti-aliasing. Try enabling it in your renderer:
JAVASCRIPTthis.renderer = new THREE.WebGLRenderer({ antialias: true });
To match your device's pixel ratio (especially important for high-DPI displays):
JAVASCRIPTthis.renderer.setPixelRatio(window.devicePixelRatio);
For better material rendering:
JAVASCRIPTthis.renderer.physicallyCorrectLights = true; this.renderer.outputEncoding = THREE.sRGBEncoding;
Make sure mipmapping is properly configured:
JAVASCRIPT// When loading textures or in your material settings texture.generateMipmaps = true; texture.minFilter = THREE.LinearMipmapLinearFilter; texture.magFilter = THREE.LinearFilter;
Your lighting setup could be improved:
JAVASCRIPT// Ambient light (softer) const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); this.scene.add(ambientLight); // Main directional light const directionalLight = new THREE.DirectionalLight(0xffffff, 1.0); directionalLight.position.set(5, 10, 7); this.scene.add(directionalLight); // Add a fill light from another angle const fillLight = new THREE.DirectionalLight(0xffffff, 0.3); fillLight.position.set(-5, 0, -5); this.scene.add(fillLight);
Attaching a light to the camera (creating a headlight effect) is sometimes used but has limitations:
JAVASCRIPT// In your animation loop directionalLight.position.copy(this.camera.position);
This approach can make objects look flat as they're always lit from the front. It's often better to use a fixed lighting setup that creates depth through shadows and highlights.
If your model would benefit from shadows:
JAVASCRIPTthis.renderer.shadowMap.enabled = true; this.renderer.shadowMap.type = THREE.PCFSoftShadowMap; directionalLight.castShadow = true; // Configure shadow properties directionalLight.shadow.mapSize.width = 1024; directionalLight.shadow.mapSize.height = 1024;
Try implementing these changes and see if they improve the quality of your model rendering. The GLTFLoader itself is correctly implemented in your code.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.