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 Winterblood · Mar 14, 2014 at 02:16 AM · shaderalphacgfragment

CG Shader: Setting output alpha changes RGB too

I have a shader that very nearly works, except for the final alpha setting in the fragment func. I'm trying to use the colour from one texture+UV and the alpha from another texture+UV. It works perfectly if I take the modified alpha and stick it in one of the colour channels (ie. it modifies the colour channel in exactly the way I want to modify the alpha channel)...but if I actually apply it to the alpha channel, the shader reverts to using the complete RGBA from MainTex, instead of the RGB from PaperTex.

I'm guessing I have some alpha setting wrong which confuses the shader compiler? Thanks for any suggestions!

 Shader "Custom/Paper Shader" {
     Properties
     {
         _MainTex ("Base (RGBA)", 2D) = "white" {}
         _PaperTex ("Paper (RGB)", 2D) = "white" {}
     }
     
     SubShader
     {
         Tags {"Queue"="Transparent" "RenderType"="Transparent"}
         Blend SrcAlpha OneMinusSrcAlpha
         AlphaTest GEqual 0.1    
         Pass
         {
             CGPROGRAM
             #pragma exclude_renderers ps3 xbox360 flash
             #pragma fragmentoption ARB_precision_hint_fastest
             #pragma vertex vert
             #pragma fragment frag
                   
             // uniforms
             uniform sampler2D _MainTex;
             uniform sampler2D _PaperTex; 
             uniform float4 _MainTex_ST;  //texture name + ST is needed to get the tiling/offset
             uniform float4 _PaperTex_ST;
             
             struct vertexInput
             {
                 float4 vertex : POSITION; 
                 float4 texcoord : TEXCOORD0;
             };
          
             struct fragmentInput
             {
                 float4 pos : SV_POSITION;
                 half2 uv : TEXCOORD0;
                 half2 uv2 : TEXCOORD1;
             };
          
             fragmentInput vert( vertexInput i )
             {
                 fragmentInput o;
                 o.pos = mul( UNITY_MATRIX_MVP, i.vertex );
                   o.uv = i.texcoord;
                 o.uv2 =  i.vertex.xy * _PaperTex_ST.xy + _PaperTex_ST.zw;    // xy is tiling and zw is offset
                 return o;
             }
          
             half4 frag( fragmentInput i ) : COLOR
             {
                 half alpha = tex2D( _MainTex, i.uv ).a;
                 half4 colour = tex2D( _PaperTex, i.uv2 );
                 colour.r = alpha; // This has correct effect on the red channel
                 //colour.a = alpha; // This makes the shader use _MainTex for RGBA
                 return colour;
             }
          
             ENDCG
         }
     }
     
     FallBack "Diffuse"
 }
 
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 Winterblood · Mar 14, 2014 at 02:27 AM 0
Share

Seconds after posting, I've fixed it:

             half4 frag( fragmentInput i ) : COLOR
             {
                 half4 main = tex2D( _$$anonymous$$ainTex, i.uv );
                 half4 colour = tex2D( _PaperTex, i.uv2 );
                 colour.rgb *= main.rgb;
                 colour.a = main.a;
                 return colour;
             }


I always planned to multiply the colours together, so I thought I'd try it and see what happened. Works perfectly now.

I'd still love to know why the simpler case fails though. Seems to be an overzealous optimisation I$$anonymous$$HO, but if it's something I should be aware of, then please enlighten me!

avatar image Winterblood · Mar 14, 2014 at 02:28 AM 0
Share

One last guess: I put the alpha into a lone half, so the alignment didn't match and screwed up the swizzling?

avatar image Winterblood · Mar 14, 2014 at 02:45 AM 0
Share

Oh, and the source texture for PaperTex has no alpha channel. That probably didn't help :)

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Shader that renders fragment behind 0 Answers

Need help with this Shader; camera Solid color is being rendered on skybox. 0 Answers

Getting the world position of the pixel in the fragment shader. 1 Answer

custom Standard Shader alpha issues. 1 Answer

Find out if an object is in front of a target in fragment shader? 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