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 001c62f

Browse files
author
Adam Leung
committed
Extended Part 4
1 parent 701c49d commit 001c62f

File tree

2 files changed

+133
-23
lines changed

2 files changed

+133
-23
lines changed

‎Assets/Shaders/Tutorial_Shader.shader

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
}
1515

16-
void fragmentFunction () {
17-
16+
void fragmentFunction (){
1817
}
1918
ENDCG
2019
}

‎README.md

Lines changed: 132 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ 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. 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"__
6466

6567
To start we'll add this code:
6668

@@ -102,7 +104,6 @@ Underneath our properties block we'll have our subshader:
102104
```
103105
Shader "Unlit/Tutorial_Shader" {
104106
Properties {
105-
...
106107
}
107108
108109
SubShader {
@@ -117,7 +118,6 @@ Then we have our pass:
117118
```
118119
Shader "Unlit/Tutorial_Shader" {
119120
Properties {
120-
...
121121
}
122122
123123
SubShader {
@@ -133,7 +133,6 @@ Within our pass, we have the actual rendering code block:
133133
```
134134
Shader "Unlit/Tutorial_Shader" {
135135
Properties {
136-
...
137136
}
138137
139138
SubShader {
@@ -149,28 +148,127 @@ Anything within CGPROGRAM and ENDCG is where we actually write our shading code.
149148

150149
Next, we'll tell Unity what our vertex and fragment functions are:
151150
```
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+
155166
}
156167
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+
164194
}
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+
165255
}
166256
```
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;
168258

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:
170268
```
171269
Shader "Unlit/Tutorial_Shader" {
172270
Properties {
173-
...
271+
174272
}
175273
176274
SubShader {
@@ -179,18 +277,31 @@ Shader "Unlit/Tutorial_Shader" {
179277
#pragma vertex vertexFunction
180278
#pragma fragment fragmentFunction
181279
182-
void vertexFunction () {
280+
#import "UnityCG.cginc"
281+
282+
struct appdata {
283+
float4 vertex : POSITION;
284+
float2 uv : TEXCOORD0;
285+
};
286+
287+
struct v2f {
288+
float4 position : SV_POSITION;
289+
float2 uv : TEXCOORD0;
290+
};
291+
292+
v2f vertexFunction (appdata v) {
293+
v2f OUT;
183294
295+
return OUT;
184296
}
185297
186-
void fragmentFunction () {
298+
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
187299
188300
}
189301
ENDCG
190302
}
191303
}
192304
}
193305
```
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.
195306

196-
## Part 5: Basic Shading
307+
## Part 5: Skeleton of a Unlit Shader

0 commit comments

Comments
(0)

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