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

C# 将纹理从顶部平铺到矩形,c#,.net,xna,textures,farseer,C#,.net,Xna,Textures,Farseer,我试图以某种方式将纹理平铺到矩形。例如,以下是我的纹理: 当前,它将其平铺到我的矩形中,以便在矩形顶部切断纹理: 我希望平铺纹理,使其在矩形底部被切断: 我使用以下逻辑在游戏中创建和绘制一个矩形并平铺它: public class HoopsGame : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Body _ground; Texture2D _groundTexture

我试图以某种方式将纹理平铺到矩形。例如,以下是我的纹理:

当前,它将其平铺到我的矩形中,以便在矩形顶部切断纹理:

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

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

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);
    }
}
public class HoopsGame:游戏
{
图形管理器图形;
SpriteBatch SpriteBatch;
身体-地面;
纹理2d_地面纹理;
世界(世界),;
公共篮球场()
{
graphics=新的GraphicsDeviceManager(此);
}
受保护的覆盖无效初始化()
{
Content.RootDirectory=“Content”;
_世界=新世界(新矢量2(0f,9.8f));
int地面高度=(int)(graphics.graphicsdevelope.Viewport.Height*0.05);
矩形地面矩形=新矩形(0,graphics.GraphicsDevice.Viewport.Height-地面高度,graphics.GraphicsDevice.Viewport.Width,地面高度);
_ground=BodyFactory.CreateRectangle(_world,groundRectangle.Width,groundRectangle.Height,0f,groundRectangle);
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
_地面纹理=内容。负荷(“法庭”);
}
受保护的覆盖无效UnloadContent()
{
Content.Unload();
}
受保护覆盖无效更新(游戏时间游戏时间)
{
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色。白色);
spriteBatch.Begin(SpriteSortMode.FrontToBack,BlendState.不透明,samplestate.LinearWrap,DepthStencilState.Default,光栅化状态.CullNone);
spriteBatch.Draw(_groundTexture,新矢量2(0,graphics.GraphicsDevice.Viewport.Height-((矩形)_ground.UserData.Height),(矩形)_ground.UserData,Color.White);
spriteBatch.End();
基础。抽签(游戏时间);
}
}

您没有说得非常清楚,但我认为您的砖块纹理在代码中是_groundTexture。我对microsoft XNA没有太多的经验,但由于它是一种可平铺纹理,您是否考虑过获取矩形的尺寸,然后获取可平铺纹理的尺寸,然后执行如下操作:

//this is of course pseudocode

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


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

最终有效的方法是使用不同的
绘制方法,特别是
绘制(纹理2D纹理,矩形目标矩形,矩形?源矩形,颜色)
,并偏移源
矩形的Y坐标,如Orren所述:

spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullNone);
Rectangle source = (Rectangle)_ground.UserData;
if (source.Height > _groundTexture.Height)
    source.Y += source.Height - _groundTexture.Height;
if (_groundTexture.Height > source.Height)
    source.Y -= _groundTexture.Height - source.Height;
Rectangle destination = new Rectangle(0, graphics.GraphicsDevice.Viewport.Height - ((Rectangle)_ground.UserData).Height, source.Width, source.Height);
spriteBatch.Draw(_groundTexture, destination, source, Color.White);
spriteBatch.End();

偏移会很好,但我不确定如何在XNA中应用纹理偏移。很抱歉,我对XNA不太精通,但当您编写:spriteBatch.Draw(_groundTexture,新矢量2(0,graphics.GraphicsDevice.Viewport.Height-((矩形)_ground.UserData.Height),(矩形)_ground.UserData,Color.White);您不是在SpriteBatch.Draw(Texture2D,RectangleCoordinates,Color)表单中为矩形分配纹理吗。然后我假设你可以做一些类似的事情:SpriteBatch.Draw(Texture2D,(长方形x,长方形y-y偏移),Color),不幸的是,似乎没有什么可以做的。
Vector2
只是翻译。只有一个
绘图(Texture2D texture,Rectangle destinationRectangle,Rectangle?sourceRectangle,Color Color)
方法可以做类似的事情,但仍然没有用。首先绘制sourceRectangle,然后将其缩放到destinationRectangle。因此,我需要确保源矩形均匀地适合我的
Texture2D
,然后绘制,但问题是,到目前为止,我找不到偏移纹理的方法。这似乎比它的价值更麻烦。如果我修改源矩形,似乎可以工作。我知道了,我会尽快发布完整的代码解决方案。谢谢你的帮助,我真的很感激!