Having trouble loading a model with GLTFLoader in Vue2

I am utilizing GLTFLoader to import a model into my Vue2 application. My implementation follows the standard Three.js template. I directly copied the GLTFLoader code from the Three.js documentation. Despite creating a simple example, I am facing difficulties in loading the model. I have thoroughly checked my code for errors, but couldn't find any:

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


<script>
import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'

export default {
  name: 'Scene',

  data() {
    return {
      camera: null,
      scene: null,
      renderer: null,
      mesh: null
    }
  },
  methods: {
    init() {

        this.camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
        this.camera.position.z = 1;
        this.scene = new THREE.Scene();

        this.renderer = new THREE.WebGLRenderer({antialias: true});
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        this.$refs.container.appendChild(this.renderer.domElement);

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

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

For a reproducible example, you can check: https://codesandbox.io/s/vue-2-playground-forked-q6lchp?file=/src/components/Model.vue

Answer №1

I have successfully addressed and fixed the existing as well as subsequent issues in the codesandbox. Check it out here: https://codesandbox.io/s/vue-2-playground-forked-tlpdi9.

The following problems have been fixed:

  1. The Home component was missing from your App component.
  2. You should use three instead of three-js.

There are additional updates in the linked codesandbox, such as adjusting the far value for your camera and incorporating lighting to enhance the visibility of the avocado.

https://i.sstatic.net/cTcRh.png

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

What is the best way to save characters from different languages into variables?

I created an application that reads words from a TXT file and stores them in a database. The issue arises when I encounter words from other languages that contain non-English characters, resulting in the following problem: Is it possible to store these ch ...

Vue: Implementing seamless transitions for dynamic component instances sharing identical templates

Currently, I am encountering an issue while working with Vue 3 alongside Tailwind CSS in terms of transitioning between dynamic components. Following the steps outlined in the official documentation, I have attempted the following code snippet: ...

What could be causing a case where this.props.posts is undefined in React and Redux

Recently, I attempted to follow a React and Redux tutorial on a blog. However, when working with the PostList component, I encountered an error related to the reducer binding. My main goal was simply to render the renderPost function by calling the reducer ...

What is the process for transferring a file's contents to my server?

I am currently working on allowing users to import an OPML file that I parse server-side in my Rails application. However, I am facing difficulties as it appears that my server is not receiving the information correctly (neither the success nor error funct ...

trigger the eventHub within a Vuex store

I'm trying to figure out how I can utilize this.$eventHub.$emit('something'); within vuex. The reason I need this is because I'm using a plugin called vuex-persist-indexeddb, which has a method called rehydrated that fires when the data ...

Using jQuery, check if the input contains any phrases from the array that are suitable for children

I stumbled upon some code designed for a chat system. My plan is to implement it as a child-friendly global chat, so that I can avoid any blame associated with inappropriate content. The basic premise of the code involves checking user input against an arr ...

AJAX request post parameters not recognized in ColdFusion form scope

I am currently developing a ColdFusion 8 training application that involves creating AJAX requests without using any libraries like jQuery. This is to support a basic CRUD application where data is retrieved and processed through various layers of the syst ...

Import a component in Vue Router based on certain conditions

Currently, I am looking to conditionally import a component in the Vue router and here is my current setup: children: [ { path: ':option', component: () => import('../components/Option1.vue'), }, ], I need to dynamically ...

Exploring the Power of NPM Modules in an Electron Renderer

Having trouble accessing lodash in an electron renderer. I'm a beginner with Electron and know that both the main process and renderer (local html file) have access to node. I can require something from node core like fs and it works fine, but when I ...

Trouble arises in Vuex when attempting to convert data into an observer

I have implemented Vuex to manage the state of my application. One issue I am facing is that when I dispatch an action to retrieve data from a server API and then commit a mutation, the data seems to be transformed into observer objects instead of remainin ...

`The resurgence of CERT_FindUserCertByUsage function in JavaScript`

I am currently grappling with unraveling the connection between C++ dlls and JavaScript. There is a snippet of js code that reads: cert = CERT_FindUserCertByUsage(certDB, certName.nickname,certUsageEmailSigner, true, null); where the variable cert is ini ...

Populate a Textbox Automatically using a Dropdown List

MVC 4 Changing multiple display fields based on DropDownListFor selection Having some issues trying to implement the solution mentioned above. It seems like there might be a problem with either my javascript code or the controller. JavaScript in View ...

Getting the most out of geometry vertices with Threejs: mastering partial transforms

In my current project, I am working with a mesh that consists of approximately 5k vertices. These vertices have been merged from multiple geometries into a flat array. Initially, I was able to modify individual vertices successfully by setting the flag `ve ...

The response you have received is delayed by one response

I seem to be facing an issue with my Node.js server where the response I receive is always delayed by one. The response I expect to get at the time of pressing the upload button is always received one step later. After starting the Node server, the initia ...

Instructions for including dependencies from a globally installed npm package into a local package

I've noticed that although I have installed a few npm packages globally, none of them are showing up in any of my package.json files. What is the recommended npm command to automatically add these dependencies to all of my package.json files? ...

Send the id of the chosen row to the Component tag within the blade file

I'm working on passing the id of the currently selected row within a for loop when it's clicked on. The goal is to then pass that id to a Vue component. In my index.blade file: @foreach($cats as $cat) <tr> <td class="catme" d ...

How can I change the background color of a parent div when hovering over a child element using JavaScript

I am working on a task that involves three colored boxes within a div, each with a different color. The goal is to change the background-color of the parent div to match the color of the box being hovered over. CSS: .t1_colors { float: left; wid ...

Tips for resolving the "unsafe-eval" issue in Vue3 on the client-side platform

My app is built using Express, cors, and helmet. I have incorporated Vue3 on the client-side only, with the library file being self-hosted in the public folder. To add the module to the client-side, I simply include the following script: <script type=&q ...

Guide on importing a React.Component from a Content Delivery Network (CDN) and displaying it within a separate React

Note: None of the solutions provided are effective [DO NOT REMOVE THIS NOTE] Here's a simple question for you - I have a project named Project Y, created using the command npx create-react-app react-project. Now, in the App.js file of Project Y, I h ...

Troubleshooting the issue with formatting dates in AngularJS

I need help converting 2015-04-17 12:44:38.0 to dd/MM/yyyy format using angularjs <td ng-bind="item.MON_FE_DUEDATE | date:'dd-MM-yyyy'"></td> However, it is still displaying 2015-04-17 12:44:38.0 only. Can anyone please point out w ...