How to add/load multiple object in OBJLoader

How to load multiple object with single intense in OBJLoader.

What have you tried so far? You just need to call loader.load() as shown in the examples, except multiple times.

Yes, I have used same process as multiple time .

I need global object to handle easy or manipulate global object.

I am defining and explain some example.

var loader = new THREE.OBJLoader( manager );
loader.load('obj/RB_inside_Right_side_chain.obj', function ( object ) {
					
				inside_object=object;
});

	loader.load('outside.obj', function ( object ) {
                 outside_object=object;
});

I want to need manipulate both inside_object and outside_object.

  1. Update texture and color of both object.
  2. Resize object(if reside inside object then automatic resize outside object)

resize=>

function resize(){
             inside_object.scale.x = 0.042;
             inside_object.scale.y = 0.04;

                outside_object.scale.x = 0.042;
                outside_object.scale.y = 0.04;
}

=> it is working but code is repeating.

please help me.

What code is repeating? If you mean that you are repeating loader.load, then that’s normal. You will need to call it once for each object that you load.

Thank you @looeee .

Yes code is repeated.
=>inside_object related functionality
=>outside_object related functionality

=> Inside and outside object both are similar. if apply any functionality of inside object then as also would be apply outside object.

=>inside object
image

=> outside object
image

So what you are saying is that you want to do this:

inside_object.scale.x = 0.042;
inside_object.scale.y = 0.04;

outside_object.scale.x = 0.042;
outside_object.scale.y = 0.04;

in 2 lines instead of 4?

Yes,

This type of functionality have many times.
For example:-

  1. Change the texture color.
inside_object.traverse( function ( child ) {
    child.material.map = THREE.ImageUtils.loadTexture(texture_img);
} );

outside_object.traverse( function ( child ) {
    child.material.map = THREE.ImageUtils.loadTexture(texture_img);
} );
  1. Resize the object:-
inside_object.scale.y = 0.04;
outside_object.scale.x = 0.04;
  1. Rotation:-
outside_object.rotation.y = 1.57;
inside_object.rotation.y = 1.57;

.
Have the many type of functionality.

Code reuse in JavaScript is usually accomplished with helper functions. For example:

var insideObject, outsideObject;

function applyTransforms(object) {
  object.scale.x = 0.042;
  object.scale.y = 0.04;
}

loader.load('inside.obj', function(object) {
  applyTransforms(object);
  insideObject = object;
  next();
});

loader.load('outside.obj', function(object) {
  applyTransforms(object);
  outsideObject = object;
  next();
});

function next() {
  if (insideObject && outsideObject) {
    console.log('done');
  }
}

See Eloquent JavaScript • Functions for more information about how to do this.

2 Likes

@sandeep can you please format format your code when posting here? I’ve edited your posts above, check the edits to see how to do this.