Сам шедер:
Синтаксис:
Используется csharp
Shader "Custom/Some_lightmapped_shader" {
Properties{
_MainTex("Texture 1", 2D) = "white" {}
}
SubShader {
Tags{ "RenderType" = "Opaque" }
Pass{
// Disable lighting, we're only using the lightmap
Lighting Off
CGPROGRAM
// Must be a vert/frag shader, not a surface shader: the necessary variables
// won't be defined yet for surface shaders.
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
#ifdef LIGHTMAP_ON
float2 uv1 : TEXCOORD1;
#endif
};
struct appdata
{
half4 vertex : POSITION; // vertex position
float2 texcoord : TEXCOORD0;
#ifdef LIGHTMAP_ON
float2 texcoord1 : TEXCOORD1;
#endif
};
sampler2D _MainTex;
float4 _MainTex_ST; // Define this since its expected by TRANSFORM_TEX; it is also pre-populated by Unity.
v2f vert(appdata i) {
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
// UnityCG.cginc - Transforms 2D UV by scale/bias property
// #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
o.uv0 = TRANSFORM_TEX(i.texcoord, _MainTex);
// Use `unity_LightmapST` NOT `unity_Lightmap_ST`
#ifdef LIGHTMAP_ON
o.uv1 = i.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
return o;
}
half4 frag(v2f i) : COLOR{
half4 main_color = tex2D(_MainTex, i.uv0);
// Decodes lightmaps:
// - doubleLDR encoded on GLES
// - RGBM encoded with range [0;8] on other platforms using surface shaders
//inline fixed3 DecodeLightmap(fixed4 color) {
// #if defined(SHADER_API_GLES) && defined(SHADER_API_MOBILE)
//return 2.0 * main_color.rgb;
//#else
//return (8.0 * main_color.a) * main_color.rgb;
//#endif
//}
#ifdef LIGHTMAP_ON
main_color.rgb *= DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1));
#endif
return main_color;
}
ENDCG
}
}
}
Properties{
_MainTex("Texture 1", 2D) = "white" {}
}
SubShader {
Tags{ "RenderType" = "Opaque" }
Pass{
// Disable lighting, we're only using the lightmap
Lighting Off
CGPROGRAM
// Must be a vert/frag shader, not a surface shader: the necessary variables
// won't be defined yet for surface shaders.
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
#ifdef LIGHTMAP_ON
float2 uv1 : TEXCOORD1;
#endif
};
struct appdata
{
half4 vertex : POSITION; // vertex position
float2 texcoord : TEXCOORD0;
#ifdef LIGHTMAP_ON
float2 texcoord1 : TEXCOORD1;
#endif
};
sampler2D _MainTex;
float4 _MainTex_ST; // Define this since its expected by TRANSFORM_TEX; it is also pre-populated by Unity.
v2f vert(appdata i) {
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
// UnityCG.cginc - Transforms 2D UV by scale/bias property
// #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
o.uv0 = TRANSFORM_TEX(i.texcoord, _MainTex);
// Use `unity_LightmapST` NOT `unity_Lightmap_ST`
#ifdef LIGHTMAP_ON
o.uv1 = i.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
return o;
}
half4 frag(v2f i) : COLOR{
half4 main_color = tex2D(_MainTex, i.uv0);
// Decodes lightmaps:
// - doubleLDR encoded on GLES
// - RGBM encoded with range [0;8] on other platforms using surface shaders
//inline fixed3 DecodeLightmap(fixed4 color) {
// #if defined(SHADER_API_GLES) && defined(SHADER_API_MOBILE)
//return 2.0 * main_color.rgb;
//#else
//return (8.0 * main_color.a) * main_color.rgb;
//#endif
//}
#ifdef LIGHTMAP_ON
main_color.rgb *= DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1));
#endif
return main_color;
}
ENDCG
}
}
}
И скрины:
На обоих скринах одна и та же модель. Только на первом - модель, для которой в сцене были запечены lighmap`ы, а вторая добавлена после запекания и соотвествующей текстуры теней не имеет. Шейдер у них один и тот же (приведен выше), но почему-то та модел у которой есть lightmap - не отображается.
Почему?
Что мне нужно исправить???