Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
How to import JS file in main JS file in Threejs

I'm still new in Threejs ... I want to put some of my threejs code in another JS file and then use it in my main.js file This is a simple similar code:

main.js

import * as THREE from 'three'
import Box from './classes/Parts.js'
const box = new Box()
scene.add(box)

Parts.js

class Box {
  constructor() {
    this.geom = new THREE.BoxGeometry(2, 2, 2);
    this.mat = new THREE.MeshBasicMaterial({
      color: 0xff0000
    });
    this.mesh = new THREE.Mesh(this.geom, this.mat);
  }
}
export default Box;
This is something similar to this

But I get this error: THREE.Object3D.add: object not an instance of THREE.Object3D.

What did I do wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can only add custom objects to the scene graph which are derived from THREE.Object3D. That is not the case for your Box class. Rewrite your code like so:

class Box extends THREE.Mesh {
  constructor() {
    const geom = new THREE.BoxGeometry(2, 2, 2);
    const mat = new THREE.MeshBasicMaterial({
      color: 0xff0000
    });
    super(geom,mat);
  }
}
export default Box;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!