Hello Guest

Author Topic: Attempting to create an outline around my sprites  (Read 4674 times)

Doghelmer

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Attempting to create an outline around my sprites
« on: July 02, 2015, 06:26:10 am »
I'm trying to use this asset to create outlines around the sprites in my game:
http://forum.unity3d.com/threads/free-open-source-2d-outline-image-effect.314362/

It works completely fine with Unity's default sprite renderer, but produces results like the attached image when used with tk2DSprites.  Basically, instead of conforming to the sprite's shape, a rectangular outline is produced.  I'm not sure what might be causing this.

While obviously I wouldn't expect anyone to know about the linked asset that I'm using, I'm wondering anyone has any ideas of a particular setting for my sprites that could result in this working properly?  Any suggestions are welcome.

Additionally, if anyone knows of an alternative method of creating outlines, I'd be interested to hear that as well.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Attempting to create an outline around my sprites
« Reply #1 on: July 02, 2015, 09:51:24 am »
I don't know how that works, but try a cutout shader, might work better.

TDippingSauce

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Attempting to create an outline around my sprites
« Reply #2 on: February 21, 2016, 09:38:47 am »
Hi unikron, I'm having the same problem now and want to know logics behind this weird result. (TBH It's first time touching unity shader system, but I'm learning while trying to fix this)

So I put 2 sprites in the scene - one using tk2dSprite, one using unity5's SpriteRenderer.

Assign these objects to this library's script. Then in MonoBehaviour OnPreCull() method

1. It automatically moves my outline objects to temporary layer
2. Apply Buffer shader to my outline objects.
3. Captures temp layer to RenderTexture instance using temp camera.(<- Seems like this is the bug point)
4. Then draw outline using outline shader in OnRenderImage() method with Graphics.Blit()

I've checked step 2's Buffer Shader Surface code, It looks like this :

Code: [Select]
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
float alpha = c.a * 99999999;
o.Albedo = _Color * alpha;
o.Alpha = alpha;
}

And in step 3's shader(which draws an actual outline) I just modified a bit to draw alpha value as red value. Screenshot of this test :



So it seems like there's a problem in capturing camera to RenderTexture in OnPreCull() method, It has transparent pixels as Colored pixels. Well, I think it's for some reason but I'm not smart enough to figure this out...How can I fix this? I'd appreciated if you help.
« Last Edit: February 21, 2016, 09:45:06 am by TDippingSauce »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Attempting to create an outline around my sprites
« Reply #3 on: February 23, 2016, 09:26:03 pm »
Why is it multiplying c.a * 9999999999? I think it might just need to use alpha test (cutout) there instead of trying to make it happen by multiplying alpha like that. Eg. CutoutVertexColor in tk2d will do that, looks like you want to multiply it by a _Color (no need to premultiply alpha if you're using cutout)