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 Posthoc · Feb 19, 2014 at 11:28 PM · shaderalphafragmentwipe

Tweak Alpha on custom fragment shader

Hi guys,

I'm new to shaders but I've been following some tutorials and I tried a LOT of things on this particular shader... to no avail.

I got the code from here. This shader cycles through 2 textures over time, with a "wipe" effect (see source for a demo). Now that I've learned and kind of understand most of it, I'm trying to change it to be able to use textures with alpha.

Here's the shader code :


 Shader "Wipe" {
 
 Properties{
     _tex0 ("Texture1", 2D) = "white" {}
     _tex1 ("Texture2", 2D) = "white" {}
 }
 
 SubShader{
     Tags {"Queue"="Geometry"}
     
     Pass{
         CGPROGRAM
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
              
             sampler2D _tex0;
             sampler2D _tex1;
             float4 _tex0_ST;
 
             struct v2f {
                 float4 pos : POSITION;
                 float4 color : COLOR0;
                 float4 fragPos : COLOR1;
                 float2  uv : TEXCOORD0;
             };
             
             v2f vert (appdata_base v){
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.fragPos = o.pos;
                 o.uv = TRANSFORM_TEX (v.texcoord, _tex0);
                 o.color = float4 (1.0, 1.0, 1.0, 1);
                 return o;
             }
 
             half4 frag (v2f i) : COLOR{
                 float animtime = _Time*10.0;
                 float2 q = i.uv.xy / float2(1,1);
                 float3 oricol = tex2D (_tex0,float2(q.x,q.y)).xyz;
                 float3 col = tex2D (_tex1,float2(i.uv.x,i.uv.y)).xyz;
                 float comp = smoothstep( 0.2, 0.7, sin(animtime) );
                 col = lerp(col,oricol,  clamp(-2.0+2.0*q.x+3.0*comp,0.0,1.0));

                 // custom alpha bit
                 float4 textureColor = float4(col,1);
                 if(textureColor.a < 1.0){
                     discard;
                 }
                 
                 return textureColor;
             }
         ENDCG
 }
 }
 //FallBack "VertexLit"
 }



I tried to implement a small code snippet in the last function for alpha support. I tried changing Tags to {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}, setting Cull and ZWrite to Off, adding Blend SrcAlpha OneMinusSrcAlpha... basically tried discarding transparent fragments, alpha testing, and blending, but I must be doing something wrong.

I set my two textures (which are PSDs, Photoshop file format with Alpha) to "Advanced" Texture Type, with "Generate Mip Maps" turned off, "Alpha is Transparency" turned on, and "Format" to "RGBA 32 bit". With these settings and the shader above, my textures preview as they should, but my textured plane is black...

Anyone could either help me out or tip me in the right direction please ?

Thanks in advance !


EDIT

This is what I get with the unedited shader from here + PSD set to normal (no advanced texture settings) : alt text

This is what I get with current shader (code above) + PSD set to RGBA 32-bit, no Mip Maps, and Alpha is Transparency turned on :

alt text

And this is what I'm trying to achieve (but not with a still image, I want to keep the "Wipe" effect).

unity_base.jpg (287.1 kB)
unity_black.jpg (241.2 kB)
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
5
Best Answer

Answer by Posthoc · Feb 23, 2014 at 04:56 PM

Got my answer from http://stackoverflow.com/questions/21967969/add-alpha-support-to-fragment-shader-glsl

Here's the proper code for what I was trying to achieve :

  Shader "Wipe" {
 
         // Editor controllers
         Properties{
         _tex0 ("Texture1", 2D) = "white" {}
         _tex1 ("Texture2", 2D) = "white" {}
         }
 
     SubShader{
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
         ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass{
         CGPROGRAM
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             sampler2D _tex0;
             sampler2D _tex1;
             float4 _tex0_ST;
 
             struct v2f {
                 float4 pos : POSITION;
                 float4 color : COLOR0;
                 float4 fragPos : COLOR1;
                 float2  uv : TEXCOORD0;
             };
 
             v2f vert (appdata_base v){
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.fragPos = o.pos;
                 o.uv = TRANSFORM_TEX (v.texcoord, _tex0);
                 o.color = float4 (1.0, 1.0, 1.0, 1);
                 return o;
             }
 
             half4 frag (v2f i) : COLOR{
                 float4 oricol   = tex2D(_tex0, i.uv);
                 float4 col      = tex2D(_tex1, i.uv);
                 float  animtime = _Time * 10.0;
                 float  comp     = smoothstep(0.2, 0.7, sin(animtime));
                 float  coeff    = clamp(-2.0 + 2.0 * i.uv.x + 3.0 * comp, 0.0, 1.0);
                 float4 result   = lerp(col, oricol, coeff);
 
                 return result;
             }
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

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

Related Questions

CG Shader: Setting output alpha changes RGB too 0 Answers

Getting alpha information in shader 1 Answer

My shader stops working if my texture colours are all low-alpha 0 Answers

Changing material color´s alpha in self iluminated shader 1 Answer

Scripting Shader: How to control Alpha with slider? 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