gpt4 book ai didi

c# - XNA 中的无限滚动背景

转载 作者:行者123 更新时间:2023-12-03 22:17:49 27 4
gpt4 key购买 nike

我正在这样的横向卷轴平台游戏中绘制地面纹理:

spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, _camera.GetViewMatrix(Parallax));
foreach (Sprite sprite in Sprites)
sprite.Draw(spriteBatch);
spriteBatch.End();

在 Sprite.Draw() 中

        public void Draw(SpriteBatch spriteBatch)
{
if (Texture != null)
spriteBatch.Draw(Texture, Position, new Rectangle(0, 0, Texture.Width, Texture.Height), Color.White, Rotation, Origin, Scale, SpriteEffects.None, ZIndex);
}

如何让纹理在 X 方向无限重复?

任何解释如何执行此操作的资源都会很棒。谢谢!

我的相机实现基于此:http://www.david-gouveia.com/2d-camera-with-parallax-scrolling-in-xna/

编辑

这是我试过的,我为背景做了一个单独的绘制:

//Draw Background
spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.LinearWrap, null, null,null, camera.GetViewMatrix(Vector2.One));
groundSprite.Draw(spriteBatch, (int)camera.Position.X - (int)camera.Origin.X / 2);
spriteBatch.End();

在 Sprite.Draw() 中:

public void Draw(SpriteBatch spriteBatch, int scrollX = 0)
{
if (Texture != null)
spriteBatch.Draw(Texture, Position, new Rectangle(scrollX, 0, Texture.Width, Texture.Height), Color.White, Rotation, Origin, Scale, SpriteEffects.None, ZIndex);
}

还有一些问题。

This is the start This is the issue now with the camera matrix.

第二次编辑

我突然想到了。我在地面上将视差设置为 1。它应该为 0,这样它就不会真正移动。当您使用我在下面选择的答案时,它看起来就像在移动。

谢谢!

最佳答案

不要在空间中滚动 Sprite ;滚动用于渲染它的纹理坐标。

将采样器设置为包裹其 UV 坐标:

spriteBatch.Begin(..., SamplerState.LinearWrap, ...);

然后在渲染时指定一个源矩形:

var source = new Rectangle(scrollX, 0, texture.Width, texture.Height);
spriteBatch.Draw(texture, destination, source, Color.White);

如果生成的 UV 坐标脱离纹理的边缘,它们将被纹理采样器包裹,这应该会为您提供所需的“滚动”效果。

关于c# - XNA 中的无限滚动背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8352587/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com