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 Muhwu · Dec 10, 2014 at 06:46 PM · shaderspritemasking

Sprite inside sprite masking

I'm quite new to Unity and since it seems to contain all sorts of shaders, I was wondering whether a shader of this type is publicly available. Basicly, what I'm looking for is a simple shader that clips sprite from the parts inside another sprite so that the it is in the shape of the inner sprite.

To give you an example, here's a picture:

Sprite 1 as a mask for Sprite 2 but both being rendered

[Update] This is the closest I've gotten so far. It renders the second texture in the silhouette of the first one

 Shader "AlphaMask" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _AlphaTex ("Alpha (A)", 2D) = "white" {}
     }
     SubShader {
         Tags { "RenderType" = "Transparent" "Queue" = "Transparent+1"}
         ZWrite On
     
         ColorMask RGB

         Blend SrcAlpha OneMinusSrcAlpha
     
         Pass {
             SetTexture[_AlphaTex] { Combine texture, primary }
             SetTexture[_MainTex] { Combine previous, texture}
         }
     }
 }

The problem with this is that it does NOT render the underlying texture correctly (i.e. the silhouette) but fills the pixels with pretty much random stretched pixel data from the texture being rendered inside of it. I've tried all sortsa multipass attempts, but I haven't been able to create the desired effect.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by lolzrofl · Dec 11, 2014 at 12:20 AM

In Unity 4.6 you can use UI masks to achieve this.

Here is a tutorial: http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-mask

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 Muhwu · Dec 11, 2014 at 04:13 AM 0
Share

Thanks, but I wanna do this with raw sprites, not UI images. I guess the underlying implementation is there somewhere, but is there a way to use that without having to have UI.Images?

avatar image
0

Answer by Muhwu · Dec 12, 2014 at 11:24 AM

Alright, this is not a complete answer, but what I ended up doing (and it actually fits my purpose better in the long run is) that I have a rather simple shader:

 Shader "AlphaMask" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _AlphaTex ("Alpha (A)", 2D) = "white" {}
     }
     SubShader {
             Tags { "RenderType" = "Transparent" "Queue" = "Transparent+1"}
         ZWrite On
         ZTest LEqual
         
         ColorMask RGB
     
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass {
             SetTexture[_MainTex] { Combine texture }
             SetTexture[_AlphaTex] { Combine texture, previous * texture}
         }
     }
 }

I used the AlphaMask shader found here: http://answers.unity3d.com/questions/590876/rewrite-simple-alphamask-fixed-function-shader-to.html as a base with a little twist.

This is not a complete solution in the sense that it only properly clips the _AlphaTex to be the size (and shape) of the _MainTex, but it doesn't render _MainTex underneath. This is fine though, because I ended up rendering my main object with a basic SpriteRenderer on the bottom and then the "overlay stuff" on top. Having it as a separate SpriteRenderer+shader combination allows me to render any number of stuff on top of it.

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

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

Related Questions

Custom shader that behaves like Sprite mask 1 Answer

Alpha cutoff of sprite based on another texture 1 Answer

Sprites consisting of two packed textures 0 Answers

How can I disable masked object shadow with DepthMask shader 1 Answer

Allow 2D sprite to receive light from any direction and show on both sides 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