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.
- which shader fits to my targets best?
- 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
public class TestDrawer : MonoBehaviour
{
public MeshRenderer quadFrag;
public MeshRenderer quadSurf;
public Shader yellowShaderFrag;
public Shader yellowShaderSurf;
public Shader textureCopyShader;
private void Start()
{
RenderTexture rtSurf = new RenderTexture(512, 512, 1);
Graphics.Blit(CreateTexture(), rtSurf, new Material(yellowShaderSurf));
RenderTexture rtFrag = new RenderTexture(512, 512, 1);
Graphics.Blit(CreateTexture(), rtFrag, new Material(yellowShaderFrag));
quadFrag.material = new Material(textureCopyShader);
quadFrag.material.SetTexture("_MainTex", rtFrag);
quadSurf.material = new Material(textureCopyShader);
quadSurf.material.SetTexture("_MainTex", rtSurf);
}
public Texture2D CreateTexture()
{
Color[] color = new Color[512*512];
for (int i = 0; i < 512 * 512; i++)
{
color[i] = Color.red;;
}
Texture2D texture = new Texture2D(512,512);
texture.SetPixels(color);
texture.Apply();
return texture;
}
}
Shader "Unlit/YellowFrag"
{
SubShader
{
Tags{ "Queue"="Transparent" "RenderType" = "Opaque" "LightMode" = "ForwardBase" }
Pass
{
Lighting Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
};
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv0 = v.texcoord;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return float4(1,1,0,1);
}
ENDCG
}
}
}
Shader " Custom/YellowSurface"
{
SubShader
{
Tags { "Queue"="Geometry-9" "IgnoreProjector"="True" "RenderType"="Transparent" }
Lighting Off
LOD 200
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = float3(1,1,0);
o.Alpha = 1;
}
ENDCG
}
Fallback "Transparent/VertexLit"
}
Shader "Custom/TextureCopy"
{
SubShader
{
Tags { "Queue"="Geometry-9" "IgnoreProjector"="True" "RenderType"="Transparent" }
Lighting Off
CGPROGRAM
#pragma surface surf SimpleLambert//Lambert
half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
//half NdotL = dot (s.Normal, lightDir);
half4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
fixed4 _Color;
sampler2D _MainTex;
sampler2D _BlendTex;
float _BlendAlpha;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D( _MainTex, IN.uv_MainTex );
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/VertexLit"
}
Correct result:
Xiaomi result: