Env loaded but not working?

Hello boys:

In my project, I encountered a problem, the following is the phenomenon:

The problem that is hard to describe.
When I keep refreshing (F5), there’s a probability
When I change the code with(HMR Hot Module Replacement), there is also a probability that it will occur

I don’t know if this is a problem, and I’m worried that this problem will be exposed in my production environment.So I wrote a method called reloadEnv, but it doesn’t work, and the same goes for the callback onError

There was a chance that env would load but the scene would not be bright. I only used env to illuminate the scene throughout the scene

The picture below shows a mistake. The scene is dark!!!

The image below shows a correct scene, with the model illuminated

However, I did not repeat this problem with Box on PG!!!

When the problem occurs, you should try to see if the env file is loaded or not by looking in the inspector at the corresponding texture. Also, you should try to use Spector, and when the problem occurs, take a snapshot. You will see in-depth what are the parameters of the rendering of the car and see if the env texture is there.

Hello :slight_smile:

Most likely it comes from asynchronous JS stuffs.

Even more if you have the problem with a car (loading time non negligeable) and not with a cube (instant).

Keep in mind that :

loadSomeAsset(params, function(){
    // on loaded Some Asset
})
loadSomeEnv(params, function(){
    // on loaded Some Env
})

Here you have no clue which one triggers first. So if you have some code with the env, do be done when the asset is loaded, encapsulate the callbacks :


loadSomeAsset(params, function(){
    // on loaded Some Asset
    loadSomeEnv(params, function(){
        // on loaded Some loadSomeEnv
    })
})

Sorry by advance if I explain something absolutely obvious for you :smiley: But by experience, most of “random after F5” stuffs come from non consistent code execution order, due to bad asynchronous handling :stuck_out_tongue:

1 Like

Also as a side note because you mentioned this, be aware of your code because if you have side effects , HMR is not going to always deal with these. That is where you need to rely on hooks to call into these side effect code structures manually, or you need a browser refresh. Although then we are at the other aspects of the problem. I’m pretty sure it is not an engine problem and rather some code you need to change… as @Evgeni_Popov pointed out , use the inspector to debug.

I think even though env is loaded last, it should light up my scene when it’s loaded…

Well, when the problem came up again, I tried to debug it, having only seen it load in devtools

@Evgeni_Popov

Sorry to bother you again!!
I recorded three times with SpectorJS, of which two errors were recorded, but I couldn’t see anything abnormal, because I am also a novice to SpectorJS, I can only compare and capture, and

Unfortunately, I cannot upload json files,it is too big;

When should the scene load env?

this is my env;
Env_Demo.zip (337.4 KB)

I would appreciate it if you could give me some advice

// load skybox
const skyboxTask = this.assetsManager.addCubeTextureTask("skybox", CONTROL_SKYBOX_TEXTURE_NAME);
skyboxTask.onSuccess = ({ texture }) => {
  texture && reloadEnv(texture);
  this.scene.createDefaultSkybox(texture, false, 1000, 1);
};


// load env
const envTask = this.assetsManager.addCubeTextureTask("env", CONTROL_HDR_TEXTURE_NAME);
envTask.onSuccess = ({ texture }) => {
  texture && reloadEnv(texture);
  this.scene.environmentTexture = texture;
};
envTask.onError = (_, __, exception) => {
  console.log("hdr error", exception);
};

I loaded skybox and env at the same time, and when I removed skybox, I refreshed it about 50 times and there were no errors。Could this be the cause?

Are you really sure that the problem is due to the env file not being loaded? Have you tried explicitly not loading the env file and checking whether the rendering is the same as when the problem occurs?

Unfortunately, we can’t tell what’s going on from Spector’s screenshots alone. Can you set up a live link somewhere so we can try it out for ourselves?