Home Technical Talk

Shader - Fresnel Warp

polycounter lvl 12
Offline / Send Message
Ace-Angel polycounter lvl 12
Heya people, how you doing?

I was taking a gander on some materials from the Source engine, and came across this line:

$FRESNELWARP "fresnelranges"

From what I understood, it's basically a textures that wraps around the model, but with a Fresnel as it's feed? Although I'm not too sure about it.

I also tried looking for the written shaders, but cannot find them, so if anyone has any idea what I need to do, that would be grand.

Replies

  • Drew++
    Options
    Offline / Send Message
    Drew++ polycounter lvl 14
    hmm, it might be something like this.
    float3 vReflect = reflect( -vEyeDir, vWorldNormal );                // Reflect view through normal
    float LdotR = saturate(dot( vReflect, vLightDir ));                    // L.R    (use half-angle instead?)
    specularLighting = pow( LdotR, fSpecularExponent );                    // Raise to specular exponent
    
    // Optionally warp as function of scalar specular and fresnel
    if ( bDoSpecularWarp )
    specularLighting *= tex2D( specularWarpSampler, float2(specularLighting.x, fFresnel) ); // Sample at { (L.R)^k, fresnel }
    
    specularLighting *= saturate(dot( vWorldNormal, vLightDir ));        // Mask with N.L
    specularLighting *= color;                                            // Modulate with light color
    
    I'm not 100% sure though. Test it out!
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    Hey Drew, cheers for the snippet, it looks good on my end (just had to change a few things around to compensate for Max :P ) Looking back on the documents and Source shader, it seems like there is some extra magic going on?

    Here is what I mean:
    KdTde.jpg

    Just to be clear, you code worked perfectly fine with the Fresnel Warping lookup texture, so much kudos for that.

    In the document, it's also talking about a warp color texture, which is supposed to be related to the Fresnel Warp, but I have no idea what it is and my google-fu in said terms is coming up empty.

    If you (or anyone else) have hints on what it could be, that would be very much appreciated, because I'm honestly lost on what a 3D Texture which looks like a UV Map is supposed to be or if I'm reading too much into it and it's infact a simple XYZ projection.

    Cheers!
  • Drew++
    Options
    Offline / Send Message
    Drew++ polycounter lvl 14
    Yeah it's basically the same thing, but for diffuse light & using a tex1D
    // fResult is a saturated dot(N, L); 
    // Don't forget half lambert, if needed! saturate(NDotL * 0.5 + 0.5);   
     
    if ( bDoLightingWarp )
    {
       fResult = 2.0f * tex1D( lightWarpSampler, fResult );
    }
    

    *edit* a buddy if mine has a cool implementation of this, has a neat trick for smoothing it out. http://blog.legend286.co.uk/lightwarps/
  • Drew++
    Options
    Offline / Send Message
    Drew++ polycounter lvl 14
    Oh, another thing if you're doing it half-lambert, it needs to be squared.

    here's how the code should be all-together
    float3 DiffuseTerm( float3 worldNormal, float3 lightDir)
    {
        float fResult;
    
        float NDotL = dot( worldNormal, lightDir );    // Unsaturated dot (-1 to 1 range)
    
        if ( bHalfLambert )
        {
            fResult = saturate(NDotL * 0.5 + 0.5); // Scale and bias to 0 to 1 range
    
            if ( !bDoLightingWarp )
            {
                fResult *= fResult;    // Square
            }
        }
        else
        {
            fResult = saturate( NDotL ); // Saturate pure Lambertian term
        }
    
        float3 fOut = float3( fResult, fResult, fResult );
        
        if ( bDoLightingWarp )
        {
            fOut = 2.0f * tex1D( lightWarpSampler, fResult );
        }
    
        return fOut;
    }
    
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    Cheers Drew, much appreciated :D
  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 19
    Sorry if I missed this, but what do the 3 axises in the 3D-texture represent? The direction of the surface normal in camera space?
  • Ashaman73
    Options
    Offline / Send Message
    Ashaman73 polycounter lvl 6
    In the document, it's also talking about a warp color texture, which is supposed to be related to the Fresnel Warp
    From this doc I would say, that it is very similar to a color correction LUT (lookup table). That is you map the present RGB color to new RGB color (therefore a 3d texture). Instead of applying it to the whole scene (=>color correction post processing) you can apply it to the source texture. Either completely (fresnel mask=0) or at the silhouette of the model (fresnel term & mask).

    The effect is most likely an artistic accentuation of the silhouette. The mask seems to support both effects, the light warp and the color fresnel term.
  • Ashaman73
    Options
    Offline / Send Message
    Ashaman73 polycounter lvl 6
    Pseudo shader code(color fresnel warp):
    vec3 textureColor = tex2d(modelTexture, uv);
    float fresnelMask = tex2d(fresnelTexture,uv);
    vec3 warpedColor = tex3d(warpTexture, textureColor.rgb);
    float fresnelTerm = calculateFresnelTerm(viewVector,normalVector);
    float alpha = fresnelTerm * fresnelMask ;
    vec3 finalColor = alpha * warpedColor + (1-alpha) * textureColor;
  • Hazardous
    Options
    Offline / Send Message
    Hazardous polycounter lvl 12
    kodde wrote: »
    Sorry if I missed this, but what do the 3 axises in the 3D-texture represent? The direction of the surface normal in camera space?

    Exact same question I had.

    Is it basically to drive the color of the fresnel?

    EDIT: Duh... just read valve's pic. lol

    proceed.....
Sign In or Register to comment.