Search Unity

Lighting an object from surrounding vertex colors?

Discussion in 'Shaders' started by Tiernan98, Jan 1, 2021.

  1. Tiernan98

    Tiernan98

    Joined:
    Jul 11, 2017
    Posts:
    42
    Hi,

    I have a game scene that uses vertex colors for its lighting (as in, normal Unity lights have no effect), with the main environment light setting set to white. If a vertex color is black, it will appear black, but if it is white the texture will appear lit. Hopefully, this makes sense.

    Currently, the character has no shadows or color added from the surrounding environment and looks out of place. I was wondering if there is some technique to project the surrounding vertex colours on to the character to make them look grounded in the scene?

    Any advice appreciated. :)
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Assuming your shader is still using the standard lighting functions, then you can place Light Probe Groups in the scene which your shader will then sample additional lighting from, though you'll have to also as a Light Probe Proxy Volume onto the skinned character so it can pick that data up.

    https://docs.unity3d.com/Manual/class-LightProbeProxyVolume.html
     
  3. Tiernan98

    Tiernan98

    Joined:
    Jul 11, 2017
    Posts:
    42
    Sorry, I am a programmer with not much in-depth knowledge of Unity lighting. What do you mean by the "standard lighting functions"? The shader I have selected on the environment is "Particles/Standard Surface" and there are no Unity light objects in the scene.

    Would your method work with this?
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    If you have baked lighting into the scene yes and that your character is using a standard shader like that as well. You won't get light from dynamic light sources bounced off the environment onto the character though, only the baked bounce lighting. Anything more would require a custom lighting solution by you or an existing third party one.
     
  5. Tiernan98

    Tiernan98

    Joined:
    Jul 11, 2017
    Posts:
    42
    Thanks. So I added a light probe proxy volume to my character and changed its mesh renderer so that it used the volume. I tried placing light probes around the scene as you normally would, then I set my environment model to static and turned on "Baked Indirect" GI, then let it generate the lighting. Now my character is just completely black lol instead of being fully lit and out of place like before.

    Does this mean the vertex color doesn't bounce because its not technically coming from a light source, as its just a predefined vertex color?
     
  6. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Is your baked mode set to Shadowmask? And are your lights set as Mixed?

    What shader are you using on the character?
     
  7. Tiernan98

    Tiernan98

    Joined:
    Jul 11, 2017
    Posts:
    42
    I can try it with Shadowmask.

    With regards to lights though, that is the thing about this scene and the reason why I was wondering if there was a way to have the vertex colour apply to the character because there are zero lights in the scene. Otherwise I would not have this problem.

    The scene's "lighting" comes purely from its vertex colour. With the standard shader this would not be visible, which is why I am using the standard surface particle shader on the environment. The environment is completely visible and looks as it should; its just the character that does not look correct.

    The shader on the character is the Standard Unity shader. The only thing lighting the character previously was the environment color set in the lighting settings.
     
  8. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Okay well that would be an issue then yeah, need lights for lighting to bake haha. One option would be to modify a surface shader to output your vertex color to the Emission output and then bake... Then your light probes should be able to capture that and thus effect your character.

    Code (CSharp):
    1. Shader "Custom/StandardVertexEmissive"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.  
    13.         CGPROGRAM
    14.         #pragma surface surf Standard fullforwardshadows
    15.         #pragma target 3.0
    16.  
    17.         sampler2D _MainTex;
    18.         fixed4 _Color;
    19.  
    20.         struct Input
    21.         {
    22.             float2 uv_MainTex;
    23.             float4 color : COLOR; //Vertex color
    24.         };
    25.  
    26.         void surf (Input IN, inout SurfaceOutputStandard o)
    27.         {
    28.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color * IN.color;
    29.             o.Albedo = c.rgb;
    30.             o.Emission = c.rgb;
    31.         }
    32.         ENDCG
    33.     }
    34.     FallBack "VertexLit"
    35. }
    edit: Hmm, the issue is getting the lightmapper to actually pick up the vertex colors though... Creating a custom META pass doesn't seem to be helping =/
     
    Last edited: Jan 2, 2021