I’m trying to implement a simple fresnel shader that i got from this example:
I’m using the camera-controls plugin as a camera for a game, so just to make sure, I rewrote it with the new controls, but it works as well:
I’m working on a bigger game and need to implement that shader for a sphere. Weirdly, when I implement it, although it’s exactly the same shader, it doesn’t adapt to the cameras position. The fresnel is kind of stuck to a certain direction. Here’s a screencast:
The game has become a little complex, so i can’t too easily write a fiddle for the demo. Maybe someone with more experience can already have an idea what could be the problem. I thought maybe somehow “cameraPosition” doesn’t get passed correctly? Is this possible?
If more info is needed, I’d gladly prepare another fiddle.
Just a random shot: maybe you’ve got
.matrixAutoUpdate = false; somewhere in your code for that object of shpere?
I tried to repeat the issue, using
.matrixAutoUpdate = false for a spherical mesh: Edit fiddle - JSFiddle - Code Playground
Note, that the object has updated matrix to position it properly in the scene, far from its center, then
matrixAutoUpdate = false is used. And the result looks similar to what you’ve got on the video provided.
If you set it to true, does it work for you then?
On my browser there’s no difference if I set
matrixAutoUpdate = false or
matrixAutoUpdate = true actually.
But your idea seems right. Because it the game it’s “far” off center
(9,8,177) it might be the reason. Also the “rotation” of the fresnel is different depending on where the sphere is placed in the scene.
Is it possible, that there is a logical error in this part of the shader? That it maybe doesn’t calculate the right viewing direction if the camera and the object is offset?
vec3 viewDirectionW = normalize(cameraPosition - vPositionW);
When I set it to
vec3 viewDirectionW = vec3(0, 0, 1); the shader is also just fixed. Maybe this calculation is not adapting to the cameras perspective?
Maybe one needs to pass
modelViewMatrix to the shader because of the offset?
Thank you so much!
Works like a charm!
I hope, the example is helpful