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. Dismiss Notice

Problems with Depth Mask Shader on the Iphone

Discussion in 'iOS and tvOS' started by macdude2, Sep 17, 2011.

  1. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    Ok, I have been working incredibly hard on getting a script to work. This script however uses a depth mask shader. This shader works fine on the computer, but when I export it to the iPhone, the iPhone crashes when trying to render the depth mask. I have looked on the profiler and there is a slight increase in work load (.2ms/frame) but it is not noticeable. What could be causing the crash? I am almost positive I have worked with depth mask shaders before and made them run on the iPhone. The shader I am using for the depth mask is below. In Xcode, I get a a Exec_bad_access error and I also get another weird error:

    Unhandled Exception: System.NullReferenceException:
    Current language: auto; currently c++

    The part of the code to handle the depth mask does use C#, but I have used c# elsewhere in my code and it has not made a difference. Thanks for any help.


    Code (csharp):
    1. Shader "DepthMask"
    2. {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.     }
    6.  
    7.     SubShader
    8.     {
    9.         Tags {
    10.             "Queue" = "Geometry-101"
    11.         }
    12.  
    13.         // Draw into the depth buffer in the usual way.  This is probably the default,
    14.         // but it doesn't hurt to be explicit.
    15.  
    16.         ZTest LEqual
    17.         ZWrite On
    18.  
    19.         // Don't draw the RGB colour channels, just the alpha channel.  This means
    20.         // that nothing visible is drawn.  Ideally, I would have liked to set ColorMask
    21.         // to draw nothing at all, but it doesn't seem to be possible to say anything
    22.         // like ColorMask None.
    23.  
    24.         ColorMask A
    25.  
    26.         // Do nothing specific in the pass:
    27.  
    28.         Pass {
    29.             // Turn off lighting, because it's expensive and the thing is supposed to be
    30.             // invisible anyway.
    31.  
    32.             Lighting Off
    33.  
    34.             Color [_Color]
    35.         }
    36.     }
    37.  
    38.     FallBack "Diffuse", 1
    39. }
    40.  
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Prove to yourself that your problem has nothing to do with the shader by putting it on something in an otherwise empty scene with no scripts. A shader's not going to cause a null reference exception, and I've verified for a couple people around here that that shader, and others in the same "family", work just fine. The problems are always elsewhere; it's kind of weird stuff, and complications love to arise with it.
     
  3. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    Yeah, just tried the shader with another scene and it works. So I really have no idea what the problem is with my script. Is there there anyway I can debug the Xcode debug log?thanks.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Actually a shader repeatedly crashed unity for me when I used (I don't quite remember what exactly it was) but it was something to do with having a material block with vert stuff without using vert stuff. It's slightly buggy in the rarest cases with shaders that "have stuff in it that shouldn't be in it".

    I wish I bothered to submit a bug report at the time since it was reproducable.
     
  5. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    GRrrrr, I really wish there was a away to solve this. All I am doing is switching between a render texture and a depth mask if the player is within a certain distance to the object. O the computer this witch performs fine, but on the iPad or iPhone, it crashes upon trying to render the depth mask. Is there no way for me to debug the Xcode error??? How would anyone else go around debugging the problem that I am having? It would be quite tedious to go through my new code line/scenario by line to see where the iPhone is getting stuck.
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    do you switch or do you try to render into a render texture? cause on mobiles RTs don't support the depth mode (should not crash normally but lead to an error in the console already in the editor under emulation though)
     
  7. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    I am just switching materials from one with the render texture to one that has the depth mask. I get absolutely no errors in unity while exporting it or while running it. I have absolutely no idea what could be going wrong.
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Ensure any blocks like color or vertex stuff that shouldn't be there, isn't there, ie strip it down.
     
  9. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    What do you mean 'blocks like colors or vertex stuff'? I have some particles around the part that I am rendering the texture to, could that be the problem? The surface is just a couple of oval shaped planes sitting close together one directly in front of the other. It is a pretty basic system.
     
  10. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    OK, so I made just a little progress. I tried my code in another scene. Now the game doesn't crash, but the depth mask isn't rendered either. Instead I get a more detailed null reference exception in the editor:

    NullReferenceException
    at UnityEngine.Material..ctor (UnityEngine.Shader shader) [0x00000] in <filename unknown>:0
    at Portal.GetPortalMaterials () [0x00000] in <filename unknown>:0
    at Portal.LateUpdate () [0x00000] in <filename unknown>:0

    (Filename: Line: -1)

    The code for the Portal.GetPortalMaterials is below.

    Material[] GetPortalMaterials() {
    Material[] newMaterials = new Material[2];

    newMaterials[0] = new Material(Shader.Find("Alpha Blended"));
    newMaterials[1] = new Material(Shader.Find("DepthMask"));

    newMaterials[0].SetColor("_TintColor", new Color(0,0,0,0));

    return newMaterials;
    }

    In the late update script, I call this function when the player gets within a certain distance of the portal. What is the problem with this code? Why is it causing a null reference? It works on the computer fine.
     
  11. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    Finally figured it out! All I had to do was cache the materials I was changing and it suddenly worked. How strange.
     
  12. Elie-Charest

    Elie-Charest

    Joined:
    May 6, 2011
    Posts:
    14
    This may be a stupid question, but how do you cache materials?
     
  13. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Same here, I have the same problem, and I wonder what you actually mean by cache as well...

    Bye,

    Jean
     
  14. Jim_Zee_King

    Jim_Zee_King

    Joined:
    Mar 10, 2014
    Posts:
    10
    Sorry to bring this thread back from the dead, but i have the same problem and i would like to know what you meant by caching the mats...

    Thanks a lot!
     
    Last edited: Mar 10, 2014