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
@@ -60,7 +60,9 @@ Everything in the scene should look white and without shadows or shading, like t
60
60
61
61
## Part 4: Skeleton of a Unlit Shader
62
62
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".
63
+
Time to start writing our shader! Let's open our Tutorial_Shader.shader file we created 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.
64
+
65
+
__Note: All shaders in Unity are written in language called "ShaderLab"__
@@ -133,7 +133,6 @@ Within our pass, we have the actual rendering code block:
133
133
```
134
134
Shader "Unlit/Tutorial_Shader" {
135
135
Properties {
136
-
...
137
136
}
138
137
139
138
SubShader {
@@ -149,28 +148,127 @@ Anything within CGPROGRAM and ENDCG is where we actually write our shading code.
149
148
150
149
Next, we'll tell Unity what our vertex and fragment functions are:
151
150
```
152
-
Shader "Unlit/Tutorial_Shader" {
153
-
Properties {
154
-
...
151
+
CGPROGRAM
152
+
#pragma vertex vertexFunction
153
+
#pragma fragment fragmentFunction
154
+
ENDCG
155
+
```
156
+
Here, we're saying we have a vertex function called "vertexFunction", and a fragment function called "fragmentFunction"".
157
+
158
+
We'll define those functions aswell:
159
+
```
160
+
CGPROGRAM
161
+
#pragma vertex vertexFunction
162
+
#pragma fragment fragmentFunction
163
+
164
+
void vertexFunction () {
165
+
155
166
}
156
167
157
-
SubShader {
158
-
Pass {
159
-
CGPROGRAM
160
-
#pragma vertex vertexFunction
161
-
#pragma fragment fragmentFunction
162
-
ENDCG
163
-
}
168
+
void fragmentFunction () {
169
+
170
+
}
171
+
ENDCG
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)
174
+
175
+
We'll also add a data structure called *appdata*, and modify our vertex function so that it takes in a appdata structure:
176
+
177
+
```
178
+
CGPROGRAM
179
+
#pragma vertex vertexFunction
180
+
#pragma fragment fragmentFunction
181
+
182
+
#import "UnityCG.cginc"
183
+
184
+
struct appdata {
185
+
186
+
};
187
+
188
+
void vertexFunction (appdata v) {
189
+
190
+
}
191
+
192
+
void fragmentFunction () {
193
+
164
194
}
195
+
ENDCG
196
+
```
197
+
When we give Unity an argument to call the vertex function with, it will look into the structure of that argument (in this case, our *appdata* structure) and attempt to pass in values to it based on the model that is being drawn. We can define data that we want Unity to pass in by declaring variables like this:
198
+
199
+
```
200
+
[type] [name] : [semantic];
201
+
```
202
+
So for example, we can ask Unity for the positions of the vertices of this model like this:
203
+
```
204
+
float4 vertex : POSITION;
205
+
```
206
+
For now we'll ask Unity to give us the position of the vertices and the coordinates of the UV like so:
207
+
```
208
+
struct appdata {
209
+
float4 vertex : POSITION;
210
+
float2 uv : TEXCOORD0;
211
+
};
212
+
```
213
+
If you want to learn more about providing vertex data to vertex functions, you can read [here.](https://docs.unity3d.com/Manual/SL-VertexProgramInputs.html)
214
+
215
+
Lastly for the vertex function setup, we'll create one more struct called *v2f* (which stands for vertex to fragment) that will contain the data we'll be passing into our fragment function. We'll also make sure our vertex function returns data of this struct and create and return a blank one while we're at it:
216
+
```
217
+
CGPROGRAM
218
+
#pragma vertex vertexFunction
219
+
#pragma fragment fragmentFunction
220
+
221
+
#import "UnityCG.cginc"
222
+
223
+
struct appdata {
224
+
float4 vertex : POSITION;
225
+
float2 uv : TEXCOORD0;
226
+
};
227
+
228
+
struct v2f {
229
+
};
230
+
231
+
v2f vertexFunction (appdata v) {
232
+
v2f OUT;
233
+
234
+
return OUT;
235
+
}
236
+
237
+
void fragmentFunction () {
238
+
239
+
}
240
+
ENDCG
241
+
```
242
+
Just like before we can define some data in v2f that we want to pass from our vertex function to our fragment function.
243
+
```
244
+
struct v2f {
245
+
float4 position : SV_POSITION;
246
+
float2 uv : TEXCOORD0;
247
+
};
248
+
```
249
+
*If you're curious about SV_POSITION vs POSITION, SV stands for "system value" and represents in our v2f struct that this will be the final transformed vertex position use for rendering.*
250
+
251
+
Okay we're almost ready, we just need to edit our fragment function. First, we'll modify it to take in the v2f struct and make it return a fixed4 value:
252
+
```
253
+
fixed4 fragmentFunction (v2f IN) {
254
+
165
255
}
166
256
```
167
-
Here, we're saying we have a vertex function called "vertexFunction", and a fragment function called "fragmentFunction"".
257
+
Our output for the fragment function will be a colour represented by (R, G, B, A) values;
168
258
169
-
We'll define those functions aswell:
259
+
Lastly, we're going to add an output semantic SV_TARGET to our fragment function like so:
260
+
```
261
+
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
262
+
263
+
}
264
+
```
265
+
This tells Unity that we're outputting a fixed4 colour to be rendered.
266
+
We're now ready to start actually coding the meat and potatoes of our vertex and fragment functions!
267
+
Here's our basic skeleton that we've made up to this point:
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.
0 commit comments