World space camera position and view direction in an outline shader


I’m creating an outline shader based on surface normals and view direction. The logic is: if a fragment is near the edge of a surface, it gets colored in black - an outline. The shader finds out whether a fragment is near an edge by obtaining a dot product of it’s normal (interpolated from vertex shader in world space) and the view direction (i’d like this to be world space camera position minus interpolated world space vertex position). So far, I can’t seem to figure out how to get the world space position of the camera. What I try to do alternatively is get my view direction from my model-view matrix like this: vec3 viewDir = vec3(modelViewMatrix[2].xyz); - which is just the 3rd colums of the matrix.

If I compute the shader it outputs a correct result. However - if I move in the scene and change the position of my camera, the result does not seem to recompute , which is not what I want. I want the result to update itself as I move my camera, but it looks like it gets computed once and does not react to a change in camera position. I suspect I compute the view direction incorrectly. (I want to include images here but can’t because the board won’t let me post with images :/)

The reason why I want to use the world space camera position for this is because I have implemented the shader with GLSL in the game engine Unity - it provides a uniform _WorldSpaceCameraPos, which computed the camera world space position for me. Now, when I need to compute it myself, I can’t. Could anyone please give me tips on how to get the world space camera position? Apologies for the clunky english, it’s 5AM at the time of writing this post.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.