shader开发_5.Surface shader官方例子(注释版本)

上一章unity 默认生成了一个 surface shader 这里来了解一下它

 1 Shader "Custom/myshader01" {          //shader的路径名称
 2     Properties {                   //资源属性代码块
 3         _Color ("Color", Color) = (1,1,1,1)  //定义一个纯白不透明
 4         _MainTex ("Albedo (RGB)", 2D) = "white" {} //2D贴图(可在面板进行赋值)
 5         _Glossiness ("Smoothness", Range(0,1)) = 0.5 //【光泽度】Range 表示一个范围 后面的=表示默认值
 6         _Metallic ("Metallic", Range(0,1)) = 0.0      //【金属强度】Range 表示一个范围 后面的=表示默认值
 7 
 8     }
 9     SubShader {                                       //SubShader的路径名称                                            
10         Tags { "RenderType"="Opaque" }//标签,通过设置的标签决定什么时候渲染(这里表明在渲染不透明物体时渲染)        
11                 LOD 200 //  与unity Quality Settings  关联 如果Quality Settings指定小于当前设定 这此shader不可用                                    
12         CGPROGRAM                          //CG语言标记开始
13         // Physically based Standard lighting model, and enable shadows on all light types
14         #pragma surface surf Standard fullforwardshadows  //编译指令  surf 自定义函数 关照模型 [阴影类型]
15 
16         // Use shader model 3.0 target, to get nicer looking lighting
17         #pragma target 3.0                                  //编译平台
18 
19         sampler2D _MainTex;     //如果你要在CG中使用Properties的定义 需要在这里声明                           
20 
21         struct Input {           //此结构提承载计算中的信息传递
22             float2 uv_MainTex; //纹理坐标 (其实是引用的 Properties的_MainTex 名字之所以不同,是因为纹理坐标必须要以 uv开头  )
23         };
24         //同理 这里是上面声明的定义
25         half _Glossiness;
26         half _Metallic;
27         fixed4 _Color;
28 
29         void surf (Input IN, inout SurfaceOutputStandard o) {  //
30              //inout 表示此参数既可以输入 又可以输出    
31             //inout SurfaceOutputStandard o  unity surface shader预定义的结构体
32             // Albedo comes from a texture tinted by color   
33             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; //根据定义的颜色获得一个新的颜色
34             o.Albedo = c.rgb;                                                                  //设置返回的颜色值
35             // Metallic and smoothness come from slider variables
36             o.Metallic = _Metallic;                                                          //返回的贴图
37             o.Smoothness = _Glossiness;                                             //返回的光泽度
38             o.Alpha = c.a;                                                                       //返回的透明信息
39         }
40         ENDCG                    //CG语言标记结束
41 
42     }
43     FallBack "Diffuse"
44 
45 } 

 

posted @ 2016-08-28 18:43  大.罗  阅读(1748)  评论(0编辑  收藏  举报