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
1
Question by mpashanin · May 02, 2013 at 08:17 PM · 2dshaderalphaholesee-through

See-through hole via shaders on a 2D plane

Hi!

I need to make a see through hole on a 2D texture using a custom shader. Defined variables are: r (hole radius) x,y (hole position, either by float or vector2) slope (not necessary, but a useful feature to set a alpha slope for our hole)

The way it should work is when a user wants to see through a building in a 2D game, he simply moves his mouse above it and then we change input variables via simple script command (renderer.material.SetFloat( "_Radius", radius);, etc) we make that texture see-through, like the example below:

alt text

The building and grass are two separate orth 2d textures.

Is there a way of doing it without using Alpha test or anything expensive like that? (iOS project)

Thanks a lot guys

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 DaveA · May 02, 2013 at 08:18 PM 0
Share

This type of thing has been asked before, you should try search. Even google.

avatar image hoy_smallfry · May 02, 2013 at 08:20 PM 0
Share

I believe this is a duplicate question. You can probably get the answer on how to do this here

avatar image mpashanin · May 02, 2013 at 08:22 PM 1
Share

The example you linked is using Alpha Test (cutoff), which is extremely expensive for iOS devices. I could not find anything to solve my problem that does not use that

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Jessy · May 03, 2013 at 09:48 PM

Whether it's expensive depends on how many pixels you're blending. You haven't specified what orientation your world is in, so the properties here are in UV space. You could do it in world space, in 3D, as a generic solution, but I think it might be faster to only deal with two dimensions.

 Shader "Hole Thing" {
 
 Properties {
     _Center ("Hole Center", Vector) = (.5, .5, 0 , 0)
     _Radius ("Hole Radius", Float) = .25
     _Shape ("Hole Shape", Float) = .25
     _MainTex ("Main Texture", 2D) = ""
 }
 
 SubShader {
     Tags {"Queue" = "Transparent"}
     Blend SrcAlpha OneMinusSrcAlpha
     Pass {
         CGPROGRAM
         struct appdata {
             float4 position : POSITION;
             half2 texCoord : TEXCOORD;
         }; 
         
         struct v2f {
             float4 position_clip : SV_POSITION;
             half2 position_uv : TEXCOORD;
         };
         
         #pragma vertex vert
         uniform half4 _MainTex_ST;
         v2f vert(appdata i) {
             v2f o;
             o.position_clip = mul(UNITY_MATRIX_MVP, i.position);
             o.position_uv = _MainTex_ST.xy * i.texCoord + _MainTex_ST.zw;
             return o;
         }
         
         #pragma fragment frag
         uniform sampler2D _MainTex;
         uniform half2 _Center;
         half _Radius, _Shape;
         fixed4 frag(v2f i) : COLOR {        
             fixed4 fragColor = tex2D(_MainTex, i.position_uv);
             half hole = min(distance(i.position_uv, _Center) / _Radius, 1.);
             fragColor.a *= pow(hole, _Shape);
             return fragColor;
         }
         ENDCG
     }
 }
 
 }
Comment
Add comment · Show 3 · 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 mpashanin · May 04, 2013 at 03:39 AM 2
Share

@Jessy saves the day once again. Now using a simple line we can change the position of our hole (orthogonal xy coords): renderer.material.SetVector( "_Center", Vector4(Input.mousePosition.x/1000,Input.mousePosition.y/1000,0,0));

Here's a working example: https://dl.dropboxusercontent.com/u/18351237/Web_pl.html

Thank you very much @Jessy, what an efficient and elegant solution.

avatar image RyanNielson · May 23, 2013 at 02:38 AM 0
Share

@Strab, any chance you can post the project that you linked to above? I'd like to implement this shader in a similar way that you did and am having trouble.

avatar image Andre_Santos · Jun 07, 2015 at 04:27 PM 0
Share

Hello Guys! I m trying to use you solution but in a wall between the player and the camera.

Need two helps my friends: 1° Is there a way to make this shader cast shadow? 2° A raycast from a camera hit the wall...how can i use the hit's coordenates to change the hole offset?

I tried that line insede FixedUpdate:

 _hit.collider.gameObject.GetComponent<Renderer>().material.SetVector("_Center", new Vector4(_hit.point.x,_hit.point.y,0,0));


Thanks bros!!!!!!

avatar image
1

Answer by whydoidoit · May 03, 2013 at 10:02 PM

@Jessy's got a good shader there, just thought I'd pitch a different option.

Why not just render a cylinder with the DepthMask shader though - that would do it if you could live without the edge fading and is very easy to position at the mouse point, especially if overlapping two different textures.

Comment
Add comment · Show 1 · 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 mpashanin · May 04, 2013 at 04:10 AM 0
Share

I'm afraid the edge fading is crucial :(

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

Cutting hole from player character to camera through multiple sprites with edge blending 0 Answers

Cancel out mesh alpha 1 Answer

See-through hole/hide away walls in a 2D environment (via shader)? 0 Answers

Alpha vs non-alpha unlit shaders 0 Answers

Custom shader of sprite results in black alpha. 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