Hello Guest

Author Topic: Alpha masking sprite(s)  (Read 8748 times)

jeong1135

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
Alpha masking sprite(s)
« on: January 02, 2014, 03:00:40 pm »
Happy New Year 2d Toolkit!

I spent days searching for solution to this but I could not just find the right answer.

http://2dtoolkit.com/forum/index.php/topic,3336.msg16153.html#msg16153

This came quite close to answering my question. Unfortunately, the link at the answer expired.

Here is a picture that describes the situation.



Given the alpha mask, one material should be drawn inside the mask and the other material should be drawn outside the mask.

Can this effect be achieved using shaders and 2d Toolkit?

If the solution is non-trivial, are there any helpful links for reference?

Thank you.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Alpha masking sprite(s)
« Reply #1 on: January 02, 2014, 05:53:21 pm »
You will need to register 2d toolkit to get access to the private support forum for that other link.
http://2dtoolkit.com/forum/index.php?index.php/topic,34.0.html

About your question -
This can be done with a shader, but it will need a bit of work.
1. You need to create 2 atlases in sync for the light and dark sprites. This has to be done manually right now. These links talk about creating normal maps in sync, but the same principle applies.
http://2dtoolkit.com/forum/index.php/topic,1143
http://2dtoolkit.com/forum/index.php/topic,947

2. The alpha mask is a bit tricker, but if you only have one of those associated with each sprite, you can do that in a shader directly. The way I recommend trying / going about this is a shader that lerps between light and dark sprites (the uvs should match based on #1).

The alpha mask is sampled in world space based on on the sprites world position, i.e. the uvs are derived in the vertex shader from a position and size parameter. This position and size parameter can be passed in using Renderer.SetPropertyBlock to each sprite.

jeong1135

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Alpha masking sprite(s)
« Reply #2 on: January 03, 2014, 12:18:27 am »
Thank you for the prompt response as always, Unikron. You are my hero!

Could this approach be simplified if I create two separate tk2dSprite objects with separate SpriteCollection (possibly with two cameras)?

If this theory is valid, I guess I just have to wait for the group registration to get my hands on that alpha mask code. I hope 2 cameras won't drop performance significantly on iOS devices.

I'm very devastated by the steep learning curve of shader, especially with Shaderlab with lack of publications related to the topic.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Alpha masking sprite(s)
« Reply #3 on: January 03, 2014, 12:38:39 pm »
No I don't think you can do this with 2 cameras, the shader solution is going to be easier. Shaderlab is an undocumented pile of fun, the best place to start is the Unity built in shaders which you can get here: http://unity3d.com/unity/download/archive