Search Unity

Custom vertex animations, shadows and Unity 4.2: broken

Discussion in 'Shaders' started by larsbertram1, Sep 3, 2013.

  1. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    hi there,

    due to unity’s new feature of batching shadow collectors and shadow casters shaders which use a custom vertex animation seem to cast and receive wrong shadows (tested in unity 4.2.1 indie, forward rendering): the position of the projected shadows does not fit the position of the drawn mesh:

    $Bildschirmfoto 2013-09-03 um 16.27.01.png

    can anybody confirm this or does even have a solution for this?

    lars
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Are they object-space vertex animations?

    You might have to make them world-space if you can, so that no matter how they're batched, it'll look correct.
     
  3. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900

    hi farfarer,

    many thanks for the answer.

    the animations are object space based, right.
    and i have already tested to make them use world space but then scaled instances got distorted…
    well i will have to test some more,

    lars
     
  4. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    hi there,

    finally i have solved my problem.
    i had to covert ALL vertex related inputs (position and normal) into world space AND use unity scale…

    so this might be how a vertex animation could look like this:

    Code (csharp):
    1.  
    2. inline float4 myVertecAnimation (float4 pos, float3 normal, float your animationParameter) {
    3.   pos.xyz = mul( _Object2World, pos).xyz;
    4.   normal.xyz = mul( _Object2World, float4(normal, 0.0) ).xyz;
    5.  
    6.   // your animation here, eg.:
    7.  
    8.   pos.xyz += normal.xyz * animationParameter.xxx;
    9.  
    10.   // finally bring pos back to local space and scale it!
    11.  
    12.   pos.xyz = mul( _World2Object, pos).xyz * unity_Scale.w;
    13.   return pos;
    14. }
    15.  
    this function will work in all passes: the regular one, shadow collector and shadow caster pass.
    absolutely no rocket science but nevertheless it took me quite a while…

    lars
     
    Last edited: Sep 3, 2013