You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each shader has 1 or more subshaders. If you're deploying to multiple platforms it can be useful to add multiple subshaders; For example, you might want a subshader that is higher quality for PC/Desktop and lower quality but faster subshader for mobile.
115
+
Every shader has one or more subshaders. If you're deploying to multiple platforms it can be useful to add multiple subshaders; For example, you might want a subshader that is higher quality for PC/Desktop and lower quality but faster subshader for mobile.
Each subshader has atleast one pass, which is actually where the object get rendered. Some effects require having multiple passes, but we'll just focus on one for now.
130
+
Each subshader has atleast one pass, which is actually where the object gets rendered. Some effects require having multiple passes, but we'll just focus on one for now.
131
131
132
132
Within our pass, we have the actual rendering code block:
133
133
```
@@ -170,9 +170,9 @@ CGPROGRAM
170
170
}
171
171
ENDCG
172
172
```
173
-
Before we start shading, we need to setup some data structures and our two functions in a way that we can take Unity's given data and give it back to Unity. First, we'll include *UnityCG.inc*. This file includes a number of helper functions that we can use. If you want a full list of them, you can go [here.](https://docs.unity3d.com/Manual/SL-BuiltinFunctions.html)
173
+
Before we start shading, we need to setup some data structures and our two functions in a way that we can take Unity's given data and data back to Unity. First, we'll include *UnityCG.inc*. This file includes a number of helper functions that we can use. If you want a full list of them, you can go [here.](https://docs.unity3d.com/Manual/SL-BuiltinFunctions.html)
174
174
175
-
We'll also add a data structure called *appdata*, and modify our vertex function so that it takes in a appdata structure:
175
+
We'll also add a data structure called *appdata*, and modify our vertex function so that it takes in an appdata structure:
176
176
177
177
```
178
178
CGPROGRAM
@@ -340,7 +340,7 @@ Properties {
340
340
}
341
341
```
342
342
Here we're defining a colour for us to use, called *_Colour* and it will be shown as "Totally Rad Colour!" in the Unity inspector. We're also giving it a default value of white.
343
-
If you save and return to Unity now, when inspect the material, you should see this:
343
+
If you save and return to Unity now, when you inspect the material, you should see this:
Now in order to use the colours from the texture for our fragment function, we need to *sample* it as certain points. Thankfully, CG has a function that does this for us, called *tex2D*.
416
+
Now in order to use the colours from the texture for our fragment function, we need to *sample* it at certain points. Thankfully, CG has a function that does this for us, called *tex2D*.
417
417
418
418
```
419
419
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
420
420
return tex2D(_MainTexture, IN.uv);
421
421
}
422
422
```
423
-
tex2D takes in the texture (ie: sample2D) we want to sample, and the UV coordinate we want to sample with. In this case, we're providing it with out main texture and giving it the point on the model where we want to get the colour from, then returning that result as our final colour. Now, if you save and return back to Unity and inspect the material, we can select the bowl texture for our "Main Texture". You'll see the models update, and the bowl model in particular (the model the texture was made for) should look like a bowl of soup!
423
+
tex2D takes in the texture (ie: sample2D) we want to sample, and the UV coordinate we want to sample with. In this case, we're providing it with our main texture and giving it the point on the model where we want to get the colour from, then returning that result as our final colour. Now, if you save and return back to Unity and inspect the material, we can select the bowl texture for our "Main Texture". You'll see the models update, and the bowl model in particular (the model the texture was made for) should look like a bowl of soup!
Next, we'll talk about how control shaders with Unity scripts. For this example, we'll reuse the _Colour property we added before. First, lets set it as a colour tint for our shader by doing this in our fragment function:
We're just multiplying the output colour by our _Colour property to tint it. Here's what that looks like in the editor:
606
+
607
+

608
+
609
+
Alright, lets start scripting. We'll add a new script to all the objects and we'll call it *RainbowColour.cs*
610
+
611
+

612
+
613
+
In our script, we'll start by declaring two private variables for our Renderer and our Material:
614
+
615
+
```csharp
616
+
usingSystem.Collections;
617
+
usingSystem.Collections.Generic;
618
+
usingUnityEngine;
619
+
620
+
publicclassRainbowColour : MonoBehaviour {
621
+
622
+
Rendererrend;
623
+
Materialmaterial;
624
+
625
+
voidStart () {
626
+
627
+
}
628
+
629
+
voidUpdate () {
630
+
631
+
}
632
+
}
633
+
```
634
+
635
+
We'll also get references to them in our Start() function:
636
+
```csharp
637
+
voidStart () {
638
+
rend=GetComponent<Renderer>();
639
+
material=rend.material;
640
+
}
641
+
```
642
+
We will use Material.SetColor(...) to set the colour in our shader. This function's first argument is a string, which is the name of the property we want to set. The second argument is the colour we want to set the property to.
643
+
```csharp
644
+
voidStart () {
645
+
rend=GetComponent<Renderer>();
646
+
material=rend.material;
647
+
material.SetColor("_Colour", Color.magenta);
648
+
}
649
+
```
650
+
651
+
Now notice when we start our game, the tint colour changes to magenta!
652
+
653
+

654
+
655
+
There are many functions for getting and setting properties for materials from within scripts, and you can find all of them [here.](https://docs.unity3d.com/ScriptReference/Material.html)
0 commit comments