Simple Questions: Protocol for Loading Objects/Textures and Defining Variables

A couple of very simple questions:

1. Object and Texture Loaders

I enter the following line each time before loading a texture:
loader = new THREE.TextureLoader(loadingManager);

I enter the following line each time before loading a gltf object:
gltfLoader = new THREE.GLTFLoader(loadingManager);

Do I need to enter these lines each time I load a new texture or object?
Or can I simply enter each line once at the beginning of my program?

2. Defining Variables

To save space, I often define global variables using the following format:
var xrotation, yrotation, zrotation;

However, sometimes the program will not work unless I define the variables as follows:
var xrotation = 0;
var yrotation = 0;
var zrotation = 0;

Does anyone know why this is sometimes necessary?

You can use one loader for multiple objects or texture, you don’t have to create new loader for every asset.

const gltfLoader = new GltfLoader()

gltfLoader.load(gltf1)
gltfLoader.load(gltf2)
gltfLoader.load(gltf3)
gltfLoader.load(gltf4)

when you define var xRotation like this, it means xRotation = undefined and so probably you’re doing some mathematical operations on variable before you assign it to the valid number so it might be breaking because of that.

so what you can do is either assign it to data type you know you are gonna use that variable for, like var xRotation = 0
or wherever you are using that variable just checked if it undefined or not like this before doing some operations on it

if(xRotation){
   some operation on variable
}
1 Like

Thanks for the quick responses:.

  1. I eliminated all the redundant loader assignments and the program works fine.
  2. Regarding variables, that is exactly what was happening. I was using a variable before I had given it a numerical value.
1 Like