- Unity 2018 Shaders and Effects Cookbook
- John P. Doran Alan Zucconi
- 666字
- 2025-04-04 16:34:19
How to do it...
With our scene generated, we can move on to the shader writing steps:
- In the Project tab in your Unity editor, right-click on the Chapter 2 folder and select Create | Folder.
- Rename the folder that you created as Shaders by right-clicking on it and selecting Rename from the drop-down list, or by selecting the folder and hitting F2 on the keyboard.
- Create another folder and rename it as Materials.
- Right-click on the Shaders folder and select Create | Shader | Standard Surface Shader. Then, right-click on the Materials folder and select Create | Material.
- Rename both the shader and material as StandardDiffuse.
- Launch the StandardDiffuse shader by double-clicking on the file. This will automatically launch a scripting editor for you and display the shader's code.
You will see that Unity has already populated our shader with some basic code. This, by default, will get you a basic shader that accepts one texture in the Albedo (RGB) property. We will be modifying this base code so that you can learn how to quickly start developing your own custom shaders.
- Now let's give our shader a custom folder from which it's selected. The very first line of code in the shader is the custom description that we have to give the shader so that Unity can make it available in the shader drop-down list when assigning to materials. We have renamed our path as Shader "CookbookShaders/StandardDiffuse", but you can name it whatever you want and rename it at any time so don't worry about any dependencies at this point. Save the shader in your script editor and return to the Unity editor. Unity will automatically compile the shader when it recognizes that the file has been updated. This is what your shader should look like at this point:
Shader "CookbookShaders/StandardDiffuse" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
- Technically speaking, this is a Surface Shader based on physically-based rendering (PBR). As the name suggests, this type of shader achieves realism by simulating how light physically behaves when hitting objects.
- After your shader is created, we need to connect it to a material. Select the material called StandardDiffuse that we created in step 4 and look at the Inspector tab. From the Shader drop-down list, select CookbookShaders | StandardDiffuse. (Your shader path might be different if you chose to use a different path name.) This will assign your shader to your material and make it ready for you to assign to an object.
To assign material to an object, you can simply click and drag your material from the Project tab to the object in your scene. You can also drag material to the Inspector tab of an object in the Unity editor in order to assign it.
The screenshot of an example is as follows:

Not much to look at at this point, but our shader development environment is set up and we can now start to modify the shader to suit our needs.