Unity3d's Advanced rendering-shader Forge

Source: Internet
Author: User
Tags mul

The author introduces: Jiang Xiewei, IT company technology partner, it senior lecturer, CSDN Community expert, guest editor, best-selling author, national patent inventor; published books: Teach You architecture 3D game engine, electronic industry press andUnity3d Actual combat core technical details of the electronic industry publishing house.

CSDN Video URL: http://edu.csdn.net/lecturer/144

In the previous blog to the reader about the use of Shader Forge application, Shader Forge this component is very convenient to use, especially for those who are not very understanding of Shader programming developers, use it can quickly build a Shader, This is very similar to the Unreal UE4 engine editor, especially for the next generation game. The following shows the power of the reader as follows:

Look at the effect of the show, it is built with Shader Forge, using Diffuse,normal,gloss,emisson, etc., do not know, we look at the use of the Unity3d engine comes with the shade effect is as follows:

This is the same environment using the Unity3d engine comes with the standardshader effect, its setting interface is as follows:

Since the Unity3d engine comes with a Shader that does not meet our needs, then use Shader Forge to solve the problem, Shader Forge provides an editor for the reader in the previous blog about the operation between the materials, Shader The Forge Editor also operates with this principle, with the following effects:

The performance in the Unity3d editor is as follows:

The adjustment of parameters can be adjusted according to the effect, this model realizes the effect of high light, normal and spontaneous light, the shader code generated by shader forge uses the vertex shader and fragment shader, which will be converted to shader, the core code is as follows:

  Vertexoutput Vert (Vertexinput v) {vertexoutput o = (vertexoutput) 0;                O.uv0 = v.texcoord0;                O.normaldir = Unityobjecttoworldnormal (v.normal);                O.tangentdir = Normalize (Mul (Unity_objecttoworld, FLOAT4 (v.tangent.xyz, 0.0)). xyz);                O.bitangentdir = Normalize (Cross (o.normaldir, O.tangentdir) * V.TANGENT.W);                O.posworld = Mul (Unity_objecttoworld, V.vertex);                FLOAT3 Lightcolor = _lightcolor0.rgb;                O.pos = Mul (UNITY_MATRIX_MVP, V.vertex);                Unity_transfer_fog (O,o.pos);            Transfer_vertex_to_fragment (o) return o;                } float4 Frag (Vertexoutput i): COLOR {i.normaldir = normalize (I.normaldir);                float3x3 tangenttransform = float3x3 (I.tangentdir, I.bitangentdir, I.normaldir);                FLOAT3 viewdirection = normalize (_WORLDSPACECAMERAPOS.XYZ-I.POSWORLD.XYZ); FLOAT3 _Normal_var = Unpacknormal (tex2d (_normal,transform_tex (I.uv0, _normal)));                FLOAT3 normallocal = _normal_var.rgb; FLOAT3 normaldirection = Normalize (Mul (normallocal, tangenttransform));                perturbed normals float3 lightdirection = normalize (_WORLDSPACELIGHTPOS0.XYZ);                FLOAT3 Lightcolor = _lightcolor0.rgb; FLOAT3 halfdirection = normalize (viewdirection+lightdirection);//////lighting:float attenuation = light_a                Ttenuation (i); FLOAT3 Attencolor = attenuation * _lightcolor0.xyz;/////////gloss:float4 _gloss_diffuse_var = Tex2D (_glos                S_diffuse,transform_tex (I.uv0, _gloss_diffuse));                Float gloss = (_gloss_diffuse_var.r*_gloss); float Specpow = EXP2 (Gloss * 10.0+1.0);//////specular:float ndotl = max (0, Dot (normaldirection, LIGHTDI                rection));          FLOAT4 _light_var = tex2d (_light,transform_tex (I.uv0, _light));      FLOAT3 Specularcolor = (_light_var.rgb*_light_slider); FLOAT3 Directspecular = (Floor (attenuation) * _lightcolor0.xyz) * POW (max (0,dot (halfdirection,normaldirection)),                SPECPOW) *specularcolor;                FLOAT3 specular = directspecular;///////Diffuse:ndotl = max (0.0,dot (normaldirection, lightdirection));                FLOAT3 directdiffuse = max (0.0, ndotl) * ATTENCOLOR;                FLOAT3 Indirectdiffuse = FLOAT3 (0,0,0); Indirectdiffuse + = Unity_lightmodel_ambient.rgb;                Ambient light Float4 _df_var = tex2d (_df,transform_tex (I.uv0, _DF));                FLOAT3 Diffusecolor = (_DF_VAR.RGB*_DF_COLOR.RGB); FLOAT3 Diffuse = (Directdiffuse + indirectdiffuse) * diffusecolor;//////emissive:float4 _emission_color_v                AR = tex2d (_emission_color,transform_tex (I.uv0, _emission_color));              FLOAT3 emissive = (_emission_color_var.rgb*_emission_slider);//Final color:  FLOAT3 Finalcolor = diffuse + specular + emissive;                Fixed4 Finalrgba = fixed4 (finalcolor,1);                Unity_apply_fog (I.fogcoord, Finalrgba);            return Finalrgba; }

So that we can achieve the next generation effect rendering, is not very cool? Many people worry about its performance, in fact, the iOS side frame rate is still possible, full frame 30 case, can run to about 25 frames.

Unity3d's Advanced rendering-shader Forge

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.