Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lubwn · May 23, 2017 at 06:24 AM · androidunity 5shaderbuildpink

Pink custom shader on Android build

I found and re-written some custom shader. Added Alpha with sliders etc. It works just nicely in Unity but after Android build it turn out to be all pink.

Here is the shader:

 Shader "Lubo/UnlitGradientOneOne"
 {
      Properties{
          _TopColor("Color1", Color) = (1,1,1,1)
          _TopColorAlpha ("Color1 Transparency", Range (0.01, 1)) = 0.5 
          _BottomColor("Color2", Color) = (1,1,1,1)
          _BottomColorAlpha ("Color2 Transparency", Range (0.01, 1)) = 0.5 
          _MainTex ("Main Texture", 2D) = "white" {}
          _Alpha ("Object Transparency", Range (0.01, 1)) = 0.5 
      }
          SubShader
      {
          Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
          Pass
          {
              ZWrite Off
              Blend One One
  
              CGPROGRAM
              #pragma vertex vert
              #pragma fragment frag
  
              #include "UnityCG.cginc"
  
              sampler2D _MainTex;
              float4 _MainTex_ST;
 
              float _Alpha;
              float _TopColorAlpha;
              float _BottomColorAlpha;
 
              fixed4 _TopColor;
              fixed4 _BottomColor;
              half _Value;
  
              struct v2f {
                  float4 position : SV_POSITION;
                  fixed4 color : COLOR;
                  float2 uv : TEXCOORD0;
                  fixed4 alpha : _Alpha;
              };
  
              v2f vert (appdata_full v)
              {
                  v2f o;
                  o.position = UnityObjectToClipPos (v.vertex);
                  o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
                  o.color = lerp (_TopColor * _TopColorAlpha, _BottomColor * _BottomColorAlpha, v.texcoord.y);
                  return o;
              }
  
              fixed4 frag(v2f i) : SV_Target
              {
                  float4 color;
                  color.rgb = i.color.rgb;
                  color.a = tex2D (_MainTex, i.uv).a * i.color.a * _Alpha;
                  return color;
              }
              ENDCG
          }
      }
  }

Tried various solutions:

  • setting skybox with directional light.

  • In Edit -> Project settings -> Graphics added my shader to array "Always included shaders".

  • Moved shader to location where all the other shaders are, thought It might help

  • Checked build logs and did not found anything suspicious.

I was thinking whether the shader is even compilable or compatible with android devices. Am I missing something? I am new to shaders and does not quite understand if I wrote something bad in there.

EDIT: After two days "awaiting moderation" I finally resolved this problem. And I can not even "reply" to my own thread and mark it as correct answer. Why?

I found out the program was unable to compile it, it simply worked in Unity, but refused to work on any android device.

Steps which led to successful repair:

  • Found out there is in Unity built-in shader compiler, which will tell you an error. Simply click on shader and there is a button "compile and show code", there is also a small arrow with various options, disable "skip unused shader_features", now compiler will tell you errors when compiling the code.

  • Based on this thread I found out about "UNITY_INITIALIZE_OUTPUT(v2f, o);" - I am not sure what it does, but it surely helped to resolve an error about not initialized variable "o"

  • Now the finally resolving step, removing _Alpha variable, since I already programmed the shader to have alpha for each of the colors, the mail alpha was unnecessary

Here is the final code of the shader which works:

 Shader "Lubo/UnlitGradientOneOne"
 {
      Properties{
          _TopColor("Color1", Color) = (1,1,1,1)
          _TopColorAlpha ("Color1 Transparency", Range (0.01, 1)) = 0.5 
          _BottomColor("Color2", Color) = (1,1,1,1)
          _BottomColorAlpha ("Color2 Transparency", Range (0.01, 1)) = 0.5 
          _MainTex ("Main Texture", 2D) = "white" {}
 
      }
          SubShader
      {
          Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
          Pass
          {
              ZWrite Off
              Blend One One
  
              CGPROGRAM
              #pragma vertex vert
              #pragma fragment frag
  
              #include "UnityCG.cginc"
  
              sampler2D _MainTex;
              float4 _MainTex_ST;
 
 
              float _TopColorAlpha;
              float _BottomColorAlpha;
 
              fixed4 _TopColor;
              fixed4 _BottomColor;
              half _Value;
  
              struct v2f {
                  float4 position : SV_POSITION;
                  fixed4 color : COLOR;
                  float2 uv : TEXCOORD0;
 
              };
  
              v2f vert (appdata_full v)
              {
                  v2f o;
                  UNITY_INITIALIZE_OUTPUT(v2f, o);
                  o.position = UnityObjectToClipPos (v.vertex);
                  o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
                  o.color = lerp (_TopColor * _TopColorAlpha, _BottomColor * _BottomColorAlpha, v.texcoord.y);
                  return o;
              }
  
              fixed4 frag(v2f i) : SV_Target
              {
                  float4 color;
                  color.rgb = i.color.rgb;
                  color.a = tex2D (_MainTex, i.uv).a * i.color.a;
                  return color;
              }
              ENDCG
          }
      }
  }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Android build error: unrecognized pixel format 264 0 Answers

Unity android Unable to forward network traffic to device 7 Answers

Failed to re-package resources after adding unity in-app purchase service 1 Answer

How to use #extension with GLSL shader 0 Answers

My simple shader not working correctly on Android 0 Answers


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