Making Materials Transparent in Unity - c#

So I'm trying to add an effect to a game object where the transparency of its materials will grow over time until it hits a threshold, then go back to normal, and repeat for a predetermined amount of time. I made it so I can make them transparent after a bit of research, but my issue is now just trying to use the best transparent shader.
I first used the UI/Unlit/Transparent shader where I could just grab the alpha value and increment/decrement it as I wanted and made it go transparent, but my issue was that the tint of that material would make it blindingly white, which I don't know if I can do anything about since I am using PNGs as the material so I can't just edit the tint to not affect the material I think.
Then I used the Unlit/Transparent shader but that only has _MainText or Diffuse, which doesn't have a way to edit the transparency without a custom shader is what I've found after scrolling through Unity Answers and other StackOverflow posts.
Is my only option to write a custom shader for Diffuse to get this to work or is there something obvious that I'm missing for these shaders?

Alright I figured out that all I had to do was check off the "Use Alpha Clip" option on the UI/Unlit/Transparent shader and it only uses the alpha variable and not the tint

Related

Is there any way to render an object while standing in it?

I have a first person controller and currently if I walk into a slightly transparent object, it disappears until I walk out of it.
I have a 'water' cube object that is very light blue and transparent, and when I move my camera into third person view then enter the water, my screen turns light blue which is good. In first person mode (which is what I'm trying to figure out), the cube disappears and my screen color remains the same.
I know it has something to do with my camera but even after going through all the features on unity docs and changing a few settings in the inspector, it all remains the same.
Like mentioned in the comments your cube is not visible because the backside of its faces is being culled, meaning they are not rendered. This is not a camera setting but a property of the shader your water cubes material is using.
You could create get a duplicate of the shader your are using where you add Cull Off to change this.
Read about it here.
If you want to go that route you need the source files for your shader. Assuming it is one of the Unity Built in shaders you can get them from the Unity Download archive here for your Unity version.
Clipping the camera trough objects is rarely a desired and you should look at a different way of achieving your effect like using a post-processing volume because the "water" would still be a cube and be drawn behind everything in front of its faces.
For example in a FPS type game this would result in the gun not changing color.

How do I add transparency to a diffuse mesh renderer?

I've been trying to implement a door for my game but when I change the material to the open door material I get a result like this:
These are the settings for the material:
I would like to be able to have an open door with normal transparency instead of this weird glitch. Any help would be much appreciated.
I figured out the solution and it's quite simple! The shader needed to be Legacy Shaders/Transparent/Cutout/Diffuse. My shader was Legacy Shaders/Diffuse,
You might not need to change the shader. In the unity's standar shader if the render mode is changed to transparent then the change in the color's alpha takes place in the game.
It usually happens that the alpha change does not work when it is changed in the color palette because the render mode needs to be changed from the opaque mode for the tranparency to work.

Getting "giggly" effect when slowly moving a sprite

How do I remove this "giggly" effect when slowly moving a sprite?
I have tried adjusting Antialiasing values in QualitySettings and Filter Mode in ImportSettings in the Unity Editor but that doesn't change anything.
Ideally, I would like to keep the Filter Mode to Point (no filter) and anti aliasing turned on to 2x
The sprite is located inside a Sprite Renderer component of a GameObject.
I have uploaded my Unity Project here: http://www.filedropper.com/sprite
I really don't know how to fix the problem... Can anyone help with my personal project?
I cooked up a quick animation to demonstrate what's happening here:
The grid represents the output pixels of your display. I've overlaid on top of it the sliding sprite we want to sample, if we could render it with unlimited sub-pixel resolution.
The dots in the center of each grid cell represent their sampling point. Because we're using Nearest-Nieghbour/Point filtering, that's the only point in the texture they pay attention to. When the edge of a new colour crosses that sampling point, the whole pixel changes colour at once.
The trouble arises when the source texel grid doesn't line up with our output pixels. In the example above, the sprite is 16x16 texels, but I've scaled it to occupy 17x17 pixels on the display. That means, somewhere in every frame, some texels must get repeated. Where this happens changes as we move the sprite around.
Because each texel is rendered slightly larger than a pixel, there's a moment where it completely bridges the sampling points of two adjacent pixels. Both sampling points land within the same enlarged texel, so both pixels see that texel as the nearest one to sample from, and the texel gets output to the screen in two places.
In this case, since there's only a 1/16th scale difference, each texel is only in this weird situation for a frame or two, then it shifts to its neighbour, creating a ripple of doubled pixels that appears to slide across the image.
(One could view this as a type of moiré pattern resulting from the interaction of the texel grid and the sampling grid when they're dissimilar)
The fix is to ensure that you scale your pixel art so each texel is displayed at the size of an integer multiple of pixels.
Either 1:1
Or 2:1, 3:1...
Using a higher multiple lets the sprite move in increments shorter than its own texel size, without localized stretching that impacts the intended appearance of the art.
So: pay close attention to the resolution of your output and the scaling applied to your assets, to ensure you keep an integer multiple relationship between them. The blog post that CAD97 links has practical steps you can take to achieve this.
Edit: To demonstrate this in the Unity project you've uploaded, I modified the camera settings to match your pixels to units setting, and laid out the following test. The Mario at the top has a slightly non-integer texel-to-pixel ratio (1.01:1), while the Mario at the bottom has 1:1. You can see only the top Mario exhibits rippling artifacts:
You might be interested in this blog post about making "pixel-perfect" 2D games in Unity.
Some relevant excerpts:
If you start your pixel game with all the default settings in Unity, it will look terrible!
The secret to making your pixelated game look nice is to ensure that your sprite is rendered on a nice pixel boundary. In other words, ensure that each pixel of your sprite is rendered on one screen pixel.
These other settings are essential to make things as crisp as possible.
On the sprite:
Ensure your sprites are using lossless compression e.g. True Color
Turn off mipmapping
Use Point sampling
In Render Quality Settings:
Turn off anisotropic filtering
Turn off anti aliasing
Turn on pixel snapping in the sprite shader by creating a custom material that uses the Sprite/Default shader and attaching it to the SpriteRenderer.
Also, I'd just like to point out that Unless you are applying Physics, Never Use FixedUpdate. Also, if your sprite has a Collider and is moving, it should have a Kinematic RigidBody attached even if you're never going to use physics, to tell the engine that the Collider is going to move.
Same problem here. I noticed that the camera settings and scale are also rather important to fix the rippling problem.
Here is What Worked for me:
Go to Project Settings > Quality
Under Quality Make the default Quality as High for all.
Set the Anistropic Texture to "Disabled"
Done, And the issue is resolved for me.
Image Reference:
enter image description here

Is there a way to make an object half opaque?

Is there a way to make an object half opaque? I tried this:
renderer.material.color = new Color(255f, 255f, 255f, 0.5f);
But it just makes the item white and the textures are no longer showing.
I have tried using Transparent/Bumped Specular but that makes the object too transparent, even when I don't want the object transparent at the time.
What I am trying to accomplish is to make it transparent to look like the player is inactive during a character selection screen. When the player presses A they will go to full opaque and then can select their player.
Editor Mode:
Play Mode:
So there are a couple things that I'd like to point out to you on this. First off, Unity's Color class is based on values from 0 to 1. So you have 255f in your RGB's, this is giving you what you expect now, but really it's just setting them to 1 in the constructor. So if you want colors in the future do:
new Color(rValue/255f, gValue/255f, bValue/255f, aValue/255f);
Just wanted to point that out while you were here.
So to your actual question, what you're doing with that line of code is you're saying, take the material attached to this object and make it completely white (if i'm wrong with 1,1,1 being white, someone please comment) and half transparent. But that doesn't apply to the object itself, that only applies to the material attached.
So if you want to modify the platform to be transparent, my recommendation is store both the transparent and non-transparent materials that you want on the script where you're changing which one is being used. Then in the code, instead of changing colors, change what material the renderer is using instead.
Hope this helps.

2D Pixel Shader removing alpha channel from color

So, I'm trying to make a game in XNA, and I'm applying a pixel shader to my level so I can add effects to the gameplay screen. The shader itself is working nicely, but it has one flaw that really pushes back performance. As long as the shader is active, anything I draw to the screen acts as though it was drawn with it's alpha channel at the maximum. I'm drawing a lot of things using XNA's built in Color class, and much of it has varying alpha. However, the shader seems to ignore all of this. This occurs even if I just return the source from the shader.
I've seen a few possible solutions. The first of these was to draw to a RenderTarget2D and, while this did solve this issue, it caused more serious problems with alpha that I don't even want to go into.
I have seen a few people claim that multiplying the colours by the alpha value in the shader seems to help, but that made absolutely no difference, which suggests to me that the alpha value input is always being read as 1.
Here are some images to illustrate my problem:
Hopefully, someone knows how to resolve this.
In your Draw() method, you need to call:
GraphicsDevice.BlendState = BlendState.NonPremultiplied; before calling any methods that invoke your shader (i.e. DrawUserIndexedPrimitives, etc.).
Otherwise, the framework assumes the alpha value has been premultiplied and that it can just blit the RGB values right to the framebuffer.

Categories