Unity3d the final chapter of physically-based rendering based on physical rendering

Source: Internet
Author: User

Previously summary:

Emphasis on basic algorithms

Unity3d based on physical rendering physically-based rendering specular BRDF

Plus article

Unity3d implementation of physically-based rendering based on physical rendering

Finally we use fragment shader implementation, plus diffuse diffuse reflection, the code and the previous surface almost, just more reflect direction calculation, reflect direction of the calculation method for the CG function library in the function reflect,

float3 Reflect (FLOAT3 I, Float3 N);
I is the in incident direction, N is the normal discovery direction, where the incident direction is view direction.

FLOAT3 Refdir = Reflect (-viewdir,n);

Refer to the diffuse of Unreal engine under Siggraph 2013.
Their method is new diffuse = diffuse color/π.
Change pi to controllable parameters, and tune into the effect we want.

An external variable _reflamount is established for cubemap and diffuse, and the higher the _reflamount, the more obvious the surrounding scenery.
This is the effect of this article



_reflamount = 0.5

_reflamount = 0
Do you have any feeling of bleeding?

_reflamount = 1

Tall on the Stockings black


_reflamount = 1

_reflamount = 0

_reflamount = 0.5

Compare with Unity:

Diffuse:


Specular:


This is the effect of Unreal Engine published in SIGGRAPH :



Unfortunately, I do not have such a large model to do experiments, unfortunately, the face of the replacement

The code is as follows:

Shader "Custom/reflect new ops3" {Properties{_maintex ("Base (RGB)", 2D) = "White" {}_maintint ("Main Color", Color) = (1, 1 , 1, 1) _cubemap ("Cubemap", CUBE) = "" "{}_sc (" Specular color ", color) = (1, 1, 1, 1) _gl (" Gloss ", Range (0, 1)) = 0.5_nmips (" N MIPSF ", Range (0, 5)) = 0.5_reflamount (" Reflection Amount ", Range (0.01, 1)) = 0.5}subshader{pass{//of the pass rendering of parallel light tags{" Lightmode "=" forwardbase "}cull backcgprogram#pragma vertex vert#pragma fragment Frag#include" Unitycg.cginc "Float4 _ Lightcolor0;samplercube _cubemap;float4 _sc;float _gl;float4 _maintint;float _nmips;float _ReflAmount;uniform  Sampler2d _maintex;float4 _maintex_st;struct v2f {float4 pos:sv_position;float2 uv_maintex:texcoord0;float3 lightDir: TEXCOORD1;FLOAT3 viewdir:texcoord2;float3 Normal:texcoord3;}; v2f Vert (Appdata_full v) {v2f O;o.pos = Mul (UNITY_MATRIX_MVP, V.vertex);//switch to world coordinates o.normal = V.normal;o.lightdir = ObjSpac Elightdir (V.vertex); o.viewdir = Objspaceviewdir (V.vertex); O.uv_maintex = Transform_tex (V.texcoord, _MaiNtex); return o;} #define PIE 3.1415926535float4 Frag (v2f i): color{float3 Viewdir = normalize (I.viewdir); Float3 Lightdir = Normalize (I.lig  Htdir); Float3 H = normalize (Lightdir + viewdir); Float3 N = normalize (i.normal); float _sp = Pow (8192, _GL); float D = (_sp + 2)/(8 * PIE) * POW (dot (N, H), _sp),//float f = _sc + (1-_SC) *pow ((1-dot (H, Lightdir)), 5); float F = _sc + (1-_SC) *pow (2, -10 * dot (H, lightdir)), Float k = min (1, _GL + 0.545), float v = 1/(k* dot (viewdir, h) *dot (Viewdir, h) + (1-k)) ; float all = D*f*v;//float3 Refdir = N-LIGHTDIR/2;//HFLOAT3 Refdir = reflect (-viewdir,n); FLOAT3 ref = Texcubelod (_cube Map, Float4 (Refdir, _nmips-_gl*_nmips)). rgb;//* _REFLAMOUNT;FLOAT3 C = tex2d (_maintex, I.uv_maintex); Float3 diff = Dot (l Ightdir, N);//diff/= Pie;diff = (1-all) *diff;//return float4 (c * (diff + all), 1) * _lightcolor0;return float4 (Lerp (C,  Ref, _reflamount) * (Diff*_maintint + All), 1) *_lightcolor0;//return float4 (ref* (_maintint+0.2) * (1-dot (Lightdir, N))) + c * (difF*_maintint + All), 1) *_lightcolor0;//return Float4 (Lerp (c, ref, _reflamount) * (diff* (_maintint + 0.2) * (1-dot (Lightdir, N)) + All), 1) *_lightcolor0;} ENDCG}}}

-----by wolf96


Unity3d the final chapter of physically-based rendering based on physical rendering

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.