您的位置:首页 > 移动开发 > Unity3D

Unity Shader学习笔记(一) shader的基础结构

2016-12-20 13:40 295 查看
Shader "Unlit/Shader_1"  //shader 路径
{
Properties          //在这里写的东西都会呈现在Unity检视面板中,可在外部调节  格式:_Name("显示名称",Type) = defaultValue
{
_MainTex ("Texture", 2D) = "white" {}
}
//一个Shader中可以有多个SubShader ,但是unity会选择第一个可以在相应显卡执行的SubShader来执行,其后面的就会跳过
//渲染状态设置  CullBack  背部剔除 当背部面向摄像机的时候就不会显示
//Cull Front  前面剔除 当正面面向摄像机的时候就不会显示
//Cull Off    剔除关闭 关闭剔除功能
//Ztest Less Greater/LEqual/GEqual/Equal/Always   根据像素在Z轴上的位置来进行处理
//Zwrite On/Off   摄像机近处物体遮挡远处物体

SubShader
{
//设置标签
//Queue                    渲染顺序指定
//RenderType               着色器分类
//DisableBatching          批处理开关
//ForceNoShadowCasting     阴影投射
//IgnoreProjector          用于半透明物体不受投射
//CanUseSpriteAtlas        用于2D精灵
//PreviewType              Untiy预览用模型
Tags { "RenderType"="Opaque" }
LOD 100

//SubShader中可能有多个Pass,但每个Pass会依次被执行
//也可以调用其他Shader中的Pass  用法:UsePass"MyShader/MYPASS"  调用的pass名必须大写
//Pass中的Tags  LightMode    指定Unity中的渲染途径
//              RequireOptions  渲染这个Pass需要的条件,如果条件不够则不被渲染
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
float4 _MainTex_ST;

v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
//FallBack是本shader 中的内容不被支持时,选用那种低级的Shader替换   FallBack“ShaderName”
}
https://onevcat.com/2013/07/shader-tutorial-1/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Shader unity3d