gpt4 book ai didi

c# - 从顶部将纹理平铺到矩形

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:24 25 4
gpt4 key购买 nike

我正在尝试以某种方式将纹理平铺到矩形中。例如,这是我的纹理:

enter image description here

目前,它将它平铺到我的矩形,这样纹理在矩形的顶部被切断:

enter image description here

我想平铺我的纹理,使其在我的矩形底部被切断:

enter image description here

我使用以下逻辑在我的游戏中创建和绘制一个矩形并将其平铺:

public class HoopsGame : Game
{

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Body _ground;
Texture2D _groundTexture;
World _world;

public HoopsGame()
{
graphics = new GraphicsDeviceManager(this);
}

protected override void Initialize()
{
Content.RootDirectory = "Content";
_world = new World(new Vector2(0f, 9.8f));
int groundHeight = (int)(graphics.GraphicsDevice.Viewport.Height * 0.05);
Rectangle groundRectangle = new Rectangle(0, graphics.GraphicsDevice.Viewport.Height - groundHeight, graphics.GraphicsDevice.Viewport.Width, groundHeight);
_ground = BodyFactory.CreateRectangle(_world, groundRectangle.Width, groundRectangle.Height, 0f, groundRectangle);

base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
_groundTexture = Content.Load<Texture2D>("court");
}

protected override void UnloadContent()
{
Content.Unload();
}

protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);

spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullNone);
spriteBatch.Draw(_groundTexture, new Vector2(0, graphics.GraphicsDevice.Viewport.Height - ((Rectangle)_ground.UserData).Height), (Rectangle)_ground.UserData, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}
}

最佳答案

你没有说得很清楚,但我假设你的砖 block 纹理在代码中是 _groundTexture 。我真的没有很多使用 Microsoft XNA 的经验,但由于它是可平铺的纹理,您是否考虑过获取矩形的尺寸,然后是可平铺纹理的尺寸,然后执行如下操作:

//this is of course pseudocode

offset_y = Rectangle_Y_Coordinate % Texture_Y_Coordinate

此模块化操作将为您提供将被切断的纹理数量。例如,如果您的矩形的高度为 20,而您的纹理的高度为 7,则您的纹理将被第三次映射,但最多只能达到 6 的高度,因为 20 % 7 = 6。

然后,当您将纹理映射到您的矩形时,只需在给它您的 y 坐标时减去该 y 偏移量,您就会得到想要的结果。如果我有点不清楚,请告诉我。干杯,祝你好运。

关于c# - 从顶部将纹理平铺到矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28929197/

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