aframe error: The function Entity.setObject3D encountered an object that did not meet the requirements of being an instance of THREE.Object3D

Hey there, I've been experimenting with adding a mesh to my aframe component, but I keep encountering an unusual error even when using a simple code snippet like the one below:

AFRAME.registerComponent('mysquare', {
      init: function(){
        var el = this.el; 

        var box = new THREE.BoxGeometry(40, 5, 40);
        var boxMesh = new THREE.Mesh(box);
        boxMesh.position.set(25, 0, 25);
        el.setObject3D("mesh", boxMesh);

      }
    });

home.html

<ion-content>
      <div></div>
      <a-scene embedded>
         <a-entity mysquare></a-entity>
       </a-scene> 
</ion-content>

Error message

Error: Entity.setObject3D was called with an object that was not an instance of THREE.Object3D.

I also tried using the add function but received the same error. Why could this be happening?

I am currently developing my app with Ionic 2 and aframe v.0.7.1. I also tested with version 0.5.0, but encountered the same issue.

Answer №1

After much trial and error, I was able to find a way to resolve the issue without directly including the threejs library.

The import section that worked for me looked like this

import * as THREE from 'three';
declare var AFRAME;

I decided to remove the first import statement and miraculously everything started working perfectly. Now, I can easily create a 3D object using this method

el.setObject3D("mesh", new AFRAME.THREE.Object3D());

Answer №2

During my work on an Angular 9 project, I encountered a situation where webpack was being used to manage dependencies.

The specific imports in question were:

import { Component, OnInit } from '@angular/core';
import * as AFRAME from 'aframe';
import * as THREE from 'three';

...

AFRAME.registerComponent('box', {
...
  // Create mesh.
  this.mesh = new THREE.Mesh(this.geometry, this.material);

  // Set mesh on entity.
  el.setObject3D('mesh', this.mesh);
...

The error message displayed in the console matched the one you are experiencing:

core:a-node:error Failure loading node:   
Error: `Entity.setObject3D` was called with an object that was not an instance of THREE.Object3D.

A closer look at the transpiled code revealed that the import for THREE was treated as a Module object:

/* harmony import */ var three__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! three */ "./node_modules/three/build/three.module.js");

This meant that

three__WEBPACK_IMPORTED_MODULE_2__
was used as the reference for creating the Mesh, rather than the global THREE object. Meanwhile, the imported AFRAME did have a reference to the global THREE.

three__WEBPACK_IMPORTED_MODULE_2__ === THREE
// false
aframe__WEBPACK_IMPORTED_MODULE_1__.THREE === THREE
// true

In TypeScript, AFRAME.THREE can be recognized as the THREE type, eliminating the need for importing THREE separately if using AFRAME.THREE.

Here is an example illustrating the usage of registerComponent:

AFRAME.registerComponent('box', {
  schema: {
    width: {type: 'number', default: 1},
    height: {type: 'number', default: 1},
    depth: {type: 'number', default: 1},
    color: {type: 'color', default: '#AAA'}
  },
  init() {
    const data = this.data;
    const el = this.el;
    // Extract THREE
    const {THREE} = AFRAME;

    // Create geometry.
    this.geometry = new THREE.BoxBufferGeometry(data.width, data.height, data.depth);

    // Create material.
    this.material = new THREE.MeshStandardMaterial({color: data.color});

    // Create mesh.
    this.mesh = new THREE.Mesh(this.geometry, this.material);

    // Set mesh on entity.
    el.setObject3D('mesh', this.mesh);
  }
});

Answer №3

After some experimentation, I found a way to integrate it smoothly with my Angular 13 application. Here's how I did it:

npm install aframe
npm install -D @types/aframe
npm install -D @types/three

Next, I imported it into one of my components like this:

import * as AFRAME from 'aframe';

Finally, I utilized it in my code by registering a new component:

AFRAME.registerComponent('test-obj', {
    init: function () {
    }
});

https://i.sstatic.net/VbQi4.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

The third if-clause is inaccessible

I am facing a challenge that I believe has a simple solution, but I'm unable to see it right now. I have three if-clauses that should be triggered based on the length of an array. Surprisingly, the first two clauses work perfectly fine, but I can&apos ...

Is it feasible to incorporate Mat-Paginator into a standard table within Angular?

I created an HTML table in Angular to display data from an API, and now I'm looking to incorporate a mat-paginator. Below is the code snippet: <table class="table" style="text-align: center;"> <thead class="thead-lig ...

"Ways to retrieve an array of dates within a specified range of date and time

I am working with date fields in my project { tripScheduleStartDate: '2018-12-05T18:30:00.000Z', tripScheduleEndDate: '2018-12-07T18:30:00.000Z', } Is there a way to generate a datetime array from the start date to the end date, lik ...

The texture in THREE.js does not appear when using the SVGRenderer

We attempted the following code snippet: var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.0001, 999); camera.position.set( 0, 0, 3 ); scene.add( camera ); var material = new THREE.Mes ...

Broswer-based location retrieval APIs that do not rely on window.navigator to prompt users for their location

Is there an API available, other than those from Google, that can provide user location data for use with Mapbox GL and React? I'm already familiar with using window.navigator's getCurrentPosition method to access geolocation information. const ...

Dynamic HTML Table with Ajax Click Event Trigger

I have implemented an HTML table that refreshes automatically every 5 seconds and contains some buttons. The issue I am facing is that the event only triggers before the initial refresh. <?php require_once ('UserTableHtm ...

The JSON file containing API data is stored within the _next folder, making it easily accessible to anyone without the need for security measures or a login in the Next

When accessing the protected user Listing page, we utilize SSR to call the api and retrieve all user records which are then rendered. However, if one were to check the Network tab in Chrome or Firefox, a JSON file containing all user data is generated and ...

Display or conceal a div element individually

My task involves modifying this code, which is the original version of a FAQ with three answers that can be toggled to show or hide on click. I need to revise it so that only one answer is displayed at a time and the others close. I received advice to us ...

What causes the React white screen of death to disappear when this code is commented out?

Currently, I am working on a React application that pulls trivia questions and answers from an API to create a game interface. Everything has been going smoothly with the development process until I imported a decode function to properly display the trivi ...

Leveraging fetch for sending JSON payloads

I am currently facing an issue while attempting to post multiple points of data from a form input. Despite my efforts, it seems that the form data does not make it to the json output payload as expected. I have verified this through network output inspect ...

Strange behaviors when using setinterval with classes

Currently working on enhancing a slider functionality, and I have observed that everything is functioning smoothly - function Slider(element) { this.i = 0; this.element = element; var self = this; this.timer = window.setInterval(functi ...

Problem: The variable "$" is not defined in angular datatables causing a ReferenceError

Trying to implement Paging and sorting in my table, but encountered an error even after following all the steps mentioned here: . Tried troubleshooting the issue with no success. Ensured that all dependencies were installed properly. Below is the compo ...

What is the process for updating or upserting a document in Mongoose?

Maybe it's the timing, maybe it's me struggling with sparse documentation and not quite grasping the concept of updating in Mongoose :) Let's break it down: I have a contact schema and model (abbreviated properties): var mongoose = requir ...

Leveraging JavaScript event handlers within a progress wizard located within an update panel

I need assistance with implementing a password strength meter for a textbox within a wizard control. The issue I'm facing is that the password box does not become visible until step 4, preventing me from registering an event handler onload(). Placing ...

The build process encountered an error while processing the module vue-router.esm.js in a Vue.js

Just started using VueJs and decided to incorporate the cli-plugin-unit-jest into my project. However, after adding it, I encountered the following error: Failed to compile. ./node_modules/vue-router/dist/vue-router.esm.js Module build failed: Error: ENO ...

Calculate the total of the temporary information entered into the input field

I'm totally new to all of this and definitely not an expert, but there's something that really bothers me. I found a website where you have to complete a captcha to log in. It goes like this: <input type="text" class="form-contr ...

Verify for any alterations in the code by utilizing the inspect element feature

Currently dealing with a security concern regarding my older Java application. Is there a method available to detect any modifications made to my code (HTML or script) through Developer Tools on the user's end? This would allow me to prevent form subm ...

Validation with Javascript can trigger the display of a hidden element

Hello there, I could really use some assistance with the JavaScript part of this code. I have two JavaScript functions: one for validating if a radio checkbox is selected, and another for revealing the "answer". Currently, an alert pops up if no selections ...

Extracting specific components from an XML document?

I am relatively new to XML and currently working on handling the following XML file. Is there a way for me to extract specific information from each company? For example, I am interested in only displaying the content of the industry element. <compan ...

Using Ajax to send data from a parent JSP to a child JSP and then refresh the page

Can anyone assist me in resolving my issue? I'm attempting to incorporate an existing JSP partial (child) into another JSP page (parent). These pages are controlled by Java Controller classes in a Weblogic 12c environment using Spring. The child JSP i ...