What is the correct method for properly loading the GLB model?

I am new to three.js and have some questions that I need help with.

First Method: Using three.js to load a model (a duck in glb format). As shown below: https://i.sstatic.net/IsYHG.png

Second Method: Utilizing to load the same model As shown below: https://i.sstatic.net/nirc0.png

The results from method 2 are better, as the model is clearer and positioned correctly.

I believe there may be issues with the scene settings, camera setup, and rendering parameters, but I'm unsure how to adjust them.

On the right side of Method 2, gltf-viewer.donmccurdy.com displays various parameters for the model. How should these parameters correspond in the code?

<template>
    <div ref="container"></div>
</template>

<script>
    import * as THREE from "three";
    import {GLTFLoader} from "three/examples/jsm/loaders/GLTFLoader.js";
    // Importing OrbitControls
    import {OrbitControls} from "three/examples/jsm/controls/OrbitControls"

    export default {
        name: "Model",
        data() {
            return {
                scene: null,
                camera: null,
                renderer: null,
                control: null,
                model: null,
            };
        },
        methods: {
            // Setting up the scene
            initScene() {
                this.scene = new THREE.Scene();
            },
            // Configuring the camera
            initCamera() {

                this.camera = new THREE.PerspectiveCamera(
                    50, // Field of View
                    window.innerWidth / window.innerHeight, // Aspect Ratio
                    0.01, // Near Plane
                    1000 // Far Plane
                );
                // Setting camera position
                this.camera.position.x = 0.02;
                this.camera.position.y = 5;
                this.camera.position.z = 10;
            },
            // Rendering setup
            initRenderer() {
                this.renderer = new THREE.WebGLRenderer({antialias: true});
                this.renderer.setSize(window.innerWidth, window.innerHeight);
                this.$refs.container.appendChild(this.renderer.domElement);
            },
            // Lighting
            initLight() {
                const light = new THREE.AmbientLight(0xffffff, 1);
                this.scene.add(light);
            },
            // OrbitControls
            initControl() {
                this.control = new OrbitControls(this.camera, this.renderer.domElement);
                this.control.enableDamping = true;
                this.control.dampingFactor = 0.05;
                this.control.autoRotate = true;
                this.control.autoRotateSpeed = 2.0;
            },
            // Loading the model
            initModel() {
                let _this = this;

                const loader = new GLTFLoader();
                loader.load(
                    "Duck.glb",
                    (glb) => {
                        console.log(glb);
                        _this.model = glb.scene;
                        _this.scene.add(_this.model);
                    },
                    function (xhr) {
                        console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
                    },
                    function (error) {
                        console.log("An error happened:", error);
                    }
                );
            },

            init() {
                // Set up scene
                this.initScene();
                // Configure camera
                this.initCamera();
                // Setup renderer
                this.initRenderer();
                // Lighting
                this.initLight();
                // OrbitControls
                this.initControl();
                // Load model
                this.initModel();
            },

            animate() {
                requestAnimationFrame(() => {
                    this.animate();
                });
                this.renderer.render(this.scene, this.camera);
            },
        },
        mounted() {
            this.init();

            this.animate();

            // Window resize listener
            window.addEventListener("resize", () => {
                this.renderer.setSize(window.innerWidth, window.innerHeight);
                this.camera.aspect = window.innerWidth / window.innerHeight;
                this.camera.updateProjectionMatrix();
            });
        },
    };
</script>

Thank you for your help!

Answer №1

First and foremost, it is important to establish the pixel ratio as suggested by @Mugen87

Experiment with different types of lighting as well. Here's an example:

initLight() {
    const ambientLight = new THREE.AmbientLight(0xffffff, 1);
    this.scene.add(ambientLight);

    const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
    directionalLight.position.set(5, 5, 5);
    this.scene.add(directionalLight);
}

Try out various lighting options, adjusting position, intensity, etc., and do the same with models.

If you are inquiring about the panel on the right side in Don’s App, that is the Debug UI. You can utilize lil-gui for that, a debugger that comes highly recommended as it can save you a significant amount of time.

npm install lil-gui

import GUI from ‘lil-gui’

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display the picture for a few moments, then conceal it. A button will appear to reveal the following image after a short period

For my project, I want to create a webpage for school where I display one image that disappears after 5 seconds, followed by a button. The user must click the button before the next image appears and stays for another 5 seconds. This sequence repeats each ...

Tips for assigning date ranges to arrays and then conducting comparisons with date input information

I'm developing an application with two date input fields. The current function I have calculates the sum of the digits in each field until it reaches a single digit, and then displays the result along with some text. Now, I need to create another fun ...

The hyperlink and criteria provided are accurate, however, the webpage remains stagnant

My login page is supposed to redirect to the contact confirmation page when I receive a 200 response from the server, but it's not working. Can you help me find my mistake? const [form, setForm] = useState({}); async function checkLogin() { tr ...

"Utilizing Ajax to dynamically update the content within a

Just getting started with JavaScript and ajax, so please bear with me. I'm attempting to use an ajax function to update content in a div when a link is clicked. However, I keep running into the error: "ajaxpage is not defined". The ajaxpage functio ...

Differences between "face-based" and "vertex-based" characteristics when utilized in fragment shader

Working on a highly intricate webgl project using three.js, I am tackling the challenge of controlling mesh materials through shaders. Despite the complexity, I am encountering a major obstacle for which I have not found a satisfactory solution. Let' ...

There was a problem retrieving the product information from the API

I have been struggling to pinpoint the exact issue at hand. The foundation of HTML and CSS is pre-written, with JavaScript responsible for generating the core elements to populate the DOM. Within my script, I am attempting to retrieve product data in ord ...

Navigating to a new state using Ionic can be done with the $state.go method within

I am experiencing an issue with Ionic routing after receiving a Push Notification. The notification contains a custom key, for example 'myKey'. When the user clicks on the notification message, it should open the application and the callback func ...

Send out Chrome Desktop notifications to reach all users

I recently integrated Chrome Desktop notifications into my website by following the steps outlined in this helpful guide: Chrome desktop notification example After adding the script code to my site, I was able to get it working smoothly. However, I encou ...

Is there a way to inject C++ text into an input field on a webpage using QWebEngine?

I want to integrate a website with QWebEngine to manipulate input commands using Qt's event filters and more. The specific website I am working with requires a username/email and password, and I aim to manage the input of this text on my end. This inv ...

Is there a way to verify the format of a date string that includes a timezone?

Is there a way to validate the format of this date: April 14, 2022 14:00 UTC, ensuring it follows the pattern MMMM DD, YYYY HH:mm <timezone>? I attempted using moment for validation, but it lacks support for timezone formatting. moment(date, 'M ...

Using React to pass a link into a reusable component

I am currently utilizing a reusable component in React where I pass mostly text and some props. One of the texts requires a linked word, but I am unsure how to embed that link within a string. Any suggestions on how to pass a string with embedded links? Th ...

Activating CORS for .ajax POST requests

I am encountering an error with my .ajax request that reads as follows: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.com/api/GetData. This issue can be resolved by either moving the resource to ...

Printing a webpage through an FGL printer (Boca System) step-by-step guide

I am currently developing a ticketing system website that requires admins to print tickets using a Boca printer. The issue I am facing is that the Boca printer utilizes FGL (Friendly Ghost Language) as a standard for ticket printing. Is there a "secret" m ...

Dropping anchor whilst skipping or jumping

One of my website elements is a drop anchor that connects from a downwards arrow situated at the bottom of a full-page parallax image to another section on the same page. The HTML code snippet for the drop anchor is as follows: <section id="first" cla ...

Is there a way to retrieve the browser console log with Selenium and Java?

Seeking advice on capturing logs for any JavaScript event triggers or errors using Selenium and Java. Any suggestions would be greatly appreciated. I have attempted the following code, but it does not seem to be working as intended: public void HomePage ...

Adjusting the content of a single text box by typing in another

Is it feasible to automatically convert a Nepali date input in one textbox into an English date and display it in another textbox without any page refresh? I have a PHP function that can translate dates between Nepali and English, and I want it to execute ...

The module 'tcp' is missing in Node.js and cannot be located

The node application crashes unexpectedly at the specified line of code: var tcp = require('tcp'), An error message is displayed: node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: C ...

Triggering the onClick event in React to invoke another class components

I'm currently working on implementing a modal popup (stored in the PostView class) so that it appears when any post in the postcontainer class is clicked. As I am new to React, I would greatly appreciate any suggestions for enhancing the code. Post C ...

Connecting two sets of data from a mongoDB database using vue.js

Hey there, I'm a newcomer to vue and mongodb. I've set up two collections - one for storing user details and the other for business details. When a business registers through a form, their information is saved in mongodb. Now, I've created a ...

What exactly entails static site generation?

I have been exploring the concept of static site generation (SSG). The interesting question that arises is how SSG can fetch data at build time. In my latest application, I implemented an event handler to retrieve data based on a user's search keyword ...