Mixing GUILayout and GUI and making them coexist together (Useful when editor scripting)

January 20 | 15

Warning:

Even tho Unity 4.6 has come out with the new Unity GUI, you still need sometimes to use the old legacy GUI and GUILayout API specially if you are working with editor scripting.

Being said that, DONT use GUI/GUILayout functions for GUI on your projects (except editor scripting) if you are using Unity 4.6+; using GUI/GUILayout  in your actual game is slow and consumes lots of draw calls!.

 

So now that everything is clear here’s a little bit o what happened:

I am programming a new tool for the asset store and I was needing to display some textures in the inspector view (after all the GUI was written using GUILayout…), but Unfortunately “EditorGUI.DrawPreviewTexture()” doesnt have a layouted version :/ and I didnt wanted to do the whole same thing again using plain GUI. so I came up with an interesting idea that I wanted to share on making GUI and GUILayout coexist in the same GUI :).

So I’m going to expose having GUILayout code and inserting GUI code. and making them both coexist in the same Repaint().

 

Mixing  GUILayout with GUI

this happens when we have lots of GUILayout calls same as when we are building our window with pure GUILayout calls and there are some functions that are available only for GUI.

In my case I was wanting to create a tool for displaying the materials’ textures of any given object and some small info:

So the tool is organized and also looks like this:

ProMaterialCombiner

 

 

 

 

 

 

 

 

 

 

 

and it even maintains its proportions! :D

Resizeable

 

 

 

 

 

 

 

 

 

 

 

So how do I do it?, its actually pretty simple, basically what I create is an area (with GUILayout) and inside the area I create a ScrollView (also with GUILayout) and then in the inside we put all our GUI (relative to the Area we created and not to the window) and we fill everything with GUILayout.Spaces(px)!

 

Sounds complex? its not at all!, trust me :),  let me show you a really simple code:

Basically what we need to do is to create an empty area from the GUILayout space and fill it with “spaces” that are consistent with what we draw on the GUI space. Let me draw it for you:

CombinedGUILayoutAndGUI

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Mixing GUI with GUILayout.

Mixing GUI with GUILayout is basically the same process you just have to take into account the space consumed by the GUILayout and then just offset the other GUI objects so they can coexist.

I’ll leave this part to the reader tho.

 

Like this post?, feel free to subscribe to my posts, I’ll keep you posted on anything that I write :).

Any comments/suggestions/feedback let me know :).

 

 

Reducing Draw Calls (also named SetPass calls) on Unity5.

January 10 | 15

So I have been working on the standard shader support for Unity 5 and doing some tests regarding how feasible is to reduce draw calls with the new standard shader and actually the results where pretty positive!.

Lets talk about the standard shader a little bit. The standard shader is a physically based shader that is consistent across diverse lighting conditions, its multi-platform and texture slots enable different shader features.

Here’s a small introductory video on what can be achieved with this one new shader.

 

 

This standard shader “mutates” and becomes more faster/slower depending on the textures you are using on the texture slots. being said that these two shaders are different (even if they are using the standard shader):

StandardShaders

So these two shaders (even if they are “Standard”); they are totally different shaders when rendering a scene, hence they have to be handled totally different if we want to reduce some draw calls (Now called SetPass calls).

Before Talking about how to reduce draw calls in Unity 5 I will talk about how draw calls are reduced regardless the shader is being used and then I will talk about how to reduce draw calls with the Standard shader in Unity5.

 

Reducing Draw Calls in Unity The Manual Way.

So, if we want to reduce draw calls in Unity or in any other engine the way to do it is using as less materials as possible.

Basically it all gets reduced to these single steps:

 

  1. Sort all the materials and gather them by the type of shader they are using.
  2. With the materials that share the same shader gather their textures in a bigger texture (atlas texture).
  3. Create one material that will contain the shader and the atlas texture(s) created on step 2.
  4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.
  5. Assign to your remapped meshes the material we created on step 3.

 

 1. Sort all the materials and gather them by the type of shader.

I’m going to use for easiness of explanation purposes one simple scene that will have all the meshes use the same type of materials but different textures.

In our case we are going to use this scene (which contains 4 materials each of them with a Bumped Diffuse shader):

0SampleScene4Materials

 

 

 

 

 

 

 

 

 

 

 

Notice the SetPass calls: 4

So we have  for this scene these materials to process:

SceneMaterials

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. With the materials that share the same shader gather their textures in a bigger texture (Atlas texture).

In our case we have 2 textures per shader as the Bumped Diffuse shader uses a normal map and a base texture, so what we need to do is to have 2 textures like this:

AtlassedTextures

 

 

 

 

 

 

 

 

Save these textures in the project view, we are going to need them later on:

ProjectView

 

 

 

 

 

 

3. Create one material that will contain the shader and the atlas texture(s).

Create a material and assign (on this case) the two textures to the material with the shader used (in our case “Bumped Diffuse”):

3Material

 

 

 

 

 

 

 

 

4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.

This modification can be done on any 3D editor (Maya/ 3D studio / Max / Blender / etc) of your choice.

This is the most boring/demanding step as it involves you modifying your UV coords of your meshes and make them match the  texture’s atlas pixels (the texture we created on step 2).

So what we do is that to each of our UV coords located all around [0,0 – 1,1] we Re-position them on a smaller sub-set of [0,0-1,1] that matches the pixels on the atlas texture we created on step 2.

4UVREMAP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And… we do this for each of our meshes that share the same shader.

 

 

 5. Assign to the remapped meshes the material created on step 3.

We are almost there! still we have a couple of more steps to do:

 

  1. Select all your UV-remapped-meshes and place them where the old meshes where located and deactivate the old meshes.
  2. Assign the created material on step 3 to all your UV-remapped-meshes.

5ObjectsSameMAt

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And by here, we are finished!, now lets hit play:

5OptimizedScene

 

 

 

 

 

 

 

 

 

 

 

 

Before Optimizing if you go back to Step 1, we had 4 SetPass calls and after all the optimization has been done we end up with 1 SetPass calls!. which is amazing! its 75% draw call reduce on this case.

 

Reducing Draw Calls in Unity 5 with the Standard shader.

By here you should know more or less how the optimization process is done with the standard shader. as I wrote on the beginning,  the standard shader internally “mutates” and becomes more faster/slower depending on the textures you are using on the shader’s texture slots (and also renders differently depending on the textures that are used). being said that we have to gather our object’s materials if they are using the Standard shader by the textures used. i.e: Gather all the meshes that only have colors; Gather all the meshes that only have an Albedo texture.. and so on.

After gathering our objects by the textures used we just need to follow the same steps we use for any other shader:

 

  1. Sort all the materials and gather them by the type of shader they are using (In the standard shader case gather them by the textures used).
  2. With the materials that share the same “shader” gather their textures in a bigger texture (atlas texture).
  3. Create one material that will contain the shader and the atlas texture(s) created on step 2.
  4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.
  5. Assign to your remapped meshes the material we created on step 3.

Here’s a small scene I created for testing purposes:

StandardShaderScene

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If you compare the original draw calls (91) and the optimized scene’s draw calls (22) you can see that there’s ~75% draw call decrease!. Which is amazing!

Also, if you can try to combine meshes as much as possible, this also helps a lot!.

Reducing Draw Calls on Unity Automatically

Knowing that this is a really cumbersome process I decided to create a tool that automatically gathers your objects and sort them by the type of shaders they use and automagically “Bakes” all your unoptimized scenes!, you can find the package here, and if your scene only uses Diffuse and Bumped Diffuse shaders please be my guest and use it for free!

 

 

 

Like this post?, feel free to subscribe to my posts, I’ll keep you posted on anything that I write :).

Any comments/suggestions/feedback let me know :).

 

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:

TexturePacking

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.

BeforeAfter

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.

Space Invaders in SMFL and C++

March 13 | 14

I was planning to write this over the last year but never happened, now that I have some free time I’ve put this post together and here it goes.

I like to work on stuff different from Unity from time to time, and I was feeling rusty on my C++ skills so I needed a small project which I could finish over a week on my free time to study some C++. After thinking what to do I decided I wanted to do a game but wanted to use something different from SDL because I already have worked with it before so after googling on different frameworks I found SMFL (which I loved it for its easiness) and decided to give it a try.

The game I ended up doing was a space invaders clone. This is an OSX project developed in XCode. but it should be easy to port it to windows and linux. it should be a matter of just setting up the IDE and SMFL in your platform.

I also published the code I made with an MIT License, so you can do whatever you want with it. If you want to check/use the code, feel free to fork it from here

Here’s how it looks:
)