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"
}

参考:file://localhost/Applications/Unity/Unity.app/Contents/Documentation/Documentation/Manual/ShaderTut2.html

Vertex and fragment programs的更多相关文章

  1. UnityShader之顶点片段着色器Vertex and Fragment Shader【Shader资料】

    顶点片段着色器 V&F Shader:英文全称Vertex and Fragment Shader,最强大的Shader类型,也是我们在使用ShaderLab中的重点部分,属于可编程管线,使用 ...

  2. Vertex And Fragment Shader(顶点和片段着色器)

    Vertex And Fragment Shader(顶点和片段着色器) Shader "Unlit/ Vertex­_And_Fragment_Shader " { Proper ...

  3. Unity cg vertex and fragment shaders(二)

    着色器的一般结构: Shader "MyShader/MyShaderName" { Properties { // ... properties here ... } SubSh ...

  4. Unity cg vertex and fragment shaders(一)

    cg片段 Cg程序片段写CGPROGRAM和ENDCG之间 开始时的片段可以作为#pragma语句编译指令 Pass { // ... the usual pass state setup ... C ...

  5. 3D Computer Grapihcs Using OpenGL - 07 Passing Data from Vertex to Fragment Shader

    上节的最后我们实现了两个绿色的三角形,而绿色是直接在Fragment Shader中指定的. 这节我们将为这两个三角形进行更加自由的着色——五个顶点各自使用不同的颜色. 要实现这个目的,我们分两步进行 ...

  6. 3D Computer Grapihcs Using OpenGL - 06 Vertex and Fragment Shaders

    从这里就接触到了可编程图形渲染管线. 下面介绍使用Vertex Shader (顶点着色器)和 Fragment Shader(像素着色器)的方法. 我们的目标是使用这两个着色器给三角形填充绿色. 添 ...

  7. Vertex and Fragment Shader

    Semantics语义词: 定义:GPU工作时,数据通常暂存在寄存器,那么在Cg中,语义词就指定了输入/输出数据和图形硬件寄存器之间的映射关系. 原理:根据输入语义,图形处理器从某个寄存器取数据:然后 ...

  8. Unity Shaders Vertex & Fragment Shader入门

    http://blog.csdn.net/candycat1992/article/details/40212735 三个月以前,在一篇讲卡通风格的Shader的最后,我们说到在Surface Sha ...

  9. 【Unity Shaders】Vertex & Fragment Shader入门

    写在前面 三个月以前,在一篇讲卡通风格的Shader的最后,我们说到在Surface Shader中实现描边效果的弊端,也就是只对表面平缓的模型有效.这是因为我们是依赖法线和视角的点乘结果来进行描边判 ...

随机推荐

  1. LuaJavaBridge

    http://www.360doc.com/content/14/0117/13/9200790_345940368.shtml quick目录结构介绍 http://cn.cocos2d-x.org ...

  2. Fuel9.0安装openstack过程中所踩过的坑2018最新版

    坑一,安装好后,无法访问Web UI画面 访问https//10.20.0.2:8443无法打开UI画面.首先我们不管以后的步骤,打不开是很不爽的. 解决方法:把下面网卡1,网卡2,网卡3的界面名称都 ...

  3. luvcview,使用mplayer查看摄像头和luvcview保存YUV图像视频的播放(转)

    luvcview,使用mplayer查看摄像头和luvcview保存YUV图像视频的播放 在mplayer中查看摄像头,可使用如下命令:mplayer tv:// -tv driver=v4l2:in ...

  4. J2EE项目在weblogic下的改动

    1.struts所有配置文件放到classes根目录下 2〉java.lang.ClassCastException:weblogic.xml.jaxp.RegistryDocumentBuilder ...

  5. python 读取Linux服务器上的文件

    使用Python语句,读取Linux远端服务器上的文件打印到控制台的代码实现: 下载包:paramiko import paramiko #服务器信息,主机名(IP地址).端口号.用户名及密码 hos ...

  6. 老齐python-基础3(列表)

    1.定义一个列表 >>> a = [] #创建一个空列表 >>> type(a) #查看数据类型 <class 'list'> >>> ...

  7. 基于EFCore的数据Cache实现

    .NetCore 内置缓存加入到EFCore操作中,数据更新或者查询时自动更新缓存.github地址 2019-04-27 初步完成逻辑代码编写,尚未经过测试,诸多细节有待完善. 2019-04-28 ...

  8. MongoDB在windows平台分片集群部署

    本文转载自:https://www.cnblogs.com/hx764208769/p/4260177.html 前言-为什么我要使用mongodb 最近我公司要开发一个日志系统,这个日志系统包括很多 ...

  9. Netty--TCP粘包和拆包

    TCP协议以流的方式进行数据传输,它无法理解其上层协议的数据意义,而是根据TCP缓冲区的大小对数据进行拆分或组装,即上层一个完整的包可能被拆为几个TCP包来发送,或上层几个包会被组合为一个TCP包发送 ...

  10. bzoj 3867: Nice boat

    题意:给定一个正整数序列,操作是1.区间赋值,2.区间大于x的数与x取gcd,最后输出操作后的序列 用平衡树维护相同数组成的连续段,每次操作至多增加两个连续段,操作2记录一下区间最小值然后暴力修改,每 ...