
上QQ阅读APP看书,第一时间看更新
How to do it...
Open up the SimpleDiffuse shader you've created and make the following changes:
- In the Properties section, remove all of the variables except for _Color:
Properties
{
_Color ("Color", Color) = (1,1,1,1)
}
- From the SubShader{} section, remove the _MainTex, _Glossiness, and _Metallic variables. You should not remove the reference to uv_MainTex as Cg does not allow the Input struct to be empty. The value will simply be ignored.
- Also, remove the UNITY_INSTANCING_BUFFER_START/END macros and the comments used with them.
- Remove the content of the surf() function and replace it with the following:
void surf (Input IN, inout SurfaceOutputStandard o)
{
o.Albedo = _Color.rgb;
}
- Your shader should look as follows:
Shader "CookbookShaders/Chapter03/SimpleDiffuse" {
Properties
{
_Color ("Color", Color) = (1,1,1,1)
}
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
struct Input
{
float2 uv_MainTex;
};
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o)
{
o.Albedo = _Color.rgb;
}
ENDCG
}
FallBack "Diffuse"
}
The two lines below CGPROGRAM is actually one line and is cut off due to the size of the book.
- As this shader has been refitted with a Standard Shader, it will use physically-based rendering to simulate how light behaves on your models.
If you are trying to achieve a non-photorealistic look, you can change the first #pragma directive so that it uses Lambert rather than Standard. If you do so, you should also replace the SurfaceOutputStandard parameter of the surf function with SurfaceOutput. For more information on this and the other lighting models that Unity supports, Jordan Stevens put together a very nice article about it, which you can see here: http://www.jordanstevenstechart.com/lighting-models
- Save the shader and pe back into Unity. Using the same instructions as in the Creating a basic Standard Surface Shader recipe located in Chapter 2, Creating Your First Shader, create a new material called SimpleDiffuseMat and apply our newly created shader to it. Change the color to something different, such as red, by clicking on the window next to the Color property in the Inspector window while selected.
- Then, go into the Models folder of this book's example code and bring the bunny object into our scene by dragging and dropping it from the Project window into the Hierarchy window. From there, assign the SimpleDiffuseMat material to the object:

- You can double-click on an object in the Hierarchy tab in order to center the camera on the object that's been selected.