c# - Windows Phone 7 上的 UV 纹理偏移动画

标签 c# windows-phone-7 animation xna xna-4.0

如果没有自定义着色器 (WP7),如何实现简单的 UV 纹理动画?我只需要它像视频中那样进行偏移:https://www.youtube.com/watch?feature=player_detailpage&v=VbDjHMer0Bw#t=309s

我可以在模型上甚至只是一堆顶点上执行此操作吗?

最佳答案

如果您更改正在绘制的顶点,则无需自定义着色器即可实现此操作。

如果您在某处有顶点数组,则可以在更新中执行以下操作:

var speed = new Vector2(0.1f, 0.1f);
Vector2 uvOffset = speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

for (int i = 0; i < vertices.Length; ++i)
{
    vertices[i].TextureCoordinate += uvOffset;
}

由于顶点经常变化,因此最好使用 DrawUserPrimitivesDrawUserIndexedPrimitives 直接绘制它们,例如:

GraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3);

在 XNA Model 上,所有内容都存储在非动态 VertexBuffer 中,这意味着您唯一的选择是复制缓冲区、更改它,然后将其重新复制回.请注意,这可能会非常慢并且占用内存,具体取决于您的模型的复杂程度。所以请记住这一点。

var speed = new Vector2(0.1f, 0.1f);
Vector2 uvOffset = speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

//unset first
GraphicsDevice.SetVertexBuffer(null);

foreach (ModelMesh mesh in model.Meshes)
{
    foreach (ModelMeshPart mp in mesh.MeshParts)
    {                    
        //copy array first to change it
        var newVertices = new VertexPositionNormalTexture[mp.VertexBuffer.VertexCount];
        mp.VertexBuffer.GetData<VertexPositionNormalTexture>(newVertices);

        //offset all texture coords
        for (int i = 0; i < newVertices.Length; ++i)
        {
            newVertices[i].TextureCoordinate += uvOffset;
        }

        //set data back into buffer
        mp.VertexBuffer.SetData<VertexPositionNormalTexture>(newVertices);
    }
}

如果您在数组中捕获初始纹理坐标,那么您始终可以在一定数量后返回它们;这样你就不会被限制不断地包裹整个纹理。

关于c# - Windows Phone 7 上的 UV 纹理偏移动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15939123/

相关文章:

vb.net - WP7 MVVM 如何从 ViewModel 调用 me.setfocus()?

iOS mapView animatesDrop

css - React Native Animated 基于多个值插入不透明度

c# - 调用 CacheItemUpdateCallback 时如何获取被删除的项目

c# - EF CodeFirst 设置默认字符串长度并使用 DataAnnotations 覆盖?

c# - WPF 选择 DataGrid 中的所有 CheckBox

windows-phone-7 - 如何在 Windows Phone 7 模拟器上设置电子邮件帐户?

windows-phone-7 - 使用 Inputscope ="Search"关闭单词建议

c# - 在 sql 查询 c# 中转义单引号

android - 使用 RecyclerView 的动画列表项扩展?