Add fog to the scene

I add fog to my scene with three .js (3d visual javascript programming). This is a cosmic scene. The logic is that when the camera moves toward the sun, the yellow fog thickens, which creates the illusion that space illuminates more, the closer you are to the sun. And vice versa, when you move away from him. This, in essence, should add two mists, they both work when I use one or the other, but never at the same time.

I am trying to create a conditional expression that determines which fog should be activated based on the position of the camera z, seeing that the effects of the fog become visible only when the camera is in the immediate vicinity of 1000 (pixels?) Or above it.

This statement only works for condition Fog # 1, and not for # 2. What am I doing wrong here? could it be because by default camera.position.z is already within 1000? Then how can I use the current position, and not by default. Is it really possible to have two fogs in a scene?

Thank.

if (camera.position.z < 1000)  /*Fog condition #1:   the farther away, the darker it gets*/            

{ 
var varColor = 0xffff00;
var varNear = 800;
var varFar = 5;
}

else if (camera.position.z > 1000)     /*Fog condition #2:    the closer to the sun object, the lighter it gets*/
{
var varColor = 0x000000;
var varNear = 50;
var varFar = 7000;
}

scene.fog = new THREE.Fog( varColor, varNear, varFar);
+4
source share

Source: https://habr.com/ru/post/1657413/


All Articles