Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Meta pass and Precomputed Realtime GI Question

Discussion in 'Global Illumination' started by MaT227, Aug 6, 2015.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Hey everyone,

    I am writing a shader and I would like to use some special values for the precomputed realtime GI. I am using a vertex fragment shader as it seems that it offers much more controls than a surface shader.

    Unfortunately it doesn't seems possible as the data used in the meta pass are static, I mean they come from the static textures used in the material and the colors or values transformation applied.
    As an example, if I make some modification on the self-illumination value in any other pass other than the meta pass, the result is not applied.

    Is there a way to use the data calculated in the forwardbase pass or forwardadd pass or from any other pass ? Is there maybe another way to do that maybe using Command buffers ?

    If someone could give me a clue it would help me so much because for the moment I don't see any way to do that.
    Thank you very much.
     
  2. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Any idea ?
     
  3. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Code (CSharp):
    1. Shader "Custom/orangeShader"{
    2.  
    3.     Properties {
    4.         _Color ("Color", Color)=(1,1,1,1)
    5.         _MainTex ("Albedo (RGB)",2D)="white"{}
    6.         _Glossiness ("Smoothness", Range(0,1))=0.5
    7.         _Metallic ("Metallic", Range(0,1))=0.0
    8.  
    9.         _GIAlbedoColor ("Color Albedo (GI)", Color)=(1,1,1,1)
    10.         _GIAlbedoTex ("Albedo (GI)",2D)="white"{}
    11.     }
    12.  
    13.     SubShader {
    14.     // ------------------------------------------------------------------
    15.     // Extracts information for lightmapping, GI (emission, albedo, ...)
    16.     // This pass it not used during regular rendering.
    17.         Pass
    18.         {
    19.             Name "META"
    20.             Tags {"LightMode"="Meta"}
    21.             Cull Off
    22.             CGPROGRAM
    23.  
    24.             #include"UnityStandardMeta.cginc"
    25.  
    26.             sampler2D _GIAlbedoTex;
    27.             fixed4 _GIAlbedoColor;
    28.             float4 frag_meta2 (v2f_meta i): SV_Target
    29.             {
    30.                 // we're interested in diffuse & specular colors,
    31.                 // and surface roughness to produce final albedo.
    32.                
    33.                 FragmentCommonData data = UNITY_SETUP_BRDF_INPUT (i.uv);
    34.                 UnityMetaInput o;
    35.                 UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o);
    36.                 fixed4 c = tex2D (_GIAlbedoTex, i.uv);
    37.                 o.Albedo = fixed3(c.rgb * _GIAlbedoColor.rgb);
    38.                 o.Emission = Emission(i.uv.xy);
    39.                 return UnityMetaFragment(o);
    40.             }
    41.            
    42.             #pragma vertex vert_meta
    43.             #pragma fragment frag_meta2
    44.             #pragma shader_feature _EMISSION
    45.             #pragma shader_feature _METALLICGLOSSMAP
    46.             #pragma shader_feature ___ _DETAIL_MULX2
    47.             ENDCG
    48.         }
    49.        
    50.         Tags {"RenderType"="Opaque"}
    51.         LOD 200
    52.  
    53.         CGPROGRAM
    54.         // Physically based Standard lighting model, and enable shadows on all light types
    55.         #pragma surface surf Standard fullforwardshadows nometa
    56.         // Use shader model 3.0 target, to get nicer looking lighting
    57.         #pragma target 3.0
    58.  
    59.         sampler2D _MainTex;
    60.  
    61.         struct Input {
    62.             float2 uv_MainTex;
    63.         };
    64.        
    65.         half _Glossiness;
    66.         half _Metallic;
    67.         fixed4 _Color;
    68.        
    69.         void surf (Input IN,inout SurfaceOutputStandard o){
    70.             // Albedo comes from a texture tinted by color
    71.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex)* _Color;
    72.             o.Albedo = c.rgb;
    73.             // Metallic and smoothness come from slider variables
    74.             o.Metallic = _Metallic;
    75.             o.Smoothness = _Glossiness;
    76.             o.Alpha = c.a;
    77.         }
    78.         ENDCG
    79.     }
    80.  
    81.     FallBack "Diffuse"
    82. }
     
  4. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Thank you @KEngelstoft
    I am also wondering if there is an easy way to transfer the results calculated in the forwardbase, forwardadd or deferred pass to the meta pass.
    The only way to do that in my opinion is to use a new buffer but this will result in calculating the lighting twice and constrain it to screenspace, no ?

    To make it quick do you have any clue or idea on how transferring the lighting data to the meta pass (deferred and forward). Because I am trying to send my translucency results calculated in forwardadd and deferred pass to the meta pass.
     
  5. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    You should render the translucency information into a render target and sample that as a texture in the meta pass. That way you don't have to do the lighting calculation twice.
     
  6. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    @KEngelstoft Could you be a bit more explicit about the process of using a render target to sample the result of the forward or deferred lighting pass in the meta pass ? I've already tried render target but I didn't really dig into it and I didn't achieved to make it work. I also didn't found any tutorial about it.

    Just to know, the result will be in screenspace so, everything outside screen won't be taken into account.
     
  7. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    Remember that the meta pass is running in lightmap space, so you have to calculate the translucency results in the same space and there will be a 1-to-1 mapping from your translucency to the meta pass.
     
  8. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    @KEngelstoft Thank you for all you help. This might be a little off-topic but as this is the first time I'll be using RenderTargets, I have some difficulties about understanding the whole process of using it with all the passes (deferred, forwardadd). And there is the lightmap space calculation I need to dig it too. :)
    I can output the result of those passes to another rendering target in addition to its regular output ? How ?

    Thank you again for all the precisions you're giving. I hope this will be useful to someone else too. :)