- Home /
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?
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]
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)
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.
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.
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..
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! :)
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613185706im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Custom shader of sprite results in black alpha. 0 Answers
Rewrite simple alphamask fixed function shader to surface or cg shader 2 Answers
Cutting hole from player character to camera through multiple sprites with edge blending 0 Answers
Cancel out mesh alpha 1 Answer
Alpha vs non-alpha unlit shaders 0 Answers