Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Lightmaps don't work in Replaced shaders

Discussion in 'Shaders' started by 0xDEADCODE, Feb 28, 2012.

  1. 0xDEADCODE

    0xDEADCODE

    Joined:
    Jul 26, 2011
    Posts:
    25
    I've been trying to figure out this problem for some time now with no success. If you try to use a replaced shader in a scene that is currently using lightmaps, the lightmap variables are never set in the shader. This causes unpredictable texture problems since the shader attemps to calculate lightmaps based on garbage values.

    To show what exactly is happening, I have included a link to a web player build of the project.
    http://friso.ca/LightmapIssue_WebPlayer/LightmapIssue.html

    In this project, all materials are using the same default shader shown below. This is the same shader that is passed in to the replacement shader:
    Code (csharp):
    1.  
    2. Shader "Custom/BasicDiffuseShader" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         LOD 200
    9.        
    10.         CGPROGRAM
    11.         #pragma surface surf Lambert
    12.  
    13.         sampler2D _MainTex;
    14.  
    15.         struct Input {
    16.             float2 uv_MainTex;
    17.         };
    18.  
    19.         void surf (Input IN, inout SurfaceOutput o) {
    20.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    21.             o.Albedo = c.rgb;
    22.             o.Alpha = c.a;
    23.         }
    24.         ENDCG
    25.     }
    26.     FallBack "Diffuse"
    27. }
    28.  
    The script below is what is currently setting up the replacement shader:
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class TestReplacedShaderCall : MonoBehaviour {
    7.  
    8.     public Shader m_replacedShader = null;
    9.     public Camera m_Camera;
    10.     private RenderTexture m_RT;
    11.  
    12.     void Start(){
    13.         m_RT = RenderTexture.GetTemporary(Screen.width, Screen.height, 32);
    14.         m_Camera.targetTexture = m_RT;
    15.         m_Camera.enabled = true;
    16.         m_Camera.SetReplacementShader(m_replacedShader, "");
    17.     }
    18.    
    19.     void LateUpdate(){
    20.         //Using this method produces the same output as using SetReplacementShader() in Start()
    21.         //m_Camera.enabled = false;
    22.         //m_Camera.RenderWithShader(m_replacedShader, "");      
    23.     }
    24.  
    25.     void OnGUI(){
    26.         GUILayout.BeginArea(new Rect(Screen.width * 0.5f, 0, Screen.width * 0.5f, Screen.height * 0.5f));
    27.         GUILayout.Box(m_RT);
    28.         GUILayout.EndArea();
    29.     }
    30. }
    31.  
    Because the lightmap data is not being initialized in the shader, the shader variable "unity_LightmapST" is garbage, which is used in all the calculations for determining the correct lightmap offset and scaling. Is there a way to ensure that this variable still gets set? or pass in my own variable to replace it without iterating through every object and calling Material.SetVector()?
     
  2. mtpabis

    mtpabis

    Joined:
    Oct 18, 2012
    Posts:
    13
    Hi, have you found the solution yet?

    Can anyone shed some light on this issue (no pun intended)?