Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 kinectalicous · Sep 05, 2015 at 07:29 AM · shadermaterialwebcamtexturealpha-channel

Set one color transparent in texture / shader

Hi,

I have a webcamtexture (have set it as guitexture, or also as game object texture) and want to set one specific color transparent for green box effect. I can't find a shader or any texture/material definition that would do that. Obviously I do not have pngs with alpha channel, since it is webcamtexture - can anyone help?

thank you!

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
3

Answer by wibble82 · Sep 05, 2015 at 11:16 AM

What about something like this? I've done it for a surface shader, though you could do something similar with a fragment shader or something else. The key is that I provide a transparent colour and a threshold. If the shader detects the texture colour is too close to the transparent one, it discards the pixels. In theory, if you wanted to get cleverer you could make it modify the alpha based on closeness, then fade out elegantly as you get close to the transparent colour. Should get you started though :)

 Shader "Custom/transparent_col" {
     Properties {
         _Color ("Color", Color) = (1,1,1,1)
         _TransparentColor ("Transparent Color", Color) = (1,1,1,1)
         _Threshold ("Threshhold", Float) = 0.1
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
     }
     SubShader {
         Tags { "Queue"="Transparent" "RenderType"="Transparent" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Lambert alpha
 
         sampler2D _MainTex;
 
         struct Input {
             float2 uv_MainTex;
         };
 
         fixed4 _Color;
         fixed4 _TransparentColor;
         half _Threshold;
 
         void surf (Input IN, inout SurfaceOutput o) {
             // Read color from the texture
             half4 c = tex2D (_MainTex, IN.uv_MainTex);
             
             // Output colour will be the texture color * the vertex colour
             half4 output_col = c * _Color;
             
             //calculate the difference between the texture color and the transparent color
             //note: we use 'dot' instead of length(transparent_diff) as its faster, and
             //although it'll really give the length squared, its good enough for our purposes!
             half3 transparent_diff = c.xyz - _TransparentColor.xyz;
             half transparent_diff_squared = dot(transparent_diff,transparent_diff);
             
             //if colour is too close to the transparent one, discard it.
             //note: you could do cleverer things like fade out the alpha
             if(transparent_diff_squared < _Threshold)
                 discard;
             
             //output albedo and alpha just like a normal shader
             o.Albedo = output_col.rgb;
             o.Alpha = output_col.a;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }
 
Comment
Add comment · Show 4 · 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 Noxury · Sep 30, 2017 at 11:11 PM 0
Share

Great! Do you know how to make the texture not affected by light/turn it to unlit? I added Lighting Off before CGProgram but it don't work.

avatar image cma80 · Jan 31, 2018 at 10:36 AM 0
Share

I'm using that script on unity 2017.2.0f3 and it works but as output I've only black and alpha image where black is all that is not discarded... how I can have original color * _color ins$$anonymous$$d of black?

avatar image AUEzzat · Jun 18, 2018 at 01:37 AM 0
Share

@Noxury a little late but change that line o.Albedo = output_col.rgb; to that o.Emission = output_col.rgb;

avatar image venenomz97 · Dec 10, 2021 at 11:43 AM 0
Share

Having an issue with HDRP where it was rendering the background regardless of culling. Tried this, and it works! The background is transparent, but every other object is black. Any advice?

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

7 People are following this question.

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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

How to make keywords per material works properly (not in Editor) 0 Answers

Unity VR texture shimmer? 1 Answer

Camera Fade Script - Works only in editor 0 Answers

How can I activate a shader at specific times? 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