My Obj is not loading while iam trying to give path from project folder

44 Views Asked by At
import * as THREE from 'three';
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js';

// Create a 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);

// Lighting
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 as needed
scene.add(directionalLight);

// Create an instance of OBJLoader
const loader = new OBJLoader();

// Specify the path to your .obj file and ensure it's correct and accessible
loader. Load (
    '../src/assets/models/test.obj', // Make sure this path is correct!
    function (object) { // This function is called after the object is loaded
        scene.add(object); // Add the loaded object to the scene
        console.log('OBJ file loaded successfully.');

        object.position.set(0, 0, 0); // Adjust as necessary
        object.scale.set(0.01, 0.01, 0.01); // Adjust as necessary
        camera.lookAt(object.position);

        // Traverse the object to modify materials
        object.traverse(function(child) {
            if (child instanceof THREE.Mesh) {
                child.material = new THREE.MeshBasicMaterial({ wireframe: true });
                child.material.side = THREE.DoubleSide; // Make the material double-sided
            }
        });

        animate(); // Start animation loop after the model is loaded
    },
    function (xhr) { // Optional: Progress callback
        console.log((xhr.loaded / xhr.total * 100) + '% loaded');
    },
    function (error) { // Error callback
        console.error('An error happened:', error);
    }
);

// Animation loop
function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}

I'm trying to import the obj to my viewer which im not able to. 'm thinking it is because of path which is on the project folder

I am getting a black screen.

enter image description here

How can I fix this?

1

There are 1 best solutions below

0
Łukasz D. Mastalerz On

Your obj is loaded. Try set scale it more. If i would have to guess, has red color... Look at scale:

object.scale.set(0.01, 0.01, 0.01);

Make it bigger

object.scale.set(2, 2, 2);

enter image description here