Unity 使用Shader实现序列帧动画

1 篇文章 0 订阅
1 篇文章 0 订阅

序列帧动画图片使用shader逐帧播放
可以直接使用,拿走不谢。
可以挂到材质上放入Image组件的材质中使用。

Shader "Custom/FrameAnim"
{
    Properties
    {
        _Color("Color", Color) = (1, 1, 1, 1)//颜色
        [PerRendererData]_MainTex("Image Sequence", 2D) = "white" {}//图片
        _HorizontalAmount("Horizontal Amount", Float) = 2//水平列数
        _VerticalAmount("VerticalAmount", Float) = 2//垂直行数
        _Speed("Speed", Range(1, 10)) = 30//播放速度
    }
    SubShader
    {
        LOD 200
        Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}     
        PASS
        {
            Tags { "LightMode" = "ForwardBase" }
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha
   
            CGPROGRAM
            #pragma vertex vert             
            #pragma fragment frag           
            #include "UnityCG.cginc"
            #include "Lighting.cginc"
   
            fixed4 _Color;
            float _Speed;
            float _HorizontalAmount;
            float _VerticalAmount;
            sampler2D _MainTex;

            float4 _MainTex_ST;
            struct _2vert
            {
                float4 vertex: POSITION;
                float4 texcoord: TEXCOORD0;
            };
            struct vert2frag
            {
                float4 pos: SV_POSITION;
                float2 uv: TEXCOORD0;
            };
   
            vert2frag vert(_2vert v)
            {
                vert2frag o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }
            fixed4 frag(vert2frag i) : SV_Target
            {
            	//floor返回小于等于的最大整数
            	//获得虚拟时间,并进行拉伸取整,缩短图片间播放间隔,方便取模
                float time = floor(_Time.y * _Speed);
                //获得总图片数量 _HorizontalAmount * _VerticalAmount
                //对总数取模,得到当前时间点要播放的图片索引号,即整体的第几张图片
                float row = time % (_HorizontalAmount * _VerticalAmount);
				
				//水平方向取模,获取行的索引号
                float posx = floor(row % _HorizontalAmount);
                //floor(row / _HorizontalAmount)能获得纹理坐标竖直方向的序列数
                //Unity的纹理坐标竖直方向的顺序和序列帧纹理中竖直方向上的顺序是相反的。
                //用总体-1-纹理坐标序列数得到序列帧纹理中竖直方向索引号
                float posy = _VerticalAmount - floor(row / _HorizontalAmount) - 1;
				
				//添加到uv坐标上
                half2 uv = i.uv + half2(posx, posy);
                //缩小范围
                uv.x /= _HorizontalAmount;
                uv.y /= _VerticalAmount;
                fixed4 color = tex2D(_MainTex, uv);
                //添加颜色
                color.rgb *= _Color;
                return color;

            }
            ENDCG
        }
    }
    FallBack "Transparent/VertexLit"
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity DOTS(Data-Oriented Technology Stack)中,序列帧动画可以使用ECS(Entity Component System)和Burst Compiler来提高游戏性能。以下是基本步骤: 1. 创建一个Animation组件,用于存储动画帧的信息。 ``` [Serializable] public struct Animation : IComponentData { public int frameCount; public float frameTime; public float elapsedTime; } ``` 2. 创建一个AnimationSystem系统,用于更新动画帧的信息。 ``` public class AnimationSystem : SystemBase { protected override void OnUpdate() { float deltaTime = Time.DeltaTime; Entities.ForEach((ref Animation animation) => { animation.elapsedTime += deltaTime; int frameIndex = (int)(animation.elapsedTime / animation.frameTime) % animation.frameCount; // 更新动画帧 }).ScheduleParallel(); } } ``` 3. 创建一个RenderMesh组件,用于显示动画帧。 ``` public struct RenderMesh : IComponentData { public Mesh mesh; public Material material; public int subMesh; } ``` 4. 创建一个RenderSystem系统,用于显示动画帧。 ``` public class RenderSystem : SystemBase { private EndSimulationEntityCommandBufferSystem _entityCommandBufferSystem; protected override void OnCreate() { _entityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>(); } protected override void OnUpdate() { EntityCommandBuffer commandBuffer = _entityCommandBufferSystem.CreateCommandBuffer(); Entities.ForEach((Entity entity, ref RenderMesh renderMesh, in Animation animation) => { // 更新RenderMesh的Mesh和Material commandBuffer.SetSharedComponent(entity, new RenderMesh { mesh = ..., // 根据动画帧更新Mesh material = renderMesh.material, subMesh = renderMesh.subMesh }); }).ScheduleParallel(); } } ``` 5. 在场景中创建一个实体,并添加Animation和RenderMesh组件。 ``` Entity entity = EntityManager.CreateEntity(); EntityManager.AddComponentData(entity, new Animation { frameCount = ..., // 动画帧数 frameTime = ..., // 动画帧时间间隔 elapsedTime = 0f }); EntityManager.AddSharedComponentData(entity, new RenderMesh { mesh = ..., // 初始Mesh material = ..., // 初始Material subMesh = 0 }); ``` 这样,序列帧动画就可以在Unity DOTS中实现了。注意,这只是一个基本示例,具体实现可能会因游戏需求而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值