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

Sample "unity_lightmap" outside the fragment shader

Discussion in 'Shaders' started by Fallc, Jan 11, 2016.

  1. Fallc

    Fallc

    Joined:
    Mar 13, 2015
    Posts:
    48
    Hello!

    For my current work, I need to iluminate procedual geometry (gras). By channeling the UV2 coordinates way trought the pipeline (vertex-> hull -> tesselation -> domain -> geometry -> fragment) and finally sampling them inside the fragment shader like below I made very good results.

    Code (csharp):
    1. DecodeLightmap(tex2D(unity_Lightmap, i.lightmapUV));
    It would be sulficiant for me to sample the lightmap inside the domainshader or atleast the geometry shader. But using tex2Dlod like below:

    gives the error
    It seems to me that "unity_Lightmap" is simpily not defined outside the fragmentshader, Is this a bug? Or is it technically not possible to accsess the texture outside the fragmentshader (I can't really image that)?

    TL;DR "unity_Lightmap" can't be sampled outside the fragmentshader. Why?
     
  2. mlhope

    mlhope

    Joined:
    May 23, 2013
    Posts:
    4
    Running into the same problem. Bump.
     
  3. Magdoca

    Magdoca

    Joined:
    Jun 3, 2019
    Posts:
    7
    I know this is old, but it is the only thread I was able to find when looking into this. I was able to sample the lightmap in the vertex shader with this macro:

    Code (CSharp):
    1. o.ambient = UNITY_SAMPLE_TEX2DARRAY_LOD(unity_Lightmap, uv.xy, 0).rgb;
     
    HeyItsLollie likes this.
  4. HeyItsLollie

    HeyItsLollie

    Joined:
    May 16, 2015
    Posts:
    68
    Lifesaver, thank you for sharing this.
    I was wracking my brain trying to figure out how to make unity_Lightmap and tex2Dlod talk to each other, so that I could sample Unity's lightmaps as vertex colors. Just had to make sure that the right UVs were being passed. (in my case, I had to remove ".rgb" from the end for some reason.)
     
  5. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    400
    unity_Lightmap.SampleLevel(samplerunity_Lightmap, o.uv2), 0.0);