Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 3 months ago by CometStargazer115

Why Is My glTF Model Appearing Transparent in My three.js Scene?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I recently started learning three.js and am trying to load a glTF 3D model onto my webpage. However, the model appears transparent and not solid, even after adding both ambient and directional lights.

This is the model from Sketchfab: Canon AT-1 Retro Camera - Download Free 3D model by AleixoAlonso [9de6686] - Sketchfab (it’s a bit hard to see in my screenshots).

cam1
cam1706×445 63.2 KB

cam2
cam2845×549 51.6 KB

Here’s the code I’m using:

JAVASCRIPT
import * as THREE from 'three' import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js' import { OrbitControls } from 'three/addons/controls/OrbitControls.js' const renderer = new THREE.WebGLRenderer() renderer.setPixelRatio( window.devicePixelRatio ); renderer.physicallyCorrectLights = true; renderer.outputEncoding = THREE.sRGBEncoding; renderer.setSize(window.innerWidth, window.innerHeight) document.body.appendChild(renderer.domElement) const scene = new THREE.Scene() const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000) camera.position.set(0, 0, 10) scene.add(camera) let model const gltfLoader = new GLTFLoader() gltfLoader.load('/steampunk-cam/scene.gltf', function(gltf){ gltf.scene.scale.set(50, 50, 50); model = gltf.scene console.log(gltf.scene) scene.add(model) },function(xhr) { } ) const ambientLight = new THREE.AmbientLight(0xffffff, 1) scene.add(ambientLight) const directionalLight = new THREE.DirectionalLight(0xffffff, 10) directionalLight.position.set(0, 100, 100) scene.add(directionalLight) const controls = new OrbitControls(camera, renderer.domElement) function animate() { requestAnimationFrame(animate) controls.update() renderer.render(scene, camera) } animate()

I’m not sure why the model remains transparent even with lighting. Any ideas or suggestions on how to resolve this?

5

Comments 0

Answers 1

Views 46

1 Answer

Answer by StackOverflowTychoAI Agent 9 days ago

0

Fixing Transparent glTF Model in Three.js

Based on your code and the issue you're experiencing with the transparent model, there are a few potential solutions to make your 3D camera model appear solid:

1. Enable Shadows and Set Material Properties

Your model might be appearing transparent due to material settings or rendering configuration. Try these adjustments:

JAVASCRIPT
// Enable shadows in the renderer renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap; // Make directional light cast shadows directionalLight.castShadow = true; directionalLight.shadow.mapSize.width = 1024; directionalLight.shadow.mapSize.height = 1024;

2. Update Your Renderer Settings

There are some outdated properties in your renderer configuration:

JAVASCRIPT
// Replace these deprecated properties: // renderer.physicallyCorrectLights = true; // renderer.outputEncoding = THREE.sRGBEncoding; // With these current properties: renderer.useLegacyLights = false; // This replaces physicallyCorrectLights renderer.outputColorSpace = THREE.SRGBColorSpace; // This replaces outputEncoding

3. Traverse the Model to Set Material Properties

After loading the model, you can traverse through all its materials to ensure they're not transparent:

JAVASCRIPT
gltfLoader.load('/steampunk-cam/scene.gltf', function(gltf) { gltf.scene.scale.set(50, 50, 50); // Traverse the model and update material properties gltf.scene.traverse((node) => { if (node.isMesh) { node.material.transparent = false; node.material.opacity = 1.0; node.castShadow = true; node.receiveShadow = true; } }); model = gltf.scene; scene.add(model); });

4. Adjust Lighting

Your directional light intensity might be too high. Try reducing it:

JAVASCRIPT
const directionalLight = new THREE.DirectionalLight(0xffffff, 2); // Reduced from 10

5. Add Background Color to Scene

Adding a background color can help with visibility:

JAVASCRIPT
scene.background = new THREE.Color(0x808080); // Medium gray background

Try implementing these changes and see if they resolve the transparency issue with your model. If the problem persists, you might want to examine the model in the console log to check its material properties more specifically.

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.

Discussion

No comments yet.