Purpose of mipmaps for 2D sprites? - unity3d

In current Unity,
For use in Unity.UI as conventional UI ..
for any "Sprite (2D and UI)", in fact it always defaults to having "Generate Mip Maps" turned ON. Every time you drop an image in, you have to turn that "off" and apply.
As noted in the comments, these days you can actually use world space UI canvasses, and indeed advanced users may indeed have (say) "buttons that float over the head of Zelda and they are in the far distance". However if you're a everyday Unity user adding a button, just turn it off :)

In Unity, "sprites" can still be positioned in 3D space. For example, on a world space canvas. Furthermore, mipmaps are used when the sprite is scaled. This is because the mipmap sampling is determined by the texel size rather than the distance.
If a sprite is flat and perfectly scaled then there is no reason to use mipmaps. This would likely apply to your icon example.
I suspect that it is enabled by default for 2D games where sprites will often not be perfectly scaled. To clarify, a sprite does not need to be on a canvas. Sprites can exist as their own GameObject with a Sprite Renderer (not on a canvas.) When this is the case, scaling the camera view will change the sprite's size on the screen resulting in mipmapping due to the texel size changing. This results in making the sprite always perfectly scaled challenging without a canvas.

Related

Recommendations for clipping an entire scene in Unity

I'm looking for ways to clip an entire unity scene to a set of 4 planes. This is for an AR game, where I want to be able to zoom into a terrain, yet still have it only take up a given amount of space on a table (i.e: not extend over the edges of the table).
Thus far I've got clipping working as I want for the terrain and a water effect:
The above shows a much larger terrain being clipped to the size of the table. The other scene objects aren't clipped, since they use unmodifed standard shaders.
Here's a pic showing the terrain clipping in the editor.
You can see the clipping planes around the visible part of the terrain, and that other objects (trees etc) are not clipped and appear off the edge of the table.
The way I've done it involves adding parameters to each shader to define the clipping planes. This means customizing every shader I want to clip, which was fine when I was considering just terrain.
While this works, I'm not sure it's a great approach for hundreds of scene objects. I would need to modify whatever shaders I'm using, and then I'd have to be setting additional shader parameters every update for every object.
Not being an expert in Unity, I'm wondering if there are other approaches that are not "per shader" based that I might investigate?
The end goal is to render a scene within the bounds of some plane.
One easy way would be to use Box Colliders as triggers on each side of your plane. You could then turn off Renderers on objects staying in the trigger with OnTriggerEnter/OnTriggerStay and turn them on with OnTriggerExit.
You can also use Bounds.Contains.

Unity 2D - How to make Resolution-Independent colliders on my sprite

I'm currently working on a school project where we have to merge a 2010 game with an arcade-style game. I chose to merge COD:Blackops Zombie mode with Pacman.
Right off the bat I'm having issues. I'm trying to figure out how I can make the colliders for my level scale with the resolution.
I have an orthographic camera that has a size of 5.
This is what my level currently looks like.
As you can see above, the level itself is a single sprite, placed in a canvas that has Scale with Screen Size enabled.
I've placed a couple of 2D colliders on empty game objects on it for testing purposes.
The current resolution is 1920x1080, which is a 16:9 aspect ratio.
However, if I change the resolution to something like 800x600, which is a 4:3 aspect ratio... then the colliders are completely wrong now.
I thought that the colliders would scale with screen size just like the level sprite does, but I was wrong.
I am now trying to find an alternative approach to making the level have colliders that also change with the size of the sprite...
If you need more information please let me know and I will gladly update my question with the requested information.
To keep things simple, first start with a square aspect canvas.
Make an Empty GameObject, and place it at the bottom left corner of your canvas.
Put all your colliders inside this empty GameObject, and set them up so they match your play-field.
Now all you'll need to do is have a script on this empty gameobject that sets in Update():
Vector2 screenScale = new Vector2(Screen.width, Screen.height).normalized;
transform.localScale = new Vector3(screenScale.x, screenScale.y, 1);
This will make it scale everything inside it to the same proportions as the screen. Alternatively, you could use the Canvas's size if you want to make it based on that instead of the screen itself.

How to get rid of "shadow teeth" in Unity?

I tried everything but nothing can affect this. The only thing is when I change shadow resolution to "low", it becomes more smooth (obviously), but still not the best. Those shadows also look better if angle of view is less acute. Quality settings are the best, light source is a spotlight. Material on those things uses standard shader. What do I do wrong?
Image is enlarged.
You...can't. :(
The problem is that the shadows being cast are essentially just a texture. And texture points (aka "pixels") are square. This shadow texture is then "cast" from the light source (think about the light as being a camera: every pixel that it can see that is an object, that becomes a "dark" pixel in the lightmap; its a bit more complicated than that, but not by much).
Your objects and light are definitely not square up from each other. And in fact, can never be as your cubes are rotated five to ten degrees from each other forming a curve. Which means that some edge, somewhere, is going to get jaggy. This also explains why changing the light's position and orientation to a different angle affects the result: those edges more closely (or less closely) align with where the lightmap pixels are.
You can try various settings, such as Stable Fit or higher quality shadows (this is really just "use a bigger texture" so those jaggies get smaller as the same volume is covered by more shadow-pixels) but fundamentally you're not going to get a better result.
Unless...
You use baked lighting. Open up the Lighting window (Window -> Lighting), set your lights as baked rather than forward/deferred (this means they will not be realtime and may not move or otherwise change) and then in the Lighting window, bake your lights.
This essentially creates a second texture that is wrapped around your objects and gives them shadows and the pixels line up differently and generally give smoother shadow edges when the object's faces align with the shadow-casting edge (such as your stacked cubes). The textures are also much larger than the runtime light textures because it doesn't have to be recomputed every frame (realtime lights are restricted so they don't consume gigabytes of video RAM).
Baking will take a while, let it do its thing.
Have you tried with Stable Fit (under Quality settings)?

Offsetting the rendered result of a camera in Unity

I am trying to make everything that is rendered by my perspective "Camera A" appear 100 points higher. This is due to the fact that my App has an interface with an open space on the upper part.
My app uses face detection to simulate the face movement into an in game avatar. To do this I compute the "Model-View-Matrix" to set it into the camera's "worldToCameraMatrix".
So far this works well, but everything is rendered with the center as the origin, now i want to move this center origin a certain distance "up" so that it matches my interface.
Is there a way to tell Unity to offset the rendered camera result?
An alternative I thought about is to render into a texture, then I can just move the texture itself, but I thought there must be an easier way.
By the way, my main camera is orthographic, and i use this one to render the camera texture. In this case simply moving the rendering game object quad up does the trick.
I found a property called "pixelRect", the description says:
Where on the screen is the camera rendered in pixel coordinates.
However moving the center up seems to scale down my objects.
You can set the viewport rect/orthosize so that its offset or you can render to a render texture and render that as a overlay with a offset or diffirence in scale.
Cheers

Unexpected sprite tearing?

I making a 2d game and I am having some very random problems... My sprites are not displaying as they should be. Below is a screen shot of what I see when I run my game. This bug is present in the game window and the scene window, both when the game is running and not running. And the glitch is not because of the tiles being offset (The background is made up of tiles), if you look closely the player sprite is also glitched up. I have tried restarting unity and my computer, one of which has worked. I have been having this problem ever since i started using tilesets (using one PNG image and cropping out smaller sprites by setting the sprite mode to multiple, instad of using just one PNG image where there is only one sprite and the sprite mode is singular). How do I fix this? (BTW I am using Oryx's lo-fi fantasy and sci-fi sprites at www.oryxdesignlab.com)
This happend when your atlas is not sliced correctly. When you making 2D game there are several important things about sprites.
Set Filter Mode to Point
Set Fromat to 16 bit or TrueColor (in special cases)
Make sure that your atlas is sliced properly.
Edit:
Adjust "Max Size" - make sure is value represents number equal or bigger than spritesheet size
The best way in sliceing atlas in Sprite Editor is NOT to use automatic. I got glithes like that in my project too and those steps can eliminate them.
Have a look at your Sprite in the Sprite Editor. I think you can get to it by double clicking.
Your sprites will have sort of boxes around them to identify each individual sprite (if you set your texture to multiple). One of these boxes will not have sliced correctly, and will have overlapped into another sprite, hence why you're seeing what looks like the arm of another sprite in your picture :)