Untiy 3d Shaderlab_ 7th _ forward render path an important light source under the _2_forward render path

Source: Internet
Author: User
Tags mul

7th Chapter forward Render Path
The main purpose of this chapter is to make it possible to customize the shader of your forward render path by allowing you to correctly access Unity's light source data while not using unity's surface shader. If the reader does not have this requirement, or if it has been understood how unity holds the source data for the forward render path, then this chapter can be skipped.
Important light sources under the 7.2Forward rendering path
7.2.1 Material for design inspection

Its editor,


The main code of the _worldspacelightposo.x.shader file is as follows:
Shader "tut/lighting/forward/lab_1/add/_worldspacelighpos0.x" {subshader {pass{tags{"lightmode" = "ForwardBase"} Cgprogram#pragma vertex vert#pragma fragment frag#pragma multi_compile_fwdbase#include "Unitycg.cginc" #include " Lighting.cginc "struct vertout{float4 pos:sv_position;float4 color:color;}; Vertout Vert (Appdata_base v) {vertout o;o.pos=mul (Unity_matrix_mvp,v.vertex); O.color=float4 (0,0.5,1,1);// Output blue return o;} FLOAT4 Frag (vertout i): Color{return I.color;} Endcg}//end passpass{tags{"Lightmode" = "Forwardadd"}blend one zero//the effect of the blending mode on forward base cgprogram#pragma vertex Vert#pragma fragment Frag#include "Unitycg.cginc" #include "lighting.cginc" struct VERTOUT{FLOAT4 pos:sv_position; Float4 Color:color;}; Vertout Vert (Appdata_base v) {vertout o;o.pos=mul (Unity_matrix_mvp,v.vertex); o.color=_worldspacelightpos0.x;// There are 4 similar shader, output XYZW component return o respectively;} FLOAT4 Frag (vertout i): Color{return I.color;} Endcg}//end Pass}}
The Forwardadd pass of this material uses the blend mode of blend one zero, so the output of the last executed Forwardadd pass can be output correctly, and if the Forwardadd pass is executed at the end, The result of the Forwardbase pass will be output of 1 bright blue (0,0.5,1.0).
7.2.2 When there is no pixel light source

There are two parallel lights and 4 point lights in the current scene, but all are vertex light sources. Now we can compile this scenario and run it, as shown in the results. Visible to Lightmode = Forwardbase in the pass _woridsapcelightpos0 is not set a light source data, and the right signal output is forwardbase pass output of bright blue, This indicates that the Forwardadd pass is not executed in the current case.


7.2.3 The presence of pixel parallel light

We change a parallel light to pixel light source, as shown in the Fowardbase Pass, _WORLDSPACELIGHTPOS0 and _lightcolor0 have data, but the right of the bright blue signal output tells us at this time Fowardadd Pass is still not being executed.


7.2.4 When there is a pixel point light source

If we then set one or more point light sources as pixel light, without pixel parallel light, you can see the result shown, the signal output on the right shows that the Forwardadd pass contains the light source data and is executed. The left side of the Fowardbasepass has data inside the _worldspacelightposo, but the corresponding _lightcolor0 is not, so the combined output using _WORLDSPACELIGHTPOS0 and _lightcolor0 is still black and has no effect on the final illumination.


The reason for this is the fact that Unity's light source data prepared for Fowardadd pass was not cleared in time by unity when it returned to preparing the data for fowardbase. Unity is a performance concern because, after all, the frequent data transfer between the CPU and the GPU is a resource-intensive thing.
Summary of Pixel light sources under 7.2.5 forward rendering path At this point we can summarize,in Lightmode = Forwardbase,lightmode=fowardadd Pass, _worldsapcelightpos0 will only contain pixel light source. Within the Forwardbase pass, only pixel parallel light in the scene with Rendermode as important will contain valid _WORLDSPACELIGHTPOS0 and _lightcolor0 combinations.

Untiy 3d Shaderlab_ 7th _ forward render path an important light source under the _2_forward render path

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.