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 701c49d

Browse files
author
Adam Leung
committed
Part 4 complete
1 parent e6ed99f commit 701c49d

File tree

4 files changed

+146
-58
lines changed

4 files changed

+146
-58
lines changed

‎Assets/Shaders/Tutorial_Shader.shader

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,22 @@
1-
Shader "Unlit/Tutorial_Shader"
2-
{
3-
Properties
4-
{
5-
_MainTex ("Texture", 2D) = "white" {}
1+
Shader "Unlit/Tutorial_Shader" {
2+
Properties {
3+
64
}
7-
SubShader
8-
{
9-
Tags { "RenderType"="Opaque" }
10-
LOD 100
115

12-
Pass
13-
{
6+
SubShader {
7+
Pass{
148
CGPROGRAM
15-
#pragma vertex vert
16-
#pragma fragment frag
17-
// make fog work
18-
#pragma multi_compile_fog
19-
20-
#include "UnityCG.cginc"
9+
#pragma vertex vertexFunction
10+
#pragma fragment fragmentFunction
2111

22-
struct appdata
23-
{
24-
float4 vertex : POSITION;
25-
float2 uv : TEXCOORD0;
26-
};
12+
void vertexFunction () {
2713

28-
struct v2f
29-
{
30-
float2 uv : TEXCOORD0;
31-
UNITY_FOG_COORDS(1)
32-
float4 vertex : SV_POSITION;
33-
};
14+
}
3415

35-
sampler2D _MainTex;
36-
float4 _MainTex_ST;
37-
38-
v2f vert (appdata v)
39-
{
40-
v2f o;
41-
o.vertex = UnityObjectToClipPos(v.vertex);
42-
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
43-
UNITY_TRANSFER_FOG(o,o.vertex);
44-
return o;
45-
}
46-
47-
fixed4 frag (v2f i) : SV_Target
48-
{
49-
// sample the texture
50-
fixed4 col = tex2D(_MainTex, i.uv);
51-
// apply fog
52-
UNITY_APPLY_FOG(i.fogCoord, col);
53-
return col;
54-
}
16+
void fragmentFunction () {
17+
18+
}
5519
ENDCG
5620
}
5721
}
58-
}
22+
}

‎Images/Skeleton_1.png

69.5 KB
Loading[フレーム]

‎Images/Skeleton_2.png

136 KB
Loading[フレーム]

‎README.md

Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ For our purposes, we'll simplify the rendering pipeline. Here's a image showing
2020

2121
I like to think of shaders as programs that transform one type of information (model data, colours, etc.) to another type of information (pixels/fragments). Object data is data that is inherit to the object. Things such as points in the model, normals, triangles, UV coordinates, etc. Custom Data/Properties are things that we can pass into a shader to use. Things such as colours, textures, numbers, etc.
2222

23-
The first step of the shader pipeline is the vertex function. Vertexes, as you might know, are just points. The vertex function will work with the vertices in the model and prepare them for the next step, the fragment function.
23+
The first step of the shader pipeline is the vertex function. Vertices, as you might know, are just points. The vertex function will work with the vertices in the model (Along with other data such as normals) and prepare them for the next step, the fragment function.
2424

2525
The fragment function will take in vertices and shade them in. Think of it like a painter and their paint brush. It ultimately outputs pixel data, in a (R, G, B, A) format.
2626

@@ -30,11 +30,11 @@ Lastly, the pixels are pushed to a frame buffer, where they may be manipulated f
3030

3131
So before we start writing some shader code, let's setup our scene. Create a new project in Unity, and import all the assets:
3232

33-
[Bowl Model](./Assets/Models/Bowl.blend)
33+
*[Bowl model](./Assets/Models/Bowl.blend)
3434

35-
[Noise texture](./Assets/Textures/Noise.png)
35+
*[Noise texture](./Assets/Textures/Noise.png)
3636

37-
[Bowl texture](./Assets/Textures/Bowl.png)
37+
*[Bowl texture](./Assets/Textures/Bowl.png)
3838

3939
Add a cube, a sphere, and the bowl model to a new scene and save the scene. Here's what your scene should look like after:
4040

@@ -48,7 +48,7 @@ Next, right click in the Project view (Or go to Create) and add a new Unlit Shad
4848

4949
Then, right click the shader file we just made and go to Create > Material. Unity will automatically create a material that uses that shader with the correct name.
5050

51-
__Note: a "Material" in Unity is just a *instance* of a shader. It just hold onto the custom data/properties.__
51+
__Note: a "Material" in Unity is just a *instance* of a shader. It just saves the values of the custom data/properties.__
5252

5353
![Setup 3](./Images/Setup_3.png)
5454

@@ -60,13 +60,137 @@ Everything in the scene should look white and without shadows or shading, like t
6060

6161
## Part 4: Skeleton of a Unlit Shader
6262

63-
Time to start writing our shader! Let's open our Tutorial_Shader.shader file we create before. You'll see Unity automatically generates some code for us to use/build off of. For the sake of this tutorial, delete all of this and make the .shader file blank.
63+
Time to start writing our shader! Let's open our Tutorial_Shader.shader file we create before. You'll see Unity automatically generates some code for us to use/build off of. For the sake of this tutorial, delete all of this and make the .shader file blank. All shaders in Unity are written in language called "ShaderLab".
6464

6565
To start we'll add this code:
6666

67+
```
68+
Shader "Unlit/Tutorial_Shader" {
69+
...
70+
}
71+
```
72+
These lines of code just specify where the shader code is. The string in quotes after the *Shader* keyword specify to Unity where you'll find the shader.
73+
74+
For example:
6775
```hlsl
68-
Shader "Unlit/Tutorial_Shader"
69-
{
76+
Shader "A/B/C/D/E_Shader" {
77+
...
78+
}
79+
```
80+
![Skeleton 1](./Images/Skeleton_1.png)
81+
82+
If you save your shader and switch back to Unity, you'll notice all our objects now are pink:
83+
84+
![Skeleton 2](./Images/Skeleton_2.png)
85+
86+
This is a fallback shader that Unity will use whenever your shader has errors in it. If you ever get pink objects, you can click on your shader file in the project window and look at the inspector to see the corresponding errors. For now, we'll have pink objects because we haven't completed our shader.
87+
88+
Next up is the properties block:
89+
90+
```
91+
Shader "Unlit/Tutorial_Shader" {
92+
Properties {
93+
...
94+
}
95+
}
96+
```
97+
98+
The properties block is where we can pass in that custom data we were walking about before. Anything we declare here will be shown in the Unity editor for us to change and be exposed to scripting aswell.
99+
100+
Underneath our properties block we'll have our subshader:
101+
102+
```
103+
Shader "Unlit/Tutorial_Shader" {
104+
Properties {
105+
...
106+
}
70107
108+
SubShader {
109+
...
110+
}
71111
}
72-
```
112+
```
113+
114+
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+
116+
Then we have our pass:
117+
```
118+
Shader "Unlit/Tutorial_Shader" {
119+
Properties {
120+
...
121+
}
122+
123+
SubShader {
124+
Pass {
125+
...
126+
}
127+
}
128+
}
129+
```
130+
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.
131+
132+
Within our pass, we have the actual rendering code block:
133+
```
134+
Shader "Unlit/Tutorial_Shader" {
135+
Properties {
136+
...
137+
}
138+
139+
SubShader {
140+
Pass {
141+
CGPROGRAM
142+
...
143+
ENDCG
144+
}
145+
}
146+
}
147+
```
148+
Anything within CGPROGRAM and ENDCG is where we actually write our shading code. For Unity this is a variant of HLSL and CG shading languages.
149+
150+
Next, we'll tell Unity what our vertex and fragment functions are:
151+
```
152+
Shader "Unlit/Tutorial_Shader" {
153+
Properties {
154+
...
155+
}
156+
157+
SubShader {
158+
Pass {
159+
CGPROGRAM
160+
#pragma vertex vertexFunction
161+
#pragma fragment fragmentFunction
162+
ENDCG
163+
}
164+
}
165+
}
166+
```
167+
Here, we're saying we have a vertex function called "vertexFunction", and a fragment function called "fragmentFunction"".
168+
169+
We'll define those functions aswell:
170+
```
171+
Shader "Unlit/Tutorial_Shader" {
172+
Properties {
173+
...
174+
}
175+
176+
SubShader {
177+
Pass {
178+
CGPROGRAM
179+
#pragma vertex vertexFunction
180+
#pragma fragment fragmentFunction
181+
182+
void vertexFunction () {
183+
184+
}
185+
186+
void fragmentFunction () {
187+
188+
}
189+
ENDCG
190+
}
191+
}
192+
}
193+
```
194+
If you save your shader now and return to Unity, your objects will disappear! That's because this is bare skeleton for a unlit shader, but we're not returning anything for Unity to actually render.
195+
196+
## Part 5: Basic Shading

0 commit comments

Comments
(0)

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