Untiy 3d shaderlab_ 6th _vertexlit render path _4_ vertex illumination and unity the third Way to store light

Source: Internet
Author: User
Tags mul

6.4 Vertex lighting and unity the third Way to store light


6.4.1 Unity Light source for vertex pass
is it hopeless? How can the world do without light? Of course not, the light is coming, unity put it inUnity_lightposition[4] Array.
can simply tell you a result: within Lightmode = Vertex's pass, unity_lightposition[4] andUnity_lightcolor[4] is the most reliable first-choice means of accessing the source data, regardless of the camera's Renderingpath,Vertexlit,forward or deferred,unity will update the light source information in this array in a timely and accurate manner.
6.4.2 designed for testing scenarios

Open the scene under the Lab_ 3 folder, which is shown in the editor of the game as it is in the day. The right side of the blue area aboveThe Lower Corner is a 6 legend showing the relationship between the grayscale color and the number, and the leftmost is the 4 point light in the scene WorldSpaceThe xyz coordinate in the above is the representation of the 3 grayscale material balls of the 4 point light source, in the following is a 4 point light sourcenumbers (x, y, x) are represented. Yellow Point light from top to bottom yellow (0.7, 0.5, 0.2), Green Point LightGreen (0.7, 0.5, 0.5, red Point light source red (0.5, 0.2, 0.2) and blue Point Light source B1ue (0.2, 0.5,0.7). The reason for this from yellow to blue is that we can predict the 4-color point light by using the lunimance function that unity uses to calculate the light intensityUnity_lightposition[4] In the order of precedence in the array. To the right of the representation of the world coordinates of the light source in this set of grayscale and number, iscorresponds to the grayscale and numeric representation of the light source in the camera space. Because the coordinates of the camera in the scene are ( -0.3,-0.3,-0.3),and the camera is not spinning, so we can easily figure out the XYZ coordinates of these 4 point lights in the camera space, yellowPoint Light source yellow (1.0,0.8, 0.5), Green Point Light (1.0,0.8, 0.8), Red dot source Red (0.8,0.5,0.5), and Blue Point Light (0.5,0.8,1.0) 4 Point Lightthe WorldSpace and cameraspace coordinates are in the (0,1) interval and can be correctly represented by the grayscale on the screen, and ifUnity_lightposition[4] The coordinates of the 4 point light sources in the cameraspace are represented in the, then the grayscale representation should be highlighted as a whole0.3. The output column of each vector in the unity_lightposition[4] array labes The 3/shader folder, as in the previousThe two sections are just as simple as the corresponding XYZW component output to grayscale. In addition, it also outputs theUnity_lightatten[4] Array to the second-to-last volleyball body, unity Lightcolor[4] to the countdown to the first volleyball body.



6.4.3 Point Light in vertex illumination
First we turn off the two parallel lights in this scenario, simplifying the next step to analyze the complexity of the problem, and then the camera'sRenderingpath are set to Vertexlit, Forward, Deferred, respectively, and compile and run. The results of my test werethese 3 kinds of renderingpath, Lightmode = Vertex's pass, unity_lightposition[4] and unity Lightcolor[4]all contain the correct data. And according to the gray-scale graph coding, we can be very certain in these 3 kinds of renderingpath, Lightmode = Vertex's pass, Unity_lightposition[4] is the coordinates of camera space.


6.4.4 Compute the shadervertexlights function of vertex illumination
in Unitycg.cginc, there is a function that references this variable, and this function is as follows:

FLOAT3 shadevertexlights (float4 vertex,float3 normal) {        Float3 Viewpos = Mul (Unity_matrix_mv,vertex). xyz;        FLOAT3 viewn = Mul ((float3x3) unity_matrix_it_mv,normal);        FLOAT3 lightcolor = unity_lightmodel_ambient.xyz;        for (int i=0;i<4;i++) {         float3 tolight = UNITY_LIGHTPOSITION[I].XYZ-VIEWPOS.XYZ*UNITY_LIGHTPOSITION[I].W;         float lengthsq = dot (tolight,tolight);         float Atten = 1.0/(1.0+LENGTHSQ * unity_lightatten[i].z);         float diff = max (0,dot (Viewn,normalize (tolight)));         Lightcolor + = Unity_lightcolor[i].rgb * (diff * atten);        }        return lightcolor;}
First we can find that the vertex is transformed by this matrix UNITY_MATRIX_MV, which shows that FLOAT4Unity_lightposition[4] is really a source of light in the view space, which is the source of the current camera spaceto a vector or position. Notice how this function calculates the attenuation, if you want to manually write the shade on the light calculation As a result, consistent with unity, it is important to use the same calculation method for attenuation .

pixel light source in 6.4.5 vertex illumination
The question now is : unity_lightposition[4] only works on vertex point Light, and it works on direction light source ., if it works, the corresponding square i sentence vector is world space or camera Space. If the camera is rotated,this is important. We will now run the program that was just build again, change the pixel of the light source or the vertex feature,we will find that under 3 Renderingpath, Unity_lightposition[4] are updated in a timely manner, and for pixel lightThe source, which is in the unity_lightposition[4] position more forward .

6.4.6 of parallel light in vertex illumination
Now the question is : Unity_ lightposition[4] Does it work for parallel light? We can continue to try and put some lightchange the source to a parallel light, or directly enable two already existing parallel lights, we will find that under 3 Renderingpath, unity_lightposition[4] and unity_lightcolor[4] all have timely light source data . and you're actually doing the exercise It is also found that in the case of the same pixel or vertex, the order of the parallel light in unity_lightposition[4] and Unity_lightcolor[4] is more forward. And all of our light sources have one advantage, that is, their Euler angles are (30, 300, 0), this angle is good, because they are represented as directional vectors are in the (0,1) interval, in our screen color (0,1) Represents the range.
the only problem left now is that if it is a parallel light , then the corresponding directional vector in unity_lightposition[4] is livingboundary coordinates or CAMERASPACE, this problem is not as simple and intuitive as position vectors. Think about it, not really.is too difficult.
Open the scene under the Lab 4 folder, the composition of this scene is basically the same as the previous test scenario, and the difference is that I will be 4The light source is changed to a parallel light, and then the red and blue two parallel light sources are bound to the camera to become its childrenThe other two yellow and green parallel lights remain in world coordinates. This way, if the unity_lightposition[4]The parallel light direction vectors are relative to world coordinates, so when we rotate the camera, the yellow and green two parallel Light Fingerssignal should not be strong or weak, and red and blue two parallel light signals should correspond to the signal strengthchange. If the parallel light force direction vector in Unity_lightposition[4] is cameraspace, the opposite is true. Organization of the scenethe structure and the initial knowledge state 6.11 are shown.
This time we can do this test directly in the editor, so that we have to add sliders to control the camera angle of trouble,
and I guarantee the results are reliable.
we rotate the result at any camera angle in the editor. The results are shown, left in the world coordinate system the indicator signal of the two directional vectors of the parallel light changes when the camera is rotated, and as the indicator signal of two flat-line light of the camera sub-object , there is no strong or weak change when the camera rotates. Now I can tell you responsibly, The parallel light direction vectors in unity Lightposition[4] are also in Cameraspace .
Summary of Light information in 6.4.7 vertex illumination
for the Lightmode=vertex pass, the effective way to access the light source is to read Unity_lightposition[4] andUnity_lightcolor[4], these two arrays are guaranteed to work effectively under Unity's 3 Renderingpath. Need to be aware, the position vector of the point light source in unity_lightposition[4] and the directional vector of the parallel light are all in cameraspace. If you want to write a shader that is only vertex illuminated, and you do not want to manipulate these annoying arrays, you can use the Shadevertexlights function in Unitycg.cginc directly.
The next thing to keep in mind is that unity does not clean up some invalid data for different passes, and you may unity_lightpos[x, y,z]0, and _worldspacelightpos0 the light source data that is left in the last time an object executes another type of pass So do not use them for lighting calculations in Lightmode for vertex.
6.4. Examples of 81 vertex illumination implementations
of course, above this in the more abstract use of color signal way big head, nothing, I have a more straight in colorThe view version is under the Lighting/lab_3c folder. The scenes under this folder are used in the _indicator folder .Unity_lightposition_o.shader, Unity_lightposition_1.shader, Unity_lightposition_2.shader, andUnity_lightposition_3.shader, these 4 materials to the unity_lightposition[4] array of light sources in the vertex pass to doA simple rendering that can be verified by a button operation on the Green Control Panel, a 6.13 of its runtime
is shown.
the code for Unity-lightposition_ O.shader is as follows:
Hader "Tut/lighting/vertexlit/indicator/unity_lightposition_0" {Properties {_color ("Base Color", color) = (1,1,1,1)} Subshader {pass{tags{"lightmode" = "Vertex"}cgprogram#pragma Vertex vert#pragma fragment Frag#include "Unitycg.cginc" # Include "Lighting.cginc" uniform float4 _color;struct vertout{float4 pos:sv_position;float4 color:color;}; Vertout Vert (Appdata_base v) {//unity_lightposition in viewspace,ie,the camera spacefloat3 viewpos = Mul (unity_matrix_mv , V.vertex). XYZ;FLOAT3 viewn = Mul ((float3x3) UNITY_MATRIX_IT_MV, v.normal); FLOAT3 Lightcolor = Unity_lightmodel_ AMBIENT.XYZ;FLOAT3 tolight = unity_lightposition[0].xyz-viewpos.xyz * unity_lightposition[0].w;float lengthSq = dot ( Tolight, tolight); float atten = 4.0/(1.0 + lengthsq * unity_lightatten[0].z); float diff = max (0, Dot (viewn, normalize ( Tolight)) Lightcolor + = Unity_lightcolor[0].rgb * (diff * atten); Vertout O;o.pos=mul (Unity_matrix_mvp,v.vertex); O. COLOR=FLOAT4 (lightcolor,1) *_color;return o;} FLOAT4 Frag (vertout i): color{rEturn I.color;} Endcg}//end Pass}}




Untiy 3d shaderlab_ 6th _vertexlit render path _4_ vertex illumination and unity the third Way to store light

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.