1

I'm trying to create a custom object using ThreeJS called Model that is composed of other custom objects I have defined, such as Part. This is where I get my error:

const Model = function() {
      this.mesh = new THREE.Object3D();
      this.mesh.name = 'model';

      //creates instance of part
      this.lowerPart = new Part();
      this.lowerPart.position.set(1, 2, 3, -75);  //TypeError here
      this.lowerPart.rotation.set(0, 0, 0);
      this.mesh.add(this.lowerPart);
}

However, when I run my program, it says that it cannot read the property 'set' in undefined, referencing this.lowerPart.position.set(1, 2, 3, -75);.

Here is how I basically defined Part:

 const Part = function () {
      this.mesh = new THREE.Object3D();
      this.mesh.name = 'part';

      const partShape = new THREE.Shape();
      partShape.lineTo( 40, 80 );
      partShape.lineTo( 60, 80 );
      partShape.lineTo( 60, 100 );
      partShape.lineTo( 40, 100 );

      //extrude settings defined....

      const partGeometry = new THREE.ExtrudeGeometry(partShape, extrudeSettings);
      const partMesh = new THREE.Mesh(partGeometry, new THREE.MeshStandardMaterial({
        color: 0x1c4bc9,
        flatShading: true
      }));
      this.mesh.add(partMesh);
    };
    Part.prototype = Object.create(THREE.Object3D.prototype);
    Part.prototype.constructor = Part;

Another thing to note in my function Model is that when this.lowerPart is created, it is labeled as unused. I am unsure of the reason since it is type Object3D which has these properties.

The console also proves that this.lowerPart is an instance of Part.

I've looked and tried most of the suggested StackOverflow questions related to my problem. Out of all of them, this seems the most relevant: Uncaught TypeError: Cannot read property 'set' of undefined However, it didn't work for me.

Any suggestions to how I can fix my issue is greatly appreciated!

0

1 Answer 1

1

If you want to derive Part from THREE.Object, you also have to execute the following line of code in the constructor of Part (it should be the first line in the ctor):

THREE.Object3D.call( this );

Only then it's possible to access properties like position, rotation or scale with an instance of Part.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your response! Unfortunately, when I added this, it gives me this error: THREE.Object3D.add: object not an instance of THREE.Object3D. I think it may be referring to where I call my Model function with model = new Model(); but it doesn't give me the exact line where the error is occurring.
I also see that I get another error where I added THREE.Object3D.call(this); that says TypeError: Cannot set property 'uuid' of undefined
Any chances to share your code as a live example? In such cases, it's much easier to provide help when the community can debug your issue.
Yes, I just added it in: jsfiddle.net/hannahbernal/0bkc8ujw/18 I'm new to JSFiddle so I wasn't sure how to attach the JS file to the HTML, but what I have now is the basics of my program
Ok, I think your answer did resolve my issue! I think the problem was that I made an instance of my Model and I needed it to be a function, instead of a variable in my case. Thank you for the help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.