- Unity 5.x Shaders and Effects Cookbook
- Alan Zucconi Kenneth Lammers
- 601字
- 2021-07-16 12:59:36
Diffuse shading
Before starting our journey into texture mapping, it is important to understand how diffuse materials work. Certain objects might have a uniform color and smooth surface, but not smooth enough to shine on reflected light. These matte materials are best represented with a Diffuse shader. While in the real world, pure diffuse materials do not exist; Diffuse shaders are relatively cheap to implement and find a large application in games with low-poly aesthetics.
Getting ready
There are several ways in which you can create your own Diffuse shader. A quick way is to start with the Standard Shader in Unity 5 and edit it to remove any texture, similarly to what was previously done in Chapter 1, Creating Your First Shader.
How to do it...
Let's start with our Standard Shader, and apply the following changes:
- Remove all the properties except
_Color
:_Color ("Color", Color) = (1,1,1,1)
- From the
SubShader{}
section, remove the_MainTex
,_Glossiness
, and_Metallic
variables. You should not remove the reference touv_MainTex
as Cg does not allow theInput
struct to be empty. The value will be simply ignored. - Remove the content of the
surf()
function and replace it with the following:o.Albedo = _Color.rgb;
- Your shader should look as follows:
Shader "CookbookShaders/Diffuse" { Properties { _Color ("Color", Color) = (1,1,1,1) } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Standard fullforwardshadows #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" }
As this shader has been refitted from 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 SurfaceOutputStandard
with SurfaceOutput
.
How it works...
The way shaders allow you to communicate the rendering properties of your material to their lighting model is via a surface output. It is basically a wrapper around all the parameters that the current lighting model needs. It should not surprise you that different lighting models have different surface output structs. The following table shows the three main output structs used in Unity 5 and how they can be used:
The SurfaceOutput
struct has the following properties:
fixed3 Albedo;
: This is the diffuse color of the materialfixed3 Normal;
: This is the tangent space normal, if writtenfixed3 Emission;
: This is the color of the light emitted by the material (this property is declared ashalf3
in the Standard Shaders)fixed Alpha;
: This is the transparency of the materialhalf Specular;
: This is the specular power from0
to1
fixed Gloss;
: This is the specular intensity
The SurfaceOutputStandard
struct has the following properties:
fixed3 Albedo;
: This is the base color of the material (whether it's diffuse or specular)fixed3 Normal;
half3 Emission;
: This property is declared ashalf3
, while it was defined asfixed3
inSurfaceOutput
fixed Alpha;
half Occlusion;
: This is the occlusion (default1
)half Smoothness;
: This is the smoothness (0
= rough,1
= smooth)half Metallic;
:0
= non-metal,1
= metal
The SurfaceOutputStandardSpecular
struct has the following properties:
fixed3 Albedo;
.fixed3 Normal;
.half3 Emission;
.fixed Alpha;
.half Occlusion;
.half Smoothness;
.fixed3 Specular;
: This is the specular color. This is very different from theSpecular
property inSurfaceOutput
as it allows specifying a color rather than a single value.
Using a Surface Shader correctly is a matter of initializing the surface output with the correct values.
- JavaScript修煉之道
- 新一代通用視頻編碼H.266/VVC:原理、標(biāo)準(zhǔn)與實(shí)現(xiàn)
- Clojure for Domain:specific Languages
- Python計(jì)算機(jī)視覺編程
- Bootstrap Essentials
- Python High Performance Programming
- HTML+CSS+JavaScript網(wǎng)頁(yè)設(shè)計(jì)從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開發(fā)視頻大講堂)
- Learning Node.js for .NET Developers
- Emgu CV Essentials
- .NET Standard 2.0 Cookbook
- 智能手機(jī)故障檢測(cè)與維修從入門到精通
- Python 3 數(shù)據(jù)分析與機(jī)器學(xué)習(xí)實(shí)戰(zhàn)
- Java7程序設(shè)計(jì)入門經(jīng)典
- Maven for Eclipse
- Python編程入門(第3版)