Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Sunday, July 22, 2018

Unity starter tips - performance.

"Uh oh. It's another one of those starter tips thing from people that thing they know game engines."
Yeah, it's one of those things. Things that I run into my self, and had to hunt them down all over the place when my projects had issues. Of course this is by no means the be-all-end-all list. But it is a start!

1) Remember to set up your project correctly in the Player Settings, set up skinning and batching depending on what you need (Leave batching on of course...), what to log...

2) Know your target platform. Not all platforms are created equal. You won't put the same effects (or the same quality effects) in PC and mobile. Mobile will just get single digit fps, if it will even work in the first place.

3) Set up your physics layers! If you have a physics layer for your player character and you know for sure that the character will never touch or interact in any way with objects belonging to a certain physics layer, disable their interaction in the physics matrix. There is no need to let the engine try to figure out if these two are touching since you know that they won't.

4) Bake stuff in maps! Even if you are targeting DirectX12/Vulkan with all the eye-candy possible, why waste cycles with having dynamic effects enabled for something that you know is going to be static? Bake those shadows and that occlusion. You'll take a tiny bit more to set them up, but you'll save on resources that you'll be able to use elsewhere and the people playing your game won't complain about fps drops.

5) Batch stuff! And Ι'm not talking only about dynamic batching because if abused, it may end up costing frames instead of saving. If you are making a detailed environment in a 3D modeling package, when it's time to export stuff, export all the stationary objects as one mesh, so when they go over in the real time engine then they will be treated as a single mesh.

6) Texture atlas. Continuing from #5, combine your textures for that scene into one big one, so when the engine goes to load the textures it won't go like "stop rendering, read from disk, load into memory" for every single texture. Sure you can't combine eeeeverything, especially the textures for character because if those characters are used in a different scene, then the whole texture will be loaded, even the pieces that aren't used.

7) When making a new script, always remove that Update function if it won't be used. If it's left there in the script, then the engine will still go in the loop every frame to see what it should do in it. It won't skip a function just because it's empty.

8) Comparing Tags. gameObject.CompareTag("thing") is faster than gameObject.tag=="thing".
So if trying to see if this is the right tag, use CompareTag. It is a native Unity function, compiles to fewer calls, it has less of an overhead... long story short, it uses less stuff and things add up.

9) Pool objects. It's really tempting to simply have a call that creates/spawns an enemy from a script. You just write it and when the game is about to use the enemy, spawn it. Well... that introduces hiccups when the entity is spawned, especially if there are alot of stuff going on with it. And if you have lots spawning; as we said already - things add up. Have a pool of enemies ready, just disabled or hidden out of sight, and when needed teleport the enemy where it's needed and have a boolean for that enemy that he is awake. When destroyed/killed, simply hide them again and set the boolean that they are free to be used again later on. This is really especially useful for example when dealing with waves of enemy ships. When they blow up, don't Destroy() the object, simply hide them.

10) Try to use Coroutines instead of Update functions in scripts. As we said in #7, Update is always checked even if it won't do anything. One of the first ways people just starting unity will use to try and control objects, is Update. At first, my self included at first, people will put a boolean in an IF check, and if true the object will do something. That hogs the main execution thread of the project. So use Coroutines instead. What are Coroutines? They are small routines that can be spun off into their own thing, running along the main thread, but unlike Update, they don't stay there looping and never letting the main thread continue. Coroutines can be called when needed, paused and delayed and when done they stop and go away. Useful for timers and controlling stuff!

Sunday, June 4, 2017

Flashing and curtains. (I'm talking about flash and the sensor curtains of your dSLR...)

That flash. I'm talking about that thing over there to the right. The external flash that you hook up to your dSLR.
Many people think that you just strap it on and then the camera will take care of the rest.
That's true up to a point. The whole deal with flash is to allow you to manipulate light, not just make dark things brighter. It can help you with motion, it can make bright walls even brighter to allow you for some silhouette shots, make things appear warmer and softer or colder and harsher.

It can also change the direction of things. What do i mean by that? Most of us have seen those images of things falling into water, or water droplets in fashion shots. You know, the ones where there is a model in front of a dark background and lots of water raining down. Many of us amateur photographers just point the flash and fire it. Don't do that. I've seen posts in photography forums of water shots with a slow shutter speed (to get that water movement) and when someone says "Your rain looks as if it's going upwards" they reply with "that's because of the flash."

NO. It is NOT because a flash was used and "that's how things work".
It's the settings used on the flash unit.

There is a certain trait of the way SLR cameras work and that is (are) the curtains. They are your shutter, they are what is being affected by your "shutter speed" setting, and they really are curtains. Or rather flaps nowadays that cover the sensor. (Click here for an awesome slow mo video on shutter movement from The Slow Mo Guys on YouTube™.)
What they do is that they change the duration that light actually hits the sensor and exposes the scene. How they work is by first dropping the first part which lets light come in and hit the sensor and after some time, the second one drops covering it back up. Then they rewind back up to the top and re-arm themselves for the next shot. The defaults on a flash is to fire up when the first curtain drops (which is fine if nothing is moving). By the time the second curtain drops to cover the sensor (or film, all SLRs work the same) the flash is all done. In fact it's done way before the curtain drops.

1st Curtain
--------------
So what does that mean and what does it have to do with movement?
Everything. The sequence of events is as follows: 1) 1st Curtain drops, exposure begins. 2) Flash fires and instantly fills everything with bright light. 3) Sensor is still fully exposed taking in ambient light, flash stopped firing light. 4) 2nd Curtain drops to cover the sensor.
So, the the sensor took in the majority of light at the very beginning because of the flash and after the flash was done, it was still taking in ambient light.
Imagine a water droplet coming down. It reflected lots of light at the very beginning from the flash and when the flash was done it reflected back the less intensive ambient light. That is what makes the ghosting effect, and because the majority of light reflected back was at the very beginning, the brightest and sharpest imprint of the image is when the subject falling was at the very top position; when the flash fired and then the ambient light subsequently left a fading-out trail as the droplet keeps falling down. And our brain interprets that as moving upwards because of persistence of vision. The same thing that when you move something fast right to left, you see the object at the far left with a tail behind it.

If the flash unit used does not have any controls on it (as is the case with some really small units) then the camera can change those settings through a dedicated flash menu.

And yes, that is my oooold Canon 450D that is missing an eyepiece, the rubber is falling out, it has been drenched in sea water, rain, beer, coffee and tea.

The image shows the settings for the external flash, but they are still present for the pop out flash as well.


2nd Curtain
---------------
All anyone has to do on the flash is to set it to fire at the 2nd Curtain. That's it. The camera will trigger the flash just moments before releasing the 2nd Curtain to cover up the sensor and in doing so ensures that the majority of light hits the subject at the end, and thus reverting the motion of the moving objects to what our brain is expecting.

The sequence of events now changes to: 1) 1st Curtain drops starting the exposure. 2) Ambient light comes in, no flash. Moving objects reflect only the softer ambient light. 3) Flash fires, moving objects get a burst of bright light for an instant and then immediately after ... 4) 2nd Curtain drops, ending exposure.

This is not something that is only available in pro equipment. They are normal functions of consumer grade SLR cameras and external flashes. (Note: With Canon cameras you can't set 2nd Curtain if you are in optical wireless mode. Cheap RF wireless triggers or studio strobes can be used, but that's a story for another time. If you can go wireless then you probably don't need lessons on what the 2nd Curtain is.)
















The above (shitty) example shows the difference with 1st and 2nd curtain. In both instances the fan is spinning counter-clockwise. But notice the difference in the ghosting/blurring between the two images. Sure, most people won't care about the direction a fan is spinning as long as it's spinning, but this is just to show the difference. 1st Curtain of course can be used creatively. If for example someone wants to make someone flying upwards with motion blur, then they can have the person fall downwards and shoot with the flash set to 1st Curtain. So it'll be like those water droplets we discussed previously, where the flash would freeze the subject at the top of their motion and then ambient light would create motion blur going downwards.

Friday, January 31, 2014

Camera settings and their actual effect.

A friend just bought his first dSLR and immediately started taking pictures to get to know it, which is the best thing to do once you get one. Don't ogle over it; just go out and start taking pictures, while noticing what changes in your photos when you change a setting at a time.

During his first tries though the results didn't go along with the mental images he had. And I'm guessing here that the manual may be at fault a bit. Most often the manual says that you should try and expose the image correctly with the exposure meter. Yes, you should, even the cheapest of the dSLRs now have great sensors and can forgive a step up or down. (A bit underexposed or over exposed. If the circumstances are such that you can't get a perfect exposure, you can fix it later in lightroom or photoshop. Though that should be the last resort...)

Aperture for emphasis.
Manuals most of the time say that the aperture setting is for changing the lens's iris, allow more or less light in and that will make the depth of field shallow or deep. Now i know you'll yell "semantics!" and you'll be right i guess, but it should be the other way around, that the aperture setting allows you isolate a subject or bring everything in focus. I'm putting it like that to make it easier to realize just by reading it from the first time, that if you want to make a single thing stand out, that's what you'll use, instead that "it changes the amount of light through the lens and in doing so makes stuff blurry or not." People tend to think of non-sharp photos when you say blurry, instead of thinking isolated subjects with bokeh.

Shutter speed as a time function.
Same thing with the shutter. It says to keep a fast shutter to avoid shakiness in your photos. Well yeah, if the results came out shaky and you don't have Image Stabilization, tripod or steady hands, using a faster shutter will help with that. But don't think of the shutter as the thing to make your images ... not shaken. Grip the camera firmly, have the shutter button semi pressed and ready to just press it just a tad more to release the shutter and you can do well with a slow shutter speed. You should think the shutter speed as a way to express time instead, add movement and life in a still photograph. There is this thing that people say; that you should keep your shutter speed at, or above the lens's focal length, meaning that you should have 1/50 for a 50mm lens. Let me tell you that you can go handheld a lot lower than that with just a tiny bit of practice and no IS. If you want to capture a car moving fast, you can use lets say 1/200 and get a steady car, with the wheels blurry from spinning, and the trees blurry from you tracking the car. But do that with a human walking and it's just a frozen thing. May as well be standing still. Go for a slower shutter speed, one that will get your subject's limbs just starting to go blurry from movement and then use the aperture to cut down light. Want to isolate the subject as well? Use a lower ISO setting. Don't have a lower ISO setting? Well at that point you'll need to get an ND filter to cut down light I'm afraid. But the point is that there's still a way.

So don't think the settings as the way to get your exposure meter at the center. Instead think of what you want to show in the picture and use the tools for that, there lots of times you'd want to have motion blur.

Thursday, December 5, 2013

The strange animal called "photographer" and it's environment.

I've been meaning to post something like this for some time now but (being me), I would either forget about it or postpone it until "tomorrow".

Sometimes people wonder about what they can do when others are giving them angry stares or even chased away from places while trying to take photos. It's been said a lot of times by many, I didn't discover the solution to this but it does really work. It has to do with how one behaves at each location. Basically you get back what you show. By that I mean that if you are fidgety and anxious, people will look at you funny because if you weren't doing something shady, why would you be anxious?

If you go in a public place, don't just go there with your camera up in your face aiming it at people. Warm up to them first. Don't go there rushing either, make room in your day for two to three hours and go to the place you thought that there would be some nice picture opportunities and take some time to just show people that you are there with a camera, let them warm up to the idea that there's someone with a camera and might take their picture. Don't walk around with the camera in your hands ready or have it hanged around your neck like a tourist. I won't be forcing anyone on how to wear their camera, I just found it really easier and out of my way if I have the camera slung across my left shoulder coming down to my right side. I can just pull it in front of me like those movies with the marines drawing their guns :P. Just as fast and out of the way as well.

In malls or other privately owned places it's important to ask for permission and to make people realize that you just want to take some pictures and isn't stalking people. Don't shove your lens in people's faces or sharply pop up to take photos of kids... Having a couple of business cards with you is a also a nice idea in case someone wants to throw you out while you still need to stay as it helps even further in making people realize that you aren't up to something no good. Who would give out their details if they wanted to cause trouble? (I know Joker would, but if you are The Joker and want to take pics while you are on the job all the more power to you. Send a couple pics over.)

If you notice someone that glares at you for taking pictures just breath in, go over there and ask them if they would like to see the photos you took.

So, stay calm; don't rush it; be relaxed and don't forget to smile in a non threatening way :P. (It helps)