Skip to main content


Showing posts from January, 2018

How To Move An Object Through A Set Of Points [The Correct Way]

Move An Object Through A Set Of Points
In order to move an objects through various points, we need a way to access those points. So for accessing them we will use an array of transforms.  We will be using a Co-routine to update the object's position and to help it "Find Da Way". I'm pretty sure by the time you read this that the meme is long dead.
While going through each point by lerping through each point may seem like a viable option but it leads to results like this: This type of movement where it takes the same amount of time to cover the same distance may not be the type of movement you are looking for. First we will see how to get to this point then learn about the better way of moving stuff. private IEnumerator MoveToSeat(Transform[] path) { float t = 0; Vector2 startPostion; float distance; for (int i = 0; i < path.Length; ++i) { startPostion = transform.position; t = 0; while (t…

Making Objects Move With Sines & Cosines

Sines & Cosines Can Do Magic If you haven't realized the capability of sines and cosines in making objects move in game then you are in for a treat.
In order to make the objects move you just have to set the x position & y position respectively. Here are some of the things we will be making:
First we just make the ball travel in a circle.
The 'itrValue' is some value that keep changing every frame like Time.timeSinceLevelLoad  
Position in x-axis : Sin(itrValue) * radius
Position in y-axis : Cos(itrValue) * radius Now we will make it tilt towards a side:
Position in x-axis : Sin(itrValue) * radius
Position in y-axis : Cos(itrValue + offset) * radius We will now make it move in a flower petal path:
Position in x-axis : Sin(itrValue) * (radius + Cos(itrValue* frequency)
Position in y-axis : Cos(itrValue) * (radius + Cos(itrValue* frequency)
Now a random-esque path, useful when object need to move in a way that looks semi-random like the movement of a swarm of mosquitoes:

Noise Generation Algorithms : Voronoi Part 2

If you haven't gone through Part 1, do so now or perish.😁.
Now we will go over making a more cooler version of what we previously did.
This is what we will end up with:
 In this part we will go over the actual 'Noise' part of it.
For that we need a pseudo random number generator, We already made one in a previous tutorial on making White Noise with shaders.
But for this case we need to make 2D noise rather than 1D noise like we did before.
So we will go over our random function:
float2 random2(float2 p) { return frac(sin(float2(dot(p,float2(117.12,341.7)),dot(p,float2(269.5,123.3))))*43458.5453); } If you have gone the previous pseudo-random number generator that was made.. nothing really complicated is happening here.
We are still using the 'frac' function to give us the fractional part of a a number made with the 'dot' function which contains a float2 and some arbitrary numbers as parameters.
Now the fragment shader where all the magic happens:
fixed4 f…

Noise Generation Algorithms : Voronoi - Part 1

What is Voronoi ? You may have heard of Voronoi noise if you have worked with software like blender or substance painter. It's really good way of making surfaces that look like they have sharp creases enclosing a smooth surface. Usually used to make hardened lava flows, parched ground as well as more organic looking structures like skin and animal hide, living cells.
In this part we will see how to create a Voronoi pattern in Unity and how to set your own points in the shader itself to create the Voronoi pattern.
We will go over the 'Noise' part of Voronoi noise in Part 2.
The basic element that the Voronoi pattern depends is the 'Distance Field' function.
Take the case where there are 'N' points and we have to find the distance field of those points,
We have to calculate the distance between each pixel and the point closest to it.
This is what we will end up having in this tutorial:
We will be creating a script that passes a set of Vector2s to a shader wh…

C# Fundamentals - Funcs & Actions

C# Fundamentals - Funcs & Actions Before continuing on Actions & Funcs, you should have an understanding of delegates in C#.
A tutorial on delegates can be found HERE.
This is going to be a short and sweet run down of what they are.
Actions Actions are delegates which have a return type of void. The parameters of an Action is set by using the type parameters.
[<modifiers>] Action<[Param1 type],[Param2 type],..[ParamN type]> <Name>
using System; namespace BitShiftProductions { public class Program { public Action<string, int> ActionDelegate; public static void CallMethod(string text, int digit) { Console.WriteLine ("Called Method with action : {0} {1}", text, digit); } public static void Main(string[] args) { Program p = new Program(); p.ActionDelegate = CallMethod; p.ActionDelegate.Invoke("Hello", 44); } …

Normal Based Texture Shader For Unity

Normal Based Texturing This shader uses the normals of the mesh to determine what texture is to be applied.
We will get to know how to access normal data in the shader as well as how to apply a texture based on it.
This is what we will end up with after getting normal data :
Now let's look the structs needed :
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; }; struct v2f { float2 uv : TEXCOORD0; fixed4 norm : COLOR; float4 vertex : SV_POSITION; }; As you can see I added an appdata member called float3 normal of type 'NORMAL', this will be used in the vertex shader to then later on pass to the fragment shader.
In the v2f struct I added a member called fixed4 norm of type COLOR, this will be used in fragment shader to do the actual work.
Let's see what the vertex shader is doing now:
v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); = v.normal; o.norm.w = 1.0; o.uv = TRANSFORM_TEX(v.uv…

Unity Gizmos - What They Are & Why They're Useful

Unity Gizmos Many Unity developers when they are starting out or for that matter even after they are proficient in Unity, they may never come across / never use it.
Gizmos are really powerful debugging tools that are provided in Unity and they run in the scene view of the editor. Iteration times (implementing something & testing ) can really be improved a lot because you able to interact with the scene in way that is custom fit to your needs and requirements.
I usually use Gizmos to make a prototype before I make a prototype, which I'm sure a lot of you do.
As Gizmos runs in the editor scene view and because it comes featured with a lot of debugging tools right out of the box, I don't have to implement my own.
To make custom Gizmos you have to write it in the 'OnDrawGizmos' or 'OnDrawGizmosSelected' functions. These are just like the Start() & Update() functions that are there.
The OnDrawGizmos function is called every frame in the editor whereas the On…

C# Fundamentals - Events

Events To understand what Events are you have to know what Delegates are and how they function ( that was actually a pun - learn and come back ), A brief tutorial on Delegates can be found HERE.
Events can be stuff like key presses, player deaths, achievement unlocks, clicks,  mouse movements, etc..., or something like system generated notifications like window resize or minimize.
Events are just basically delegates with some extra protection stuff thrown in to prevent accidents.
We will go over these 'accidents' later on in this tutorial.
Events unlike delegates can not be declared outside the class body  as it is an instance of a delegate and instances can not exist outside a class body. These events are associated with event handlers by using delegates which may exist in the same class or some other class.
The class which contains the event notifies all other class about whether the event occurred.
This class is called the 'Publisher' class. Those classes that acc…

Noise Generation Algorithms : White Noise With Shader Example

White Noise Many of you may know what white noise is and how it can be used in code.
But there are a spectrum of  'Coloured Noises' as well these include ones like brown noise, pink, blue and violet noise all these have slightly different properties and can be very useful in creating different effects. More details on coloured noises can be found HERE.
A lot of movies as well as video games like City Skylines use white noise to add a feeling of realism and texture to the on screen content, it's also referred to as a grain filter or overlay. It is a really subtle effect and should not be over used.
Creating white noise boils down to the random function used.
While writing C++ code or C# code we have access to good pseudo-random functions from their standard libraries... but while writing code in shaders we usually have to create our own pseudo-random number generator.
One really good way of making one is by using the frac( in Unity & HLSL ) or fract( in GLSL ) functio…

C# Fundamentals - Delegates

DelegatesDelegate means 'a representative', just as it's meaning it's implementation is the same as well. You have a delegate representing a function of yours.
A delegate is a wrapper around a pointer to a function ( you might be familiar with function pointers in C++ ). These delegates are also type safe and also have some features that make it way more useful than just a function pointer which we will be going over.
To use delegates first we need to declare one, It's syntax basically looks like this : 
[<modifiers>] delegate [return type] <Name>([Parameter list])
public delegate int MyDelegate(string str);Delegates can be defined directly under a namespace or under a class.
Delegates can point to either static or instance methods.
To use a delegate we have to first instantiate a delegate.To Instantiate a delegate we have to pass a method as parameter or directly assign.
MyDelegate myDelegate = new MyDelegate(method); // As parameterMyDelegate myDelegate …

How To Animate A Fish Swimming With Shaders

Animate Fish Swimming With Shaders We are going to make swimming animation by using only shader code.
By the time we are done, it's going to look like this.
You will probably need the fish model used in this tutorial, that can be found HERE. Can use your own model but the shader code might have to be modified accordingly because of the orientation of the model that you might be using ( issues with whether the X axis & Z axis is flipped ).
The shader used way out performs a similar scene with skeletal animations applied on the fish models.
On a previous benchmark I did comparing the shader animation with the skeletal animation there was a difference of 28 FPS( on average ) with 50 fish.
The shader we are going to make is really powerful and flexible and don't think that it's limited to making fishes swim😀.

So this mesh oriented like this when imported into unity and this is important to understand because this means that the model's vertices have to be moved along the X-…

C# Fundamentals - Generics

C# Fundamentals - Generics Generic classes and methods provide us mainly three things :
1) Reusability
2) Type safety (certain value will not show undefined behavior)
3) Efficiency
It is similar to templates in C++ but it does not provide the same flexibility as that of templates.
Generics allow you to write a class or method that can work with any data type. When the compiler encounters a constructor for the class or a function call for the method, it generates code to match with the specified data type.
Let's look at a generic class called 'Node'.
public class Node<T> { private Node<T> next; private T data; public Node(T t) { data = t; } public T GetData(){ return data; } public Node<T> GetNext(){ return next; } public void SetNext(Node<T> node){ next = node; } } We will now see what all of that means.
public class Node<T> Where T is a placeholder for any data type (int,float,string,user-defined...etc).
It's a g…

Making An Audio Visualizer In Unity With Blend Shapes & Audio API - Part 2

There is a Part 1, check that out before going through this one.
So now we will look at Unity's Audio API, not the entire thing just the part that allows us to do stuff like this.
This is a representation of the audio data based on it's frequency and the how loud (amplitude) that particular part of the frequency spectrum is.
Let's start coding the thing.
We will make class called AudioVisualizer.
Member variables:
[Range(1.0f,4500.0f)] public float multiplier; public int minRange = 0; public int maxRange = 64; private SkinnedMeshRenderer skinnedMeshRenderer; private float prevAvg = 0.0f; Nothing special here, we will get to know what these are used for as we go on.
Start Function:
void Start () { skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer> (); } Update Function:
void Update () { float[] spectrum = new float[64]; AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris); if (maxRange < minRange)…

Assets Worth Checking Out