Syntax

  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 program snippets are written between GLSLPROGRAM and ENDGLSL keywords.

  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.

  Standard include files match those provided for Cg shaders; they just have .glslinc extension: 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:

    attribute vec4 Tangent;

  Data from vertex to fragment programs is passed through varying variables, for example:

    varying vec3 lightDir; // vertex shader computes this, fragment shader uses this

Examples

  为了使用ShaderLab中的Property,需像下面这样搞:

Properties
{
//可以从编辑器中选择颜色
_Color ("Main Color", Color) = (,0.5,0.5,)
} #ifdef FRAGMENT
//这就是从Unity编辑器给GLSL shader传递数据的方法,定义uniforms类型变量
uniform vec4 _Color ;
void main()
{
gl_FragColor = _Color;
}
#endif

  完整代码如下所示:

Shader "simple_Color_Shader"
{
Properties
{
//可以从编辑器中选择颜色
_Color ("Main Color", Color) = (,0.5,0.5,)
}
SubShader
{
Tags { "Queue" = "Geometry" }
Pass
{
GLSLPROGRAM
#ifdef VERTEX
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#endif #ifdef FRAGMENT
//这就是从Unity编辑器给GLSL shader传递数据的方法,定义uniforms类型变量
uniform vec4 _Color ;
void main()
{
gl_FragColor = _Color;
}
#endif
ENDGLSL
}
}
}

参考:

1、http://www.58player.com/blog-2532-69127.html

2、file://localhost/Applications/Unity/Unity.app/Contents/Documentation/Documentation/Components/SL-GLSLShaderPrograms.html

GLSL in ShaderLab的更多相关文章

  1. CSharpGL(11)用C#直接编写GLSL程序

    CSharpGL(11)用C#直接编写GLSL程序 +BIT祝威+悄悄在此留下版了个权的信息说: 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharp ...

  2. Unity ShaderLab学习总结

    http://www.jianshu.com/p/7b9498e58659 Unity ShaderLab学习总结 Why Bothers? 为什么已经有ShaderForge这种可视化Shader编 ...

  3. Unity Shader (一)ShaderLab 语法

    一.什么是Shader Shader(着色器):是可以在GPU上运行的一段程序,通过Shader可以进行一些渲染相关的设置. 二.什么是ShaderLab 目前面向GPU的编程有三种高级图像语言:HL ...

  4. 使用Visual Studio SDK制作GLSL词法着色插件

    使用Visual Studio SDK制作GLSL词法着色插件 我们在Visual Studio上开发OpenGL ES项目时,避免不了写Shader.这时在vs里直接编辑shader就会显得很方便. ...

  5. 基于虎书实现LALR(1)分析并生成GLSL编译器前端代码(C#)

    基于虎书实现LALR(1)分析并生成GLSL编译器前端代码(C#) 为了完美解析GLSL源码,获取其中的信息(都有哪些in/out/uniform等),我决定做个GLSL编译器的前端(以后简称编译器或 ...

  6. CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果

    CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果 开始 一图抵千言.首先来看鼠标拖动太阳(光源)的情形. 然后是鼠标拖拽旋转模型的情形. 然后我们移动摄像 ...

  7. CSharpGL(15)用GLSL渲染2种类型的文字

    CSharpGL(15)用GLSL渲染2种类型的文字 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合 ...

  8. CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection)

    CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection) 2016-08-13 由于CSh ...

  9. C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字

    C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字 +BIT祝威+悄悄在此留下版了个权的信息说: 上一篇得到了字形贴图及其位置字典(可导出为XML).本篇就利用此贴 ...

随机推荐

  1. 给网站添加IPv6 DNS记录

    一. 1) 使6box提供的DNS64服务,解析出自己网站对应的IPv6域名 Windows: Win+R打开运行,输入cmd,打开命令提示符,输入以下命令 Nslookup www.6box.cn  ...

  2. CentOS常用基础命令汇总

    1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...

  3. SEO方案

    前端需要注意哪些SEO 合理的title.description.keywords:搜索对着三项的权重逐个减小,title值强调重点即可,重要关键词出现不要超过2次,而且要靠前,不同页面title要有 ...

  4. webpack extract-text-webpack-plugin

    备注:  提炼上面引用的css   1. 插件配置 const path = require("path"); const extracttextplugin = require( ...

  5. PHP---如何修改域名的指定的根目录

    如何修改域名的指定的根目录 环境:linux 使用工具:xShell 修改域名指定的文件根目录需要修改nginx的配置文件 第一步:连接xShell 第二步:进入根路径找到nginx的配置文件 cd ...

  6. netstat 命令 与 ss 命令

    http://www.cnblogs.com/peida/archive/2013/03/11/2953420.html http://www.ttlsa.com/linux-command/ss-r ...

  7. mysql插入中文出错,提示1366 - Incorrect

    提示这样 1366 - Incorrect string value: '\xC0\x86' for column 'platecl' at row 1 是因为编码的问题(在这儿我们需要的是gbk,当 ...

  8. 适配:px与dp转换

    public class DensityUtil { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context con ...

  9. java代码---I/O文件内容复制

    总结:主要是弄清输入流,输出流 输入流和输出流都是针对程序而言,把文件内容读取到程序是输入流 从程序把内容写入文件是输出流 package com.a.b; import java.io.*; imp ...

  10. 操作系统-百科:Linux

    ylbtech-操作系统-百科:Linux Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNI ...