Direct3d/C# - Blur area under a rectangle drawn on a sprite - c#

Any idea how to do it? I am drawing a rectangle that is supposed to be a half-transparent window. I managed to do the transparency by drawing a half-transparent texture, but I also want to blur whatever is under the window.
Normally (eg. using GDI) I would create a bitmap of the area, blur it and paint it as the background of my window. With Direct3D I don't even know how to get the area with whatever is already rendered on it. Or even there can be a different approach, can't it. Please help.

The D3D way is to use a pixel shader to "blur" the area underneath your rect.
This link shows you how to use a pixel shader in C#.
And this link has a guassian blur pixel shader.

It DOES require having your backbuffer as a texture. You can then render the whole thing to a NEW texture and blur the relevant part before putting your semi-trans window over the new texture.
Edit: AFAIK you can't use the Draw function inside a shader. You will need to write your own sprite renderer. The Begin and Draw set up a whole load of states that will break your usage of a vertex shader.

Related

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:
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.
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)
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!

C#: Image unfolding to a rectangle

I need an advice in image processing. I have WF application coded in C# which finds me a coordinates by given parameters and based on this coordinates I would like to crop the image to a circle and unfold this circle to a rectangle.
So just to summarize my questions - How should I correctly crop the image in pictureBox to a circle (ellipse) image and how to unfold this circle to a rectangle?
I hope I described my problem well and I will be very grateful for every advice about how should I continue.
I guess you want the image to be cropped to a circle and then have that circle expand to a rectangle?
This would be the way to do it:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.clip.aspx
You then just animate the EllipseGeometry to make it bigger than the image and then maybe remove the clip if you like.

RenderTarget2D not preserving transparent background

I'm working on a project. All 2D using spriteBatch.
I'm having things like explosions use custom effects that do not apply to the rest of the image. So here is my flow so far:
1)Clear the Background
2)Draw all the explosion sprites
3)Capture that image and implement my effects into a separate RenderTarget2D
4)Draw my Background
5)Draw the RenderTarget2D created in step 3
6) Draw everything else
The problem I'm running into is the RenderTarget2D created is not transparent in the areas not drawn on. As a result, the background drawn in step 4 is not shown.
I have tried GraphicsDevice.Clear(Color.Transparent) following any calls to SetRenderTarget(null). However, I am still getting that purple background.
Any ideas?
I'd post code, but there's too much for you all to have to parse through.
if you follow this flow it should work;
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(Color.Transparent);
// Draw stuff to texture
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(BackgroundColor); // Important to clear here
// Draw background
// Draw texture
// Draw stuff

Fill clipped area with color

I'd like to achieve an effect in XNA where I move a clipping plane through an object, and the object gradually disappears WHILE the clipped area is filled with a custom color or texture.
This is what I was able to achieve via HLSL:
And this is what I would actually need:
I think the teapot isn't a really great example because the models I would use would always be fully closed at all times.
Are there any good solutions to this?
I think that if you just have calculated if a pixel is front or back, you donĀ“t need to clip it if the pixel is front, you only should set the pixel normal to the plane normal value, and calculate light as usual...

Cut holes in a Texture2D

I want to know how to remove part of a Texture from a Texture2D.
I have a simple game in which I want to blow up a planet piece by piece, when a bullet hits it "digs" into the planet.
The physics are already working but I am stuck on how to cut the texture properly.
I need to create a function that takes a Texture2D a position and a radius as input and returns the new Texture2D.
Here is an example of the Texture2D before and after what I want to accomplish.
http://img513.imageshack.us/img513/6749/redplanet512examplesmal.png
Also note that i drew a thin brown border around the crater hole. If this is possible it would be a great bonus.
After doing alot of googling on the subject it seems the best and fastest way to achieve the effect i want is to use pixel shaders.
More specifically a shader method called 'Alpha mapping'. Alpha mapping is done by using the original texture and another greyscale texture that defines what parts are visible or not.
The idea of the shader is to go through each pixel in the original texture and check how black each pixel in the greyscale image is at the same coordinate. The blacker the pixel in the greyscale picture is the higher the alpha value (more visible) the pixel in the original texture becomes. Since all this is done on the GPU it is lightning fast and leaves the CPU ready to do the actual logic for the game.
For my example I will create a black image to use as my greyscale image and then draw white circles on this corresponding to the parts i want to remove.
I've found a MSDN examples with working source code for XNA 4 that does this (the cat example):
http://create.msdn.com/en-US/education/catalog/sample/sprite_effects
EDIT:
I got this to work quite nicely. Created a small tutorial with source code here: http://syntaxwarriors.com/2012/xna-alpha-mapping-with-pixel-shaders/
A good way of doing this is to render a "hole texture" using alphablend on top of your planet texture. Think of it like drawing an invisibility circle over your original texture.
Take a look at this thread for a few nice links worms-style-destructible-terrain.
To achieve your brown edges I'd guess you'd need to take a similar approach. First render the hole to your terrain with say radius 10px. Then you render another circle from the same origin point but with a slightly larger radius, say 12px. You'd then need to set this circle to a blendmode that results in a brown color.
look at my class here:
http://www.codeproject.com/Articles/328894/XNA-Sprite-Class-with-useful-methods
1.Simply create an object of Sprite class for your planet
Sprite PlanetSprite = new Sprite(PlanetTexture2D , new Vector2(//yourPlanet.X, //yourPlanet.Y));
2.when the bullet hits the planet, make a circle texure2d by the center of collision point using "GetCollisionPoint(Sprite b)" method
-you can have a Circle.png with transparent corners
-or you can create a Circle using math(which is better if you want to have bullet power)
3.then create an Sprite object of your circle
4.now use the "GetCollisionArea(Sprite b)" to get the overlapped area
5.now use the "ChangeBatchPixelColor(List pixels, Color color)" where pixels is the overlapped area and color is Color.FromNonPremultiplied(0, 0, 0, 0)
-note you don't need to draw your circle at all, after using it you can destroy it, or leave it for further use

Categories