L
L
lPestl2016-08-29 18:06:30
3D
lPestl, 2016-08-29 18:06:30

Shader in Unity (v 5) for Android makes objects that have LightMap assigned invisible. How to fix and display Lightmaps?

I am writing a game for Android. (UPD: I am writing on Unity3d v5.4.0f2, the texture baking system has changed in the 5th version). There is a need for a custom shader that displays baked shadow textures (lightmap). I'm new to writing shaders. Googling, I found a "blank" for such a shader. It works as it should when the PC, Mac & Linux standalone development platform is enabled, but it stops displaying objects that already have textures baked onto them. After I slightly modified it, it began to display at least objects that do not have a lightmap assigned. What do I need to fix to make lightmaps show up on objects?
The shader itself:

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
    }
  }
}

And screenshots: Both screenshots
088d0a50b133408ca1c1b201d060a1ba.PNG0bbb2828d7aa41ba8bb56a2faa4038b3.PNG
show the same model. Only on the first one - a model for which lighmaps were baked in the scene, and the second one was added after baking and does not have the corresponding shadow texture. They have the same shader (given above), but for some reason the model that has a lightmap is not displayed.
Why?
What do I need to fix???

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lPestl, 2016-08-31
@lPestl

If suddenly someone has the same trouble - I write the solution here.
In order for lightmaps to be displayed in the Editore, it was enough for me to switch from Legacy Vertex Lit to any other in Player Settings -> Other Settings -> Rendering Path. True, Legacy Deffered (light prepass) caused the most severe artifacts during assembly on the device. As a result, I stopped at the Deffered position. See screenshot:
But after that, another problem arose: lightmaps are displayed in the Editor, and baked shadows are not visible when assembled on the device. Having surfed through a bunch of forums, I accidentally stumbled somewhere that just a shader that contains the #pragma multi_compile line for lightmaps does not display lightmap on the device. I don’t know if this is a bug in the unit or something unknown, but after I commented out the line
#pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
​​and all occurrences of LIGHTMAP_ON and LIGHTMAP_OFF ​​- everything worked for me on the device and shadows baked in lightmaps began to be displayed. Thank you all, stop camera.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question