• 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 kbm · Mar 10, 2015 at 09:25 PM · 2dshaderalphacleaning

I want to fade out this Texture Masking Shader (using Rendertextures) and I need your help

So I just spent more than 8 hours researching on Google, Answers, Stackoverflow etc. why this wouldn't work, to no avail:

I followed this tutorial here: http://studio.openxcell.com/remove-dustfog-object-unity-swipe.html

I used the instructions and shaders in there to make this "dust cleaning" effect work - but I have a problem fading out the image. Here's what I tried to do, in Properties I added an _Alpha Property as a Float:

     Properties {
         _MainTex ("Main", 2D) = "white" {}
         _MaskTex ("Mask", 2D) = "white" {}
         _Alpha ("Alpha", Float) = 0.5
     }

Then I would try to insert that _Alpha Variable into this line of code (instead of the "1.0" that was there previously):

             fixed4 frag(vert2frag input) : COLOR
             {
                 fixed4 main_color = tex2D(_MainTex, input.texcoord);
                 fixed4 mask_color = tex2D(_MaskTex, input.texcoord);
                 fixed4 _Alpha;
                 
                 return fixed4(main_color.r, main_color.g, main_color.b, main_color.a * (_Alpha - mask_color.a));
             }

So far so good, but instead of letting me change the Alpha Channel of the Texture it produces a strange "flickering" of the image on Mac, and on Windows it gives me this error:

Shader error in 'TextureMasking': incorrect number of arguments to numeric-type constructor at line 52 (on d3d11)

I really need help, I want a dust-cleaning effect for my sprites but also want to alpha-fade this. It seems like NO one has done this before (which seems strange) and I can't find anything on the Asset Store (I would gladly pay for it at this point).

Can anyone help?

Comment
Add comment · Show 3
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 meat5000 ♦ · Mar 10, 2015 at 09:25 PM 0
Share

See what happens if you place this:

 main_color.a * (_Alpha - mask_color.a)

in another set of brackets. If that fails, try using the mul keyword to multiply ins$$anonymous$$d, in the line before.

A * (B - C) = AB - AC [TWO ARGU$$anonymous$$ENTS]

avatar image kbm · Mar 10, 2015 at 11:28 PM 0
Share

Thanks for your comment, meat5000. Unfortunately, neither the first nor the second trick seem to work, I still get an error:

Shader error in 'Texture$$anonymous$$asking': incorrect number of arguments to numeric-type constructor at line 51 (on d3d11)

when I write it like this:

 return fixed4(main_color.r, main_color.g, main_color.b, mul(main_color.a, (_Alpha - mask_color.a)));

what's interesting, when I write it like THIS:

 return fixed4(main_color.r, main_color.g, main_color.b, ((main_color.a * 1.0) - mask_color.a));

so a 1.0 ins$$anonymous$$d of the _Alpha variable, everything works. but I need the 1.0 to be accessible from outside the shader (inside a C# script)

avatar image vanshika1012 · Jan 13, 2017 at 10:18 AM 0
Share

I am using the same technique.. I am not able to run it on any iOS device. It show me weird flickering and also it process really slow.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by illogicalcrow · Mar 10, 2015 at 09:51 PM

Pretty sure you have 'fixed4 _Alpha' in your frag shader when it shouldn't be. 'uniform float _Alpha' should be somewhere just under CGPROGRAM outside of the functions/structs and you'll be able to access the property in the frag function without issue.

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 kbm · Mar 10, 2015 at 11:19 PM 0
Share

Thanks for your reply. Unfortunately, when I put the "fixed4 _Alpha" underneath CGPROGRA$$anonymous$$ like this:

 Shader "Texture$$anonymous$$asking" {
     Properties {
         _$$anonymous$$ainTex ("$$anonymous$$ain", 2D) = "white" {}
         _$$anonymous$$askTex ("$$anonymous$$ask", 2D) = "white" {}
         _Alpha ("Alpha", Float) = 0.5
     }
 
     SubShader {
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
         ZWrite Off
         ZTest Off
         Blend SrcAlpha One$$anonymous$$inusSrcAlpha
         Pass {
             CGPROGRA$$anonymous$$
             #pragma vertex vert
             #pragma fragment frag
             #pragma fragmentoption ARB_precision_hint_fastest
             #include "UnityCG.cginc"
 
             uniform sampler2D _$$anonymous$$ainTex;
             uniform sampler2D _$$anonymous$$askTex;
             uniform float4 _$$anonymous$$ainTex_ST;
             uniform float4 _$$anonymous$$askTex_ST;
             uniform fixed4 _Alpha;
 
             struct app2vert
             {
                 float4 position: POSITION;
                 float2 texcoord: TEXCOORD0;
             };
 
             struct vert2frag
             {
                 float4 position: POSITION;
                 float2 texcoord: TEXCOORD0;
             };
 
             vert2frag vert(app2vert input)
             {
                 vert2frag output;
                 output.position = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, input.position);
                 output.texcoord = TRANSFOR$$anonymous$$_TEX(input.texcoord, _$$anonymous$$ainTex);
                 return output;
             }
             
             fixed4 frag(vert2frag input) : COLOR
             {
                 fixed4 main_color = tex2D(_$$anonymous$$ainTex, input.texcoord);
                 fixed4 mask_color = tex2D(_$$anonymous$$askTex, input.texcoord);
                 
                 return fixed4(main_color.r, main_color.g, main_color.b, main_color.a * (_Alpha - mask_color.a));
             }
             
             ENDCG
         }
     }
 }

It gives me an error:

Shader error in 'Texture$$anonymous$$asking': incorrect number of arguments to numeric-type constructor at line 52 (on d3d11)

It seems to only accept the _Alpha variable when it's inside the frag, but then it's bugging out..

avatar image
0

Answer by kbm · Mar 11, 2015 at 02:38 PM

Alright, I solved it:

Instead of

 uniform fixed4 _Alpha;

I needed to write

 uniform float _Alpha;

Thanks for your help! :)

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
avatar image
0

Answer by Cloggin · May 18, 2017 at 11:08 AM

Found a PDF of the original tutorial - which is no longer available at that link: http://www.bitong-wang.com/wp-content/uploads/2016/03/DustRemoveEffect.pdf

Also, the Wayback Machine has the original page, minus a few graphics: https://web.archive.org/web/20160911073339/http://studio.openxcell.com/remove-dustfog-object-unity-swipe.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Cancel out mesh alpha 1 Answer

Alpha vs non-alpha unlit shaders 0 Answers

See-through hole via shaders on a 2D plane 2 Answers

Custom shader of sprite results in black alpha. 0 Answers

Rewrite simple alphamask fixed function shader to surface or cg shader 2 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