Search Unity

Problems with transparent Vertex Shader

Discussion in 'Shaders' started by Skyfire716, Apr 6, 2017.

  1. Skyfire716

    Skyfire716

    Joined:
    Apr 5, 2017
    Posts:
    4
    Hi guys,

    i have a problem with a transparent vertex shader written in CG.
    On my computer and on a Samsung Galaxy Note 4 it works perfect but on my Xperia Z and the Xperia Z2 tablet it flickers.

    As it should look like:
    Working.PNG

    As it should not look like:
    Not Working.png

    Here is my Shader Code:

    Shader "Hologarfic" {
    Properties {
    _VertexColor ("Vertexcolor", Color) = (1,1,1,1)
    _FragmentColor ("Fragmentcolor", Color) = (1,1,1,1)
    _WireWidth ("Wire Width", float) = 0
    _Alpha ("Alpha", float) = 0
    }
    SubShader {
    Tags {"Queue"="Transparent +1" "RenderType"="Transparent"}
    Blend SrcAlpha OneMinusSrcAlpha
    Cull Off
    ZWrite Off
    LOD 200
    Pass{
    CGPROGRAM


    //pragma
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"
    //user defined variables
    uniform float4 _VertexColor;
    uniform float4 _FragmentColor;
    uniform float _WireWidth;
    uniform float _Alpha;
    float3 trianglecolor;
    //base input structs
    struct vertexInput{
    float4 vertex : POSITION;
    float4 color : COLOR;
    };
    struct vertexOutput{
    float4 pos : SV_POSITION;
    float4 color : COLOR;
    };
    //vertex funkction
    vertexOutput vert(vertexInput v){
    vertexOutput o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    o.color = v.color;
    return o;
    }
    //fragment function
    float4 frag(vertexOutput i) : COLOR{
    float3 d = fwidth(i.color);
    float3 tdist = smoothstep(float4(0,0,0,0), d*_WireWidth, i.color);
    _FragmentColor = lerp(_VertexColor, _FragmentColor, min(min(tdist.x, tdist.y), tdist.z));
    _FragmentColor = _FragmentColor * _Alpha;
    return _FragmentColor;
    }
    ENDCG
    }
    }
    }


    Have anyone an idea how i could fix the problem?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Derivatives aren't supported by all mobile hardware, or aren't supported well. Those devices should be capable of it as it's a requirement of OpenGL ES 3.0, but as you're not specifying a shader level target it's possible the devices are running the shader as es2.0 or es1.0(!).

    Try adding:

    #pragma target es3.0
     
  3. Skyfire716

    Skyfire716

    Joined:
    Apr 5, 2017
    Posts:
    4
    Thank you for your reply.

    I added #pragma target es3.0 under the #pragma fragment frag but it still doesn't work. You say that derivatives aren't supported well. Is there a way to write something like fwidth() what doesn't use derivatives? Or would it help to wirte the shader in another language?
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    No, and no I'm afraid.

    For mobile hardware I never use derivatives. Too unreliable. So it's back to the drawing board I think. Just replace it with a texture lookup. When available derivatives will still be used for some proper texture filtering, but on other hardware the fallback is a bit friendlier.
     
  5. Skyfire716

    Skyfire716

    Joined:
    Apr 5, 2017
    Posts:
    4

    You can imagine that i don't know what i'm exactly do in shaders (I know that they show the grafik and compute textures and all this but i don't know where i get things from and how i need to implement things that something else happens). I want that the underground have this wireframe. Until now i used the vertex colors red, green and blue and builded this shader with this as tamplate https://codea.io/talk/discussion/3170/render-a-mesh-as-wireframe.

    My underground mesh is varying.

    I would appreciate if you explain to me how I implement it with the texture lookup.
     
  6. Skyfire716

    Skyfire716

    Joined:
    Apr 5, 2017
    Posts:
    4
    Thank you for your help i found a solution. I deleted the line where i declarate float d = fwidht(i.color); and changed the wirewidth. Now it works!