Announcement

Collapse
No announcement yet.

Vray Fresnel to CGFX Shader Fresnel

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Vray Fresnel to CGFX Shader Fresnel

    Dear all,

    I am trying to write a script to automatically convert Vray Materials to FX-Shader for later converting them to a separate scene for a realtime engine. The script does automatically parse the materials and recursively goes through all texture nodes (also procedural). Now I have a bit of trouble to work with the fresnel formula. I understand the concept of fresnel and got in the internet a simplified formula to approximate fresnel. The formula somehow works in the shader, but the fresnel reflection is always much darker than the rendered fresnel in VRay. Can anybody tell me what is wrong with this approximation?

    //fragment shader excerpt
    float3 psOutNormal = normalize(psIn.PSiNormal.xyz);
    float3 psOutViewDir = normalize(psIn.PSiViewDir);

    CUV = normalize( reflect( psOutViewDir, psOutNormal).xzy);
    CUV.y = -CUV.y;
    CUV.x = -CUV.x;
    env = pow(texCUBE(g_ReflectionSampler, CUV), 0.454); //gamma correct HDR-cubemaps


    float nAirIOR = 1.000293;
    float matIOR = 1.6; //example
    float R0 = pow((nAirIOR - matIOR), 2) / pow ((nAirIOR + matIOR), 2);
    blendAmount = R0 + (1 - R0) * pow (1.0 - dot(psOutNormal, psOutViewDir ), 5.0 );
    psOut.Colour.rgb = lerp(diff * light, env, blendAmount); //diff is diffuse texture or color, light is hdr-lightmap texturebaked within max. In my example both are completely black, as the vray shader is in this example, just the reflection is white with fresnel index.

    Rendered Scene with reflection color rgb pure white, no camera exposure, diffuse and refraction pure black, gammacorrected:

    Click image for larger version

Name:	rendering_01.jpg
Views:	1
Size:	31.2 KB
ID:	877182

    Rendered Scene with reflection color rgb pure white, no camera exposure, fresnel reflections IOR1.6, diffuse and refraction pure black, gammacorrected:

    Click image for larger version

Name:	rendering_fresnel_1.6_01.jpg
Views:	1
Size:	56.8 KB
ID:	877179

    Cubemap rendered from max (somehow 20% brighter., but doesn't matter, rendered from the center of the sphere, box camera, no exposure):

    Click image for larger version

Name:	cubemap_01.jpg
Views:	1
Size:	38.9 KB
ID:	877180

    Vertical cross converted to dds for fx-shaders in Max:

    Click image for larger version

Name:	cubemap_rotated.jpg
Views:	1
Size:	27.1 KB
ID:	877181

    This is a screenshot from Max with the fx-shader applied on the sphere only. See that it is much darker than the fresnel of 1.6 in Vray:

    Click image for larger version

Name:	shader_fresnel.jpg
Views:	1
Size:	65.5 KB
ID:	877188

    Can anybody help me on this?

    Thanks!
    Last edited by Robert1977; 28-01-2013, 04:01 AM.
    Robert

    Max, VRay, Fusion:

    https://www.youtube.com/watch?v=g5fSLrVzpxg
    https://www.youtube.com/watch?v=bpmJgTb_9Ro

  • #2
    Think I got it. Have to gamma correct the whole rgb-out-color in the end, after the env-cubemap is multiplied with the fresnel-factor, not gammacorrecting the hdr-cubemap before:

    env = texCUBE(g_ReflectionSampler, CUV);
    psOut.Colour.rgb = pow(lerp(diff * light, env, blendAmount), 0.454);
    Robert

    Max, VRay, Fusion:

    https://www.youtube.com/watch?v=g5fSLrVzpxg
    https://www.youtube.com/watch?v=bpmJgTb_9Ro

    Comment


    • #3
      Yes, you need to apply the gamma after everything else (ideally in a post-filter, but that's a different story).

      Also, I see that you are using the Schlick approximation for the Fresnel formula. However V-Ray uses the full formula, not the approximation. This may lead to some slight differences.

      Best regards,
      Vlado
      I only act like I know everything, Rogers.

      Comment


      • #4
        Thanks, Vlado.

        maybe the formula differences are not noticeable in the VR environment. But if you want you can post the full fresnel formula used by Vray, I could try this later and test if there are strong performance issues on the hardware with large scenes.
        Strange somehow that the cubemap itself without fresnel is about 30% brighter than the one rendered on the sphere with full reflection on and also no fresnel. Camera settings are the same, without exposure. Do you know why?

        Cheers and thank you!

        Robert
        Robert

        Max, VRay, Fusion:

        https://www.youtube.com/watch?v=g5fSLrVzpxg
        https://www.youtube.com/watch?v=bpmJgTb_9Ro

        Comment

        Working...
        X