Unity3d Shaderlab Simulation Sprite Animation

Source: Internet
Author: User

Unity3d Shaderlab Simulation Sprite Animation

In the previous article, introduced through the shader simulation of the texture movement , then further, we can also put the frame animation of the sprite texture movement through the shader implementation.

Although everyone is in the game script to do a higher level of control. But the money is wayward, code code can also be code wayward Ah, we will try to do the sprite motion animation, traversing each frame.

First of all, prepare a sprite animation sequence frame, no looking for Niang want. Then create a new material ball and a new shader. Then drag the prepared sequence frame graph onto the texture of the material.

Needless to say,_maintex ("Base (RGB)", 2D) = "White" {}, passed our set frame graph.

Next, you'll create the amount of animation through the Properties, and so on.

1:add>

Properties {

_maintex ("Base (RGB)", 2D) = "White" {}

_TEXWIDHT ("Image Width", float) =0

_spritesize ("Sprite Size", float) =0

_speed ("Sprite speed", Range (0.01,30)) =0

}

The above-mentioned 3 objects, but also in subshader cgprogram under the same declaration;

2:add>

Sampler2d _maintex;

float _TEXWIDHT;

float _spritesize;

float _speed;

Then we will put the input UV value into a separate variable, to ensure the use of the code.

3:add>

void Surf (Input in, InOut surfaceoutput o) {

UV value deposited in SPRITEUV;

Float2 SPRITEUV = In.uv_maintex;

calculates the percentage of cells ;

float pixewidth = _texwidht/_spritesize;

float uvpercentage = PIXEWIDTH/_TEXWIDHT;

float Timeval = fmod (_time.y*_speed,_spritesize);

Timeval = Ceil (timeval);

calculates The offset in the x direction of the sprite ;

float xValue = spriteuv.x;

Xvalue+=uvpercentage*timeval*_spritesize;

Xvalue*=uvpercentage;

refreshes the UV value ;

SPRITEUV = FLOAT2 (XVALUE,SPRITEUV.Y);

Pass the new UV value ;

Half4 C = tex2d (_maintex, SPRITEUV);

O.albedo = C.rgb;

O.alpha = C.A;

}

Save the code and go back to the editor to preview it.

Through the above logic implementation, we are not difficult to see we first from input uv value, put him in a variable uv the Span style= "Font-family:times New Roman;" >x and y coordinate.

Next, we get in gui texwidht spritesize width and each sprite uv

This scale value Uvpercentage Show us The Sprite from the previous sprite cell to the next sprite cell. UV The offset amount.

Finally, we obtain the most recent offset value by calculating the time increment, which is used in the calculation process. fmod () and the ceil () function.

in the end we got the current UV value, passed to tex2d () function, so that our Sprite It's like playing animations. ;

Fmod (x, y): Returns The remainder of x/y, signed with X. y cannot be 0!

Ceil (x): The input parameter is rounded up until its value equals spritesize . He returned to 0;

Above we used the UV offset in the x direction , and in the same way we can add a UV offset in the y direction . Thus satisfying the larger sprite form loop.

Code Start-------------------------------------------------

Shader "91ygame/basicspriteani" {
Properties {
_maintex ("Base (RGB)", 2D) = "White" {}
_TEXWIDHT ("Image Width", float) =0
_spritesize ("Sprite Size", float) =0
_speed ("Sprite speed", Range (0.01,30)) =0
}
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD 200

Cgprogram
#pragma surface surf Lambert

Sampler2d _maintex;
float _TEXWIDHT;
float _spritesize;
float _speed;

struct Input {
FLOAT2 Uv_maintex;
};

void Surf (Input in, InOut surfaceoutput o) {
UV value deposited in SPRITEUV;
Float2 SPRITEUV = In.uv_maintex;
Calculates the percentage of cells;
float pixewidth = _texwidht/_spritesize;
float uvpercentage = PIXEWIDTH/_TEXWIDHT;

float Timeval = fmod (_time.y*_speed,_spritesize);
Timeval = Ceil (timeval);
Calculates the offset in the x direction of the sprite;
float xValue = spriteuv.x;
Xvalue+=uvpercentage*timeval*_spritesize;
Xvalue*=uvpercentage;
Refreshes the UV value;
SPRITEUV = FLOAT2 (XVALUE,SPRITEUV.Y);
Pass the new UV value;
Half4 C = tex2d (_maintex, SPRITEUV);
O.albedo = C.rgb;
O.alpha = C.A;
}
Endcg
}
FallBack "Diffuse"
}

Code End---------------------------------------------------

Unity3d Shaderlab Simulation Sprite Animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.