Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

'Scale In Lightmap' accessible from shader?

Discussion in 'Global Illumination' started by IgorAherne, Aug 21, 2021.

  1. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    I am trying to create a Shader (for debugging purposes) that will show checkerboard on any object.
    However, the checkerboard must correspond to the "Lightmap Scale" parameter from the MeshRenderer component:
    upload_2021-8-21_2-3-38.png

    I can't get my shader to work. Tried using unity_LightmapST variable, but it's always (1,1,0,0).

    This will be a ReplacementShader for the SceneView (I will use System.Reflection hack) meaning I cannot use any c# script to manually upload the value into the shader properties.
    Is there a shader-keyword for this Scale-in-Lightmap, so I can scale my uv1 coordinates?


    Code (CSharp):
    1. Shader "Whaleride/LightmapCheckerScieneView"
    2. {
    3.     Properties
    4.     {
    5.         _ColorA("Color A", Color) = (1, 0.95, 0.9, 1.0)
    6.         _ColorB("Color B", Color) = (0.9, 0.85, 0.8, 1.0)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             // make fog work
    18.  
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float2 uv1 : TEXCOORD1;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float2 uv1 : TEXCOORD1;
    30.                 float4 vertex : SV_POSITION;
    31.             };
    32.  
    33.             fixed4 _ColorA;
    34.             fixed4 _ColorB;
    35.  
    36.             v2f vert (appdata v)
    37.             {
    38.                 v2f o;
    39.                 o.vertex =  UnityObjectToClipPos(v.vertex);
    40.                 o.uv1 =  v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw; //not working :(
    41.                 return o;
    42.             }
    43.  
    44.             fixed4 frag(v2f i) : SV_Target
    45.             {
    46.                 fixed2 val = step(0.5f, frac(i.uv1));
    47.                 fixed4 col = lerp(_ColorA, _ColorB, val.x*val.y);
    48.                 return col;
    49.             }
    50.             ENDCG
    51.         }
    52.     }
    53. }
    54.  
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,624
    I believe you also need

    #pragma multi_compile __ LIGHTMAP_ON

    and for the gameobject to be lightmap static (which isn't clear if it already is from your screenshot) and probably you would also need to do a bake so the values get calculated.

    The good thing is, if you don't actually want to have baked lighting, you can store the values somewhere and set them with

    transform.GetComponent<Renderer>().lightmapScaleOffset = mySavedScaledOffset;

    (although you need to do this at runtime, since Unity does not serialize these values on the renderer like it used to a few years ago)
     
    Last edited: Aug 21, 2021
  3. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    I want to replicate the checkerboard that unity displays; Somehow they show it even if the objects weren't baked (as long as they are static). And they manage to update this checkerboard based on "Scale In Lightmap"
    upload_2021-8-21_13-52-18.png

    Added #pragma multi_compile __ LIGHTMAP_ON but the shader still can't get hold of that lightmap-scale parameter
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,624
    How accurate to Unity's pattern do you need this to be? Does it have to be exact?

    I'm not sure what Unity does exactly, but I'm pretty sure the pattern does change when you press bake, because that's when the atlas-ing happens, so before you actually bake what you're being shown is not "super" meaningful.
     
  5. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    I hope to visualize the resolution, to compare the scales of objects in my scene.
    In the current mode it's difficult to see what's happening, everything looks semi-transparent and gray :'(