Vertex and fragment programs
【Vertex and fragment programs】
When you use vertex and fragment programs (the so called "programmable pipeline"), most of the hardcoded functionality ("fixed function pipeline") in the graphics hardware is switched off. For example, using a vertex program turns off standard 3D transformations, lighting and texture coordinate generation completely. Similarly, using a fragment program replaces any texture combine modes that would be defined in SetTexture commands; thus SetTexture commands are not needed.
【Using Cg in ShaderLab】
Shaders in ShaderLab are usually written in Cg programming language by embedding "Cg snippets" in the shader text. Cg snippets are compiled into low-level shader assembly by the Unity editor, and the final shader that is included in your game's data files only contains this low-level assembly.
Note that because Cg code is compiled by the editor, you can't create Cg shaders from scripts at runtime.(OpenGL的glCompileShader提供运行时创建Shader的能力)
In general, Cg snippets are placed inside Pass blocks. They look like this:
The following example demonstrates a complete shader with Cg programs that renders object normals as colors:
Shader "Tutorial/Display Normals" {
SubShader {
Pass { CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" struct v2f {
float4 pos : SV_POSITION;
float3 color : COLOR0;
}; v2f vert (appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.color = v.normal * 0.5 + 0.5;
return o;
} half4 frag (v2f i) : COLOR
{
return half4 (i.color, );
}
ENDCG }
}
Fallback "VertexLit"
}
The whole Cg snippet is written between CGPROGRAM and ENDCG keywords. At the start compilation directives are given as #pragma statements:
- #pragma vertex name tells that the code contains a vertex program in the given function (vert here).
- #pragma fragment name tells that the code contains a fragment program in the given function (frag here).
Following the compilation directives is just plain Cg code. We start by including a built-in Cg file:
#include UnityCg.cginc The UnityCg.cginc file contains commonly used declarations and functions so that the shaders can be kept smaller (see shader include files page for details). Here we'll use appdata_basestructure from that file. We could just define them directly in the shader and not include the file of course.
【Using shader properties in Cg code】
When you define properties in the shader, you give them a name like _Color or _MainTex. To use them in Cg you just have to define a variable of a matching name and type. Unity will automatically set Cg variables that have names matching with shader properties.
Shader "Tutorial/Textured Colored" {
Properties {
_Color ("Main Color", Color) = (,,,0.5)
_MainTex ("Texture", 2D) = "white" { }
}
SubShader {
Pass { CGPROGRAM
#pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc" float4 _Color;
sampler2D _MainTex; struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
}; float4 _MainTex_ST; v2f vert (appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
return o;
} half4 frag (v2f i) : COLOR
{
half4 texcol = tex2D (_MainTex, i.uv);
return texcol * _Color;
}
ENDCG }
}
Fallback "VertexLit"
}
Vertex and fragment programs的更多相关文章
- UnityShader之顶点片段着色器Vertex and Fragment Shader【Shader资料】
顶点片段着色器 V&F Shader:英文全称Vertex and Fragment Shader,最强大的Shader类型,也是我们在使用ShaderLab中的重点部分,属于可编程管线,使用 ...
- Vertex And Fragment Shader(顶点和片段着色器)
Vertex And Fragment Shader(顶点和片段着色器) Shader "Unlit/ Vertex_And_Fragment_Shader " { Proper ...
- Unity cg vertex and fragment shaders(二)
着色器的一般结构: Shader "MyShader/MyShaderName" { Properties { // ... properties here ... } SubSh ...
- Unity cg vertex and fragment shaders(一)
cg片段 Cg程序片段写CGPROGRAM和ENDCG之间 开始时的片段可以作为#pragma语句编译指令 Pass { // ... the usual pass state setup ... C ...
- 3D Computer Grapihcs Using OpenGL - 07 Passing Data from Vertex to Fragment Shader
上节的最后我们实现了两个绿色的三角形,而绿色是直接在Fragment Shader中指定的. 这节我们将为这两个三角形进行更加自由的着色——五个顶点各自使用不同的颜色. 要实现这个目的,我们分两步进行 ...
- 3D Computer Grapihcs Using OpenGL - 06 Vertex and Fragment Shaders
从这里就接触到了可编程图形渲染管线. 下面介绍使用Vertex Shader (顶点着色器)和 Fragment Shader(像素着色器)的方法. 我们的目标是使用这两个着色器给三角形填充绿色. 添 ...
- Vertex and Fragment Shader
Semantics语义词: 定义:GPU工作时,数据通常暂存在寄存器,那么在Cg中,语义词就指定了输入/输出数据和图形硬件寄存器之间的映射关系. 原理:根据输入语义,图形处理器从某个寄存器取数据:然后 ...
- Unity Shaders Vertex & Fragment Shader入门
http://blog.csdn.net/candycat1992/article/details/40212735 三个月以前,在一篇讲卡通风格的Shader的最后,我们说到在Surface Sha ...
- 【Unity Shaders】Vertex & Fragment Shader入门
写在前面 三个月以前,在一篇讲卡通风格的Shader的最后,我们说到在Surface Shader中实现描边效果的弊端,也就是只对表面平缓的模型有效.这是因为我们是依赖法线和视角的点乘结果来进行描边判 ...
随机推荐
- 【java基础】java中ArrayList,LinkedList
[一]ArrayList 一ArrayList的内部结构 (1)ArrayList内部维护的是一个Object数组 (2)ArrayList数组扩容后数组的长度的公式:旧的数组长度+(旧数组长度> ...
- 【DUBBO】 Dubbo原理解析-Dubbo内核实现之基于SPI思想Dubbo内核实现
转载:http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577235 SPI接口定义 定义了@SPI注解 public @interfa ...
- java-文件流正确关闭资源
用文件流来拷贝一个文件,用到文件字节输入流(FileInputStream)和文件字节输出流(FileOutputStream),用输入流把字节文件读到缓冲数组中,然后将缓冲数组中的字节写到文件中,就 ...
- Python猴子补丁
属性在运行时的动态替换,叫做猴子补丁(Monkey Patch). 为什么叫猴子补丁 属性的运行时替换和猴子也没什么关系,关于猴子补丁的由来网上查到两种说法: 1,这个词原来为Guerrilla Pa ...
- mui 修改下拉刷新提示文字的显示位置
第一种: .mui-bar-nav~.mui-content .mui-pull-top-pocket { top: 126px !important; } 第二种: .mui-pull-top-po ...
- WebSocket的使用
WebSocket是长连接,如果客户端的程序没有数据实时同步的需求就没必要使用它.因为长连接会带来一定的服务器内存开销.如果Ajax就能轻松搞定的话就完全没必要兴师动众的搞WebSocket. htt ...
- bzoj 1927 [Sdoi2010]星际竞速——网络流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1927 每个点拆点保证只经过一次. 主要是如果经过了这个点,这个点应该向汇点流过去表示经过了它 ...
- 军哥 LNMP 常见问题
安装memcached出错: Install memcached...Notice: memcached-1.4.25.tar.gz not found!!!download now...--2016 ...
- Linux 文件系统目录结构
进入 Linux 根目录(即 "/",Linux文件系统的入口,也是处于最高一级的目录),运行 "ls -l" 命令,可以看到 Linux 系统目录. 1./b ...
- jmetr _MD5加密_获取签名
要达到的目的: app每个请求里面 请求头都带有一个 sign 的参数, 他的值是通过 开发自己设计的拼接方式 再通过md5加密生成 我们就是要生成这个sign的值出来 准备: 和开发要到签名组成公式 ...