官术网_书友最值得收藏!

How to do it...

With our scene generated, we can move on to the shader writing steps:

  1. In the Project tab in your Unity editor, right-click on the Chapter 2 folder and select Create | Folder.
  2. 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.
  1. Create another folder and rename it as Materials.
  2. Right-click on the Shaders folder and select Create | Shader | Standard Surface Shader. Then, right-click on the Materials folder and select Create | Material.
  3. Rename both the shader and material as StandardDiffuse.
  4. 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.

  1. 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"
}
  1. 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.
  2. 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.

主站蜘蛛池模板: 仁布县| 璧山县| 莲花县| 芦溪县| 凯里市| 沛县| 普宁市| 马关县| 青海省| 宁晋县| 平泉县| 翁牛特旗| 扬中市| 九江市| 灌阳县| 西林县| 唐河县| 婺源县| 石门县| 攀枝花市| 庄浪县| 乐都县| 黄龙县| 如皋市| 平顺县| 沙河市| 陕西省| 东阿县| 荆州市| 博罗县| 垣曲县| 兴文县| 和龙市| 隆尧县| 兴山县| 财经| 南华县| 丰县| 万全县| 平舆县| 富平县|