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
Copy file name to clipboardExpand all lines: README.md
+102Lines changed: 102 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -431,3 +431,105 @@ __Note: We can change how Textures in Unity are sampled by going back to the tex
431
431
432
432
## Part 6: Playing With Shaders
433
433
434
+
So now that we know the basics, we can start having some fun with shaders and achieve some simple effects. First, we're going to use our noise texture and achieve a sort of "dissolve" or "cutout" effect. We'll start by adding another texture property and a float property:
Notice how we've set _DissolveCutoff to be a Range from (0, 1). This represents a float value from 0 to 1 (inclusive) and this notation also allows us to easily set it's value using a slider. Now let's add them to our CGPROGRAM:
444
+
```
445
+
float4 _Colour;
446
+
sampler2D _MainTexture;
447
+
sampler2D _DissolveTexture;
448
+
float _DissolveCutoff;
449
+
```
450
+
Now we can sample the dissolve texture in our fragment function:
The *clip* function works by checking if the value given is less than 0. If it is, then we discard the pixel and draw nothing. If it isn't we keep the pixel and continue as normal. The way our code currently works is:
468
+
469
+
1. We sample the main texture for colour.
470
+
2. We sample the cutout texture for it's colour.
471
+
3. We subtract the cutoff value from the "brightness" of our cutoff sample, and...
472
+
4. If it's less than 0, we draw nothing
473
+
5. Otherwise, return the main texture sample colour.
474
+
475
+
Now, save your shader and return to Unity. Set the "Dissolve Texture" to our noise texture, and start moving the "Dissolve Cutoff" slider, you should see an effect like this:
476
+
477
+

478
+
479
+
Pretty cool huh? We can do more too. Let's try playing with the vertices before we pass them to our fragment function. Let's expose another property:
We're also going to using normals from the model, so lets add the field into the appdata struct so we can access them:
499
+
```
500
+
struct appdata {
501
+
float4 vertex : POSITION;
502
+
float2 uv : TEXCOORD0;
503
+
float3 normal : NORMAL;
504
+
};
505
+
```
506
+
Now let's add a single line to our vertex function:
507
+
```
508
+
v2f vertexFunction (appdata IN) {
509
+
v2f OUT;
510
+
IN.vertex.xyz += IN.normal.xyz * _ExtrudeAmount;
511
+
OUT.position = UnityObjectToClipPos(IN.vertex);
512
+
OUT.uv = IN.uv;
513
+
return OUT;
514
+
}
515
+
```
516
+
What we're doing here is, before we transform our vertices out of local model space, we're going to offset them a certain amount outwards by adding their normal direction times our _ExtrudeAmount. A normal is just a vector that represents the direction that the vertex is facing. Now if you save and return to Unity and play with the "Extrude Amount" value, you should see an effect like this:
517
+
518
+

0 commit comments