How do I properly achieve subtractive blending in C#, XNA? - c#

I'm working on some kind of mod for Terraria (written in C# and using XNA), in which I need to use some blend modes. I didn't have any troubles getting additive blending to work, but subtractive one causes me some problems.
I managed to display stuff with subtractive blending, but it doesn't really want to return to the standard mode. SpriteBatch.End and Begin doesn't help at all.
This is my custom BlendState:
public readonly static BlendState
bsSubtract = new BlendState{
ColorSourceBlend = Blend.SourceAlpha,
ColorDestinationBlend = Blend.One,
ColorBlendFunction = BlendFunction.ReverseSubtract,
AlphaSourceBlend = Blend.SourceAlpha,
AlphaDestinationBlend = Blend.One,
AlphaBlendFunction = BlendFunction.ReverseSubtract
},
Drawing code:
sb.End();
sb.Begin(SpriteSortMode.Immediate,bsSubtract);
(...drawing drawing blah...)
sb.End();
sb.Begin(SpriteSortMode.Immediate,BlendState.Additive);
The problem is, everything that is drawn after this code seems to still use some old options (half-transparent, bland). What am I doing wrong?
I even tried calling just sb.End() and sb.Begin() before setting the blend state back, or using another custom blend state which was a standard additive one, just with BlendFunctions set to Add, to no avail.
EDIT: Seems like setting ANY custom BlendState makes it do that...
EDIT2: Seems like the problem was me splitting the drawing to 3 separate places: one for item slots, one for tiles and one for world in general. And in one of these (items) I forgot to set the SpriteBatch before using and reset it afterwards. I should have spent more time looking at my code. Still, thanks for trying to help!
(can't close the question just yet, gonna close it after StackOverflow lets me do it)

The default blending mode is BlendState.AlphaBlend.
Try replacing BlendState.Additive with BlendState.AlphaBlend in your code. Or possibly NonPremultiplied, depending on what Terraria is actually using.
Better yet, you could read out exactly the blend state that Terraria was using, as SpriteBatch sets it on the graphics card and simply leaves it there. Here is some untested code that should do exactly that:
sb.End(); // Sets blend state
BlendState previousState = GraphicsDevice.BlendState; // Retrieve it
sb.Begin(SpriteSortMode.Immediate, bsSubtract);
// (...drawing drawing blah...)
sb.End();
sb.Begin(SpriteSortMode.Immediate, previousState); // Re-use it

Seems like the problem was me splitting the drawing to 3 separate places: one for item slots, one for tiles and one for world in general. And in one of these (items) I forgot to set the SpriteBatch before using and reset it afterwards. I should have spent more time looking at my code. Still, thanks for trying to help!

Related

Columns/Magic Jewelry remake on Unity

I want to create a Magic Jewelry clone (Tetris + match 3) using Unity for mobile. So far, I've used UI elements such as UIImage, which serves as an individual block. I created a script that will give out random colors for the box. I then parented three blocks to an empty game object named GameObjectParent.
For the movement, I created another script that subtracts the GameObjectParent's anchoredposition.y every second. In terms of collision, I created a transparent UI Image that will serve as ground triggers that will stop the GameObjectParent's movement once it's entered.
My problem now is the matching of the colored blocks, and more importantly, Instantiating the GameObjectParent. I tried using out
RectTransform gRect = theCanvas.GetComponent<RectTransform>();
var groupH = Instantiate(GameObjectParent, new Vector3(0,0,0) , Quaternion.Euler(0,0,0));
groupH.transform.parent = theCanvas.transform;
groupH.transform.localScale = new Vector2(1, 1);
But it somewhat spawns out of place. I have a "startingblock" that is currently anchored on the canvas at (50, 810), which is where the spawned blocks should start. However, when I try this:
var groupH = Instantiate(GameObjectParent, new Vector2(80,810) , Quaternion.Euler(0,0,0));
The newly cloned and spawned GameObjectParent goes out of place (21392,8712398). I don't know what's happening. Even so, if I attach the Instantiate method on a keypress, it spawns two GameObjectParents at a time, the other being slightly tilted.
I also have no idea how to match the colors of the other blocks as well. I tried searching for similar game concepts like this for unity but to no avail. There are also no tutorials/guides/pointers etc. so I really have to discover it on my own. Any thoughts about this guys? And are there any pointers, guides, or anything that you could give me?
Much appreciated!
First, I wouldn't recommend using UI elements as game objects. We only use UI elements for, well, UI. Consider using sprites or quads instead. You can check Unity tutorials for making 2d games.
For matching, you can assign a code for each color. I usually use enums and bit masking.

Is it possible to create isosurfaces using Oxyplot?

I'm using Oxyplot HeatMapSeries for representing some graphical data.
For a new application I need to represent the data with isosurfaces, something looking like this:
Some ideas around this:
I know the ContourSeries can do the isolines, but I can't find any option that allows me to fill the gaps between lines. Does this option exists?
I know the HeatMapSeries can be shown under the contourSeries so I can get a similar result but it does not fit our needs. .
Another option wolud be limiting the HeatMapSeries colours and eliminate the interpolation. Is this possible?
If anyone has another approach to the solution I will hear it!
Thanks in advance!
I'm evaluating whether Oxyplot will meet my needs and this question interests me... from looking at the ContourSeries source code, it appears to be only for finding and rendering the contour lines, but not filling the area between the lines. Looking at AreaSeries, I don't think you could just feed it contours because it is expecting two sets of points which when the ends are connected create a simple closed polygon. The best guess I have is "rasterizing" your data so that you round each data point to the nearest contour level, then plot the heatmap of that rasterized data under the contour. The ContourSeries appears to calculate a level step that does 20 levels across the data by default.
My shortcut for doing the rasterizing based on a step value is to divide the data by the level step you want, then truncate the number with Math.Floor.
Looking at HeatMapSeries, it looks like you can possibly try to turn interpolation off, use a HeatMapRenderMethod.Rectangles render method, or supply a LinearColorAxis with fewer steps and let the rendering do the rasterization perhaps? The Palettes available for a LinearColorAxis can be seen in the OxyPalettes source: BlueWhiteRed31, Hot64, Hue64, BlackWhiteRed, BlueWhiteRed, Cool, Gray, Hot, Hue, HueDistinct, Jet, and Rainbow.
I'm not currently in a position to run OxyPlot to test things, but I figured I would share what I could glean from the source code and limited documentation.

Do something similar to Auto Tone of Photoshop with Aforge.net or c#

Im developing an image skin detection app.
But there is a problem with my camera, that try to compensate the light and the result image is bad, in most of cases i have a cold or warm effect on the image.
When i use photoshop there is the AutoTone function that normalize an image and reduce this problem.
With aforge i want to use HistogramEqualization() filter but the result is very bad:
// create filter
HistogramEqualization filter = new HistogramEqualization( );
// process image
filter.ApplyInPlace( sourceImage );
So my question is:
There is a function in Accord or Aforge to have the same result of the autotone of Photoshop?
If not, there is some library or script that let to do this?
Thank you all.
I use the LevelsLinear filter and base it on image stats:
ImageStatistics stats = new ImageStatistics(sourceImage);
LevelsLinear levelsLinear = new LevelsLinear {
InRed = stats.Red.GetRange( 0.90 ),
InGreen = stats.Green.GetRange( 0.90 ),
InBlue = stats.Blue.GetRange( 0.90 )
};
levelsLinear.ApplyInPlace(sourceImage);
You can play with the range to tweak the result.
You probably don't want to equalize the histogram, because as you see, a photo that wouldn't normally have much red, would have alot of red created and make it look nasty. Instead you probably want to examine for a bias to a hue that occurs almost everywhere. For example, your original photo probably had a bias towards blue in almost every pixel, and thus probably shouldn't be there. Look for a minimum bias and remove that amount everywhere.
A more practical solution is to experiment with the white balance setting on your camera to see what gives you the best result. Choosing the right preset, will leverage an algorithm that's probably as good as what you would write by hand. But maybe you are doing this as a learning experience.

XNA clears textures mid-render?

We're prerendering large sets of textures to RenderTexture2D and this is the issue we're having:
It seems that randomly during the render of a chunk, the textures for each cell (the top and sides) will corrupt and disappear. The weird things is that they come back when the next chunk is rendered though, so it seems to be something that is occurring on a per-frame basis.
Does anyone know why this occurs (and randomly it seems; note the white rectangle is where a side texture corrupts and you can see from there on out the texture contains just transparent)?
EDIT: The sides of the cubes are being saved to Texture2D but they are still disappearing in the middle of a chunk render and then coming back on the next one. So I don't understand why graphics that are in Texture2D are disappearing and coming back, without reinitialization (and that's the weird part).
RenderTexture2D is only a temporary memory construct, and gets flushed quite quickly and regularly. It is because it is reused in an effort to save memory and to a lesser extent to speed things up. As such you should only treat it as a very temporary place to store your texture. You will want to shift it to a proper Texture2D which will be stored for longer. As just doing a simple:
Texture2D YourPic = (RenderTexture2D)SomeRenderedPic;
Will not do it. This just passes the pointer to the memory space of the rendered image. When the graphics card discards it, then it will still just vanish. What you want to do is something more like:
Color[] MyColorArray = new Color[SomeRenderedPic.Width * SomeRenderedPic.Height];
SomeRendeerPic.GetData<Color>(MyColorArray);
Texture2D YourPic = new Texture2D(
GraphicsDevice,
SomeRenderedPic.Width,
SomeRenderedPic.Height);
YourPic.SetData<Color>(MyColorArray);
Now if I have whipped up that code right then it should store the data and not the pointer into the new texture. This makes the new texture its own unique memory space that won't get flushed the same way a Render Target space would.
There is a down side to this method. It cannot be done at the full refresh rate of XNA. (Something like 60 frames a second... I think... maybe 30... I forget.) At any rate, this may not be fast enough if you need a very constant refreshing. However if you are creating a static texture that doesn't really change much if ever, then this may do the trick for you.
Hopefully this made sense as I am writing this on the fly and late at night. If this doesn't work I apologize. Feel free to write me at jareth_gk#hotmail.com if need be. If I am able to answer your questions I will be happy to.
Otherwise good luck, and be inventive. I am sure there is a solution.
x Jeremy M.
I can't say that we ever solved this issue for sure, but it appears to have been something caused by either threading or splitting the task across multiple cycles. It wasn't an issue with the RenderTarget2D since we were already doing that at the time.

Generating colours after applying opacity of black and white

I don't even know how to describe what I want, but here goes. Assume I have 3 textboxes, and I enter some colour hex code in the first one, I want to apply a black layer on top of it, and have the opacity set to 50% and get the resulting colour in the second text box. Same thing, but with white on the third one.
Let me explain: consider this image below:
In Photoshop, I have the base layer which is sort of sky blue in colour. I create two layers on top of it, one with black, one with white but both have an opacity of 50%. Now, I can use the colour picker (I) to simply select the two wanted colours.
I am having to do this an insane amount of times so I was wondering if I could produce it programatically.
I know, Ideally I should have tried out something, then give out the code which isn't working.. but this has stumped me enough that I don't even know where to start. The closest thing I've seen is Kuler so I think it is possible in flash at least, but then again, I don't know where to start.
Can you guys please point me in the right direction? Ideally, this would be so much better if it's doable in jQuery, but I have looked around and couldn't find anything quite like it. I am not asking for a working solution, just a nudge in the right direction.
If you have any questions, please ask.
Technology is not important to me, solution is - I am most comfortable with c# (at least I like to think I am) but I am a beginner in php, flash. Jquery, I am good at it with most stuff (well, who isn't?) - Whatever works is good to me. Php and Flash, I learnt it as a hobby just to familiarize myself.
Many thanks.
So I can get close, but not exactly your results, which I think is a side effect of .NET using a number in the range 1..255 for alpha when creating a color.
But nonetheless, I think this pretty much does what you want:
public class ColorUtility
{
private Color color;
public ColorUtility(Color original)
{
this.color = original;
}
public Color GetTransformedColor(Color overlay)
{
using(var bitmap = new Bitmap(1,1))
{
var g = Graphics.FromImage(bitmap);
using(Brush baseBrush = new SolidBrush(this.color))
{
g.FillRectangle(baseBrush,0,0,1,1);
}
using(Brush overlayBrush = new SolidBrush(Color.FromArgb(127,overlay)))
{
g.FillRectangle(overlayBrush,0,0,1,1);
}
return bitmap.GetPixel(0, 0);
}
}
}
usage:
var startColor = ColorTranslator.FromHtml("#359eff");
var util = new ColorUtility(startColor);
var blackOverlay = util.GetTransformedColor(Color.Black); // yields #9aceff
var whiteOverlay = util.GetTransformedColor(Color.White); // yields #1b4f80
Close to your desired results, but not exactly.
EDIT: If you change the alpha value to 128 in the utility you get
Black: #9acfff
White: #1a4f7f
This might be closer to what you want, but its still not exact.
I know I am late for the party, just wanted to show another way of doing it.
There is a jquery color plugin for this, I have never really used this, but there is a function that looks like it does what you want.. xColor is the plugin you are looking for.. if you go the combination section you will see that it says that it does what you want.
I just tried a sample on jsfiddle but the results are in line with Jamie's amazing answer. this gives out the same result colors as that of Jamie's code. so you can use either I guess.
So... What's the problem to just write what you exactly said using technology you know best? 3 boxes with colors, input for opacity percentage and output as resulting mixed colors. (I can write it on flash, but I'm not sure if prividing the whole program is appropriate on this site.)
If you don't know how to mix colors with opacity, this link should help:
http://www.pegtop.net/delphi/articles/blendmodes/opacity.htm

Categories