Hey, I have the following gltf model, which I want to add it to my scene, I still can’t visualize it, only a black dark scene…
cad.gltf (651.6 KB)
Also here’s my code:
import * as THREE from "three";
import WebGL from "three/addons/capabilities/WebGL.js";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
if (WebGL.isWebGL2Available()) {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
1,
500
);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
camera.position.z = 500;
camera.up = new THREE.Vector3(0, 0, 1);
camera.lookAt(scene.position);
const ambientLight = new THREE.AmbientLight(0xffffff, 1); // Ambient light
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1); // Directional light
directionalLight.position.set(0, 10, 10);
scene.add(directionalLight);
const loader = new GLTFLoader();
loader.load(
"cad.gltf",
function (gltf) {
const model = gltf.scene;
model.traverse((node) => {
if (node.isMesh) {
node.material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
}
});
model.scale.set(0.1, 0.1, 0.1);
scene.add(gltf.scene);
},
undefined,
function (error) {
console.log(error);
}
);
renderer.render(scene, camera);
} else {
var warning = WEBGL.getWebGLErrorMessage();
document.getElementById("container").appendChild(warning);
}