简体   繁体   English

从顶部将纹理平铺为矩形

[英]Tile a texture to a rectangle from the top

I'm trying to tile a texture to a rectangle in a certain way. 我正在尝试以某种方式将纹理平铺到矩形。 For example, here is my texture: 例如,这是我的纹理:

在此输入图像描述

Currently, it tiles it to my rectangle such that the texture gets cut off at the top of the rectangle: 目前,它将其平铺到我的矩形,以便纹理在矩形的顶部被切除:

在此输入图像描述

I want to tile my texture such that it gets cut off at the bottom of my rectangle: 我想要将我的纹理平铺,使其在矩形的底部被切掉:

在此输入图像描述

I use the following logic to create and draw a rectangle in my game and tile it: 我使用以下逻辑在我的游戏中创建并绘制一个矩形并将其平铺:

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

You didn't make it extremely clear, but I assume your brick texture is _groundTexture in the code. 你没有说清楚,但我认为你的砖纹理是代码中的_groundTexture。 I really don't have a lot of experience with microsoft XNA, but since it's a tileable texture, have you considered getting the dimensions of your rectangle, then the dimensions of the tileable texture, then doing an operation like: 我真的没有很多使用microsoft XNA的经验,但由于它是一个tileable纹理,你是否考虑过获得矩形的尺寸,然后考虑tileable纹理的尺寸,然后进行如下操作:

//this is of course pseudocode

offset_y = Rectangle_Y_Coordinate % Texture_Y_Coordinate

This modular operation will give you the amount of the texture that will get cut off. 这种模块化操作将为您提供将被切断的纹理量。 For example, if your rectangle has a height of 20 and your texture has a height of 7, you will be left with your texture being mapped a 3rd time but only up to a height of 6 because 20 % 7 = 6. 例如,如果您的矩形的高度为20,而您的纹理的高度为7,则您的纹理将被第三次映射,但最多只能达到6,因为20%7 = 6。

Then when you map the texture to your rectangle just subtract that y offset when you give it your y coordinate and you should get the desired result. 然后,当您将纹理映射到矩形时,只需在给出y坐标时减去该y偏移量,就可以获得所需的结果。 Please let me know if I was a bit unclear. 如果我有点不清楚,请告诉我。 Cheers and good luck. 干杯,祝你好运。

What worked in the end was using a different Draw method, specifically Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color) , and offsetting the source Rectangle 's Y coordinate as Orren mentioned: 最后工作的是使用不同的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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM