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
1
Question by 01000101 · Sep 19, 2016 at 09:18 PM · androidshaderpost processing

Post Processing Shader does not work on android device

I have a post-processing shader that works perfectly fine in the editor but when I build to android and try running it on my android phone, the shader doesn't work. Why is this? I am new to writing shaders but it's a really simple shader that doesn't utilize any advanced features (to my knowledge) so why can't it be run on my android device?

Shader:

 Shader "Post-Processing/BarrelDistortionAndBlurredEdges"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
         _Distortion("Distortion", float) = 0.0
         _Inverse("Inverse", float) = 1.0
         _ScreenWidth("ScreenWidth", float) = 0.0
         _ScreenHeight("ScreenHeihgt", float) = 0.0
         _BlurStart("BlurStart", float) = 0.5
         _BlurEnd("BlurEnd", float) = 0.9
         _BlurColor("BlurColor", Color) = (1, 1, 1, 1)
 
     }
     SubShader
     {
         // No culling or depth
         Cull Off ZWrite Off ZTest Always
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
             };
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.uv;
                 return o;
             }
             
             sampler2D _MainTex;
             float _Distortion;
             float _Inverse;
             float _BlurStart;
             float _BlurEnd;
             fixed4 _BlurColor;
             float _ScreenWidth;
             float _ScreenHeight;
 
             float2 cartesianToPolar(float2 cartesian)
             {
                 return float2(sqrt(pow(cartesian.y, 2.0f) + pow(cartesian.x, 2.0f)), atan2(cartesian.y, cartesian.x));
             }
 
             float2 polarToCartesian(float2 polar)
             {
                 return float2(polar.x * cos(polar.y), polar.x * sin(polar.y));
             }
 
             float difference(float r)
             {
                 return float((-1.0f + sqrt(-4.0f * (pow(r, 2.0f) - r) + 1.0f)) / 2.0f);
             }
 
             float4 frag (v2f i) : SV_Target
             {
                 float2 src = i.uv;
 
                 if(_Distortion > 0.0f) {
                     float2 res = i.uv;
                     float2 resNormalized = float2(res.x * 2.0f - 1.0f, res.y * 2.0f - 1.0);
                     float2 resPolar = cartesianToPolar(resNormalized);
 
                     float2 srcPolar = float2(resPolar.x - _Inverse * cos(radians(45.0f)) * difference(resPolar.x) * _Distortion, resPolar.y);
                     float2 srcNormalized = polarToCartesian(srcPolar);
                     src = (srcNormalized) / 2.0f + 0.5f;
                 }
 
                 float blur = 0.0f;
 
                 if(_BlurEnd > _BlurStart) {
                     float d = length(i.uv * 2.0f - 1.0f) - _BlurStart;
                     float blurZone = _BlurEnd - _BlurStart;
                     blur = d / blurZone;
                 }
 
                 if(blur < 0.0f) {
                     blur = 0.0f;
                 } else if(blur > 1.0f) {
                     blur = 1.0f;
                 }
 
                 return lerp(tex2D(_MainTex, src), _BlurColor, blur);
             }
             ENDCG
         }
     }
 }

Script:

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class BarrelDistortionAndBlurredEdges : MonoBehaviour {
 
     public float distortion = 0.0f;
     public bool inverse = false;
     public float blurStart = 0.5f;
     public float blurEnd = 0.9f;
     public Color blurColor = Color.white;
     private Material material;
 
     void Awake() {
         material = new Material (Shader.Find("Post-Processing/BarrelDistortionAndBlurredEdges"));
     }
 
     void OnRenderImage(RenderTexture src, RenderTexture dest) {
         if (material != null) {
             material.SetFloat ("_Distortion", distortion);
             if(inverse) { 
                 material.SetFloat ("_Inverse", -1.0f); 
             } else {
                 material.SetFloat ("_Inverse", 1.0f);
             }
             material.SetFloat ("_BlurStart", blurStart);
             material.SetFloat ("_BlurEnd", blurEnd);
             material.SetColor ("_BlurColor", blurColor);
             material.SetFloat ("_ScreenWidth", Screen.width);
             material.SetFloat ("_ScreenHeight", Screen.height);
             Graphics.Blit (src, dest, material);
         } else {
             Graphics.Blit (src, dest);
         }
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by 01000101 · Sep 21, 2016 at 01:35 PM

So, apparently this wasn't an error with android. When I tried to export the same shader to a standalone PC build the shader wouldn't work either. The problem was that the shader couldn't be found using the Shader.Find function because the shader wasn't in the Assets/Resources folder.

If anyone else has this problem: Try moving your shaders into the Assets/Resources folder or referencing your shader using another method than: Shader.Find()

Comment
Add comment · Show 1 · Share
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
avatar image diogoriba-bos · Jun 16, 2018 at 12:45 AM 0
Share

Thanks for co$$anonymous$$g back and documenting your findings :) This helps others like me who ran into the same issue.

I found out that you can also include your shader in the "Always Included Shaders" list under the "Edit > Project Settings > Graphics" menu. This will force your shaders to be included in the build process, and this way you don't have to put them under the Resources folder.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Radial Blur for Android/iOS 0 Answers

unity freeze when use normal map at secondary maps 0 Answers

UI CustomShader disappears when Android losing focus 0 Answers

Shader not working correctly on android 1 Answer

This shader (or Image Effect) is not working 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