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 14ac1f5

Browse files
author
Adam Leung
committed
Part 7 Complete
1 parent b150a40 commit 14ac1f5

File tree

9 files changed

+170
-8
lines changed

9 files changed

+170
-8
lines changed

‎Assets/Scripts.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Scripts/RainbowColour.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class RainbowColour : MonoBehaviour {
6+
7+
Renderer rend;
8+
Material material;
9+
10+
// Use this for initialization
11+
void Start () {
12+
rend = GetComponent<Renderer>();
13+
material = rend.material;
14+
material.SetColor("_Colour", Color.magenta);
15+
}
16+
17+
// Update is called once per frame
18+
void Update () {
19+
20+
}
21+
}

‎Assets/Scripts/RainbowColour.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Shaders/Tutorial_Shader.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
float4 textureColour = tex2D(_MainTexture, IN.uv);
4545
float4 dissolveColour = tex2D(_DissolveTexture, IN.uv);
4646
clip(dissolveColour.rgb - _DissolveCutoff);
47-
return textureColour;
47+
return textureColour * _Colour;
4848
}
4949
ENDCG
5050
}

‎Images/Scripting_and_Shaders_1.gif

988 KB
Loading[フレーム]

‎Images/Scripting_and_Shaders_1.png

185 KB
Loading[フレーム]

‎Images/Scripting_and_Shaders_2.png

40.3 KB
Loading[フレーム]

‎Images/Scripting_and_Shaders_3.gif

891 KB
Loading[フレーム]

‎README.md

Lines changed: 127 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Shader "Unlit/Tutorial_Shader" {
112112
}
113113
```
114114

115-
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+
Every shader has one 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.
116116

117117
Then we have our pass:
118118
```
@@ -127,7 +127,7 @@ Shader "Unlit/Tutorial_Shader" {
127127
}
128128
}
129129
```
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.
130+
Each subshader has atleast one pass, which is actually where the object gets rendered. Some effects require having multiple passes, but we'll just focus on one for now.
131131

132132
Within our pass, we have the actual rendering code block:
133133
```
@@ -170,9 +170,9 @@ CGPROGRAM
170170
}
171171
ENDCG
172172
```
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)
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 data 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)
174174

175-
We'll also add a data structure called *appdata*, and modify our vertex function so that it takes in a appdata structure:
175+
We'll also add a data structure called *appdata*, and modify our vertex function so that it takes in an appdata structure:
176176

177177
```
178178
CGPROGRAM
@@ -340,7 +340,7 @@ Properties {
340340
}
341341
```
342342
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:
343+
If you save and return to Unity now, when you inspect the material, you should see this:
344344

345345
![Shading Basics 2](./Images/Shading_Basics_2.png)
346346

@@ -413,14 +413,14 @@ v2f vertexFunction (appdata IN) {
413413
return OUT;
414414
}
415415
```
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*.
416+
Now in order to use the colours from the texture for our fragment function, we need to *sample* it at certain points. Thankfully, CG has a function that does this for us, called *tex2D*.
417417

418418
```
419419
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
420420
return tex2D(_MainTexture, IN.uv);
421421
}
422422
```
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!
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 our 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!
424424

425425
![Shading Basics 4](./Images/Shading_Basics_4.png)
426426

@@ -533,3 +533,123 @@ Make sure "Animated Materials" is checked on in the scene view in order to previ
533533

534534
![Playing With Shaders 3](./Images/Playing_With_Shaders_3.gif)
535535

536+
Here's our final shader:
537+
538+
```
539+
Shader "Unlit/Tutorial_Shader" {
540+
Properties {
541+
_Colour ("Colour", Color) = (1, 1, 1, 1)
542+
_MainTexture ("Main Texture", 2D) = "white" {}
543+
_DissolveTexture ("Dissolve Texture", 2D) = "white" {}
544+
_DissolveCutoff ("Dissolve Cutoff", Range(0, 1)) = 1
545+
_ExtrudeAmount ("Extrue Amount", float) = 0
546+
}
547+
548+
SubShader {
549+
Pass {
550+
CGPROGRAM
551+
#pragma vertex vertexFunction
552+
#pragma fragment fragmentFunction
553+
554+
#include "UnityCG.cginc"
555+
556+
struct appdata {
557+
float4 vertex : POSITION;
558+
float2 uv : TEXCOORD0;
559+
float3 normal : NORMAL;
560+
};
561+
562+
struct v2f {
563+
float4 position : SV_POSITION;
564+
float2 uv : TEXCOORD0;
565+
};
566+
567+
float4 _Colour;
568+
sampler2D _MainTexture;
569+
sampler2D _DissolveTexture;
570+
float _DissolveCutoff;
571+
float _ExtrudeAmount;
572+
573+
v2f vertexFunction (appdata IN) {
574+
v2f OUT;
575+
IN.vertex.xyz += IN.normal.xyz * _ExtrudeAmount * sin(_Time.y);
576+
OUT.position = UnityObjectToClipPos(IN.vertex);
577+
OUT.uv = IN.uv;
578+
return OUT;
579+
}
580+
581+
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
582+
float4 textureColour = tex2D(_MainTexture, IN.uv);
583+
float4 dissolveColour = tex2D(_DissolveTexture, IN.uv);
584+
clip(dissolveColour.rgb - _DissolveCutoff);
585+
return textureColour;
586+
}
587+
ENDCG
588+
}
589+
}
590+
}
591+
```
592+
593+
## Part 7: Scripting and Shaders
594+
595+
Next, we'll talk about how control shaders with Unity scripts. For this example, we'll reuse the _Colour property we added before. First, lets set it as a colour tint for our shader by doing this in our fragment function:
596+
597+
```
598+
fixed4 fragmentFunction (v2f IN) : SV_TARGET {
599+
float4 textureColour = tex2D(_MainTexture, IN.uv);
600+
float4 dissolveColour = tex2D(_DissolveTexture, IN.uv);
601+
clip(dissolveColour.rgb - _DissolveCutoff);
602+
return textureColour * _Colour;
603+
}
604+
```
605+
We're just multiplying the output colour by our _Colour property to tint it. Here's what that looks like in the editor:
606+
607+
![Scripting and Shaders 1](./Images/Scripting_and_Shaders_1.png)
608+
609+
Alright, lets start scripting. We'll add a new script to all the objects and we'll call it *RainbowColour.cs*
610+
611+
![Scripting and Shaders 2](./Images/Scripting_and_Shaders_2.png)
612+
613+
In our script, we'll start by declaring two private variables for our Renderer and our Material:
614+
615+
```csharp
616+
using System.Collections;
617+
using System.Collections.Generic;
618+
using UnityEngine;
619+
620+
public class RainbowColour : MonoBehaviour {
621+
622+
Renderer rend;
623+
Material material;
624+
625+
void Start () {
626+
627+
}
628+
629+
void Update () {
630+
631+
}
632+
}
633+
```
634+
635+
We'll also get references to them in our Start() function:
636+
```csharp
637+
void Start () {
638+
rend = GetComponent<Renderer>();
639+
material = rend.material;
640+
}
641+
```
642+
We will use Material.SetColor(...) to set the colour in our shader. This function's first argument is a string, which is the name of the property we want to set. The second argument is the colour we want to set the property to.
643+
```csharp
644+
void Start () {
645+
rend = GetComponent<Renderer>();
646+
material = rend.material;
647+
material.SetColor("_Colour", Color.magenta);
648+
}
649+
```
650+
651+
Now notice when we start our game, the tint colour changes to magenta!
652+
653+
![Scripting and Shaders 3](./Images/Scripting_and_Shaders_3.gif)
654+
655+
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)

0 commit comments

Comments
(0)

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