从顶部将纹理平铺为矩形

时间:2015-03-08 16:56:41

标签: c# .net xna textures farseer

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

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);
    }
}

2 个答案:

答案 0 :(得分:2)

你没有说清楚,但我认为你的砖纹理是代码中的_groundTexture。我真的没有很多使用microsoft XNA的经验,但由于它是一个tileable纹理,你是否考虑过获得矩形的尺寸,然后考虑tileable纹理的尺寸,然后进行如下操作:

//this is of course pseudocode

offset_y = Rectangle_Y_Coordinate % Texture_Y_Coordinate

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

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

答案 1 :(得分:1)

最终有效的方法是使用不同的Draw方法,特别是Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color),并按照Orren提到的方式抵消来源Rectangle的Y坐标:

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();