Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4498451

Browse files
author
Adam Leung
committed
Added comments
1 parent 5b1d54f commit 4498451

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

‎Assets/Scripts/RainbowColour.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
public class RainbowColour : MonoBehaviour {
66

7+
//References to render and material
78
Renderer rend;
89
Material material;
910

1011
// Use this for initialization
1112
void Start () {
13+
//Get references,
1214
rend = GetComponent<Renderer>();
1315
material = rend.material;
16+
17+
//Set shader value with name "_Colour" to Color.magenta
1418
material.SetColor("_Colour", Color.magenta);
1519
}
1620

‎Assets/Shaders/Tutorial_Shader.shader

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,78 @@
1-
Shader "Unlit/Tutorial_Shader" {
2-
Properties {
3-
_Colour ("Colour", Color) = (1, 1, 1, 1)
4-
_MainTexture ("Main Texture", 2D) = "white" {}
5-
_DissolveTexture ("Dissolve Texture", 2D) = "white" {}
6-
_DissolveCutoff ("Dissolve Cutoff", Range(0, 1)) = 1
7-
_ExtrudeAmount ("Extrue Amount", float) = 0
1+
Shader "Unlit/Tutorial_Shader" {//ShaderLab code
2+
Properties {//Properties/Custom data we want to use
3+
_Colour ("Colour", Color) = (1, 1, 1, 1)//Tint colour
4+
_MainTexture ("Main Texture", 2D) = "white" {}//Texture map
5+
_DissolveTexture ("Dissolve Texture", 2D) = "white" {}//Black and white dissolve texture
6+
_DissolveCutoff ("Dissolve Cutoff", Range(0, 1)) = 1//Dissolve threshold
7+
_ExtrudeAmount ("Extrue Amount", float) = 0//How far out to extrude the model
88
}
99

10-
SubShader {
11-
Pass {
12-
CGPROGRAM
10+
SubShader { //We can have multiple subshaders. Especially useful for multiple platforms/quality levels
11+
Pass { //Can have > 1 pass, each adding a draw call. Useful/Mandatory for many effects
12+
CGPROGRAM //Start of Cg code
13+
14+
//Define what our vertex and fragment shaders are called
1315
#pragma vertex vertexFunction
1416
#pragma fragment fragmentFunction
1517

18+
//Include Unity helper functions
1619
#include "UnityCG.cginc"
1720

21+
//Data we want to retrieve from our model
22+
//Such as: Vertices, normal, colour, UV coordinates, etc.
1823
struct appdata {
1924
float4 vertex : POSITION;
2025
float2 uv : TEXCOORD0;
2126
float3 normal : NORMAL;
2227
};
2328

29+
//The object we're building and passing to our fragment function
30+
//We're going to give the fragment function information about the model's...
2431
struct v2f {
2532
float4 position : SV_POSITION;
2633
float2 uv : TEXCOORD0;
2734
};
2835

36+
//Properties. Note the same names as above.
2937
float4 _Colour;
3038
sampler2D _MainTexture;
3139
sampler2D _DissolveTexture;
3240
float _DissolveCutoff;
3341
float _ExtrudeAmount;
3442

43+
//Vertex function, outputs data in v2f struct
3544
v2f vertexFunction (appdata IN) {
36-
v2f OUT;
45+
v2f OUT; //Create empty output
46+
47+
//Move the vertices of our object along their normals (facing directions)
48+
//Animate the distance over time, with magnitude _ExtrudeAmount
3749
IN.vertex.xyz += IN.normal.xyz * _ExtrudeAmount * sin(_Time.y);
50+
51+
//Return position that is translated from local object space to clip space
3852
OUT.position = UnityObjectToClipPos(IN.vertex);
53+
54+
//Simply carry over the same UV coordinates
3955
OUT.uv = IN.uv;
56+
4057
return OUT;
4158
}
4259

60+
//Fragment function, outputs (R, G, B, A) colours to render
4361
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
62+
//Sample main texture for colour
4463
float4 textureColour = tex2D(_MainTexture, IN.uv);
64+
65+
//Sample dissolve texture for dissolve colour,
4566
float4 dissolveColour = tex2D(_DissolveTexture, IN.uv);
67+
68+
//Check if colour in dissolve texture is over a certain threshold
69+
//If not, (ie: negative value) then discard the pixel
4670
clip(dissolveColour.rgb - _DissolveCutoff);
71+
72+
//Otherwise, if we get here just return tinted colour from our main texture
4773
return textureColour * _Colour;
4874
}
49-
ENDCG
50-
}
51-
}
52-
}
75+
ENDCG//End of Cg code
76+
}//End of pass
77+
}//End of SubShader
78+
}//End of Shader!

‎README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ Shader "Unlit/Tutorial_Shader" {
589589
}
590590
}
591591
```
592+
*(If you want to see a commented version, go [here.](./Assets/Shaders/Tutorial_Shader.shader))*
592593

593594
## Part 7: Scripting and Shaders
594595

@@ -652,6 +653,8 @@ Now notice when we start our game, the tint colour changes to magenta!
652653

653654
![Scripting and Shaders 3](./Images/Scripting_and_Shaders_3.gif)
654655

656+
*(If you want to see a commented version of the script, go [here.](./Assets/Scripts/RainbowColour.cs))*
657+
655658
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)
656659

657660
## Part 8: Shadows? Surface Shaders?

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /