A simple implementation of the Fresnel effect of CG

Source: Internet
Author: User
Tags mul

Fresnel effect, refers to when the light reaches two kinds of material contact surface, some light on the surface of the contact surface is reflected out, and the other part of the light will be refracted through the contact surface.

Now to use shader to achieve this effect, if you want to accurately describe the underlying physics, its computational formula is very complex, the performance of the consumption is relatively large. Our goal is to make the created image look real, so instead of using the Fresnel formula itself, we use the following empirical formula, which can achieve very good results with very few calculations.

reflectioncoefficient = max (0, min (1, bias + scale * POW (1 + dot (i,n), power))

Finalcolor = reflectioncoefficient * reflectedcolor + (1-reflectioncoefficient) * Refractedcolor

The rendering effect in Unity3d is as follows:

An example written with Shaderlab shader is as follows:

1Shader"custom/test"2 {3 Properties4     {5_cube ("Cube", Cube) =" White" {}6_etaratio ("Eta ratio",float) =0.87_fresnelpower ("Fresnel Power",float) =28_fresnelscale ("Fresnel Scale",float) =19_fresnelbias ("Fresnel bias",float) =0Ten     } One  A Subshader -     { - Tags the         { -             "Rendertype"="Opaque" -         } -  + Pass -         { + Cgprogram A             #pragmaVertex Vert at             #pragmaFragment Frag -  - uniform samplercube _cube; -Uniformfloat_etaratio; -Uniformfloat_fresnelpower; -Uniformfloat_fresnelscale; inUniformfloat_fresnelbias; -  to             structAppData +             { - float4 pos:position; the FLOAT3 Nor:normal; *             }; $ Panax Notoginseng             structv2f -             { the float4 pos:sv_position; +                 floatReflectionfactor:color; A FLOAT3 r:texcoord0; the FLOAT3 T:texcoord1; +             }; -  $ v2f Vert (AppData vi) $             { - v2f fi; -Fi.pos =Mul (UNITY_MATRIX_MVP, vi.pos); the  -FLOAT3 Worldpos =Mul (_object2world, vi.pos.xyz);WuyiFLOAT3 n =Mul (_object2world, vi.nor); theFLOAT3 i = worldpos-_worldspacecamerapos.xyz; -FI.R =reflect (i, n); Wu  -i =normalize (i); Aboutn =normalize (n); $FI.T =refract (i, N, _etaratio); -  -Fi.reflectionfactor = _fresnelbias + _fresnelscale * POW (1+Dot (i, n), _fresnelpower); -  A                 returnfi; +             } the  - float4 Frag (v2f fi): Color $             { theFLOAT4 REFLECTC =Texcube (_cube, FI.R); theFLOAT4 REFRACTC =Texcube (_cube, FI.T); the                 returnlerp (REFRACTC, REFLECTC, fi.reflectionfactor); the             } -  in ENDCG the         } the     } About}

Reprint Please specify source: http://www.cnblogs.com/jietian331/p/5564901.html

A simple implementation of the Fresnel effect of CG

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.