G
Gun_Goose
Guest
Hi, I am trying to implement a 3D fog of war shader (i have been struggling with this for a while). I have tried to build one from scratch as well as implement on I got off the market place and each time, I have run into a similar problem. I am able to create the proper effect, however the the light is not center on the position of the character I pass it (it even follows the character, expect seems to move a different ). I think it might be a problem with the fact that I am drawing everything with a matrix, and the positions would not line up, but I am not sure anymore. Any ideas?
The current shader I am testing is one I found on the market place:
vertex:
Fragment:
Here is also how I am calling the shader in the post draw event:
p_x, p_y, p_z are the characters coordinate. I have also tried switching them for the coordinates of my camera object but ended up with the same result.
The current shader I am testing is one I found on the market place:
vertex:
Code:
//
// Simple passthrough vertex shader
//
attribute vec4 in_Position;
//attribute vec3 in_Normal;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec4 WorldPos;
void main()
{
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * in_Position;
WorldPos = gm_Matrices[MATRIX_WORLD] * in_Position;
v_vColour = in_Colour;
v_vTexcoord = in_TextureCoord;
}
Code:
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec4 WorldPos;
uniform vec3 u_fog_colour; //Red, Green, Blue
uniform vec3 u_fog_height; //Z near, Z height, Falloff
uniform vec3 u_camera_position; //Cameras X, Y, Z
void main()
{
vec4 base = texture2D(gm_BaseTexture, v_vTexcoord);
float dist = length(WorldPos.xyz - u_camera_position);
float fog = u_fog_height.z * smoothstep(u_fog_colour.z, u_fog_height.x, u_fog_height.y - WorldPos.z);
float ext = exp2(-dist * fog);
vec3 finalColor = base.rgb * vec3(ext,ext,ext) + u_fog_colour.rgb * (1.0 - ext);
gl_FragColor = /*v_vColour * */vec4(finalColor, 1.0);
}
Code:
shader_set(sh_fog_try);
shader_color = shader_get_uniform(sh_fog_try, "u_fog_colour")
shader_height = shader_get_uniform(sh_fog_try, "u_fog_height")
shader_pos = shader_get_uniform(sh_fog_try, "u_camera_position")
shader_set_uniform_f(shader_color, 0, 0, 0);
shader_set_uniform_f(shader_height, 100, 25, 0.04);
shader_set_uniform_f(shader_pos, p_x, p_y, p_z);
draw_surface(application_surface,0,0)
shader_reset()