Hello Guest

Author Topic: Animated tiles  (Read 13199 times)

Biktor

  • Newbie
  • *
  • Posts: 11
    • View Profile
Animated tiles
« on: October 17, 2014, 06:39:38 pm »
First of all, excuse me because maybe this is a very basic question, but I'm a noob on Unity and 2d Toolkit.

I guess there are two techniques to animate tiles:


Could explain me this technique?
Thank you very much.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Animated tiles
« Reply #1 on: October 18, 2014, 08:53:55 am »
2. If you use material overrides, you can replace the material with ANYTHING. It doesn't even need to use the same texture as the atlas. Eg. if you want to make a water shader, you create a placeholder sprite in your main sprite collection, then override the material on that and apply a completely different water texture. You can then animate this material / shader. Your water wont be atlased with the rest but that really isn't a problem because its animated anyway.

Biktor

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Animated tiles
« Reply #2 on: October 20, 2014, 09:04:34 am »
First of all, thank you for the quick answer.

This weekend I have read some tutorials about this.
My game is targeted to mobiles and tables, so my question is:

What would be the best way to achieve this, by script ( http://wiki.unity3d.com/index.php?title=Animating_Tiled_texture) or creating a special shader that basically does the same thing?

Please give me some clue.

Thank you very much.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Animated tiles
« Reply #3 on: October 24, 2014, 07:22:12 pm »
Hi,
I think the shader will be easiest to do. Be sure to use a vertex and frag shader and not a surface shader. You can perform the animation in the vertex shader and can afford to be quite lazy there.

Biktor

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Animated tiles
« Reply #4 on: October 30, 2014, 09:35:06 am »
Ok, here is my shader:

Code: [Select]
Shader "Custom/2DAnimatedTile"
{
Properties
    { 
    _MainTex ("Base (RGB)", 2D) = "white" {}
   
        // Create the inspector values
        _TotalFrames ("Total FRames", Float) = 0.0
        _FPS ("FPS", Float) = 0.0
    }

    SubShader
    {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 100

ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha

Pass

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct v2f
{
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
};

sampler2D _MainTex;
float4 _MainTex_ST;
         
        //Create the connection to the properties inside of the   
        //CG program 
        float _TotalFrames; 
        float _FPS;
       
        float _Tick;

v2f vert (appdata_base v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}

fixed4 frag (v2f i) : COLOR
{
//ANIMATION
                        //Lets calculate the width of a single cell in our 
                       //sprite sheet and get a uv percentage that each cel takes up. 
                       float frameUVPercentage = 1.0/_TotalFrames;
                       //Lets store our UVs in a variable 
                       float2 spriteUV = i.texcoord;
                       _Tick = frac(( _Time.y + frameUVPercentage ) * _FPS) - frameUVPercentage;
                       spriteUV.x = ( spriteUV.x + frameUVPercentage ) + ( floor( _Tick * _TotalFrames ) * frameUVPercentage );

fixed4 col = tex2D(_MainTex, spriteUV);
return col;
}
ENDCG
}
}
}

Works perfectly with sprites, but... if I make a TileMap and use " Multiple Materials" on the tile sprite collection, the material is animated but is over sized and I have no idea where is the problem.

What am I doing wrong?
Some idea or clue?
« Last Edit: October 30, 2014, 09:38:39 am by Biktor »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Animated tiles
« Reply #5 on: October 30, 2014, 11:55:05 am »
 the material is animated but is over sized - what does that mean? Screenshots might help?

Biktor

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Animated tiles
« Reply #6 on: October 31, 2014, 01:11:32 pm »
Ok, I will try to explain my best ( please, excuse my english)

I use a "filmstrip texture":


So step by step:
  • I am usign your Tile Map tutorial
  • A new material is created, with my custom shader and with the filmstrip like the texture...
  • ... and use " Multiple Materials" on the tile sprite collection ( first screenshot)



If I use my shader in a quad, all works perfectly ( second screenshot):



But in the tilemap, the tile with the " multiple material" doesn't work ( is animated, but " over sized"?)



I'm playing with the tilling and offset values, but I haven't got to fix it.



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Animated tiles
« Reply #7 on: November 01, 2014, 02:36:50 pm »
The UVs are still referencing the original rectangle. The easiest (non intrusive) way to do this is to calculate uvs based on world position instead of using in.texcoord. Seeing that your sprites are rectangles it shouldn't be an issue.

Biktor

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Animated tiles
« Reply #8 on: November 05, 2014, 09:30:26 am »
I'm lost...  :-\

I'm trying to fix it with _Object2World, but... I haven't been successful.

Could anyone give me some clue about the way to fix it?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Animated tiles
« Reply #9 on: November 05, 2014, 11:46:36 am »
Something like this in the vertex shader will do the job -
Code: [Select]
o.texcoord = mul(_Object2World, v.vertex).xy * _MainTex_ST.xy + _MainTex_ST.zw;

You can then use the texture tiling / offset values to adjust the tiling ratio to get it to fit to match pixels to world unit.

Biktor

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Animated tiles
« Reply #10 on: November 06, 2014, 05:53:23 pm »
Great, now works  :)

Thank you very much.

Biktor

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Animated tiles
« Reply #11 on: March 11, 2015, 12:01:01 pm »
Sorry to resurrect this topic, but being a related question I think is the best place.

I'm using material overrides in the sprite collection for my tilemap.
The material is animated with this shader:

Code: [Select]
Shader "Custom/2DToolkit_AnimatedTexture"
{
Properties
    { 
    _MainTex ("Base (RGB)", 2D) = "white" {}
   
        // Create the inspector values
        _FrameWidth ("Frame Width", Float) = 0.0
        _TotalFrames ("Total Frames", Float) = 0.0
        _FPS ("FPS", Float) = 0.0
    }

    SubShader
    {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 100

ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha

Pass

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct v2f
{
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
};

sampler2D _MainTex;
float4 _MainTex_ST;
         
        //Create the connection to the properties inside of the   
        //CG program
        float _FrameWidth;
        float _TotalFrames; 
        float _FPS;
       
        float _Tick;

v2f vert (appdata_base v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
//o.texcoord = mul(_Object2World, v.vertex).xy * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}

fixed4 frag (v2f i) : COLOR
{
//ANIMATION 
            float2 spriteUV = i.texcoord;
            _Tick = frac(_Time.y * _FPS);
            spriteUV.x = spriteUV.x + ( floor( _Tick * _TotalFrames ) * _FrameWidth );

fixed4 col = tex2D(_MainTex, spriteUV);
return col;
}
ENDCG
}
}
}

Here is the test texture for the material:



And here the shader inspector:



The performance is great and the animations works perfectly with all sprites except with the water tiles.
The tile is not drawn correctly and consequently the animation has an ugly result.




Where is the problem?
I've been playing with the sprite collection settings with no success and I'm lost.

Thank you.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Animated tiles
« Reply #12 on: March 12, 2015, 11:15:52 am »
That is a result of bilinear filtering onto the adjacent tile. There isn't much you can do about this except to add padding manually (and make your shader more complicated as a result). Or you could just use point filtering and be done with it.

Biktor

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Animated tiles
« Reply #13 on: March 12, 2015, 04:26:26 pm »
Great, you are right  :)
Padding between each tile-frame is the solution.

2dToolkit is an awesome tool.
One more time, thank you very much.