import * as THREE from 'three';
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js';
// Setting up the scene, camera, and renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(65, window.innerWidth / window.innerHeight, 0.1, 800);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Adding lighting to the scene
const ambientLight = new THREE.AmbientLight(0x404040); // Soft white light
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(0, 1, 0); // Adjust position if needed
scene.add(directionalLight);
// Initializing the OBJLoader
const loader = new OBJLoader();
// Loading the .obj file into the scene
loader.load(
'../src/assets/models/test.obj', // Check the path is correct!
function (object) {
scene.add(object);
console.log('Successfully loaded OBJ file.');
object.position.set(0, 0, 0);
object.scale.set(0.01, 0.01, 0.01);
camera.lookAt(object.position);
// Modifying materials of the object
object.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.material = new THREE.MeshBasicMaterial({ wireframe: true });
child.material.side = THREE.DoubleSide; // making it double-sided
}
});
animate(); // Starting the animation loop after loading the model
},
function (xhr) { // Optional: Progress callback
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
},
function (error) { // Error callback
console.error('An error occurred:', error);
}
);
// Animation loop
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
I'm having difficulty importing the obj file into my viewer, possibly due to an issue with the file path within the project folder.
The screen appears black when I try to render the model.
Any suggestions on how to resolve this?