Search Unity

Light probes in custom vertex/fragment shader

Discussion in 'Shaders' started by bfogerty, Nov 2, 2014.

  1. bfogerty

    bfogerty

    Joined:
    Nov 12, 2010
    Posts:
    58
    HI!

    I would like to use lightprobes in my scene however my objects use a material with a custom shader that is not a surface shader. I have my own vertex and fragment shader but I can't find any documentation on how to bring in the light probe data into the shader. Any ideas? Thanks!
     
  2. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    700
    You can use #pragma debug and open up the compiled surface shader to see what's going on in there. I have looked at this and the way that the surface shaders do it is to use the Unity provided ShadeSH9 function in the vertex shader. Just give it your world vertex normal and it will spit out a color calculated from light probes.
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    #include "UnityCG.cginc"
    This is where ShadeSH9 is.


    Vert:
    float3 shl = ShadeSH9(float4(worldNormal,1));

    pack this into a varying or multiply your vertex colour by it for frag.
     
  4. bfogerty

    bfogerty

    Joined:
    Nov 12, 2010
    Posts:
    58
    So the following is the shader I wrote however my material is coming in as black when I multiply by the output of the ShadeSH9 function. However when I swap the shader with Unity's builtin Diffuse shader, I see my object's shadow based off the light probes. Any idea of what I am doing wrong? Thanks!

    Code (csharp):
    1.  
    2. Shader"Custom/LightMapTest" {
    3. Properties {
    4. _MainTex ("Base (RGB)", 2D) = "white" {}
    5. }
    6. SubShader {
    7. Tags { "RenderType"="Opaque""BW"="TrueProbes" }
    8. LOD200
    9.  
    10. Pass
    11. {
    12. CGPROGRAM
    13.  
    14. #pragmavertexv
    15. #pragmafragmentp
    16. #include "UnityCG.cginc"
    17.  
    18. sampler2Dunity_Lightmap;
    19. float4unity_LightmapST;
    20.  
    21. structVertOut
    22. {
    23. float4position : POSITION;
    24. float2uv : TEXCOORD0;
    25. float4vLight : COLOR;
    26. };
    27.  
    28. VertOutv (float4position : POSITION, float3norm : NORMAL, float2uv : TEXCOORD0 )
    29. {
    30. VertOutv;
    31.  
    32. v.position = mul( UNITY_MATRIX_MVP, position );
    33. v.uv = uv.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    34.  
    35. float3normal = normalize( mul( float3x3(_Object2World), norm ) );
    36.  
    37. v.vLight = float4( ShadeSH9( float4( normal, 0.0 ) ), 1.0 );
    38.  
    39.  
    40. returnv;
    41. }
    42.  
    43. structPixelOut
    44. {
    45. float4color : COLOR;
    46. };
    47.  
    48. PixelOutp( VertOutIN )
    49. {
    50. PixelOutOUT;
    51.  
    52. float4lightMapColor = tex2D( unity_Lightmap, IN.uv );
    53. lightMapColor = float4( (8.0 * lightMapColor.a) * lightMapColor.rgb, 1.0 );
    54.  
    55. float4color = float4( 1.0, 0.0, 0.0, 1.0 ) * lightMapColor;
    56. OUT.color = color * float4( IN.vLight.xyz, 1.0 );
    57.  
    58. returnOUT;
    59. }
    60.  
    61. ENDCG
    62. }
    63. }
    64. FallBack"Diffuse"
    65. }
    66.  
     
  5. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Maybe name of pass or tags?
    Try:
    Code (csharp):
    1.  
    2. Pass {
    3.         Name "FORWARD"
    4.         Tags { "LightMode" = "ForwardBase" }
     
  6. bfogerty

    bfogerty

    Joined:
    Nov 12, 2010
    Posts:
    58
    Unfortunately that didn't change anything.
     
  7. bfogerty

    bfogerty

    Joined:
    Nov 12, 2010
    Posts:
    58
    Any ideas?
     
  8. jasonMouseHand

    jasonMouseHand

    Joined:
    Feb 19, 2013
    Posts:
    10
    Hi bfogerty,
    I just got it working in Unity5, I am not sure if you still need it but I post it here anyway.
    Basically you need to have the "BW"="TrueProbes" and "LightMode" = "ForwardBase" in your tag list. But what I don't understand is why it must be forwardBase and can't be the others like deferred.
    cheer
     
  9. Jason-Michael

    Jason-Michael

    Joined:
    Jul 6, 2015
    Posts:
    20
    1. Change Rendering Path to Forward in Player Settings
    2. Add "LightMode"="ForwardBase" to your Tags
    3. I thought the Normal transformation would be better if use this :
    float3 worldNormal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal)
    ;)
     
  10. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    I mean this was 100% 3 years old at the time of your reply, and 2 major unity versions ago, but thankyou for adding your solution all the same :)
     
  11. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    What is the URP or custom SRP approach to "ShadeSH9"?
    Can't find it anywhere.
     
    nhdu73 likes this.
  12. CaptainLuft

    CaptainLuft

    Joined:
    Nov 20, 2017
    Posts:
    2
    In "Core RP" Package , EntityLighting.hlsl
    upload_2021-4-28_18-40-36.png
     
    Propagant and zezba9000 like this.