- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
 
- 纳金币
- 53488
- 精华
- 316
|
(材质选择Custorm就可以指定对应的shader)
基本结构
//shader定义名称的时候是可以定义级联的,这样在为材质选择的时候就在关联的面板中了
shader "Custorm/name"{
[Properties]
SubShaders
[FallBack]
}
一、例子-在unity3d新建一个shader,打开后可以看到
//属性面板
Properties{
_Color {"Color", Color} = {1,1,1,1};// _Color为变量属性,显示在编辑栏中的的是"Color",类型是Color(不区分大小写),默认值是{1,1,1,1}
}
//渲染片段,最少要有一个,有多个的时候只能执行一个,因为计算机硬件会从上到下自行选择可以执行的SubShaders
SubShaders{
// - surface shader or
// - vertex and program shader or
// - fixed function shader
}
//回滚,如果SubShaders没有一个能被支持运行,那么执行回滚
FallBack{}
二、Unity Shader
1、Unlit.不发光,只是一个纹理,不被任何光照影响,效率高,经常用户UI
2、VertexLit.顶点光照
3、Diffuse.漫反射
4、Normal mapped.法线贴图,使用于面片数少,但是又想表现出细节
5、Specular.高光
6、Normal Mapped Specular.高光法线贴图
7、Parallax Normal Mapped.视差法线贴图
8、Parallax Normal Mapped Specular.视差高光法线贴图.
(视差标识左右眼看见的视觉差异)
三、Fixed function shader(支持大部分硬件,速度最快)
例子1
Shader "Sbin/first"{
properties{
_Color("Main Color", color)=(1,1,1,1)
_Ambient("Ambient",color)=(0.3,0.3,0.3,0.3)
_Specular("Specular",color)=(1,1,1,1)
_Shininess("Shininess",rang(0,8))=4
_Emission("Emission",color)=(1,1,1,1)
_MainTex("MainTex",2d)=""{}
_Constant("Constanst_co",color)=(1,1,1,1)
}
SubShader{
Tags {"Queue" = "Transparent"}//渲染层级标签,渲染类型
pass{
Blend SrcAlpha OneMinusSrcAlpha//alpah叠
//color[_Color]
material
{
diffuse[_Color]//基本颜色
ambient[_Ambient]//光颜色
specular[_Specular]//高光反射
shininess[_Shininess]//发射点
emission[_Emission]//自发光
}
lighting on//打开光照后才能有材质(diffuse)效果
separatespecular on//独立的镜变高光
//纹理,只能带一个参数
settexture[_MainTex]
{
//合并(primary代表顶点采样)
//合并(previous代表的是在当前的texture之前所有的采样结果)
//单独texture那么上面定义的其它效果都会被覆盖
//texture * primary但是texture的值只是存在与0-1之间,那么颜色覆盖会变得很暗
// doubule表示双倍的效果, quad为四倍
//若想使用贴图本身的alpha,那么在添加texture 即(combine texture * primary double ,texture )只需要开启贴图本身的alpha应用即可
//constantColor[_Constant]自定义的常量设置alpha, (combine texture * primary double ,texture*constant)
combine texture * primary double
}
}
}
}
四、Sufer Shader
Shader "Sbin/test"{
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
//开启CG编程
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
struct Input{
float2 uv_MainTex;//这里引用_MainTex变量,但是这里只能支持uv/uv2这两种类型才能是金属类型
};
sampler2d _MainTex;//_MainTex是2d所以必须声明相应的
half _Glossiness;
half _Metallic;
fixed4 _Color;
//参数默认为in,标识输入, out表示输出,结构体类型有SurfaceOutputStandard, SurfaceOutputStandardSpecular(加入了高光反射)
void surf (Input IN, inout SurfaceOutputStandard o){
fixed4 c= tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
五、CG编程
基本数据类型: float half fixed bool int sampler*
顶点和片断profile (若是移动开发需要深入了解)
(向量).xyzw/rgba
|
|