Uploaded July 2019 | Updated November 2026, 3 hours ago
Lets explore how to build diffuse lighting onto our meshes. This is intended as somewhat of an introduction to some of the magic behind shaders.
There are two types of shaders you will typically create a in Unity. The first is the Standard [`Surface Shader`](docs.unity3d.com/Manual/SL-SurfaceShaders.html). These are very Unity specific and include a lot of custom code to integrate cleanly into Unity's lighting systems. Because of this, a `Surface Shader` doesn't require you to create any custom lighting and will automatically function and look natural inside of most environments. It will receive lights, cast shadows and more.
That isn't the case for the `Unlit Shader` which removes the Unity specific `surface` function for more traditional `vertex` and `fragment` ones which align very closely with how a shader might work in OpenGL or other engines (though not necessarily identically!). Like the name suggests an `Unlit Shader` has no lighting effects, the default shader will just draw a texture onto a mesh and be as true to color as possible for that texture.
Just because the shader doesn't include things like lighting doesn't mean we can't add our own though. First though, we should take a look at vertex and fragment shader functions.
- `#pragma vertex vert`: will declare a function for a vertex shader named `vert`. Vertex shaders jobs are to transform a mesh into a camera based position so that it can be drawn onto the screen. This is done using a series of matrix multiplications. Luckily that work is already done inside of Unity's templates with the code `o.vertex = UnityObjectToClipPos(v.vertex)`. That code accepts an input vertex which will be a point relative to the original position of the object (in our example we use 3 boxes, the *input* of the 3 boxes is the same vertices but the *output* is modified because while the cubes are identical they're in different positions). Vertex shaders can store other information than the position of a vertex such as the color or normal direction of the mesh and we can introduce extra information we may need here as well.
- `#pragma fragment frag` will declare a fragment shader with the name `frag`. These are passed the output information from the vertex shader and are responsible for actually putting color on the screen. They're called `fragments` and not `pixels` here because it's actually possible for multiple `fragments` to be drawn in a single pixel for a variety of reasons (meshes overlapping one another for example). Inside the `fragment shader` the values from the vertex shader are not necessarily received unmodified. Your graphics cards will do some fancy interpolation for you between these two steps. If you think of the vertices in our mesh like numbers on a number line you might end up with a line that has 0 and 1 on it. The fragment shader represents the entire line including 0 and 1, but also all the numbers in-between. In the cases of those intermediate numbers we don't usually want duplicates of 0 and 1 but the unique value at that position in the number line. Fragment shaders will do this calculation for us and interpolate colors, vectors and more for us and pass those interpolated values into the input of the fragment shader.
Using this information we can introduce a custom lighting effect by passing the normal value of each vertex into our shader. This will give us a direction facing away from the face of the mesh (the normal of the floor - as an example - is straight up). Using a `dot product` we can compare the result of that normal value to a global light source to determine how strongly the light is impacting that point on the mesh. A very quick sample of what the code to do this might look like is this:
```shader
fixed3 lightDirection = fixed3(0, -1, 0); // Create a light source from directly overhead
// A max function is used here to prevent negative values during our math.
fixed lightIntensity = max(-1 * dot(lightDirection, input.worldNormal), 0);
```
Code for the full shader will be released in the next few days.
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
Lets explore how to build diffuse lighting onto our meshes. This is intended as somewhat of an introduction to some of the magic behind shaders.
There are two types of shaders you will typically create a in Unity. The first is the Standard [`Surface Shader`](docs.unity3d.com/Manual/SL-SurfaceShaders.html). These are very Unity specific and include a lot of custom code to integrate cleanly into Unity's lighting systems. Because of this, a `Surface Shader` doesn't require you to create any custom lighting and will automatically function and look natural inside of most environments. It will receive lights, cast shadows and more.
That isn't the case for the `Unlit Shader` which removes the Unity specific `surface` function for more traditional `vertex` and `fragment` ones which align very closely with how a shader might work in OpenGL or other engines (though not necessarily identically!). Like the name suggests an `Unlit Shader` has no lighting effects, the default shader will just draw a texture onto a mesh and be as true to color as possible for that texture.
Just because the shader doesn't include things like lighting doesn't mean we can't add our own though. First though, we should take a look at vertex and fragment shader functions.
- `#pragma vertex vert`: will declare a function for a vertex shader named `vert`. Vertex shaders jobs are to transform a mesh into a camera based position so that it can be drawn onto the screen. This is done using a series of matrix multiplications. Luckily that work is already done inside of Unity's templates with the code `o.vertex = UnityObjectToClipPos(v.vertex)`. That code accepts an input vertex which will be a point relative to the original position of the object (in our example we use 3 boxes, the *input* of the 3 boxes is the same vertices but the *output* is modified because while the cubes are identical they're in different positions). Vertex shaders can store other information than the position of a vertex such as the color or normal direction of the mesh and we can introduce extra information we may need here as well.
- `#pragma fragment frag` will declare a fragment shader with the name `frag`. These are passed the output information from the vertex shader and are responsible for actually putting color on the screen. They're called `fragments` and not `pixels` here because it's actually possible for multiple `fragments` to be drawn in a single pixel for a variety of reasons (meshes overlapping one another for example). Inside the `fragment shader` the values from the vertex shader are not necessarily received unmodified. Your graphics cards will do some fancy interpolation for you between these two steps. If you think of the vertices in our mesh like numbers on a number line you might end up with a line that has 0 and 1 on it. The fragment shader represents the entire line including 0 and 1, but also all the numbers in-between. In the cases of those intermediate numbers we don't usually want duplicates of 0 and 1 but the unique value at that position in the number line. Fragment shaders will do this calculation for us and interpolate colors, vectors and more for us and pass those interpolated values into the input of the fragment shader.
Using this information we can introduce a custom lighting effect by passing the normal value of each vertex into our shader. This will give us a direction facing away from the face of the mesh (the normal of the floor - as an example - is straight up). Using a `dot product` we can compare the result of that normal value to a global light source to determine how strongly the light is impacting that point on the mesh. A very quick sample of what the code to do this might look like is this:
```shader
fixed3 lightDirection = fixed3(0, -1, 0); // Create a light source from directly overhead
// A max function is used here to prevent negative values during our math.
fixed lightIntensity = max(-1 * dot(lightDirection, input.worldNormal), 0);
```
Code for the full shader will be released in the next few days.
Join the World of Zero Discord Server: discord.gg/hU5Kq2u



![Solving FizzBuzz with C#s Pattern Matching Switch Statements
Pattern Matching was introduced into C# a few years ago and Ive not really been able to integrate it into my projects. I wanted to explore the feature a bit more by solving Fizz Buzz. I figured we could learn about it together. 😃
FizzBuzz is a common interview question that covers order of operations, conditionals and loops in the language of your choice. In this video, well be replacing the more commonly used `if` statements with a pattern matching switch expression.
The rules of FizzBuzz are:
Iterate over a series of integers.
* If the number is divisible by 3 print Fizz
* If the number is divisible by 5 print Buzz
* If the number is divisible by 3 **and** 5 print FizzBuzz
* Otherwise print the number to the console
For example if we provide 9 we should print Fizz because 9 can be evenly divided by 3 with no remainder. If we provide 10 we should print Buzz because 10 is divisible by 5. If we provide 15 we should print FizzBuzz because it is divisible by both 3 and 5.
This is the code from the video (The greater-than and less-than symbols may need to be replaced. YouTube does not support them natively.)
```
class Program
{
static void Main(string[] args)
{
for(int i = 0; i Ë‚ 20; ++i) {
Console.WriteLine(GetOutputString(i));
}
}
static string GetOutputString(int i) {
return (i%3, i%5) switch
{
(0, 0) =˃ FizzBuzz,
(0, _) =˃ Fizz,
(_, 0) =˃ Buzz,
_ =˃ i.ToString()
};
}
}
```
Here are some additional examples and tutorials on using pattern-matching in C#: https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/pattern-matching
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Solving FizzBuzz with C#s Pattern Matching Switch Statements](https://i.ytimg.com/vi/68ko-chyM2E/mqdefault.jpg)
. Some of these may not be helpful if your doing local development with an existing remote repo somewhere like GitHub or GitLab.
Files you change in the `.git` directory will not be checked into your git repository and therefore will not be visible to the rest of your team. If youd like to share your pre-commit and other hooks with your team you can place the scripts somewhere inside your project and create a symlink to them in the `.git/hooks` directory.
Note: make sure that you make your Git Hooks executable or git will not be able to access and run them!
If youd like to be able to edit `.git` files more easily inside of Visual Studio Code youll need to disable the automatic file exclusion. You can do this by updating your `settings.json` file by adding the following setting:
```json
files.exclude: {
**/.git: false
},
```
This setting prevents all `.git` directories in your project - regardless of where they are located - from being hidden.
You can also bypass Git Hooks using the ` no-verify` (shorthand `-n`) argument when running git commands in your CLI. This will completely bypass these hooks and prevent them from running. If you have a Git Hook that requires tests to pass before pushing running `git push no-verify` would allow you to push your changes for example.
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Using Git Hooks](https://i.ytimg.com/vi/6MzXs35hLOI/mqdefault.jpg)


![Sending Real-Time Messages to Unity from a Blazor Website - SignalR in Unity - Part 3
Lets complete our SignalR in Unity experiment by adding a Blazor Website to it. This will allow us to use a browser to send messages directly to Unity to create updates instead of either needing to install a Unity game or run a console app. Well quickly cover some of the components in a Blazor website as well as how some of the architecture works.
Well take a quick detour to chat about Blazor Web Assembly (Blazor WASM) vs Blazor Server because I completely forgot what project template I chose when setting up this test project. 😅
The architecture of our app looks like this:
```mermaid
graph LR
A[Browser App] ˃ B[Blazor Server]
B ˃A
B ˃ C{ASP.NET SignalR Server}
C ˃|Sends Updates to| D[Unity]
E[Console App] ˃ C
```
You can create a new Blazor project using either:
### Blazor Web Assembly
```sh
dotnet new blazorwasm
```
or
### Blazor Server
```sh
dotnet new blazorserver
```
If you want to learn more about Blazor you can get started here: https://aka.ms/getstartedwithblazor
***
If you would like to get started learning SignalR you can learn more at https://aka.ms/getstartedsignalr
My previous SignalR series shared at the [Microsoft Reactor](https://www.youtube.com/c/MicrosoftReactor1/) where we created an interactive canvas you could collaboratively draw on with the rest of the world!
1. [Building Real-Time Web Apps with SignalR](https://www.youtube.com/watch?v=k5rdhYj6TB8)
2. [Connecting a Web App to SignalR](https://www.youtube.com/watch?v=c0zmYjQ8KWA)
3. [Creating a Real-Time Web Canvas](https://www.youtube.com/watch?v=hgq1CYEJg_Q)
You can see some of the other content I have planned outside this channel in the Microsoft Reactor (and even come to our in-person events) at the [Microsoft Reactor San Francisco Meetup Page](https://www.meetup.com/Microsoft-Reactor-San-Francisco/).
***
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Sending Real-Time Messages to Unity from a Blazor Website - SignalR in Unity - Part 3](https://i.ytimg.com/vi/6yCrnWcKvpU/mqdefault.jpg)


