While working in ThreeJS using TypeScript, I encountered an issue when attempting to clone a custom object that extends Object3D. The problem arises when the field, which is required for creating a new object, becomes undefined
during the cloning process. Here's the code snippet:
import {Object3D, Vector3} from "three";
export class CustomObject extends Object3D{
// a single property
prop: Vector3;
// assigns property value during creation
constructor(prop: Vector3) {
super();
this.prop = prop;
console.log("Prop: ", this.prop);
}
}
// creates an object
let co = new CustomObject(new Vector3(0, 1, 2));
// clones the object
let coc = co.clone();
console.log("Clone prop: ", coc.prop);
Upon inspecting the console output, the following results are observed:
Prop: Vector3 {x: 0, y: 1, z: 2} <---- displayed during initial object creation.
Prop: undefined <---- displayed during clone operation.
Clone Prop: undefined <---- displayed at the end.
The question now arises: Why does prop
become undefined
after the first instance?