- Home /
Question by
Guilherme-PX3 · Apr 18, 2020 at 08:23 PM ·
prefabshadersmobileoptimizationbakedlighting
Issue while using shader for instantiating a baked-lighting prefab
I was trying to optimize my game by using baked lights on instanced prefabs, like house interiors, but the lightmap uv does not apply correctely for each object. I found some old samples on the forum, and I tryed to apply it to multiple objects.
I saw that DecodeLightmap could be the solution for it, but I still got those results as in the image, and I have a very basic knowledge about shaders.
Shader "Custom/Lightmap Prefab V2" {
Properties {
_MainTex ("Main Texture", 2D) = "white" {}
_Color2 ("Color", Color) = (1, 0.5, 0.5, 1)
}
SubShader
{
LOD 200
Tags
{
"RenderType" = "Opaque"
"Queue" = "Geometry"
}
Pass
{
Cull Off
Fog { Mode Off }
AlphaTest Off
Blend Off
CGPROGRAM
#pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
#pragma vertex vert
#pragma fragment frag
#pragma glsl_no_auto_normalization
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
half4 _Color2;
// sampler2D unity_Lightmap;
// float4 unity_LightmapST;
struct Vertex
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
float4 uv2 : TEXCOORD1;
};
struct Fragment
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
float4 uv2 : TEXCOORD1;
};
Fragment vert(Vertex v)
{
Fragment o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = (v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw);
o.uv2.xy = v.uv2.xy * (unity_LightmapST.xy / 2 + unity_LightmapST.zw / 2);
//o.rgb *= (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv[1]))) * _Power;
return o;
}
fixed4 frag(Fragment IN) : COLOR
{
fixed4 output = fixed4(0, 0, 0, 1);
output.rgb = _Color2 * tex2D(_MainTex, IN.uv.xy).rgb;
output.rgb *= 2 * DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, IN.uv2.xy));
//(DecodeLightmap(tex2D(unity_Lightmap, i.uv[1])))
return saturate(output);
}
ENDCG
}
}
Fallback Off
}
If you could explain would be awsome to me to learn more :)
sc.png
(577.8 kB)
Comment