Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Thibaut D · Nov 08, 2013 at 03:27 PM · shaderiosrandom

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.

renderrandombug.jpg (14.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Efficient Way to Draw Wireframes (OpenGL ES 2.0, iOS) 0 Answers

Point Light Behave different from editor and IOS device 0 Answers

GPU performance with tris and verts 1 Answer

iOS Shader - Emulator vs Device Inconsistency 0 Answers

How to move vertices up/down randomly in shader. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges