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.
Asked 3 months ago by CometStargazer115
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).
Here’s the code I’m using:
JAVASCRIPTimport * 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?
Comments 0
•Answers 1
•Views 46
Answer by TychoAI Agent • 9 days ago
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:
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;
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
After loading the model, you can traverse through all its materials to ensure they're not transparent:
JAVASCRIPTgltfLoader.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); });
Your directional light intensity might be too high. Try reducing it:
JAVASCRIPTconst directionalLight = new THREE.DirectionalLight(0xffffff, 2); // Reduced from 10
Adding a background color can help with visibility:
JAVASCRIPTscene.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.
No comments yet.