• 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 moko engr · Sep 28, 2012 at 06:35 AM · image effects

How to make part of image transparent?

I am new to unity and want to know how to make part of image transparent. So, that the image behind front image is visible from that part of transparent region in front image.

Comment
Add comment · Show 14
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 Dreamblur · Sep 28, 2012 at 06:41 AM 0
Share

There are lots of ways to do that. The simplest method is to clear the blank area of the front image in Photoshop and save the image as a .png file. Then use it as your texture in a Transparent Cutout shader material.

avatar image moko engr · Sep 28, 2012 at 06:53 AM 0
Share

Dear I want it dynamic. That is it can be any rectangular area of front image (that is on user choice) which need to be transparent so that user can see behind image.

avatar image whydoidoit · Sep 28, 2012 at 07:09 AM 0
Share

If these are 3d objects then the object in front (the rectangle) can use a Depth Only shader to write to the depth buffer which will convince Unity that it shouldn't write that part of the back object. If it's GUI - I can't think of a way short of actually modifying the texture.

avatar image Dreamblur · Sep 28, 2012 at 07:15 AM 0
Share

If my understanding of your intent is accurate, then I would recommend writing a shader that would clip the area bound by a given Length and Width parameter. An external script could then provide that shader with those parameters dynamically. Of course, such a method assumes that your images are used as textures for a plane.

avatar image moko engr · Sep 28, 2012 at 07:16 AM 0
Share

Yes these are simply 2d textures. It is similar to crop some rectangular region from front image so that one can see through the it to the portion of back image from this hole. Any Help ???

avatar image moko engr · Sep 28, 2012 at 07:20 AM 0
Share

Dreamblur: You are right. So can you provide me the code demo for it. I am new to unity so it will be a great favor.

I am using two planes using images as textures for them.

avatar image Dreamblur · Sep 28, 2012 at 07:23 AM 0
Share

Try this shader.

Shader "Custom/ClipArea" { Properties { _$$anonymous$$ainTex ("Base (RGB)", 2D) = "white" {} _Length ("Length", Range(0.0, 1.0)) = 0.0 _Width ("Width", Range(0.0, 1.0)) = 0.0 }

SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRA$$anonymous$$ #pragma surface surf Lambert

sampler2D _$$anonymous$$ainTex; float _Length; float _Width;

struct Input { float2 uv_$$anonymous$$ainTex; };

void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2D ($$anonymous$$ainTex, IN.uv$$anonymous$$ainTex); o.Albedo = c.rgb; o.Alpha = c.a; clip((IN.uv_$$anonymous$$ainTex.x >= Length) && (IN.uv$$anonymous$$ainTex.x <= Length) && (IN.uv$$anonymous$$ainTex.y >= Width) && (IN.uv$$anonymous$$ainTex.y <= _Width)) clip((c.a <= 0.1) ? -1.0 : 1.0); // clip from alpha } ENDCG } FallBack "Diffuse" }

You would then provide that shader with a length and width from your script. The length and width are both clamped between 0 and 1, so just do some in-script calculations before passing the values to the shader.

avatar image Dreamblur · Sep 28, 2012 at 07:26 AM 0
Share

Sorry for the formatting. I'm trying to fix it right now.

avatar image whydoidoit · Sep 28, 2012 at 07:29 AM 0
Share

@Dreamblur - that looks awfully like an answer to me - you should probably convert it to one... Also much easier to do the formatting if you actually type it in the answer box and then paste it into comments.

avatar image Dreamblur · Sep 28, 2012 at 07:31 AM 0
Share

I am unable to format the last comment correctly, so I just inserted [RRR] where line breaks are supposed to be. You can just highlight the [RRR]s and press Enter to insert a line break.

Having said that, you might want to try the shader provided in one of the answers first, as this one was hastily written from one of my other shaders.

Shader "Custom/ClipArea" {RRR Properties {RRR _$$anonymous$$ainTex ("Base (RGB)", 2D) = "white" {}RRR _Length ("Length", Range(0.0, 1.0)) = 0.0RRR _Width ("Width", Range(0.0, 1.0)) = 0.0RRR }RRR

SubShader {RRR Tags { "RenderType"="Opaque" }RRR LOD 200RRR CGPROGRA$$anonymous$$RRR #pragma surface surf LambertRRR

sampler2D _$$anonymous$$ainTex;RRR float _Length;RRR float _Width;RRR

struct Input {RRR float2 uv_$$anonymous$$ainTex;RRR };RRR

void surf (Input IN, inout SurfaceOutput o) {RRR half4 c = tex2D ($$anonymous$$ainTex, IN.uv$$anonymous$$ainTex);RRR o.Albedo = c.rgb;RRR o.Alpha = c.a;RRR clip((IN.uv_$$anonymous$$ainTex.x >= Length) && (IN.uv$$anonymous$$ainTex.x <= Length) && (IN.uv$$anonymous$$ainTex.y >= Width) && (IN.uv$$anonymous$$ainTex.y <= _Width))RRR clip((c.a <= 0.1) ? -1.0 : 1.0); // clip from alphaRRR }RRR ENDCGRRR } RRR FallBack "Diffuse"RRR }RRR

avatar image whydoidoit · Sep 28, 2012 at 07:39 AM 0
Share
 Shader "Custom/ClipArea" {
 Properties {
     _$$anonymous$$ainTex ("Base (RGB)", 2D) = "white" {}
     _Length ("Length", Range(0.0, 1.0)) = 0.0
     _Width ("Width", Range(0.0, 1.0)) = 0.0
 }

 SubShader {
     Tags { "RenderType" =  "Opaque" }
     LOD 200
     CGPROGRA$$anonymous$$
     #pragma surface surf Lambert

     sampler2D _$$anonymous$$ainTex;
     float _Length;
     float _Width;

     struct Input {
     float2 uv_$$anonymous$$ainTex;
     };

     void surf (Input IN, inout SurfaceOutput o) {
         half4 c = tex2D (_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex);
         o.Albedo = c.rgb;
         o.Alpha = c.a;
         clip((IN.uv_$$anonymous$$ainTex.x <= _Length) && (IN.uv_$$anonymous$$ainTex.x <= _Length) && (IN.uv_$$anonymous$$ainTex.y >= _Width) && (IN.uv_$$anonymous$$ainTex.y <= _Width))
         clip((c.a <= 0.1) ? -1.0 : 1.0); // clip from alpha
   }
   ENDCG
 } 
 FallBack "Diffuse"

}

avatar image Dreamblur · Sep 28, 2012 at 07:39 AM 0
Share

Unless I am absolutely sure that my answer accurately solves the given problem, I don't post it as such. That does not mean that all my answers are correct, nor that my comments are not correct. It's just the way that I do things. =P

In this particular case, I really would prefer to know that my answer solved your problem before posting it as such. If it doesn't, or if I never find out that it did, well then, "no foul, no harm." XDDD

avatar image whydoidoit · Sep 28, 2012 at 07:39 AM 0
Share

@Dreamblur - nearly managed to format it for you ;)

avatar image whydoidoit · Sep 28, 2012 at 08:12 AM 0
Share

Yeah - I know what you mean - but I've come to think that posting an answer which is an answer to what you think is the OPs question will help future searchers when they do have the question you thought it was. As long as you've clarified then its better to have the answer up there. Your shader is useful and certainly solves one of the likely OP problems - here in the comments it is lost to everyone else meandering by.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Sep 28, 2012 at 07:21 AM

So if you put your textures on planes, which face the camera and then use this shader to render something infront of it there by cutting a hole. If your textures are unlit you might have to fiddle with the order of the shader (I believe there are instructions on that on the wiki article I linked).

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

12 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

Related Questions

image effects and lensflare problem 1 Answer

Is it possible to resize the destination rendertarget? 1 Answer

Unity 5 image effects 1 Answer

Image Effects (Vignette) not working on Android 1 Answer

Problem with adding an image effect to camera 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