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

Resolved Troubles with shaders on Xiaomi

Discussion in 'Shaders' started by alternativevisual, Aug 17, 2020.

  1. alternativevisual

    alternativevisual

    Joined:
    Apr 16, 2015
    Posts:
    75
    Hello.

    I had to create shader that draws line. So, all I need is shader that calculate color for each pixel my coordinates. And make it as fast as possible without any lighting and stuff.

    I created vert/frag shader for this. But later I found out that my shader is not working on Xiaomi phones (tested on Xiaomi Redmi Note 4). After some tests I managed to create simple example shader that shows differences between Xiaomi and other phones.

    And I have two questions.
    1) which shader fits to my targets best?
    2) why my vert/frag shader is not working on Xiaomi but working in other phones?

    In test I have TestDrawer script that renderer two textures (yellow color) and with different shaders. And copy these textures to different quads. If we run this on Xiaomi phone one of quads will be not yellow. Unlike other phones.

    Example project: https://drive.google.com/file/d/159Kf4zVnP3pOVi6LAws7Cc3pvKvoZZHx/view?usp=sharing

    Code (CSharp):
    1.  
    2. public class TestDrawer : MonoBehaviour
    3. {
    4.     public MeshRenderer quadFrag;
    5.     public MeshRenderer quadSurf;
    6.  
    7.     public Shader yellowShaderFrag;
    8.     public Shader yellowShaderSurf;
    9.     public Shader textureCopyShader;
    10.  
    11.     private void Start()
    12.     {
    13.         RenderTexture rtSurf = new RenderTexture(512, 512, 1);
    14.         Graphics.Blit(CreateTexture(), rtSurf, new Material(yellowShaderSurf));
    15.  
    16.         RenderTexture rtFrag = new RenderTexture(512, 512, 1);
    17.         Graphics.Blit(CreateTexture(), rtFrag, new Material(yellowShaderFrag));
    18.  
    19.         quadFrag.material = new Material(textureCopyShader);
    20.         quadFrag.material.SetTexture("_MainTex", rtFrag);
    21.  
    22.         quadSurf.material = new Material(textureCopyShader);
    23.         quadSurf.material.SetTexture("_MainTex", rtSurf);
    24.     }
    25.  
    26.     public Texture2D CreateTexture()
    27.     {
    28.         Color[] color = new Color[512*512];
    29.         for (int i = 0; i < 512 * 512; i++)
    30.         {
    31.             color[i] = Color.red;;
    32.         }
    33.  
    34.         Texture2D texture = new Texture2D(512,512);
    35.         texture.SetPixels(color);
    36.         texture.Apply();
    37.  
    38.         return texture;
    39.     }
    40. }
    41.  
    Code (CSharp):
    1.  
    2. Shader "Unlit/YellowFrag"
    3. {
    4.     SubShader
    5.     {
    6.         Tags{ "Queue"="Transparent" "RenderType" = "Opaque" "LightMode" = "ForwardBase" }
    7.  
    8.         Pass
    9.         {
    10.             Lighting Off
    11.  
    12.             CGPROGRAM
    13.  
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.  
    17.             #include "UnityCG.cginc"
    18.             #include "AutoLight.cginc"
    19.  
    20.             struct appdata {
    21.                 float4 vertex : POSITION;
    22.                 float2 texcoord : TEXCOORD0;
    23.                 float2 texcoord1 : TEXCOORD1;
    24.             };
    25.  
    26.             struct v2f {
    27.                 float4 pos : SV_POSITION;
    28.                 float2 uv0 : TEXCOORD0;
    29.             };
    30.  
    31.             v2f vert(appdata v) {
    32.                 v2f o;
    33.                 o.pos = UnityObjectToClipPos(v.vertex);
    34.                 o.uv0 = v.texcoord;
    35.                 return o;
    36.             }
    37.  
    38.             fixed4 frag (v2f i) : SV_Target
    39.             {
    40.                 return float4(1,1,0,1);
    41.             }
    42.             ENDCG
    43.         }
    44.     }
    45. }
    46.  
    Code (CSharp):
    1.  
    2. Shader " Custom/YellowSurface"
    3. {
    4.      SubShader
    5.      {
    6.         Tags { "Queue"="Geometry-9" "IgnoreProjector"="True" "RenderType"="Transparent" }
    7.  
    8.         Lighting Off
    9.  
    10.         LOD 200
    11.  
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.  
    14.         CGPROGRAM
    15.  
    16.         #pragma surface surf Lambert
    17.  
    18.         struct Input {
    19.           float2 uv_MainTex;
    20.         };
    21.  
    22.         void surf (Input IN, inout SurfaceOutput o) {
    23.            o.Albedo = float3(1,1,0);
    24.           o.Alpha = 1;
    25.         }
    26.         ENDCG
    27.      }
    28.      Fallback "Transparent/VertexLit"
    29. }
    30.  
    Code (CSharp):
    1.  
    2. Shader "Custom/TextureCopy"
    3. {
    4.      SubShader
    5.      {
    6.         Tags { "Queue"="Geometry-9" "IgnoreProjector"="True" "RenderType"="Transparent" }
    7.  
    8.         Lighting Off
    9.  
    10.         CGPROGRAM
    11.  
    12.         #pragma surface surf SimpleLambert//Lambert
    13.  
    14.         half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
    15.               //half NdotL = dot (s.Normal, lightDir);
    16.               half4 c;
    17.               c.rgb = s.Albedo;
    18.               c.a = s.Alpha;
    19.               return c;
    20.           }
    21.  
    22.         fixed4 _Color;
    23.         sampler2D _MainTex;
    24.         sampler2D _BlendTex;
    25.         float _BlendAlpha;
    26.  
    27.         struct Input {
    28.           float2 uv_MainTex;
    29.         };
    30.  
    31.  
    32.         void surf (Input IN, inout SurfaceOutput o) {
    33.           fixed4 c = tex2D( _MainTex, IN.uv_MainTex );
    34.           o.Albedo = c.rgb;
    35.           o.Alpha = c.a;
    36.         }
    37.         ENDCG
    38.      }
    39.  
    40.      Fallback "Transparent/VertexLit"
    41. }
    42.  
    Correct result:


    Xiaomi result:
     
    Last edited: Sep 1, 2020
  2. alternativevisual

    alternativevisual

    Joined:
    Apr 16, 2015
    Posts:
    75
    Solved: depth argument in RenderTexture constructor need a 0.