- Home /
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:
[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.
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
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?
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.
Your answer
Follow this Question
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