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 Ferazel · Apr 18, 2011 at 03:41 PM · androidshadercrashsignal11

Why Do Android Devices Hate This Shader?

Hi,

I've just started Unity Android development and found out that one of the modified shaders that our project is using is causing the devices to crash.

The shader is a modified cutout soft-edge shader. I removed the color component out of the shader computation (at least I think I did) because I didn't need to do any color modulation.

When using the vanilla Soft-Edge shader it works fine, but only when I try to use this optimized soft-edge shader I run into problems. For now I'm just using the vanilla one. However, is there a reason why this version doesn't work? It works fine in the Editor, it just creates a Signal 11 crash when I run it on my HTC Nexus One device. Any help would be appreciated so that I can better understand what I did wrong.

Thanks!

Properties { _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {} _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5 }

SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout" } Lighting off

 // first pass:
 //   render any pixels that are more than [_Cutoff] opaque
 Pass {  
     CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag

         #include "UnityCG.cginc"

         struct appdata_t {
             float4 vertex : POSITION;
             float2 texcoord : TEXCOORD0;
         };

         struct v2f {
             float4 vertex : POSITION;
             float2 texcoord : TEXCOORD0;
         };

         sampler2D _MainTex;
         float4 _MainTex_ST;
         float _Cutoff;

         v2f vert (appdata_t v)
         {
             v2f o;
             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
             o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
             return o;
         }

         float4 _Color;
         half4 frag (v2f i) : COLOR
         {
             half4 col = tex2D(_MainTex, i.texcoord);
             clip(col.a - _Cutoff);
             return col;
         }
     ENDCG
 }

 // Second pass:
 //   render the semitransparent details.
 Pass {
     Tags { "RequireOption" = "SoftVegetation" }

     // Dont write to the depth buffer
     ZWrite off

     // Set up alpha blending
     Blend SrcAlpha OneMinusSrcAlpha

     CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag

         #include "UnityCG.cginc"

         struct appdata_t {
             float4 vertex : POSITION;
             float2 texcoord : TEXCOORD0;
         };

         struct v2f {
             float4 vertex : POSITION;
             float2 texcoord : TEXCOORD0;
         };

         sampler2D _MainTex;
         float4 _MainTex_ST;
         float _Cutoff;

         v2f vert (appdata_t v)
         {
             v2f o;
             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
             o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
             return o;
         }

         float4 _Color;
         half4 frag (v2f i) : COLOR
         {
             half4 col = tex2D(_MainTex, i.texcoord);
             clip(-(col.a - _Cutoff));
             return col;
         }
     ENDCG
 }

}

SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout" } Lighting off

 // first pass:
 //   render any pixels that are more than [_Cutoff] opaque
 Pass {  
     AlphaTest Greater [_Cutoff]
     SetTexture [_MainTex] {
         combine texture 
     }
 }

 // Second pass:
 //   render the semitransparent details.
 Pass {
     // Dont write to the depth buffer
     ZWrite off

     // Only render pixels less or equal to the value
     AlphaTest LEqual [_Cutoff]

     // Set up alpha blending
     Blend SrcAlpha OneMinusSrcAlpha

     SetTexture [_MainTex] {
         Combine texture 
     }
 }

}

Comment
Add comment · Show 1
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 Jessy · Apr 18, 2011 at 03:52 PM 0
Share

I don't know anything about Android. But the way that Unity handles soft edges is goofy. You shouldn't Alpha Test in the second pass, but rather, use ZTest Less, and multiply the alpha of the texture by 1/_Cutoff. That way, you don't have stupid-looking hard alpha tested edges next to ghostly apparitions of the edge. To do this in fixed function, multiply by .25/_Cutoff ins$$anonymous$$d, and use Quad in the alpha calculation. Also, "RequireOption"="SoftVegetation" is deprecated.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Jessy · Apr 18, 2011 at 06:14 PM

This does not answer your question, but here's what I was talking about, in my comment (sorry, I don't know Cg or feel a need to learn it presently):

Shader "Transparent/Cutout/Soft Edge Unlit +" {

Properties { _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {} _Cutoff ("Base Alpha cutoff (Use Script)", Float) = 1 _CutoffInverseQuarter ("Cutoff inverse / 4 (Use Script)", Float) = 1 }

Category { Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}

 SubShader {
     Pass {
         GLSLPROGRAM
         varying mediump vec2 uv;

         #ifdef VERTEX
         void main() {
             gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
             uv = gl_MultiTexCoord0.xy;
         }
         #endif

         #ifdef FRAGMENT
         uniform lowp sampler2D _MainTex;
         uniform lowp float _Cutoff;
         void main() {
             vec4 texture = texture2D(_MainTex, uv);
             if (texture.a < _Cutoff) discard;
             gl_FragColor = texture;
         }
         #endif      
         ENDGLSL
     }
     Pass {
         ZTest Less  ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha

         GLSLPROGRAM
         varying mediump vec2 uv;

         #ifdef VERTEX
         void main() {
             gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
             uv = gl_MultiTexCoord0.xy;
         }
         #endif

         #ifdef FRAGMENT
         uniform lowp sampler2D _MainTex;
         uniform lowp float _Cutoff;
         void main() {
             vec4 texture = texture2D(_MainTex, uv);
             gl_FragColor = vec4(texture.rgb, texture.a / _Cutoff);
         }
         #endif      
         ENDGLSL
     }   
 }

 SubShader {
     Pass {
         AlphaTest GEqual[_Cutoff]
         SetTexture[_MainTex]
     }
     Pass {
         ZTest Less  ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha
         SetTexture[_MainTex] {
             ConstantColor(0,0,0, [_CutoffInverseQuarter])
             Combine texture, texture * constant Quad
         }
     }
 }

}

}

The material inspector is inadequate for this, though; an EditorWindow is usable:

using UnityEngine; using UnityEditor;

class SoftEdgePlusCutoffWindow : EditorWindow {

static SoftEdgePlusCutoffWindow window; static Material material;

[MenuItem("Assets/Adjust Soft Edge + Cutoff", true)] static bool Validate () { if (window) return false;

 try {material = (Selection.activeObject as GameObject).renderer.sharedMaterial;}
 catch {material = Selection.activeObject as Material;}
 return material ? material.HasProperty("_CutoffInverseQuarter") : false;

}

[MenuItem ("Assets/Adjust Soft Edge + Cutoff", false, 2000)] static void Open () { window = GetWindow<SoftEdgePlusCutoffWindow>(true, material.name); window.autoRepaintOnSceneChange = true; }

// The alpha channel of the texture needs to be scaled so that that it is white at the cutoff value. // That way, there will always be a full invisible-opaque transition, // instead of there being a visible alpha tested edge with lower cutoff values. // You can only make the alpha 4x as bright, with a fixed function shader, // hence the .25 here, and Quad in the shader. bool listeningForGuiChanges; void OnGUI () { Event currentEvent = Event.current; if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0) { Undo.SetSnapshotTarget(material, material.name + " Cutoff Adjustment"); Undo.CreateSnapshot(); Undo.ClearSnapshotTarget(); listeningForGuiChanges = true; }

 material.SetFloat("_Cutoff", EditorGUILayout.Slider("Cutoff", material.GetFloat("_Cutoff"), .25F, 1));
 material.SetFloat("_CutoffInverseQuarter", .25F / material.GetFloat("_Cutoff"));

 if (listeningForGuiChanges &amp;&amp; GUI.changed) {
     Undo.SetSnapshotTarget(material, material.name + " Cutoff Adjustment");
     Undo.RegisterSnapshot();
     listeningForGuiChanges = false;     
 }   

}

}

This works on iOS. Please let us know what happens on Android.

Comment
Add comment · Show 3 · 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 Ferazel · Apr 18, 2011 at 08:06 PM 0
Share

Thanks for the response... unfortunately with my tests it doesn't appear to work. Your shader produces the same crash on my device with only an object with your shader in the scene.

avatar image Jessy · Apr 18, 2011 at 08:28 PM 0
Share

Is this under OpenGL ES 1.1 or 2.0? You should try forcing the rendering mode to one or the other, to see which SubShader is a problem.

avatar image Ferazel · Apr 21, 2011 at 03:59 PM 0
Share

Sorry for the late response, I tested it under 1.1 and it works fine it is just the programmable path that is causing the crash.

avatar image
2

Answer by LoungeKatt · Oct 10, 2014 at 06:33 AM

In case anyone stumbles across this like I did, Android has issues with AlphaTest and clip(), which can be resolved by editing the shader above to read:

 Shader "Transparent/Cutout/Soft Edge Unlit +" {
  
 Properties {
     _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
     _Cutoff ("Base Alpha cutoff  (Use Script)", Float) = 1
     _CutoffInverseQuarter ("Cutoff inverse / 4  (Use Script)", Float) = 1
 }
  
 Category {
     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
  
     SubShader {
         Pass {
             GLSLPROGRAM
             varying mediump vec2 uv;
  
             #ifdef VERTEX
             void main() {
                 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
                 uv = gl_MultiTexCoord0.xy;
             }
             #endif
  
             #ifdef FRAGMENT
             uniform lowp sampler2D _MainTex;
             uniform lowp float _Cutoff;
             void main() {
                 vec4 texture = texture2D(_MainTex, uv);
                 if (texture.a < _Cutoff) discard;
                 gl_FragColor = texture;
             }
             #endif      
             ENDGLSL
         }
         Pass {
             ZTest Less  ZWrite Off
             Blend SrcAlpha OneMinusSrcAlpha
  
             GLSLPROGRAM
             varying mediump vec2 uv;
  
             #ifdef VERTEX
             void main() {
                 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
                 uv = gl_MultiTexCoord0.xy;
             }
             #endif
  
             #ifdef FRAGMENT
             uniform lowp sampler2D _MainTex;
             uniform lowp float _Cutoff;
             void main() {
                 vec4 texture = texture2D(_MainTex, uv);
                 gl_FragColor = vec4(texture.rgb, texture.a / _Cutoff);
             }
             #endif      
             ENDGLSL
         }   
     }
  
     SubShader {
         Pass {
             ZTest Less  ZWrite Off
             Blend SrcAlpha OneMinusSrcAlpha
             SetTexture[_MainTex] {
                 ConstantColor(0,0,0, [_CutoffInverseQuarter])
                 Combine texture, texture * constant Quad
             }
         }
     }
 }
  
 }
Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

Android Crash, waitForCondition(LockCondition) 0 Answers

Shader.WarmupAllShaders FATAL EXCEPTION [UnityMain] 1 Answer

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR) Android Crash Issue 1 Answer

Problem with shaders on Android? 1 Answer

Signal 11 at libil2cpp [Android Crash] 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