Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

My Custom Shader is working in one project but in a existing project,it displays black texture.

Discussion in 'Shaders' started by _SIDZ_, Aug 4, 2014.

  1. _SIDZ_

    _SIDZ_

    Joined:
    Aug 4, 2014
    Posts:
    7
    I am very very new to shaders side of development apart from some basic theory knowledge. So recently in one of my project I wanted to have a shader which displays a half or only a certain amount of texture vertically or horizontally on a plane.With a slider I could control the amount of cut off value beyond which the texture won't display.With a little bit of googling and then coding,I did it and got the output I desired.But when I tried using the same in a existing project ,it keep displaying a black texture to no texture at all.Even though the material icon shows its changing.
    Please note this existing projects has a lot of plugins,I feel its because of one of them its not working.
    I have tried even taking out a prefab and then too same result.But apart from this project if i try in a fresh project it works.


    Code (CSharp):
    1. Shader "Custom/MYshader" {
    2. Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.           _TextureX ("TextureX" ,Range(0.0,1.0)) = 1.0
    5.           _TextureY ("TextureY", Range(0.0,1.0)) = 1.0
    6.        
    7.     }
    8.     SubShader {
    9.      Cull Off // since the front is partially transparent,
    10.             // we shouldn't cull the back
    11.          Blend SrcAlpha OneMinusSrcAlpha
    12.        AlphaTest Greater 0.5// specify alpha test:
    13.         Pass {
    14.             CGPROGRAM
    15.             #pragma vertex vert_img
    16.             #pragma fragment frag
    17.  
    18.             #include "UnityCG.cginc"
    19.            
    20.             uniform sampler2D _MainTex;
    21.             float _TextureX;
    22.             float _TextureY;
    23.  
    24.             float4 frag(v2f_img i) : COLOR {
    25.          float4 FillTex=tex2D(_MainTex, i.uv);
    26.        
    27.          if(i.uv.y<_TextureY || i.uv.x<_TextureX )
    28.                {
    29.              
    30.               FillTex.x=1.0;
    31.               FillTex.y=0;
    32.               FillTex.z=0;
    33.               return FillTex;
    34.          
    35.                }
    36.                 else
    37.          {    
    38.  
    39.            return (1,1,1,0);
    40.          }
    41.             }
    42.             ENDCG
    43.         }
    44.     }
    45. }
    I have attached screenshot of the working output.Its basically a texture which can be cutoff horizontally or vertically using slider.
    Through little debugging I found out that this line " if(i.uv.y<_TextureY || i.uv.x<_TextureX )" in my code doesn't work and doesn't get evaluated in the existing project.
    I have a feeling its because of some plugins which are already present in the project maybe or I might be wrong,Any help would be much
    appreciated.
     

    Attached Files:

  2. showoff

    showoff

    Joined:
    Apr 28, 2009
    Posts:
    273
    It could be that the shader is not receiving any light in the new project. I had this same issue with a shader and it was because it wasn't getting any light so I multiply it's color with a range slider which I put in emissive. And u can make the shader unlit if u want and u can just control it's brightness with the range slider
     
  3. _SIDZ_

    _SIDZ_

    Joined:
    Aug 4, 2014
    Posts:
    7
    Hi,Thanks for the reply,
    I am not using any any light since I am just setting a color(or a tex) to a pixel in the fragment shader. Like i said before ,for some reason that line I mentioned above "" if(i.uv.y<_TextureY || i.uv.x<_TextureX )"" isn't getting evaluated.So if i take it out,I can get red or whatever color i want.But that one line is becoming an issue.
    My bad for being so vague because I am completely new to the shader so not sure how to present my question with enough details to be answered.:(
     
  4. showoff

    showoff

    Joined:
    Apr 28, 2009
    Posts:
    273
    I just tested your shader in my project and it is not black. maybe it is the other plugins or tools you have in your scene which could be causing the problem.
     
  5. _SIDZ_

    _SIDZ_

    Joined:
    Aug 4, 2014
    Posts:
    7
    Yup..There are too many plugins in my project ,the thing is I am working only on one section,so I am not aware of all other components in it,hence I just wanted to know if you had any idea the kinda of plugins which might cause this problem?Since its a confidential stuff ,I just can reveal much more details ,sorry again for being so vague:(.So if you could give a pointer (or a direction to look) about the kind of plugins which might cause this problem,it would be nice.I have noticed i have game draw plugin, Fresnel something plugin .Thank you again for your reply..:)
     
  6. Chris Monson

    Chris Monson

    Joined:
    Dec 18, 2013
    Posts:
    1
    I had a very similar problem today. Like you, my shader was breaking around an if statement and displaying solid blocks. It worked perfectly in one project but not another.

    Turns out, the project where it appeared to work had DX11 enabled in player settings and the other project didn't.

    The shader inspector said SM2.0 in both cases but DX11 treated it differently.

    To get the shader to work in a non DX11 environment I added "#pragma target 3.0". Curious if this is what happened to you.
     
  7. _SIDZ_

    _SIDZ_

    Joined:
    Aug 4, 2014
    Posts:
    7
    Hi thanks for the reply,for me both the project had dx11 enabled at the setting.(I googled later a little about SM2.0 and stuff as I had no clue what it was). I added the line still ,I didn't get my shader to work.Then I tried all numbers still 5 too.:(.still no change.But I can see the change in material in inspector window ,but not in the scene or game window.


    (Just when I felt I conquered my world my doing something in shaders by myself....this happens!)
     
  8. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    could you see if this works for you?
    all i did was take away the conditional logic out in favor of some math, which is usually faster anyway

    i also commented out the blending, it's up to you if you use it or not, it doesn't make much sense to be using alpha blend WITH alpha test, i'm not really sure what you are trying to get with that

    Code (CSharp):
    1. Shader "Custom/Cutit" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _TextureX ("TextureX" ,Range(0.0,1.0)) = 1.0
    5.         _TextureY ("TextureY", Range(0.0,1.0)) = 1.0
    6.     }
    7.     SubShader {
    8.         Cull Off // since the front is partially transparent,
    9.             // we shouldn't cull the back
    10. //        Blend SrcAlpha OneMinusSrcAlpha
    11.         AlphaTest Greater 0.5// specify alpha test:
    12.         Pass {
    13.             CGPROGRAM
    14.             #pragma vertex vert_img
    15.             #pragma fragment frag
    16.             #include "UnityCG.cginc"
    17.          
    18.             uniform sampler2D _MainTex;
    19.             float _TextureX;
    20.             float _TextureY;
    21.             float4 frag(v2f_img i) : COLOR {
    22.              float4 FillTex=tex2D(_MainTex, i.uv);
    23.              float cond = -step(0, i.uv.y - _TextureY);
    24.              cond = cond + step(0, -i.uv.x + _TextureX);
    25.             clip(cond);
    26.             return float4(1,0,0,FillTex.a);  
    27.             }
    28.             ENDCG
    29.         }
    30.     }
    31. }
     
    _SIDZ_ likes this.
  9. _SIDZ_

    _SIDZ_

    Joined:
    Aug 4, 2014
    Posts:
    7
    Hi Thank you,It works now :).But I had to un-comment this line in your code(line 10),else I was getting just a a red fill up.I am trying to understand your code now(And will bug you with doubts if i don't understand...;)).iIam just curious why it didn't work before with that conditional statement. Anyways thanks again.:)
     
  10. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    if i'm not mistaken conditional logic is slow because the shader actually runs every condition and opts the one you want, so maybe there was something in your project, like a project setting or something that was forcing something in shaders to execute in a way that the branching of the shader wasn't working properly, i know i'm being vague here, it's just too difficult to know exactly what without having the project itself, it was just some presumption i had from experience, in fact i bet the problem of the original shader is a cool and weird one, if you have the time you should definitely try to find to culprit

    feel free to ask anything you want ;)