【问题标题】:Tile a texture to a rectangle from the top从顶部平铺纹理到矩形
【发布时间】:2015-05-09 20:28:33
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: c# .net xna textures farseer


    【解决方案1】:

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

    //this is of course pseudocode
    
    offset_y = Rectangle_Y_Coordinate % Texture_Y_Coordinate
    

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

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

    【讨论】:

    • 偏移会很好,但我不确定如何在 XNA 中应用纹理偏移。
    • 对不起我对XNA不太精通,但是当你写的时候: spriteBatch.Draw(_groundTexture, new Vector2(0, graphics.GraphicsDevice.Viewport.Height - ((Rectangle)_ground.UserData) .Height), (Rectangle)_ground.UserData, Color.White);您不是以 SpriteBatch.Draw(Texture2D, RectangleCoordinates, Color) 的形式为矩形分配纹理吗?然后我假设您可以执行以下操作: SpriteBatch.Draw(Texture2D,(Rectangle_x,Rectangle_y - y_offset),Color)
    • 不幸的是,这似乎没有任何意义。 Vector2 只是翻译。只有Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color) 方法可以做类似的事情,但它仍然没用。源矩形首先被绘制,然后缩放到目标矩形。所以我需要确保源矩形均匀地适合我的Texture2D,然后绘制到它,但问题是我到目前为止找不到偏移纹理的方法。这似乎比它的价值更麻烦。
    • 如果我修改源矩形似乎可以工作。我知道了,我会尽快发布完整的代码解决方案。
    • 感谢您的帮助,非常感谢!
    【解决方案2】:

    最终起作用的是使用不同的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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      相关资源
      最近更新 更多