NewProger
NewProger

Reputation: 3085

Monogame: Render only inside specified area

This may be a strange question, but I'm trying to find a way to render sprites only inside a specific allowed area rather then the entire buffer/texture.

Like so: enter image description here

Basically allowing me to draw to the buffer or texture2D as I normally would, but with actual drawing happening only inside this specified area and remaining pixels outside of it remaining untouched.

Why this is needed - I'm building my own UI system and I would like to avoid using intermediary buffers as it is quite slow when there are many UI components on the screen (and each has to draw to their own buffer to prevent child elements being drawn outside of parent bounds).

And just to clarify - this is all for simple 2D rendering, not 3D.

Upvotes: 0

Views: 1271

Answers (2)

Silver
Silver

Reputation: 492

You can use:

spriteBatch.Draw(yourTexture,
    //where and the size of what you want to draw on screen
    //for example, new Rectangle(100, 100, 50, 50)//position and width, height
    destinationRectangle,
    //the area you want to draw from the original texture
    //for example, new Rectangle(0, 0, 50, 50)//position and width, height
    sourceRectangle,
    Color.White);

Then it will only draw the area that you chose before. Hope this helps!

Upvotes: 0

reiti.net
reiti.net

Reputation: 315

If your UI is actually drawn with SpriteBatch you can use ScissorRectangle

GraphicsDevice.RasterizerState.ScissorTestEnable = true;
spriteBatch.GraphicsDevice.ScissorRectangle = ...

In 3D, you can render to a texture and draw just a portion of it - or with a shader (you could actually just send in the dimensions as parameter and set it to black in PixelShader if the Pixel is outside that Rectangle (or whatever you want to accomplish)

Upvotes: 1

Related Questions