Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Z Fighting issue only on mobile device

Discussion in 'Editor & General Support' started by Willy-The-Kid, Jul 6, 2015.

  1. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hello.

    In the early steps of development, I test my game (that is running quiet nicely in Editor) on my Ipad device and there starts the troubles.

    It seems like there is a Z-Fighting issue on device.

    Solutions I tried to fix the issue :

    1 - Change all materials used to : Mobile/Unlit
    2 - Verify that there is no alpha on any png texture
    3 - Turn texture to 24 png (no alpha)
    4 - Turn texture to tga (no alpha)
    5 - Change Camera settings
    -> Increase Near clipping plane up to 2 (can't go up that without loosing some of my scene)
    -> Decrease Far clipping plane down to 20 (can't go under without loosing some of my scene)
    -> Changed Rendering Path to "Forward"
    -> Activated / Deactivated Occlusion Culling

    I'm kinda out of idea to solve this issue. Could you help me please ?


    See image joined.
    Character should be hidden by trees. Same for the little green tree next to the character, it should be 100% hidden by the big dark green tree.

    IMG_0017.PNG
     
  2. Sickwitit

    Sickwitit

    Joined:
    Dec 22, 2014
    Posts:
    123
    Your issue looks like a simple layering/z depth problem. The closer to the camera the higher the Z value. 10 being closer than 9, and so on. What is the Z value of your trees? What is the Z value of your player? The player Z value should be lower than your tree Z value. If that's not the issue, check your layers, if you are using them. They should be on different layers.

    This also depends on the direction of your camera
     
  3. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hello Sickwitit,

    I guess the Z value you talking about is the one used in transform.position. If it's the case this is the hierarchy of one exemple tree :

    -> Prefab Tree (Clone) = x:1 y:0 z:3
    -> Child = x:0.5 y:0 z:0.5
    -> tree_mesh = x:0 y:0 z:0

    So the prefab instance definitly use a z position, but the true tree mesh is alway with Vector(0, 0, 0), cause it's local position from the parent I guess. Of corse, each instance of prefab as a different position on the map. Considering layers, all trees are on one layer, and player is on is own layer.

    Note that there is no issue when debuging on my computer, only zhen i run on IOS device.
     
    Last edited: Jul 6, 2015
  4. Sickwitit

    Sickwitit

    Joined:
    Dec 22, 2014
    Posts:
    123
    What Z index is your player at?
     
  5. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    This is a screenshot showing of positions.

    Character Z is set to 46,
    Tree Z is set to 43
    Camera Z is set to 42

    positions.png

    I start to understand what you try to tell me, the camera is watching in the wrong direction to get objects sorted nicely, right ?
     
  6. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    So following your idea, I changed my camera orientation, so now objects closers to the camera have higher Z values, and objects far from camera have lower Z value.

    But this does not resolve my issue, this is two screenshot, one in debug mode on my computer :
    As you can see , there is no Z fighting and all textures looks good.
    Screen Shot 2015-07-06 at 4.47.43 PM.png

    One on my Ipad :
    Z fightings and issues on character texture display :(
    IMG_0018.PNG
     
  7. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Any idea please ? I can't get away from this issue :(
     
  8. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Still stuck, tried this morning to create my own shader, in case this issues comes from the shader, without luck.
     
  9. Sickwitit

    Sickwitit

    Joined:
    Dec 22, 2014
    Posts:
    123
    Hmm.. I've noticed a lot of texture fighting issues when I have textures on the same layers or same Z-index. So, instead of changing the camera. Check if any textures are on the same layers or same z-index. Check all the trees. I've noticed that if I have (2 trees) on the same z-index, they will start fighting.
     
  10. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hello Sickwitit.

    I tried to separate trees and character on two different layers, no luck there.
    I tried to place each single tree on a separate layer, but seems there is only 31 different layers available.

    I'm desperate...
     
  11. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Soooo ....

    Here is a little progress on this issue. I managed to resolve a part of the display issue by creating my own shader. Basically, the main thing that resolved the issue is turning the queue to Transparent, wich looks against documentation as my mesh does not contains alpha, but I guess this force to consider Z-Depth. Now, there is not more Z-Depth issue from one mesh to another, as you can see trees in forest are not overlapping to each other.

    But there is still some poly not displayed or with wrong normals, like eyes of the character or darkest zone in trees foliages.

    IMG_0019.PNG


    If that can help anyone, this is my current shader code :
    Code (CSharp):
    1. Shader "Custom/Mobile/Test"
    2. {
    3.     Properties
    4.     {      
    5.         _MainText ("Texture", 2D)    = "white" {}
    6.     }
    7.  
    8.  
    9.     SubShader
    10.     {
    11.         Tags {"Queue" = "Transparent"}
    12.         ZWrite On
    13.      
    14.         Pass
    15.         {
    16.             Lighting off
    17.             Cull back
    18.             Offset 0,1
    19.          
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #pragma fragmentoption ARB_precision_hint_fastest
    24.             #pragma target 2.0
    25.             #include "UnityCG.cginc"
    26.          
    27.  
    28.             uniform sampler2D _MainText;
    29.             uniform float4      _MainText_ST;
    30.          
    31.             struct vertexInput
    32.             {
    33.                 float4 vertex        :    POSITION;
    34.                 float4 texcoord        :    TEXCOORD0;
    35.             };
    36.          
    37.             struct fragmentInput
    38.             {
    39.                 float4 pos            :    SV_POSITION;
    40.                 half2  uv            :    TEXTCOORD0;
    41.             };
    42.          
    43.          
    44.          
    45.          
    46.             fragmentInput vert (vertexInput i)
    47.             {
    48.                 fragmentInput o;
    49.              
    50.                 o.pos        =    mul(UNITY_MATRIX_MVP, i.vertex);
    51.                 o.uv        =    TRANSFORM_TEX(i.texcoord, _MainText);
    52.              
    53.                 return o;
    54.             }
    55.          
    56.             half4 frag (fragmentInput i):COLOR
    57.             {
    58.                 return tex2D(_MainText, i.uv);
    59.             }
    60.          
    61.             ENDCG
    62.         }
    63.     }
    64.  
    65.     FallBack "Diffuse"
    66. }
    67.  
     
  12. Sickwitit

    Sickwitit

    Joined:
    Dec 22, 2014
    Posts:
    123

    So the Z-Index issue is fixed now? Great!

    Can you provide a few more zoomed in screenshots of your other issue?
     
  13. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hello Sickwitit,

    Yeah, let's say it's "fixed" between quotations marks, because my little finger says it's probably very bad for performances to render all objects in transparent passes.

    This is some screenshots, with the expected rendering on left (the one I get on Mac) and the final rendering on the device (the one I get on Ios). basically, looks like some polys are missing on the front or are rendered in background instead of being rendered in front.


    The tree :
    - Some single leaves are missing
    - Left big leaves zone seems rendered behind middle leaves mesh
    - Rock at the bottom right is rendered behind the trunk instead of in front
    tree_screen.png


    The houses :
    - Front walls are not rendered in the back instead of in the front
    - Roof is rendered wrong, displaying inside beam outside of straw
    home_screen.png

    The character :
    - Eyes are not rendered / rendered reverse normal ?
    - Missing one poly on the hood
    - Missing the gem on torso
    - Bracers is rendered as it was transparent, we see arms inside them
    character_screen.png



    Thanks for any help .
     
  14. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Well I guess I'll have to get real support on this one. I'll contact unity team soon.
     
  15. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Ok got it fixed by creating a new project from scratch. All is working well now and I can use shaders given zith unity without any trouble. All mesh are rendered correctly. All Z-Fighting is gone.