Untiy 3d shaderlab_ 8th _3_ single light map and forward render path

Source: Internet
Author: User
Tags mul

8.3 Single light map and forward render path 8.3.1 Single-illumination mapping under vertexlit and forward
in the case of a single light map, Camera's Renderingpathto be Vertexlitwhen thean undesirable place is a static object that has been baked, and the default material is not affected by real-time lighting. Of course, this behavior can be changed by providing a custom material, but it's troublesome. when renderingpath is Forward , This hassle will not exist, through baked objects, Unity's default material will continue to be affected by the real-time pixel light source .
8.3.2 Ready-to-use self-luminous material for baking
。 Similar to the first two scenarios, the difference is that we'll show you how to make the material bake correctly in forward mode and apply the light map correctly.

The first is the green self-luminous material. Similar to the spontaneous light in vertexlit mode, this provides only the _EMISSIONLM attribute and the unity specification with the material name prefix self-illumin. The code for Forwardemission.shader is as follows:
Shader "Self-illumin/lighting/lightmapping/lab_3/forwardemission" {Properties {_maintex ("Maintexture", 2d) = "White" {}_color ("Base Color", color) = (1,1,1,1) _EMISSIONLM ("Emission (Lightmapper)", Float) = 1}subshader {pass{tags{"Lightmo De "=" forwardbase "}cgprogram#pragma vertex vert#pragma fragment Frag#pragma multi_compile_fwdbase#include" Unitycg.cginc "#include" lighting.cginc "uniform float4 _color;struct vertout{float4 pos:sv_position;float4 Color: COLOR;FLOAT3 litdir:texcoord0;float3 worldn:texcoord1;}; Vertout Vert (Appdata_base v) {float4 Worldv=mul (_object2world,v.vertex); Float3 Worldn=mul (_OBJECT2WORLD,FLOAT4 ( scaled_normal,0)). XYZ;FLOAT3 C=shade4pointlights (unity_4lightposx0,unity_4lightposy0,unity_4lightposz0,unity_ Lightcolor[0],unity_lightcolor[1],unity_lightcolor[2],unity_lightcolor[3],unity_4lightatten0,worldv.xyz,worldn ); Vertout O;o.pos=mul (Unity_matrix_mvp,v.vertex); O.litdir=worldspacelightdir (V.vertex); o.worldN=worldN;o.color= FLOAT4 (c,1.0) *_color;return o;} FLOAT4 Frag (Vertout i): Color{float4 C=max (0.0,dot (I.worldn,normalize (I.litdir))) *_lightcolor0*_color;return C+i.color+_Color;} Endcg}//end passpass{tags{"Lightmode" = "Forwardadd"}blend one onecgprogram#pragma vertex vert#pragma fragment frag# Include "Unitycg.cginc" #include "lighting.cginc" uniform float4 _color;struct vertout{float4 POS:SV_POSITION;FLOAT3 LITDIR:TEXCOORD0;FLOAT3 worldn:texcoord1;float Atten:texcoord2;}; Vertout Vert (Appdata_base v) {float3 Worldn=mul (_OBJECT2WORLD,FLOAT4 (scaled_normal,0)). Xyz;vertout O;o.pos=mul ( Unity_matrix_mvp,v.vertex); O.litdir=worldspacelightdir (V.vertex); float dist=length (O.litdir); o.atten=1/(1+dist* DIST*_WORLDSPACELIGHTPOS0.W); O.worldn=worldn;return o;} FLOAT4 Frag (vertout i): Color{float4 C=_lightcolor0*_color*max (0.0,dot (i.worldn,normalize)) I.litdir; return c;} Endcg}//end Pass}}
And then a myforward.shader, in this custom shader, we calculate the real time, but we don't calculate the effect of the illumination map.
8.3.3 Calculating the illumination map within the forwardbase
And then there's myforwardlm.shader. In this shader forwardbase pass, we calculated the effects of unity_lightmap, which is where Unity's default calculation of the illumination map is. The reason for calculation in forwardbase, rather than in Forwardadd, is that forwardadd are executed several times, respectively, in the case of multiple pixel lights. The code for Myforwardlm.shader is as follows:
Shader "TUT/LIGHTING/LIGHTMAPPING/LAB_3/MYFORWARDLM" {Properties {_maintex ("Maintexture", 2d) = "White" {}_color (" Base color, color) = (1,1,1,1)}subshader {pass{tags{"lightmode" = "forwardbase"}cgprogram#pragma vertex vert#pragma Fragment Frag#pragma multi_compile_fwdbase#include "Unitycg.cginc" #include "lighting.cginc" uniform float4 _color;        Sampler2d _maintex; FLOAT4 _maintex_st;        Scale & position of _maintex #ifndef Lightmap_off//sampler2d unity_lightmap;//beast Lightmap FLOAT4 Unity_lightmapst; Scale & position of Beast lightmap#endifstruct vertout{float4 pos:sv_position;float4 color:color;float3 litdir: TEXCOORD0;FLOAT3 worldn:texcoord1;float2 uv:texcoord2, #ifndef lightmap_offfloat2 uvlm:texcoord3; #endif};vertout Vert (Appdata_full v) {float4 Worldv=mul (_object2world,v.vertex); Float3 Worldn=mul (_object2world,float4 (scaled_ normal,0)). XYZ;FLOAT3 C=shade4pointlights (unity_4lightposx0,unity_4lightposy0,unity_4lightposz0,unity_ Lightcolor[0],unity_ligHTCOLOR[1],UNITY_LIGHTCOLOR[2],UNITY_LIGHTCOLOR[3],UNITY_4LIGHTATTEN0,WORLDV.XYZ,WORLDN); VertOut O;o.pos=mul ( Unity_matrix_mvp,v.vertex); O.litdir=worldspacelightdir (V.vertex); o.worldn=worldn;o.uv=v.texcoord.xy; #ifndef Lightmap_offo.uvlm=v.texcoord1.xy, #endifo. COLOR=FLOAT4 (c,1.0) *_color;return o; FLOAT4 Frag (vertout i): Color{float4 C=max (0.0,dot (i.worldn,normalize)) I.litdir; #ifndef LIGHTMAP_OFFFLOAT3 clm=tex2d (_MAINTEX,I.UV). Rgb*decodelightmap (unity_sample_tex2d (Unity_Lightmap,i.uvLM)); c+= FLOAT4 (clm,1.0); #endifreturn (C+i.color);} Endcg}//end passpass{tags{"Lightmode" = "Forwardadd"}blend one onecgprogram#pragma vertex vert#pragma fragment frag# Include "Unitycg.cginc" #include "lighting.cginc" uniform float4 _color;struct vertout{float4 POS:SV_POSITION;FLOAT3 LITDIR:TEXCOORD0;FLOAT3 worldn:texcoord1;float Atten:texcoord2;}; Vertout Vert (Appdata_base v) {float3 Worldn=mul (_OBJECT2WORLD,FLOAT4 (scaled_normal,0)). Xyz;vertout O;o.pos=mul ( Unity_matrix_mvp,v.vertex); o.litdir=worLdspacelightdir (V.vertex); float dist=length (O.litdir); o.atten=1/(1+DIST*DIST*_WORLDSPACELIGHTPOS0.W); o.worldN= Worldn;return o;} FLOAT4 Frag (vertout i): Color{float4 C=_lightcolor0*_color*max (0.0,dot (i.worldn,normalize)) I.litdir; return c;} Endcg}//end Pass}}
Real-time illumination after baking in 8.3.4Forward render path

You can bake it now, as shown in the results below. First we can note that, compared to vertexlit mode, the default material is no longer affected by the real-time light source, and our yellow real-time pixel light source in forward mode still has a lighting effect on baked objects . But you can try, when the Camera is in Forward mode, the real-time light is changed to the not Important vertex light source , it will not have the effect of lighting the baked objects . second , the baked light source no longer produces light for baked objects, such as a self-colored light source (light source 1) and a green light source (light source 2) in the scene, and we can move or change the color through the GUI, but it no longer produces illumination for baked objects. Unless we change the light source lightmapping mode to RealTimeonly, it will produce light for baked objects.




Untiy 3d shaderlab_ 8th _3_ single light map and 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.