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 lixoman100 · Jan 24, 2013 at 03:34 PM · shaderiosshaderlabcutoutcutoff

Cutout shader not being drawn on iOS

Hello,

I'm trying to make a simple cutout interface shader (with no lighting) for my project. It uses a variable named _Cutoff to decide which pixels to draw, and it uses the texture's alpha channel to compare with the cutoff variable. If it passes, then it draws with 1 as its alpha; if not, it draws nothing. I tried doing this with pure Shaderlab by using AlphaTest; it worked fine on the editor but not on iOS. Then I tried to write a simple fragment shader for it, as you can see below:

 Shader "GUI/Cutout" {
     Properties {
         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
     }
 
     SubShader {
         
         Tags { "Queue" = "Transparent" }
         Blend SrcAlpha OneMinusSrcAlpha
         ColorMask RGBA
         Cull Off
         Lighting Off
         ZWrite Off
         ZTest Always
         Fog { Mode Off }
         
         Pass {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             float _Cutoff;
             sampler2D _MainTex;
             float4 _MainTex_ST;
 
             struct v2f {
                 float4 pos : SV_POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             v2f vert (appdata_base v)
             {
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
                 return o;
             }
 
             half4 frag (v2f i) : COLOR
             {
                 half4 texcol = tex2D (_MainTex, i.uv);
                 if(texcol.a > _Cutoff) return half4(texcol.rgb, 1);
                 else return half4(0, 0, 0, 0);
             }
 
             ENDCG
         }
     }
 }

This works fine on the editor, but nothing at all is drawn on an iOS device. Any ideas of what I might be doing wrong? I'm using Unity 3.5.7.

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
1

Answer by psantoki · Feb 21, 2013 at 02:10 AM

Hi lixoman100

First, kudos for writing real shader code, this is very cool.

Here are a few suggestions

  1. Your shader is in the Queue "Transparent". To do cutout effects (alpha testing) put your shader into the Queue "AlphaTest". The Transparent Queue does traditional alpha blending, but it won't do a real alpha test and clip pixels.

  2. You are on the right track with _Cutoff: this is a special keyword in Unity shaderland and will be necessary for your fallback shaders to work. That said-

  3. You need follow-up definitions of your shader variables. Unity shaders define their variables twice: once in the Properties block and then again in the code below. Add these lines between the Properties and SubShader block:

    CGINCLUDE

    uniform sampler2D _MainTex;

    uniform float _Cutoff;

    ENDCG

  4. Alongside your ZWrite, ZTest defines, add this: AlphaTest Greater [_Cutoff]

  5. Finally, at the very end of the shader, define a fallback shader. This will be important if your shader ever runs on hardware that can't handle it--it will fall back to the shader you specify here. This is also crucial if you ever want shadows on your alpha cutout object.

Fallback "Transparent/Cutout/Diffuse"

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

11 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

Related Questions

Creating a shader that overlaps a diffuse with a TexGen ObjectLinear cutout 0 Answers

Efficient shader for sky plane 1 Answer

VertexLit Hilight Shader - Flicker on iOS Problem 1 Answer

Why does this shader show up black on iOS? 1 Answer

Shader - Using Multiple Pass for different shader target 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