原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0330/432.html
U3D Shader之surface shader解密
本篇文章App开发培训主要个引见关于surface shader的学问,surface shader是对vertex shader 和 fragment shader的更高一层的包装,不需求我们再去编写pass通道了,这里把在学习surface shader时的笔记分享出来,希望能帮到各位,假如有不对的中央欢送指正,代码是unity自带的surface shader的范本;
Shader "Custom/sf" {
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 {
//surface shader 中不需求编写pass通道,是对vertex shader和fragment shader的一种包装
Tags { "RenderType"="Opaque" } //tags 渲染类型的描绘,opaque表示不透明
LOD 200 //层级细节,后续再讲
CGPROGRAM //代表运用CG言语编写shader直到ENDCG完毕
// Physically based Standard lighting model, and enable shadows on all light types
//#pragma表示一个编译指令 ;surface表示该shader类型是surface shader ;
// surf表示一个函数称号,在下面的代码中编写这个函数;Standard 表示一种光照模型(函数),unity内有lightingStandard这个函数(5.0以前版本是lambert)
//fullforwardshadows是一种阴影类型,该类型支持一切的阴影类型在forward渲染途径下,shder阴影类型
//还有addshadow和tessellate类型
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
//常州网站开发培训这个指令代表运用3.0的光照,默许是2.0,越高光照效果越好
#pragma target 3.0
sampler2D _MainTex; //纹理单位sampler2D
//输入构造体,描绘uv纹理坐标 ,留意这个uv纹理变量必需是小写uv + sampler2D的变量名,uv不能少,unityshader官方如此表述
struct Input {
float2 uv_MainTex;
};
half _Glossiness; //对应于properties中的属性在函数体内的声明,留意变量类型关键字不一样了
half _Metallic; //2d--sampler2D; color--fixed4; Range--half
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_CBUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_CBUFFER_END
//inout该函数表示即时输入也是输出
void surf (Input IN, inout SurfaceOutputStandard o) { //surf函数两个参数,一个Input,SurfaceOutputStandard表示5.0以后版本中基于物理的光照模型的构造体
//之前常州企业培训是SurfaceOutput构造体(固定称号)
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;//tex2D是CG中的一个函数,主要用来运用纹理坐标从纹理中得到数据,
//然后赋值给SurfaceOutputStandard中的Albedo,Metallic,Smoothness,Alpha
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse" //上面的shader都不论用时,就默许运用diffuse shader渲染物体,扮演常州软件技术培训备胎的角色
}
上篇:上一篇:Spine动画导入COCOS2d和UNITY
下篇:下一篇:Unity渲染次第