2015-03-08 100 views
1

我試圖以某種方式將紋理平鋪到矩形。例如,這裏是我的質地:將紋理平鋪到頂部的矩形

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

你沒有做它非常清楚,但我相信你的磚的紋理是_groundTexture代碼。我真的沒有很多與微軟XNA的經驗,但因爲它是一個圖塊紋理,您是否考慮過獲得矩形的尺寸,然後是圖塊紋理的尺寸,然後執行如下操作:

//this is of course pseudocode 

offset_y = Rectangle_Y_Coordinate % Texture_Y_Coordinate 

這種模塊化操作將爲您提供將被切斷的紋理量。例如,如果矩形的高度爲20,紋理的高度爲7,則紋理將被第三次映射,但僅高達6,因爲20%7 = 6.

然後,當您將紋理映射到您的矩形時,只需在您給出y座標時減去該y偏移,就可以得到所需的結果。如果我有點不清楚,請告訴我。歡呼聲,祝你好運。

+0

抵消將是巨大的,但我不知道如何在XNA中應用紋理偏移。 – Alexandru 2015-03-08 18:24:16

+1

對不起,我不太熟悉XNA,但是當你寫: spriteBatch.Draw(_groundTexture,new Vector2(0,graphics.GraphicsDevice.Viewport.Height - ((Rectangle)_ground.UserData).Height),(矩形)_ground.UserData,Color.White); 是不是你分配一個紋理到你的矩形形式 SpriteBatch.Draw(Texture2D,RectangleCoordinates,Color)。 然後我假設你可以做類似於: SpriteBatch.Draw(Texture2D,(Rectangle_x,Rectangle_y - y_offset),Color) – 2015-03-09 00:40:45

+0

不幸的是,似乎沒有任何東西。 'Vector2'只是翻譯而已。只有一個'Draw(Texture2D紋理,Rectangle destinationRectangle,Rectangle?sourceRectangle,Color color)'方法可以做類似的事情,但它仍然沒用。 sourceRectangle首先被繪製,然後被縮放到destinationRectangle。所以我需要確保源矩形均勻地適合我的'Texture2D',然後繪製到該矩形,但問題是我無法找到一種方法來抵消紋理到目前爲止。這似乎比它的價值更麻煩。 – Alexandru 2015-03-09 22:55:34

1

到底是什麼工作,用不同的方法Draw,特別Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color),並抵消源Rectangle的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();