Unity Shaders and Effects Cookbook (4-5) cubemap with Fresnel reflections

Source: Internet
Author: User
Tags cos

Fresnel is the name of the person, he found that when we stand in the water, straight down to the surface of the water, it is not visible reflection of the sun. And when we look at the distance, we can see a very strong reflection, that is, the idiom is reflected in the sparkling. This phenomenon is named Fresnel reflex.


Just read this section of the time feel puzzled, the feelings of the book on the beginning of writing a problem, the book is so written:

As you gaze at the surface of the object, the amount of reflection increases significantly.

My understanding of this sentence is that it is on the surface, and the amount of reflection increases. ...... Really misleading.

But fortunately the code is not to deceive me.

The effect of code implementation is to conform to Fresnel reflection, directly on the surface of the object, the amount of reflection decreased.

Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

Create a new scene, material, Shader.


The complete shader code is as follows:

Shader "Cookbookshaders/chapt4-5/fresnelreflections" {Properties {_maintint ("Diffuse Tint", Color) = (1,1,1,1) _ Maintex ("Base (RGB)", 2D) = "White" {}_cubemap ("Cubemap", CUBE) = "" "{}_reflectionamount (" Reflection Amount ", Range (0,1)) =1_rimpower ("Fresnel Falloff", Range (0.1,3)) =2_speccolor ("Specular Color", Color) = (1,1,1,1) _specpower ("Specular Power ", Range (0,1)) =0.5}subshader {Tags {" rendertype "=" Opaque "}lod 200cgprogram#pragma surface Surf Blinnphong#pragma Target 3.0FLOAT4 _maintint;sampler2d _maintex;samplercube _cubemap;float _reflectionamount;float _RimPower;float _ Specpower;struct Input {float2 uv_maintex;float3 worldrefl;float3 viewdir;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex) *_maintint;//first asks for the Cos value of the normals and line-of-sight angles, and then makes Limited to (0,1) range with saturate. Because it is a cos, when the vertical surface of the line of sight, parallel normals, the value is 1.//and then 1 to reduce the COS value, to get the inverse value. So when the line of sight is perpendicular to the surface, the rim value is the smallest, 0.float rim=1.0-saturate (dot (o.normal,normalize (in.viewdir))), Rim=pow (Rim,_rimpower), O. Albedo = c.rgb;//So rim=0,o when the line of sight is perpendicular to the surface. Emission = 0. The closer the line of sightVertical, the smaller the reflection. O.emission= (Texcube (_CUBEMAP,IN.WORLDREFL). RGB * _reflectionamount) *rim;o. Specular=_specpower;o. Gloss=1.0;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}

The code comment is already there.

I drew a picture, too.



The effect of the final implementation


The area where the sight is right is seldom reflected.



Sample Project Download:

http://pan.baidu.com/s/1dF9Eu5b


Unity Shaders and Effects Cookbook (4-5) cubemap with Fresnel reflections

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.