Archive for September, 2014

Light, easy and fast Field of View 2D

September 11 | 14

So I was working on a personal project that requires some AI to have a field of view to check if the player has been seen or not by the AI.

I searched on the asset store and to my surprise there is nothing like this implemented nor in the internet, so I decided to quickly implement a fast, light and small version of a small field of view for 2D.

My implementation doesn’t use physics at all, without further ado, here’s a quick explanation of the component; to use it just drop it on a GameObject and it will run automagically.

Inspector vars
Radius: Radius of the field of view, how far it can “see”.
FOV (Field of view): Angle in degrees how wide the field of view will be, by default 90 degrees
Direction: Defines the direction where the field of view will be pointing at, this vector internally gets normalized.
Test Point: This is a transform reference to test the field of view when hitting play.

And this is how the field of view will be seen in the scene view; the green line shows the direction and the yellow lines determine the FOV edges.

FOV Scene view


Finally the source code can be found here, feel free to use it in your commercial projects and also include it in your Asset store packages if needed, still you don’t have permission to sell this script alone in the Asset store.


Lets talk about draw calls.

September 2 | 14

I’ve been thinking on making a post about draw calls but havent had the time / will to do it, so lets just get over with and start :).

Lets first define what is a draw call.
A draw call is a command to the graphics card that tells the GPU to render a certain set of vertices as triangles with a certain state (shaders, blend states, etc).

Now a frame is built of draw calls, that means the lesser draw calls you have the faster a frame finishes rendering; hence higher frames per second.

So just check this video to understand better how a single frame gets rendered in the C4 engine:

So, after watching this your might ask: how can we reduce the number of draw calls in our project?.
Well its fairly easy, try to compress as many objects/parts of a model into the less number of textures as possible.

This means that if you have lets say 5 objects with 5 materials that use a metal shader, just remap the UVs of the objects to a single texture, pack the textures into a single texture and use just 1 material for the 5 objects.

Something like this:


By doing this in Unity you can reduce up to a third the number of draw calls that you currently have in your project without losing any quality on your work.


Its important to keep draw calls as minimum as possible, this is because you can render your frames faster and also can use some rendering time in other tasks like AI.

If you are in need to reduce the number of draw calls in your project and you don’t have the time / knowledge to re-map all the UVs of your meshes you can use this tool I wrote for free to reduce your draw calls in Unity from here.

Finally if you are still interested and want to get more deep on this topic, you should checkout the nVidia presentation Batch Batch Batch!, a bit old but covers the draw call performance topic.