I have a shader that works fine in the unity editor on my machine, but it doesn’t work correctly when I run it on Android. It’s strange that it doesn’t give me a broken shader, but it also doesn’t do what it’s supposed to do, as you will see.
It’s a shader that takes in 6 planes, two for each axis, and then uses them to cross section an object. I started with this, and then modified it: Unity Asset Store - The Best Assets for Game Making
Here it is working on my machine:
The cube with the sliders on it on the bottom left is indicating how to cut the object. The shader is also on the sliders on the cross section UI cube, trimming away the exess, which you can see it not doing below.
Here it is not working correctly on my phone:
Shader "CrossSection/ThreeAAPlanesBSP" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_CrossColor("Cross Section Color", Color) = (1,1,1,1)
_Transparent("Transparent", Range(0,1)) = 1 //NB
_MainTex("Albedo (RGB)", 2D) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_Plane1Position("Plane1Position",Vector) = (0,0,0,1)
_Plane2Position("Plane2Position",Vector) = (0,0,0,1)
_Plane3Position("Plane3Position",Vector) = (0,0,0,1)
_Plane4Position("Plane4Position",Vector) = (0,0,0,1)
_Plane5Position("Plane5Position",Vector) = (0,0,0,1)
_Plane6Position("Plane6Position",Vector) = (0,0,0,1)
_EmissionColor("Emission Color", Color) = (0,0,0,0)
//cross sectioning is off
_CS_switch("_CS_switch", Float) = 0
}
SubShader{
Tags{ "Queue" = "Transparent" "RenderType"="Transparent" }
//LOD 200
Cull off
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float3 worldPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
fixed4 _CrossColor;
fixed3 _Plane1Position;
fixed3 _Plane2Position;
fixed3 _Plane3Position;
fixed3 _Plane4Position;
fixed3 _Plane5Position;
fixed3 _Plane6Position;
fixed4 _EmissionColor;
fixed _CS_switch;
bool checkVisability(fixed3 worldPos)
{
if (_CS_switch == 0)
return 0;
float dotProd1 = dot(worldPos - _Plane1Position, fixed3(1, 0, 0));
float dotProd2 = dot(worldPos - _Plane2Position, fixed3(0, 1, 0));
float dotProd3 = dot(worldPos - _Plane3Position, fixed3(0, 0, 1));
float dotProd4 = dot(worldPos - _Plane4Position, fixed3(-1, 0, 0));
float dotProd5 = dot(worldPos - _Plane5Position, fixed3(0, -1, 0));
float dotProd6 = dot(worldPos - _Plane6Position, fixed3(0, 0, -1));
return !(dotProd1 > 0 && dotProd2 > 0 && dotProd3 > 0 && dotProd4 > 0 && dotProd5 > 0 && dotProd6 > 0);
}
void surf(Input IN, inout SurfaceOutputStandard o) {
if (checkVisability(IN.worldPos))discard;
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Emission = _EmissionColor;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
Cull Front
CGPROGRAM
#pragma surface surf NoLighting noambient alpha
struct Input {
half2 uv_MainTex;
float3 worldPos;
};
sampler2D _MainTex;
fixed4 _Color;
fixed4 _CrossColor;
fixed3 _Plane1Position;
fixed3 _Plane2Position;
fixed3 _Plane3Position;
fixed3 _Plane4Position;
fixed3 _Plane5Position;
fixed3 _Plane6Position;
half _Transparent;
fixed _CS_switch;
bool checkVisability(fixed3 worldPos)
{
if (_CS_switch == 0)
return 1;
float dotProd1 = dot(worldPos - _Plane1Position, fixed3(-1, 0, 0));
float dotProd2 = dot(worldPos - _Plane2Position, fixed3(0, -1, 0));
float dotProd3 = dot(worldPos - _Plane3Position, fixed3(0, 0, -1));
float dotProd4 = dot(worldPos - _Plane4Position, fixed3(1, 0, 0));
float dotProd5 = dot(worldPos - _Plane5Position, fixed3(0, 1, 0));
float dotProd6 = dot(worldPos - _Plane6Position, fixed3(0, 0, 1));
return !(dotProd1 > 0 && dotProd2 > 0 && dotProd3 > 0 && dotProd4 > 0 && dotProd5 > 0 && dotProd6 > 0);
}
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
{
fixed4 c;
c.rgb = s.Albedo;
c.a = _Transparent;
return c;
}
void surf(Input IN, inout SurfaceOutput o)
{
if (checkVisability(IN.worldPos))discard;
o.Albedo = _CrossColor;
}
ENDCG
}
//FallBack "Diffuse"
}
I also wanted to point out that I added it to the list of always included shaders.
Any and all input would be appreciated! My shader knowledge is weak and after trying to fix this for a few days, I thought I’d see if anyone would be willing to give me a hand on this.
Thanks!