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 e75fa27

Browse files
author
Adam Leung
committed
Part 5 Complete
1 parent bf71207 commit e75fa27

File tree

9 files changed

+178
-14
lines changed

9 files changed

+178
-14
lines changed

‎Assets/Shaders/Tutorial_Shader.shader

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Shader "Unlit/Tutorial_Shader" {
22
Properties {
3-
3+
_Colour ("Colour", Color) = (1, 1, 1, 1)
4+
_MainTexture ("Main Texture", 2D) = "white" {}
45
}
56

67
SubShader {
@@ -9,7 +10,7 @@
910
#pragma vertex vertexFunction
1011
#pragma fragment fragmentFunction
1112

12-
#import "UnityCG.cginc"
13+
#include "UnityCG.cginc"
1314

1415
struct appdata {
1516
float4 vertex : POSITION;
@@ -21,14 +22,19 @@
2122
float2 uv : TEXCOORD0;
2223
};
2324

24-
v2f vertexFunction (appdata v) {
25-
v2f OUT;
25+
float4 _Colour;
26+
sampler2D _MainTexture;
2627

28+
v2f vertexFunction (appdata IN) {
29+
v2f OUT;
30+
OUT.position = UnityObjectToClipPos(IN.vertex);
31+
OUT.uv = IN.uv;
2732
return OUT;
2833
}
2934

3035
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
31-
36+
float4 textureColour = tex2D(_MainTexture, IN.uv);
37+
return textureColour;
3238
}
3339
ENDCG
3440
}

‎Assets/Textures/Bowl.png.meta

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Images/Shading_Basics_1.png

108 KB
Loading[フレーム]

‎Images/Shading_Basics_2.png

9.46 KB
Loading[フレーム]

‎Images/Shading_Basics_3.png

118 KB
Loading[フレーム]

‎Images/Shading_Basics_4.png

151 KB
Loading[フレーム]

‎Images/Shading_Basics_5.png

38.6 KB
Loading[フレーム]

‎Images/Shading_Basics_6.png

57.8 KB
Loading[フレーム]

‎README.md

Lines changed: 134 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Unity-Shader-Basics-Tutorial
22

3-
Welcome, this tutorial is supposed to be a gentle introduction into writing shaders for Unity. It assume you have some previous knowledge in working with Unity but have never touched shaders or materials.
3+
Welcome, this tutorial is supposed to be a gentle introduction into writing shaders for Unity. It assumes you have some previous knowledge in working with Unity but have never touched shaders or materials.
44

55
We'll be building up the shader in parts, stopping along the way to show what everything does.
66

@@ -179,13 +179,13 @@ CGPROGRAM
179179
#pragma vertex vertexFunction
180180
#pragma fragment fragmentFunction
181181
182-
#import "UnityCG.cginc"
182+
#include "UnityCG.cginc"
183183
184184
struct appdata {
185185
186186
};
187187
188-
void vertexFunction (appdata v) {
188+
void vertexFunction (appdata IN) {
189189
190190
}
191191
@@ -218,7 +218,7 @@ CGPROGRAM
218218
#pragma vertex vertexFunction
219219
#pragma fragment fragmentFunction
220220
221-
#import "UnityCG.cginc"
221+
#include "UnityCG.cginc"
222222
223223
struct appdata {
224224
float4 vertex : POSITION;
@@ -228,7 +228,7 @@ CGPROGRAM
228228
struct v2f {
229229
};
230230
231-
v2f vertexFunction (appdata v) {
231+
v2f vertexFunction (appdata IN) {
232232
v2f OUT;
233233
234234
return OUT;
@@ -277,7 +277,7 @@ Shader "Unlit/Tutorial_Shader" {
277277
#pragma vertex vertexFunction
278278
#pragma fragment fragmentFunction
279279
280-
#import "UnityCG.cginc"
280+
#include "UnityCG.cginc"
281281
282282
struct appdata {
283283
float4 vertex : POSITION;
@@ -289,7 +289,7 @@ Shader "Unlit/Tutorial_Shader" {
289289
float2 uv : TEXCOORD0;
290290
};
291291
292-
v2f vertexFunction (appdata v) {
292+
v2f vertexFunction (appdata IN) {
293293
v2f OUT;
294294
295295
return OUT;
@@ -304,4 +304,130 @@ Shader "Unlit/Tutorial_Shader" {
304304
}
305305
```
306306

307-
## Part 5: Shading basics
307+
## Part 5: Shading basics
308+
309+
First thing we'll do is get the correct positions of the vertices. We'll do this using a function called UnityObjectToClipPos() like so:
310+
```
311+
v2f vertexFunction (appdata IN) {
312+
v2f OUT;
313+
314+
OUT.position = UnityObjectToClipPos(IN.vertex);
315+
316+
return OUT;
317+
}
318+
```
319+
What this function does is take a vertex that is represented in local object space, and tranforms it into the rendering camera's clip space. Notice we're passing along the transformed point by setting OUT.position's value.
320+
Next, we'll give an output to our fragment function:
321+
```
322+
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
323+
return fixed4(0, 1, 0, 1); //(R, G, B, A)
324+
}
325+
```
326+
And now, the moment you've been waiting for! Save your shader and return to Unity and you'll see our beautiful green objects!
327+
328+
![Shading Basics 1](./Images/Shading_Basics_1.png)
329+
330+
Okay, this probably not that impressive to you, so lets keep building. How about, instead of returning a basic green colour, we edit our shader to return any colour we want? What we'll need to do to achieve this is start working with custom properties.
331+
332+
We can add properties we want to use by following this syntax:
333+
```
334+
name ("display name", type) = default value
335+
```
336+
So for example, we'll expose a colour value like so:
337+
```
338+
Properties {
339+
_Colour ("Totally Rad Colour!", Color) = (1, 1, 1, 1)
340+
}
341+
```
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:
344+
345+
![Shading Basics 2](./Images/Shading_Basics_2.png)
346+
347+
Before we can use this colour, we need to actually pass it into the CG code. Unity does this automatically by binding it by variable name like so:
348+
349+
```
350+
CGPROGRAM
351+
#pragma vertex vertexFunction
352+
#pragma fragment fragmentFunction
353+
354+
#include "UnityCG.cginc"
355+
356+
struct appdata {
357+
float4 vertex : POSITION;
358+
float2 uv : TEXCOORD0;
359+
};
360+
361+
struct v2f {
362+
float4 position : SV_POSITION;
363+
float2 uv : TEXCOORD0;
364+
};
365+
366+
//Get our properties into CG
367+
float4 _Colour;
368+
369+
v2f vertexFunction (appdata IN) {
370+
v2f OUT;
371+
OUT.position = UnityObjectToClipPos(IN.vertex);
372+
return OUT;
373+
}
374+
375+
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
376+
return fixed4(0, 1, 0, 1);
377+
}
378+
ENDCG
379+
```
380+
*I like to put properties after my structs to keep my code organized, but you can put it anywhere so long as its in the top scope of the CGPROGRAM*
381+
382+
We can now use our _Colour value in our fragment function. Instead of returning that green, lets just return whatever colour we want:
383+
```
384+
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
385+
return _Colour;
386+
}
387+
```
388+
And now, we can save and return to Unity. If you inspect the material and start changing our colour value, you should see all the colours of the objects change accordingly!
389+
390+
![Shading Basics 3](./Images/Shading_Basics_3.png)
391+
392+
Since we now know how to add properties, lets try adding a standard texture map. We'll need a new property for our texture:
393+
394+
```
395+
Properties {
396+
_Colour ("Colour", Color) = (1, 1, 1, 1)
397+
_MainTexture ("Main Texture", 2D) = "white" {}
398+
}
399+
```
400+
Notice how it's of type *2D* (2D Texture), and we're defaulting to a blank white texture. We've also need to get the property into CG to use it:
401+
402+
```
403+
float4 _Colour;
404+
sampler2D _MainTexture;
405+
```
406+
Then, we need to give our fragment function the UV coordinates from the model. We can do this by going back to our vertex function and passing them into the v2f struct we return like so:
407+
408+
```
409+
v2f vertexFunction (appdata IN) {
410+
v2f OUT;
411+
OUT.position = UnityObjectToClipPos(IN.vertex);
412+
OUT.uv = IN.uv;
413+
return OUT;
414+
}
415+
```
416+
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*.
417+
418+
```
419+
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
420+
return tex2D(_MainTexture, IN.uv);
421+
}
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!
424+
425+
![Shading Basics 4](./Images/Shading_Basics_4.png)
426+
427+
__Note: We can change how Textures in Unity are sampled by going back to the texture file and changing the filter mode in the inspector:__
428+
429+
![Shading Basics 5](./Images/Shading_Basics_5.png)
430+
![Shading Basics 6](./Images/Shading_Basics_6.png)
431+
432+
## Part 6: Playing With Shaders
433+

0 commit comments

Comments
(0)

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