GLSL in ShaderLab
【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
GLSL in ShaderLab的更多相关文章
- CSharpGL(11)用C#直接编写GLSL程序
CSharpGL(11)用C#直接编写GLSL程序 +BIT祝威+悄悄在此留下版了个权的信息说: 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharp ...
- Unity ShaderLab学习总结
http://www.jianshu.com/p/7b9498e58659 Unity ShaderLab学习总结 Why Bothers? 为什么已经有ShaderForge这种可视化Shader编 ...
- Unity Shader (一)ShaderLab 语法
一.什么是Shader Shader(着色器):是可以在GPU上运行的一段程序,通过Shader可以进行一些渲染相关的设置. 二.什么是ShaderLab 目前面向GPU的编程有三种高级图像语言:HL ...
- 使用Visual Studio SDK制作GLSL词法着色插件
使用Visual Studio SDK制作GLSL词法着色插件 我们在Visual Studio上开发OpenGL ES项目时,避免不了写Shader.这时在vs里直接编辑shader就会显得很方便. ...
- 基于虎书实现LALR(1)分析并生成GLSL编译器前端代码(C#)
基于虎书实现LALR(1)分析并生成GLSL编译器前端代码(C#) 为了完美解析GLSL源码,获取其中的信息(都有哪些in/out/uniform等),我决定做个GLSL编译器的前端(以后简称编译器或 ...
- CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果
CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果 开始 一图抵千言.首先来看鼠标拖动太阳(光源)的情形. 然后是鼠标拖拽旋转模型的情形. 然后我们移动摄像 ...
- CSharpGL(15)用GLSL渲染2种类型的文字
CSharpGL(15)用GLSL渲染2种类型的文字 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合 ...
- CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection)
CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection) 2016-08-13 由于CSh ...
- C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字
C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字 +BIT祝威+悄悄在此留下版了个权的信息说: 上一篇得到了字形贴图及其位置字典(可导出为XML).本篇就利用此贴 ...
随机推荐
- conduit 安装试用
备注: 测试安装环境使用docker mac 版本(目前版本已经支持kubernetes了) 1. 基本安装 curl https://run.conduit.io/install | bash 配置 ...
- vault key 管理工具
Vault is a tool for securely accessing secrets. A secret is anything that you want to tightly contro ...
- VS2010 代码前出现虚线
VS2010 代码前出现虚线的问题,解决方法: 只需要用快捷键:Ctrl+E+S三个组合键就可以让其取消或显示了
- ubuntu 修改分辨率为自定义分辨率
在ubuntu14.04虚拟机上修改自定义大小的桌面屏幕分辨率,使用的命令:cvt,xrandr 0.首先查看下当前已经提供的分辨率设置:xrandr -q root@xxx:/home/xxx/De ...
- Unit01: Web概述 、 HTML概述 、 文本处理 、 图像和超链接 、 表格 、 表单
Unit01: Web概述 . HTML概述 . 文本处理 . 图像和超链接 . 表格 . 表单 demo1.html <!-- 声明网页的版本(文档类型) --> <!doctyp ...
- FC Switch sfpshow
sfpshow - fault-finding on Brocade Fibre Channel Switches So you've hit a situation where a Fibre Ch ...
- java web 程序--注册页面/HashMap的用法。。要懂啊
思路:1.一个form表单,用户输入后,提交 2.第二个是注册页面,主要是用Map.先假设往map里面拿东西,然后判断是否为空 若为空,new 一个HashMap它的子类,然后通过map.conta ...
- java------------break;
总结: package com.mmm; public class Pnal { public static void main(String[] args) { int i = 0; while ( ...
- 使用Java读取配置文件
实现起来,相对比较简单,留个备案吧,废话也不多说,请看代码: package com.jd.***.config; import org.junit.*; import java.io.IOExcep ...
- 如何实现查询显示N个工作日有效期内的数据
功能点分析:要显示N个工作日有效期内的数据,需要考虑: 1. 可以给每条数据增加一个有效期时间字段,查询时只显示有效期之前的数据,如有效期为七天,数据的创建时间是2014-07-21,那七个工作日有效 ...