Unity3d Shaderlab BRDF Simulation

Source: Internet
Author: User

Unity3d Shaderlab BRDF Simulation

In the previous article, when it comes to using gradient texture coloring, a value is used to control the UV coordinates of the texture , but it also means that we can only get a linear illumination effect.

So can we look at the direction of the vector combined with the light direction to form a two-way reflection change, since here, it must be done,

This is our BRDF Effect , we can simply put BRDF see as the incident light on the surface of the opaque object at the same time reflected to the observation direction and the direction of the beam.

BRDF(bidirectional reflectance distribution Function specific content to explain, you can consult Baidu encyclopedia.

The light shines to an object, which first produces reflection, absorption and transmission, so BRDF The key factor is how much light is reflected, absorbed and transmitted (refracted), how it changes, classmate your junior high school physics Oh.

We first discuss the simulation of the effect, how to use the changeable texture to change our model effect.

Before starting code , we need a multi-dimensional gradient texture, so at least 3 gradients in the direction of the Color field.

This time, we borrowed a code"basicmyhalflessdiffuse" that uses a gradient texture coloring, and we're going to put the previous inline FLOAT4 Lightinghalfbrdfdiffuse (surfaceoutput s,fixed3 lightdir,fixed atten) function modified to 4 Parameters

1:>inline float4 lightinghalfbrdfdiffuse (surfaceoutput s,fixed3 lightdir,half3 viewdir,fixed atten)

2: The observation direction parameter >float rimlight = max (0,dot (S.normal,viewdir)) added to the viewing angle;

3: Modify the rgb>tex2d (_BRDFTEX,FLOAT2 (halflight,rimlight)) obtained through the txt2d () function . RGB;

Is the light model function that we have modified. So what is the final result of the rendering?

Please note that the _emissivecolor,_ambientcolor are all white. We are really using the image of the gradient to achieve it, in the above effect, we mainly show the more changeable RGB to the model rendering effect.

Summing up the process above, we can create a simple attenuation effect when we use the observation direction parameter, and we can produce various effects in different attenuation forms.

Because of the rimlight,diflight results are 0-1, so we can use these two values to select different areas of the gradient. Then the key is to understand the values we get from the point-multiplication operation and how to control the texture.

Code Start------------------------------------------------------------

Shader "91ygame/basicmyhalfbrdfdiffuse" {
Properties {
_emissivecolor ("emissive color", color) = (1,1,1,1)
_ambientcolor ("Ambient color", color) = (1,1,1,1)
_myslidervalue ("Slider Value", Range (0,10)) =1.3
_brdftex ("BRDF Texture", 2D) = ""
}
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD 200

Cgprogram
#pragma surface surf Lambert
#pragma surface surf Halfbrdfdiffuse

FLOAT4 _emissivecolor;
FLOAT4 _ambientcolor;
float _myslidervalue;
Sampler2d _brdftex;

struct Input {
FLOAT2 Uv_maintex;
};

void Surf (Input in, InOut surfaceoutput o) {
FLOAT4 C;
C=pow ((_emissivecolor+_ambientcolor), _myslidervalue);
O.albedo = C.rgb;
O.alpha = C.A;
}

Inline Float4 lightinghalfbrdfdiffuse (surfaceoutput s,fixed3 lightdir,half3 viewdir,fixed atten) {
float diflight = max (0,dot (S.normal,lightdir));
float rimlight = max (0,dot (S.normal,viewdir));
float halflight=diflight*0.5+0.5;
FLOAT3 Bfcolor = tex2d (_BRDFTEX,FLOAT2 (Halflight,rimlight)). RGB;
Float4 Col;
Col.rgb = S.albedo * _LIGHTCOLOR0.RGB*BFCOLOR;
Col.a=s.alpha;
return col;
}
Endcg
}
FallBack "Diffuse"
}

Code End--------------------------------------------------------------

Unity3d Shaderlab BRDF Simulation

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.