Hello Guest

Author Topic: Question / Feature Request  (Read 9625 times)

mrjinx

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Tumblr Blog
Question / Feature Request
« on: June 19, 2012, 08:47:19 am »
Hello There,

Thank you for making this awesome tool, its the backbone of the game im working on.

When developing games in flash you have the ColorTransform on each sprite. The nice thing with the ColorTransform is that it lets you go past the regular boundries of a color, and go into overdrive mode basically, creating overly saturated color tints and making a sprite become completely white for example. This is great for quickly creating nice user feedback having sprites blink, turn red, etc.. Is the same effect possible with 2DToolkit, if so, how? if not, i would like to request that as a feature, as it is really awesome to have.

Thanks,
Steve

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Question / Feature Request
« Reply #1 on: June 19, 2012, 11:13:03 am »
This isn't straightforward the way Flash does it. With flash a colortransform is
Y = C * colorMult + colorAdd;

This naturally requires 2 colors to be present. A unity mesh only supports 1 color attribute, and although its possible to hack it in with another attribute (eg. tangent), it is likely to use a LOT more memory like that. Also it'll be a nasty hack. I can show you how you'd do something like that using tangents, if you'd like to give it a go - its straightforward if you don't use some of the features of 2D Toolkit, mainly the generate tangents / normals one.

Alternatively, you can switch materials when you need to flash them. I posted a material to the Unity forum thread a while back, I'm just pasting it here should you wish to give it a go -

Code: [Select]
// unlit, vertex colour, alpha blended, additive overlay

// cull off

 

Shader "tk2d/BlendAdditiveVertexColor"

{

    Properties

    {

        _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}

    }

 

    SubShader

    {

        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}

        LOD 100

       

        ZWrite Off

        Blend SrcAlpha OneMinusSrcAlpha

        Cull Off

 

        BindChannels

        {

            Bind "Vertex", vertex

            Bind "TexCoord", texcoord

            Bind "Color", color

        }

 

        Pass

        {

            Lighting Off

            SetTexture [_MainTex] { combine texture + primary, texture * primary }

        }

    }

}

With this shader, setting the color will brighten the sprite (i.e. black = displays as normal, white = the sprite is white)

mrjinx

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Tumblr Blog
Re: Question / Feature Request
« Reply #2 on: June 19, 2012, 12:15:43 pm »
Nice! I will try it out to see what happens :)

mrjinx

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Tumblr Blog
Re: Question / Feature Request
« Reply #3 on: June 19, 2012, 12:21:37 pm »
Wow i must say that was exactly what i need! Perfect :)

mrjinx

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Tumblr Blog
Re: Question / Feature Request
« Reply #4 on: June 19, 2012, 01:52:05 pm »
I noticed that if i change the material of an animated sprite, it is changed back by the method UpdateMaterial() in tk2dSprite. Is that method critical or can i scrap it ? It seems to work fine even if i remove it.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Question / Feature Request
« Reply #5 on: June 19, 2012, 02:04:29 pm »
I noticed that if i change the material of an animated sprite, it is changed back by the method UpdateMaterial() in tk2dSprite. Is that method critical or can i scrap it ? It seems to work fine even if i remove it.

It is required, but you can add something to ignore setting material. Keep in mind that this will only work when the spritecollection has only one material, as when ignored, the material will never change.

Code: [Select]
static bool ignoreMaterialChange = false;

protected override void UpdateMaterial()
{
   if (ignoreMaterialChange)
   {
      if (renderer.sharedMaterial == null)
         renderer.material = collection.spriteDefinitions[spriteId].material;
   }
   else
   {
      if (renderer.sharedMaterial != collection.spriteDefinitions[spriteId].material)
         renderer.material = collection.spriteDefinitions[spriteId].material;
   }
}

Alternatively, if you know that a particular sprite is only going to be used in this way, then you could use the material override to override it for just this one sprite.