Search Unity

Standard Shader (fade) doesnt display correctly on rigged mesh

Discussion in 'Shaders' started by jamuk, Mar 26, 2015.

  1. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    hi,
    I'm using the standard shader with the fade option as I am fading the object alpha to show and hide.
    For some reason the object looks like its transparent. At first I thought the normals were reversed but they are fine. If I remove the rig it displays perfectly. I can't work out why.
    Import settings were Rig/ animation type/ legacy.
    Can someone help - many thanks in advance!
     
    Last edited: Mar 26, 2015
  2. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Please help, Anyone.... any idea's ?
     
  3. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    I thought this problem was being caused by Rigged/Skinned Objects - it turns out that any object exported from Maya directly or via FBX incurs the same problem. See attached image.
    Is this a bug or something I'm doing wrong???
     

    Attached Files:

  4. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    122
    Hum hard to tell but if it happen on any mesh and not only skinned one, I wouldn't be surprized that's it's simply a classical ordering problem of complex non-convex mesh (see here at "Transparent shader with depth write" for an example and explanation of the problem : http://docs.unity3d.com/Manual/SL-CullAndDepth.html)

    You can't enable depth write on the standard shader though, so I would recommend copying it (you can find it in the built-in shader archive here : http://unity3d.com/get-unity/download/archive) and adding the depth write to all versions.
     
  5. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Hi, I'm sorry I don't really understand, are you saying that every time I export from Maya and use the Standard shader with fade option that I have to somehow modify the shader code to include depth write?
     
  6. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    122
    In a sort, for non-convex object, if you want to avoid ordering problem, yes.

    The problem is the following :

    - usually when rendering, before being shaded, a pixel check its depth in a depth buffer. If its depth is less than the depth already in the depth buffer the pixel is shaded (shader executed). If it's more, the pixel is discarded (shader skipped for that pixel)

    - But triangles of a mesh are rendered in an arbitrary orders.

    So now consider this situation.

    A B >[Cam] <--- Triangle A is behind Triangle B from the camera point of view.

    - It's not a problem for opaque mesh, as if B is rendered after A, it will just override the pixel. If it's rendered before A, then A won't even have its shader called as the depth buffer will contain de depth of B, which is less because closer to the camera.

    - But now for transparent mesh, ordering matters! Because you need to blend, according to alpha (even if it's 1 or 0), so you need to render the two pixels.
    - But if the depth write is still enabled, if B is rendered first, then A will never be rendered. Causing rendering problem.

    So, translucent shader have depth write disable.

    Now this work "most" of the time, as backfaces are culled. So with a convexe mesh (cube, sphere etc...) only the face facing the camera will be drawn

    this face is seen from the back so not draw => | Cube | >[Cam]

    But in case of your PC case meshes, the inside of the case also have visible polygon, so they will be drawn and blended with the front outside faces, causing ordering problems, bad blending etc...


    TL;DR : if you want to fade complex non-convex meshes, there is sadly no miracle solution : you have to write a shader that does the fading with depth write enable + probably rendering after all other translucent objects ( "Queue"="Transparent+1" ) to avoid it causing glitch with other transparent things...

    TL;DR 2 : Translucency is still a hard problem with no perfect solution in 3D :(
     
  7. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    I really appreciate you help on this, but I'm not sure we are talking about the same thing.:)
    Both the objects in my image are using the same shader Unity 5 standard (fade). Yet one has a look like its normals are reversed and the other displays perfectly. The only difference between the two objects is that the one was created in Maya and the other in Unity itself.
     
  8. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    122
    Well where they are created don't seems to be the only difference.

    The one in maya seems more than a simple box, and to have interior polygon (if you place a camera INSIDE the model, you still see faces), while the unity cube only have 12 faces, all facing the exterior (if you place a camera inside the cube you don't see it, because its face are culled by backface culling)

    The unity cube LOOKS opaque, because only the facing faces are draw, so nothing get blended with them.

    The maya one LOOKS transparent, because interiors faces get blended OVER the forward faces because they probably are drawn AFTER the forward one.

    So your pixels goes through those steps :

    Cleared -> frontFaceColor -> frontFaceColor * (1 - interiorFaceAlpha) + interiorFaceColor * interiorFaceAlpha

    So your interior face just "erase" your front face, making the cube appear transparent.

    I'm sorry if I'm not clear = /. I don't know maya, but chance are if you create a simple box, export it as OBJ and import it in unity, it will behave the same as the Unity cube.
     
  9. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Thanks for the explanation and all your help, I see what your saying now. :)
    I had a look at your earlier email with the links in for the shader fix. As you might have guessed I'm not much of a coder. Do i just copy and paste the following in to the shader script?

    Shader "Reveal Backfaces" {
    Properties {
    _MainTex ("Base (RGB)", 2D) = "white" { }
    }
    SubShader {
    // Render the front-facing parts of the object.
    // We use a simple white material, and apply the main texture.
    Pass {
    Material {
    Diffuse (1,1,1,1)
    }
    Lighting On
    SetTexture [_MainTex] {
    Combine Primary * Texture
    }
    }

    // Now we render the back-facing triangles in the most
    // irritating color in the world: BRIGHT PINK!
    Pass {
    Color (1,0,1,1)
    Cull Front
    }
    }
    }
     
  10. Stephan-Tanguay

    Stephan-Tanguay

    Joined:
    Jan 27, 2011
    Posts:
    21
    jmasinteATI likes this.
  11. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    thanks, will give it a go.
     
  12. jmasinteATI

    jmasinteATI

    Joined:
    Jul 14, 2015
    Posts:
    7
    @stephan-tanguay.33764, That worked perfectly! I went with Shader 2.0.
     
unityunity