纳金网

标题: 编写顶点和片段着色器:OpenGL着色语言 [打印本页]

作者: .    时间: 2013-2-24 09:55
标题: 编写顶点和片段着色器:OpenGL着色语言
In addition to using Cg/HSL shader programs, OpenGL Shading Language (GLSL) shaders can be written directly.
除了使用Cg/HSL着色器程序,也可以直接编写OpenGL着色语言(GLSL)的着色器。
However, use of raw GLSL is only recommended for testing, or when you know you will only target Mac OS X or OpenGL ES 2.0 compatible mobile devices. In majority of normal cases, Unity will cross-compile Cg/HLSL into optimized GLSL (this is done by default for mobile platforms, and can be optionally turned on for desktop platforms via #pragma glsl).
然而使用原始的GLSL建议只在测试的时候用,或者你的着色器只以Mac OS X系统或者兼容OpenGL ES2.0的移动设备为使用目标。在大部分正常情况下,Unity将交叉编译Cg/HLSL成优化的GLSL(为了兼容移动平台而默认的。在桌面平台你可以通过 #pragma glsl命令随意的打开)。

GLSL snippets GLSL小片段
GLSL program snippets are written between GLSLPROGRAM and ENDGLSL keywords.
GLSL程序小片段是编写在GLSLPROGRAM和ENDGLSL关键字之间。
In GLSL, all shader function entry points have to be called main(). When Unity loads the GLSL shader, it loads the source once for the vertex program, with VERTEX preprocessor define, and once more for the fragment program, with FRAGMENT preprocessor define. So the way to separate vertex and fragment program parts in GLSL snippet is to surround them with #ifdef VERTEX .. #endif and #ifdef FRAGMENT .. #endif. Each GLSL snippet must contain both a vertex program and a fragment program.
在GLSL里,所有的着色器程序的入口点是一个叫做 main()的函数。当Unity加载GLSL着色器的时候,它加载一次资源(source),为顶点程序进行顶点预处理(VERTEX preprocessor)定义,然后为片段程序(fragment program)进行片段预处理( FRAGMENT preprocessor)定义。所以要对顶点和片段程序(vertex and fragment program)分开处理,在GLSL小片段里是通过 #ifdef VERTEX .. #endif 和 #ifdef FRAGMENT .. #endif命令包围住各部分。每个GLSL小片段必须包含一个顶点程序和一个片段程序。
Standard include files match those provided for Cg shaders; they just have .glslinc extension: UnityCG.glslinc.
标准的导入文件(include files)匹配提供的Cg着色器;它们只是有 .glslinc的扩展名,例如:UnityCG.glslinc.
Vertex shader inputs come from predefined GLSL variables (gl_Vertex, gl_MultiTexCoord0, ...) or are user defined attributes. Usually only the tangent vector needs a user defined attribute:
顶点着色器输入结构来自预先定义的GLSL变量(gl_Vertex, gl_MultiTexCoord0, ...)或者是用户自定义的属性。通常只有切线向量需要一个用户自定义属性:(attribute为限定符,使用在变量名前,表示该变量为用户自定义的。)
    attribute vec4 Tangent;
Data from vertex to fragment programs is passed through varying variables, for example:
数据从顶点程序到片段程序的传递是在变量类型前使用varying限定符,例如:
    varying vec3 lightDir; // vertex shader computes this, fragment shader uses this

【来源:互联网】

更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
作者: wh    时间: 2013-2-24 10:26
你在说什么呢????????????????????????????????????????




欢迎光临 纳金网 (http://wwww.narkii.com/club/) Powered by Discuz! X2.5