unity shader入门
2017-11-01 本文已影响0人
日常P
转自http://www.cnblogs.com/lixiang-share/p/5025662.html
游戏蛮牛课程地址 http://edu.manew.com/course/96
基本概念:
1.渲染管线:渲染管线也称为渲染流水线或像素流水线或像素管线,是显示芯片内部处理图形信号相互独立的的并行处理单元。在某种程度上可以把渲染管线比喻为工厂里面常见的各种生产流水线,工厂里的生产流水线是为了提高产品的生产能力和效率,而渲染管线则是提高显卡的工作能力和效率。
渲染管线.png 传统的shader.png unity中的shader.png2.shader与材质,贴图的关系
shader与材质,贴图的关系.png3.shader与渲染管线
- shader:是图形可编程方案的程序片段
- 渲染管线:是一种计算机从数据到最终图形成像的形象描述(类似汽车生产流水线)
- 材质是商品,shader是加工方法,贴图是原材料
4.三种shader的高级语言:HLSL,GLSL,CG
三种语言.png CG语言优势.png5.unity中使用ShaderLab语法编写:基本格式如下
ShaderLab的基本结构.png- Properties:属性
- SubShaders:编写的执行的shader方法,每次只能执行一组SubShader。如果第一个不适配,则往下执行第二个
-
FallBack:如果上述SubShaders都不能执行,则执行FallBack所指定的shader,往往是系统包含的绝大多数硬件都支持的shader,如下
unity自带shader.png
其中相当重要的:
- 法线贴图 Normal Mapped:法线贴图是可以应用到3D表面的特殊纹理,不同于以往的纹理只可以用于2D表面。作为凹凸纹理的扩展,它使每个平面的各像素拥有了高度值,包含了许多细节的表面信息,能够在平平无奇的物体外形上,创建出许多种特殊的立体视觉效果。
6.Fixed function shader
详情参照http://blog.csdn.net/lichaoguan/article/details/43229767
目前速度快,适用范围广的shader,功能固定
五大要点:
- properties
- material
- lighting
- setTexture
- pass
1.PASS
ShaderLab的pass通道基本语法.png2.重要的部分
Paste_Image.png关于SurfaceShader:
直接打开unity中默认生成的shader文件(5.0以后版本)可以得到以下代码:
Shader "Custom/sf" { //surface代码为自动生成固定格式的代码功能,它可以让我们省略从低级开始编写有些必须手写的固定代码段,
//从而简化过程,使用Cg可编写
Properties { //属性声明
_Color ("Color", Color) = (1,1,1,1) //颜色值,是一个4阶值
_MainTex ("Albedo (RGB)", 2D) = "white" {} //MainTex 为主纹理
_Glossiness ("Smoothness", Range(0,1)) = 0.5 //高光的光泽度
_Metallic ("Metallic", Range(0,1)) = 0.0 //金属光泽度,体现在被光直接照射的面镜面反射强,没被照射的面镜面反射弱
}
SubShader { //子shader,surface中不需要编写pass通道,它会自动生成
Tags { "RenderType"="Opaque" } //描述的是一个渲染类型,Opaque是不透明物体
LOD 200
CGPROGRAM //CG代码段,一直到ENDCG
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
//pragma为固定格式的代码,surface为类型,surf是函数名称对应下面的void surf
//standard是一个光照的方法(5.0之后版本),在UnityPBSlighting中可以找到定义
//fullforwardshadows是一个可选选项,具体查看unity手册 【writing surface shader】部分
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0 //硬件方面使用的方法
sampler2D _MainTex; //对应Properties中的_MainTex,Properties中的2D对应sampler2D
struct Input {
float2 uv_MainTex; //名字uv_的结构是必须的,代表的是纹理坐标,如果没有uv则有可能导致纹理显示失败
};
half _Glossiness; //分别对应Properties中的三个值,其中half对应的range(浮点值),fixed4对应color的四阶值
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_CBUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_CBUFFER_END
void surf (Input IN, inout SurfaceOutputStandard o) {
//surf函数用的void返回值,是因为inout中已经包含了返回值,inout可有in,out等选项
//Input IN对应之上的struct结构中对IN的定义
//SurfaceOutputStandard为unity5新增加的可用于(physically based lighing models),详见技术手册
// 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" //默认的fallback
}
7.Cg语言的基本格式:
image.png①.Cg语言中的profile:
image.png②Profile(fp20)中Cg语言与C不同的地方(Cg语言的特性):
image.pngimage.png
③Cg的标准函数库:
内容十分复杂,参考各类百科或者技术手册(类似于matlab的指令)