- Home /
Random shader bug (IOS/IOS Simulator)
Hi all!
In our last project on mobile platform we have developed a bunch of shaders for our needs. In short they all have the same struct and use a Vertex and Fragment programs and use two lights. All of them work pretty well on the editor but some time on IOS target (device and simulator) the result is totally wrong. Here is a sample (Left is the correct result. Middle and right are wrong.)
![alt text][1] [1]: /storage/temp/17598-renderrandombug.jpg
We made a simple test scene to “spawn/destroy/respawn/…” the object. 80% of the time the result is correct. And at other time the result is wrong. We don’t really manage to understand what happened, so if somebody had an idea, that will be helpful.
Here is one of the shaders we use (The others are globally the same, some are reflective, some are not, some have normal maps…)
Shader "Mobile/Reflect/NoBump/Diffuse"
{
Properties
{
_Color("Main Color", Color) = (1,1,1,1)
_SpecColor("Specular Color", Color) = (1, 1, 1, 1)
_Shininess("Shininess", Float) = 1
_Glossiness("Glossiness", Float) = 1
_MainTex("Color Map", 2D) = "white" {}
_EnvMap("Reflection Cubemap", Cube) = "_Skybox" {TexGen CubeReflect}
_ReflectPow("ReflectPow", Float) = 0.5
_Tile("Main texture Tile", Float) = 1
}
SubShader
{
Tags {"RenderType"="Opaque"}
LOD 300
Pass
{
// base
Name "BASE"
Cull off
Lighting On
CGPROGRAM
#pragma target 3.0
#pragma exclude_renderers d3d11 xbox360 ps3 flash
#pragma glsl_no_auto_normalization
#pragma glsl
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
// float type
#define FLOAT half
#define FLOAT_VEC(num) half##num
#define FLOAT_MAT(n1, n2) half##n1##x##n2
#define FLOAT2 FLOAT_VEC(2)
#define FLOAT3 FLOAT_VEC(3)
#define FLOAT4 FLOAT_VEC(4)
#define FLOAT2X2 FLOAT_MAT(2, 2)
#define FLOAT2X3 FLOAT_MAT(2, 3)
#define FLOAT2X4 FLOAT_MAT(2, 4)
#define FLOAT3X2 FLOAT_MAT(3, 2)
#define FLOAT3X3 FLOAT_MAT(3, 3)
#define FLOAT3X4 FLOAT_MAT(3, 4)
#define FLOAT4X2 FLOAT_MAT(4, 2)
#define FLOAT4X3 FLOAT_MAT(4, 3)
#define FLOAT4X4 FLOAT_MAT(4, 4)
// lights
#define AMBIENT_LIGHT UNITY_LIGHTMODEL_AMBIENT
#define LIGHT0_DIR unity_LightPosition[0]
#define LIGHT0_COL unity_LightColor[0]
#define LIGHT1_DIR unity_LightPosition[1]
#define LIGHT1_COL unity_LightColor[1]
// attenuation
#define ATTENUATION_FACTOR 2.0f
// uniform
FLOAT4 _Color;
FLOAT4 _SpecColor;
FLOAT _Shininess;
FLOAT _Glossiness;
sampler2D _MainTex;
samplerCUBE _EnvMap;
FLOAT _ReflectPow;
FLOAT _Tile;
// input
struct VertexInput
{
FLOAT4 vertex : POSITION;
FLOAT3 normal : NORMAL;
FLOAT2 texCoord : TEXCOORD0;
};
// output
struct VertexOutput
{
FLOAT4 screenPosition : POSITION;
FLOAT2 factors : TEXCOORD0;
FLOAT3 normal : TEXCOORD1;
FLOAT2 texCoord : TEXCOORD2;
FLOAT3 texCoordReflexion : TEXCOORD3;
FLOAT3 halfDir0 : TEXCOORD4;
FLOAT3 halfDir1 : TEXCOORD5;
};
// vertex shader
VertexOutput vert(VertexInput input)
{
// declare output
VertexOutput output;
// position
output.screenPosition = mul(UNITY_MATRIX_MVP, input.vertex);
// normal
output.normal = normalize(mul((FLOAT3X3)UNITY_MATRIX_MV, input.normal));
output.factors.x = clamp(dot(output.normal, LIGHT0_DIR.xyz), 0.0f, 1.0f);
output.factors.y = clamp(dot(output.normal, LIGHT1_DIR.xyz), 0.0f, 1.0f);
// tex coord
output.texCoord = _Tile * input.texCoord;
// reflexion
FLOAT3 viewDir = _WorldSpaceCameraPos.xyz - mul(_Object2World, input.vertex).xyz;
FLOAT3 worldN = mul((FLOAT3X3)_Object2World, input.normal);
output.texCoordReflexion = reflect(-viewDir, worldN);
// half vector for blinn phong
FLOAT3 position = normalize(mul(UNITY_MATRIX_MV, input.vertex).xyz);
output.halfDir0 = LIGHT0_DIR.xyz - position;
output.halfDir1 = LIGHT1_DIR.xyz - position;
// result
return output;
}
// fragment shader
FLOAT4 frag(VertexOutput input) : COLOR
{
// albedo
FLOAT3 texCol = tex2D(_MainTex, input.texCoord).rgb;
FLOAT3 albedo = texCol.rgb * _Color.rgb;
// diffuse
FLOAT3 diffuse = input.factors.x * LIGHT0_COL.rgb + input.factors.y * LIGHT1_COL.rgb;
// specular
FLOAT3 norm = normalize(input.normal);
FLOAT specFactor0 = pow(max(0.0f, dot(norm, normalize(input.halfDir0))), _Shininess);
FLOAT specFactor1 = pow(max(0.0f, dot(norm, normalize(input.halfDir1))), _Shininess);
FLOAT3 specColor = (specFactor0 + specFactor1) * _SpecColor.rgb * _Glossiness;
// reflexion
FLOAT3 texCubemapColor = texCUBE(_EnvMap, input.texCoordReflexion).rgb;
FLOAT3 emColor = texCubemapColor * _ReflectPow * _Glossiness;
// result
return FLOAT4(ATTENUATION_FACTOR * (albedo * (AMBIENT_LIGHT.rgb + diffuse) + specColor + emColor), 1.0f);
}
// end
ENDCG
}
}
Thanks in advance.