Three.js shader pointLight to global position

2019-08-03 16:44发布

All hello, I'm doing a fog shader and trying to add light sources.

Recently I asked how to correctly determine the position of the light source, the question was solved, but another problem turned out, for the fog I use the position of the modelMatrix

vec3 fogVPosition = (modelMatrix * vec4( position, 1.0 )).xyz

To calculate the intensity of light, I use the calculation of the distance from the light source to the point of fog.

float diffuseCoefficient = max( 1.0 - (distance(pointLights[i].position,fogVPosition) / plDistance), 0.0)

But how did I find out that the position of the light source is transmitted relative to the camera, so I can not calculate the distance correctly and the light source changes its position if I shuffle the camera.

Here's an example of what I did http://codepen.io/korner/pen/BWJLrq

In this example, it can be seen that the light sources are not positioned correctly on the surface.

My goal is done as on this screen: http://dt-byte.ru/f97dc222.png

This works if you put the global position of the light source manually, I just need to somehow get the position of the light source in the global position pointLights[i].position

The problem is solved!

Forgive my friends, I stepped, I did not realize that you can add a modelViewMatrix

I added one more variable ligVPosition

fogVPosition = (modelMatrix * vec4( position, 1.0 )).xyz;
ligVPosition = (modelViewMatrix * vec4( position, 1.0 )).xyz;

Then changed the fogVPosition to the ligVPosition

float diffuseCoefficient = max( 1.0 - (distance(pointLights[i].position,ligVPosition) / plDistance), 0.0)

Now everything works fine!

0条回答
登录 后发表回答