Normal Fog as d3d_set_fog but in a Shader (GM 1.4)

Discussion in 'Programming' started by Edgamer63, Mar 17, 2019.

  1. Edgamer63

    Edgamer63 Member

    Joined:
    Jan 24, 2018
    Posts:
    66
    Hello, i want to know if somebody can help me with this... i don't know how to restore my fog settings... because i drawed 3D Things with a Shader... but, i dont know where my d3d_set_fog gone ... so, i need to know how to draw a fog in a shader...


    Also Useful (RGBA Color set Uniform):

    Code:
    ///shader_set_uniform_rgba(uniform,r,g,b,a)
    
    shader_set_uniform_f(argument0,argument1 / 255,argument2 / 255,argument3 / 255,argument4);
    Source: GMLscripts.com
     
  2. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,057
    You're going to need 5 uniform or constant components. 3 for the colour of the fog. And 2 more to define its start and end distances.

    The first thing you need to do in the vertex shader is get the distance from the camera to the vertex, and pass that distance as a varying to the fragment shader. I think that can most easily be accomplished like this:
    Code:
    varying float dist;
    //....
    dist = length(vec3( gl_Position.xy / vec2(gm_Matrices[MATRIX_PROJECTION][0][0],gm_Matrices[MATRIX_PROJECTION][1][1]), gl_Position.w ));
    Some applications only use depth as a basis for calculating fog, but that causes the fog level to change weirdly as you rotate the camera. So why not use the proper distance instead? That division by a couple of matrix elements is to bring the vertex position into view space as opposed to whatever is the name of the space which results from the projection matrix multiplication. You could also just multiply the object space column vector with MATRIX_WORLD_VIEW, but that is a lot of extra math. Note: The above is for when using a perspective projection, it will give incorrect results with an orthographic projection.

    Then in the fragment shader, you use the distance value to compute the fog level. Fog level should be (dist-near)/(far-near), and you'll want to clamp that between 0 and 1. It helps if you precalculated 1/(far-near) and pass that in as the second component of your "fog" uniform. So, x component of "fog" would be its near value. And y component would be 1/(far-near).
    Code:
    float fog_level = clamp((dist-fog.x)*fog.y,0.0,1.0);
    You can then use fog_level to interpolate between the base colour of the fragment, and the fog colour:
    Code:
    vec4 base_colour = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
    gl_FragColor = vec4( mix( base_colour.rgb, fog_colour, fog_level ), base_colour.a );
    I feel like there could be some possibility of error there when dealing with sem-transparent images. So if you are making use of any, double check that produces correct results.
     
    Last edited: Mar 17, 2019
    Edgamer63 likes this.
  3. Edgamer63

    Edgamer63 Member

    Joined:
    Jan 24, 2018
    Posts:
    66
    Do you got any idea of how to use: "gl_FogCoord" and "gm_FogStart"? :/
     
  4. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,057
    I've never heard of gl_FogCoord.

    As far as I know the gm fog uniforms don't work in cursom shaders. This information might be out of date. You don't really need them though, since you can just make your own uniforms.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice