How to animate alpha in an image in Unity? - c#

Ok, I don't know if this is possible but I need to animate the alpha portion of an image - like I have an image with a shape cut out of it so the background shows through, and I want to animate the size of this hole in Unity;s animation controller.
I know it is possible to animate images by stringing together a series of different images like sprite animation, but I want to know if there is a way to animate or cut a hole in another image using an image in Unity and/or animate the alpha portion.
Is this possible? An example of an alpha image with hole cut in it:
And I would want to scale/animate that cut out inner square.

In unity, you can animate everything that can be changed in the inspector, including the size and color (including alpha) of your sprite.
However, it is not possible to animate pixels of a sprite seperately, as far as i know, meaning that you cannot change the alpha value of an area inside your sprite.
Do your images consist of a single color only or do you use complex images?

Well I guess the Shader you can find here will the closest answer to your question.
Other than that, operations on your texture will be quite heavy to perform at runtime.
Hope this helps,

Related

Unity - paint a texture with alpha to make it dissapear partially

I am trying to achieve this effect, I want to "paint" the image with alpha, like erasing part of the image exactly where the user touches or uses the mouse.
I don't know where to start.
You could put each image on a separate Layer and tell your "eraser brush" to ignore the layer behind the one you want to erase using a LayerMask. When the user clicks on the image, it erases part of the image on the first layer, but leaves the image behind it alone.
Credit to Statement from here:
LayerMask, or any "Mask" in Computer Science, is an integer which uses
its 32 bits to represent different flags. While it is an integer, the
value doesn't mean anything useful numerically. What matters is which
flags are turned on or off.
In the case of LayerMask, each flag (each bit) represents one layer.
The layers are selectable at the top of the inspector for each GameObject, you can add and name layers there, and also choose to view different combinations of them (see pic).
If you want to have 2 textures on asingle plane:
- Make a custom shader with 3 textures (background, foreground, mask)
- Then you can paint into that mask texture with grayscale color and shader fades the 2 textures based on the mask color (example, if color is full white, only foreground is visible)
Or if you want to have 3D object behind a separate texture plane (and you only need to erase the existing texture, not draw the same pattern back)
- Could use any default texture which supports alpha
- Just paint into the MainTexture in the material, painting with color alpha 0 makes the area transparent
- Texture needs to have [x] read/write enabled in the importer settings to allow drawing on it
Some related links:
Basic pixel drawing example from unity docs: http://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html
Old demo scene, painting to texture and using dissolve shader : http://unitycoder.com/blog/2011/08/10/painting-holes-with-shader-v2-0/
And search for unity blend 2 textures with mask, unity draw to texture etc. to find more examples & shaders.

Erase part of RenderTarget when drawing primitives?

I'm creating a paint like application using XNA.
I have a render target which acts as a canvas. When the user draws something I draw corresponding triangles using DrawUserPrimitives and triangle strips to make lines and other curves.
I want to implement an eraser in the application, so that the user can erase the triangles from the texture. I've used OpenGL in the past and there I would just use a blend function like so: glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
How would I do this in XNA? I tried setting the GraphicsDevice blend mode to AlphaBlend, Additive, etc.. but it did not work. Any ideas?
If you change your code to use a Texture2D instead of a RenderTarget2D, you can use RenderTarget2D.GetTexture() to get the pixel data from the RenderTarget2D.
Of course, there will probably be a performance hit, but if you can optimize the code in a way that cuts down on the number of times this is done, like temporarily drawing the background color instead of actually erasing, and then iterating through the pixels after the mouse is released, you could make it work.
There may be a better way, but I couldn't find it.

Making a lightsystem like Terraria?

I am trying to make my lighting similar to Terraria's, block-lighting. Now, I know how to make blocks darker, I can assign blocks to a certain lightlevel, but, how would I make an actual light entity, that emits light in a round shape (Can be diamond-shaped too)?
Help would be greatly appreciated, also, if I wasn't clear in my question, feel free to ask.
Basic 2D lighting is very simple. Just do a distance check from your block, to your light, and use that value to scale your light.
This is something you could do fairly simple, since Spritebatch.Draw has a nice Color tint parameter [link]
A pseudo function could be
distance = (block.position - light.position). Length();
lightPower = distance / light.MaxDistance;
finalTint = light.Color * lightPower;
Render Block, with finalTint
For more nice looking light, you could replace "distance / light.MaxDistance" with a more smooth effect.
If you also want lights to go through a few blocks like Terraria, you could count all blocks between your block and the light source. Scale your lightPower down by that amount, and you get the same effect like Terraria has.
Of course, this is a non optimized way of doing it, but should work.
The latest Terraria version however seems to have smooth per pixel lighting instead of per block [preview]. For that I assume they used a second render target and/or Pixel Shader to keep fast performance. This could be a little difficult if you are not familiar with rendering pipelines though.
Hope this helps!
I'm working on a game with a similar lighting model, and the way we do it is this:
Draw the scene, without lighting, to a render target (called the 'Scene Buffer')
Draw the scene's lights, represented as grayscale gradients of any required shape, to a second render target (called the 'Light Map')
Draw the Scene Buffer to the screen, passing in the Light Map as a parameter to the pixel shader
In the pixel shader, query the value of the Light Map at each pixel and adjust the color of the final pixel up or down as necessary.
This also gives you the ability to have colored lighting; all you have to do is tint the light gradients that you render to the Light Map. Just remember to use additive alpha blending.
The downside of this approach is that it's rather naive, and provides no easy way to occlude the lights (that is to say, they pass through walls). In our case, this isn't an issue; you might decide otherwise.

make player cutout image with kinect smoother,special anti aliasing

I tried to cutout player image with using depth image of kinect but there are some problem with that , first when im using depthStreamWithPlayerIndex ,just 320x240 reslution
can used for depth stream , second problem is the function that retrive correct color pixel from depth pixel is works up to 640x480 , cause of these two problem cutouted image is not good if you want show on a high reslution, now i want to know is anyway to fix these two problem Or an algoritm to smooth output image? something like anti-aliasing ?
Couple of things I can think of.
If you want to even out the edges of the person, then you could do this:
Make a mask that is 255 where the player is, 0 everywhere else
Smooth the mask (using Gaussian blurring with an empirically determined parameter)
Use this mask when composing the original player image with the new background
You could replace the smoothing step with morphological operations (e.g. dilation, open/close).
Once you've put the player on the new background, you could "feather" the player edges to make them stand out a bit less:
Apply Canny operator to the edge mask from above
Dilate the mask. You now have a mask that covers the outside of the player
Blur the parts of the composed image that are under the mask

C# MDX drawing transparent rectangle over a sprite

I want to make a "Pausemenu" in a Tetrisgame, when I hit Esc, the Menu pops up and that the user clearly is aware of that the game isnt running, I want to draw a transparent black rectangle over the whole game sprite, I´m using C# Managed DirectX 9.0c on .Net Framework 3.5. (I could achieve the same effect with a texture, but since in the settings the Board Width/Height can be changed, this would be an ugly solution)
Is there an easy way to achieve this?
You can simple create a small PNG, make it black and 75% opaque.
In your Game just use the Sprite Class (with alpha enabled) and scale it (with transform) to the dimensions of your viewport.
You could also use the Sprite Class to modify the Color of the Sprite when drawing, so you can make it fade in or out or give it another color.
Hope that helps!

Categories

Resources