New Neo
New Neo

Reputation: 1

explain by what criteria threejs assigns id to Obejct3d

there is an array of objects in the json file, the objects have current information about *.obj files and their paths. when referring to json as [...jsondata] array and calling each object in the scene through for(i=0; i<jsondata.lenght; i++) all are right, all objects are called up and the stage is lined up. But! The object.id assignment order is not the same as in the jsondata array, and for some reason always starts with id: 96. And most importantly id is not editable, and for me it is very important that the order in the scene was the same as in json. Can someone explain by what criteria threejs assigns this id?

Upvotes: 0

Views: 44

Answers (1)

M -
M -

Reputation: 28492

Don't rely on the Object.id property to maintain any desired order. These are automatically generated, and they can add up quickly if any of your *.obj files have children, grandchildren, lights, cameras, etc.

For example, if you want 2 cars with id = 1 & 2, each part of the car could have a new id, depending on how it was built:

window: 2,
wheels: 3,
headlamps: 4,
light: 5

By the time you start car # 2, you're already on id = 6.

If you want to store a specific attribute in certain objects, you could use the .userData property to store it.

for(let i = 0; i < jsondata.length; i++) {
   object[i].userData.order = i;
}

Or just create your own array to access them in order later:

const orderedItems = [];
for(let i = 0; i < jsondata.length; i++) {
   orderedItems.push(someObject);
}

Upvotes: 1

Related Questions