Unity in standard PBR (i)

Source: Internet
Author: User
Tags mul

Focus on the Forwardbase

Standard.shader

#pragma vertex vertbase#pragma fragment fragbase"  Unitystandardcoreforward.cginc"

Track to Unitystandardcoreforward.cginc

#ifUnity_standard_simple#include"Unitystandardcoreforwardsimple.cginc"vertexoutputbasesimple vertbase (Vertexinput v) {returnVertforwardbasesimple (v);} Vertexoutputforwardaddsimple Vertadd (Vertexinput v) {returnVertforwardaddsimple (v);} Half4 fragbase (vertexoutputbasesimple i): sv_target {returnfragforwardbasesimpleinternal (i);} Half4 Fragadd (vertexoutputforwardaddsimple i): sv_target {returnfragforwardaddsimpleinternal (i);}#else#include"Unitystandardcore.cginc"vertexoutputforwardbase vertbase (Vertexinput v) {returnVertforwardbase (v);} Vertexoutputforwardadd Vertadd (Vertexinput v) {returnVertforwardadd (v);} Half4 fragbase (vertexoutputforwardbase i): sv_target {returnfragforwardbaseinternal (i);} Half4 Fragadd (Vertexoutputforwardadd i): sv_target {returnfragforwardaddinternal (i);}#endif

Unity_standard_simple switch is a simplified version of the meaning, into the bottom of the branch

Vertex shader Vertforwardbase

First look at the output

structvertexoutputforwardbase{float4 pos:sv_position;    FLOAT4 tex:texcoord0;    Half3 Eyevec:texcoord1; Half4 tangenttoworldandparallax[3]: TEXCOORD2;//[3x3:tangenttoworld | 1x3:viewdirforparallax]Half4 Ambientorlightmapuv:texcoord5;//SH or Lightmap UVShadow_coords (6) Unity_fog_coords (7)    //next ones would not fit to SM2.0 limits, but they is always for sm3.0+    #ifUnity_require_frag_worldposFLOAT3 Posworld:texcoord8; #endif    #ifUnity_optimize_texcubelod#ifUnity_require_frag_worldposHalf3 refluvw:texcoord9; #elseHalf3 Refluvw:texcoord8; #endif    #endifUnity_vertex_output_stereo};
Unity_require_frag_worldpos want to Worldpos, want to
REFLUVW is used to make reflections, to
Unity_vertex_output_stereo is used for instantiation, do not
Simplified into

struct vertexoutputforwardbase{    float4 pos                          : sv_position;    FLOAT4 Tex                          : TEXCOORD0;    Half3 Eyevec                        : TEXCOORD1;    Half4 tangenttoworldandparallax[3]  : TEXCOORD2;    // [3x3:tangenttoworld | 1x3:viewdirforparallax]    Half4 ambientorlightmapuv           : TEXCOORD5;    // SH or Lightmap UV    Shadow_coords (6)    unity_fog_coords (7)    float3 posworld                     : TEXCOORD8;    Half3 REFLUVW                       : TEXCOORD9;};
And look at the vertex shader.
vertexoutputforwardbase vertforwardbase (Vertexinput v) {vertexoutputforwardbase o;    unity_setup_instance_id (v);    Unity_initialize_output (Vertexoutputforwardbase, O);    Unity_initialize_vertex_output_stereo (o); FLOAT4 Posworld=Mul (Unity_objecttoworld, V.vertex); #ifUnity_require_frag_worldposO.posworld=posworld.xyz; #endifO.pos=Unityobjecttoclippos (V.vertex); O.tex=Texcoords (v); O.eyevec= Normalizepervertexnormal (POSWORLD.XYZ-_worldspacecamerapos); FLOAT3 Normalworld=Unityobjecttoworldnormal (V.normal); #ifdef _tangent_to_world float4 Tangentworld=float4 (Unityobjecttoworlddir (V.TANGENT.XYZ), V.TANGENT.W); float3x3 Tangenttoworld=Createtangenttoworldpervertex (Normalworld, tangentworld.xyz, TANGENTWORLD.W); o.tangenttoworldandparallax[0].XYZ = tangenttoworld[0]; o.tangenttoworldandparallax[1].XYZ = tangenttoworld[1]; o.tangenttoworldandparallax[2].XYZ = tangenttoworld[2]; #elseo.tangenttoworldandparallax[0].XYZ =0; o.tangenttoworldandparallax[1].XYZ =0; o.tangenttoworldandparallax[2].XYZ =Normalworld; #endif    //We need this for shadow recevingTransfer_shadow (o); O.ambientorlightmapuv=Vertexgiforward (V, Posworld, Normalworld);        #ifdef _parallaxmap tangent_space_rotation; Half3 Viewdirforparallax=mul (Rotation, Objspaceviewdir (V.vertex)); o.tangenttoworldandparallax[0].W =viewdirforparallax.x; o.tangenttoworldandparallax[1].W =viewdirforparallax.y; o.tangenttoworldandparallax[2].W =viewdirforparallax.z; #endif    #ifUnity_optimize_texcubelodO.REFLUVW=reflect (O.eyevec, normalworld); #endifUnity_transfer_fog (O,o.pos); returno;}

UNITY_SETUP_INSTANCE_ID is for INSTANCE, don't
Unity_initialize_output didn't do anything.
Unity_initialize_vertex_output_stereo Don't
Normalizepervertexnormal is equal to nothing.
_tangent_to_world is a tangent space and a World Space transformation matrix Reservation
O.AMBIENTORLIGHTMAPUV = Vertexgiforward (V, Posworld, Normalworld);
Because of the intention to use PBR to do the role, no lightmap, so here equals

O.AMBIENTORLIGHTMAPUV = 0;
_parallaxmap Parallax Map, no
After removing the branch code as follows

Vertexoutputforwardbase Vertforwardbase (Vertexinput v) {    vertexoutputforwardbase o;    FLOAT4 Posworld = Mul (Unity_objecttoworld, V.vertex);    O.posworld = posworld.xyz;    O.pos = Unityobjecttoclippos (V.vertex);    O.tex = Texcoords (v);    O.eyevec = Normalizepervertexnormal (POSWORLD.XYZ-_worldspacecamerapos);    FLOAT3 Normalworld = Unityobjecttoworldnormal (v.normal); Float4 Tangentworld = Float4 (Unityobjecttoworlddir ( V.TANGENT.XYZ), V.TANGENT.W); float3x3 Tangenttoworld = Createtangenttoworldpervertex (NormalWorld, TANGENTWORLD.XYZ, TANGENTWORLD.W); o.tangenttoworldandparallax[0].xyz = TANGENTTOWORLD[0];O.TANGENTTOWORLDANDPARALLAX[1].XYZ = TANGENTTOWORLD[1];O.TANGENTTOWORLDANDPARALLAX[2].XYZ = tangenttoworld[2];    We need this for shadow receving    Transfer_shadow (o);    O.ambientorlightmapuv = 0;O.REFLUVW       = Reflect (O.eyevec, normalworld);    Unity_transfer_fog (O,o.pos);    return o;}

Now that the vertex shader is finished, the next pixel shader

Unity in standard PBR (i)

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.