【OpenGL】使用Unity来学习OpenGL

来源:互联网 发布:北国光电淘宝网 编辑:程序博客网 时间:2024/05/15 14:07

转发,请保持地址:http://blog.csdn.net/stalendp/article/details/11492525

OpenGL是原理性和实践性比较强的一门技术,在学习的时候,如果能够跟着书中的例子,一边调试一边学习,效果将很好(这属于实验的一种类型吧,能够吧知识形象化,有助于学习兴趣的提高)。市面上有许多深入浅出的书籍讲的很好,比如《OpenGL SuperBible 5th Edition》、《OpenGL 4 Shanding Language Cookbook》等(前者的例子代码写的非常好,后者把OpenGL的原理讲的比较深入)。不过把这些书中的代码跑在特定的系统上,还是需要花一些代价的(比如在mac上,SLGL只支持到1.2,很多例子中的代码就需要改了;而且配置环境也是比较麻烦的)。在探寻了很久之后,发现Unity3D提供了很好的OpenGL的学习环境,并且还有一些很好的资料可以参考(强烈推荐Kenny Lammers的《Unity Shaders and Effects Cookbook》)。本文将介绍怎么在Unity3D上学习SLGL(关于OpenGL的pipeLine、VBO、PBO等其他一些,可以参考上面推荐的两本书,特别是把《OpenGL SuperBible 5th Edition》的那套工具类搞懂,就非常OK了)。这片文章源于对Minimal Shader的一个翻译和整理。

一、配置启动项(只有windows上需要)

在Unity3d的桌面图标的属性中(右击图标选择属性,弹出的对话框中),在“目标”属性中,添加启动参数“-force-opengl”,变为如下:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. "C:\Program Files\Unity\Editor\Unity.exe" -force-opengl  

二、创建Shader

1. 创建一个新项目,在界面下方的Project面板中,选择Create->Shader,创建一个Shader,并命名为Test01,如下图:

2。双击Shader01,打开MonoDevelop编辑器,把下面的代码替换原来的,并保存:
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Shader "GLSL basic shader" { // defines the name of the shader   
  2.    SubShader { // Unity chooses the subshader that fits the GPU best  
  3.       Pass { // some shaders require multiple passes  
  4.          GLSLPROGRAM // here begins the part in Unity's GLSL  
  5.    
  6.          #ifdef VERTEX // here begins the vertex shader  
  7.    
  8.          void main() // all vertex shaders define a main() function  
  9.          {  
  10.             gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
  11.                // this line transforms the predefined attribute   
  12.                // gl_Vertex of type vec4 with the predefined  
  13.                // uniform gl_ModelViewProjectionMatrix of type mat4  
  14.                // and stores the result in the predefined output   
  15.                // variable gl_Position of type vec4.  
  16.          }  
  17.    
  18.          #endif // here ends the definition of the vertex shader  
  19.    
  20.    
  21.          #ifdef FRAGMENT // here begins the fragment shader  
  22.    
  23.          void main() // all fragment shaders define a main() function  
  24.          {  
  25.             gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);   
  26.                // this fragment shader just sets the output color   
  27.                // to opaque red (red = 1.0, green = 0.0, blue = 0.0,   
  28.                // alpha = 1.0)  
  29.          }  
  30.    
  31.          #endif // here ends the definition of the fragment shader  
  32.    
  33.          ENDGLSL // here ends the part in GLSL   
  34.       }  
  35.    }  
  36. }  
这样Shader就编辑好了。观察一下发现,这里一个文件中包含了Vertex Shader和Fragment Shader,这个有别于GLSL,不过其他就没有变化了(以后可能会有些优化,比如varying变量,在传统的GLSL中,需要在Vertex和Fragment中都定义一下,而且变量名称要一致,在Unity中可以把varying变量起到前面,写一份就可以了,减少了代码编写和错误的发生)

三、创建材质并绑定Shader

像创建Shader一样,创建材质,如下图:

方法一:把Shader,拖动到材质球上完成绑定,这样球变色了,如下:

方法二:选择材质球,在右边的Inspector中改变其Shader,如下图:

其中“GLSL basic Shader”为刚刚定义的Shader的名称:
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Shader "GLSL basic shader" { // defines the name of the shader   
  2.    SubShader { // Unity chooses the subshader that fits the GPU best  
  3. 。。。。。  

四、把材质赋予游戏物体

在编写Shader时候,一般观察材质球,就可以了解shader的效果了(选择材质,在右下角的Preview中显示);材质可以为球、立方体、圆柱体、环体,点击Preview窗口标题栏靠左的按钮可以进行切换,如下图:

而材质最终还是要赋予游戏物体的(游戏物体可以有多种形状,可以方便观察shader的效果);
1. 创建物体(这里将使用Unity预定义物体),点击菜单,如下:

然后在右上的Hierarchy中选择刚创建的Sphere,把鼠标移到Scene窗口,按键盘上的f,使得物体调整到最佳位置和大小。然后把材质拖动到物体上,如下图:


OK,到此为止,OpenGL的学习环境已经搭建好,然后推荐深入学习GLSL的教程:GLSL/Unity; 当然在其页面上也有pdf下载对应的pdf下载连接。
当然,Unity中不怎么推荐使用GLSL,取而代之的是Nvidia的cg,可以参考:http://en.wikibooks.org/wiki/Cg_Programming/Unity,教程结构是一样的,所以后续学起来应该还是比较轻松的,也可以参考着学习
这个GLSL教程还有关于Blender的,有兴趣的同学可以去参考一下:http://en.wikibooks.org/wiki/GLSL_Programming;
另外,官方也有shader的相关例子:http://wiki.unity3d.com/index.php/Shaders
Surface Shader Examples: http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html
Surface Shader Lighting Examples: http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderLightingExamples.html
Vertex and Fragment Shader Examples: http://docs.unity3d.com/Documentation/Components/SL-VertexFragmentShaderExamples.html

----

关于OpenGL的资料:

Learning Modern 3D Graphics Programming

OpenGL Programming

Anton's OpenGL 4 Tutorials

一个开发者的博客:One-minute Dungeon: Behind the scenes

游戏中法线贴图的技巧:A game of Tricks-Normal mapped Scripts


参考:
http://en.wikibooks.org/wiki/GLSL_Programming/Unity/Minimal_Shader


================
更多高级的Shader知识
=================
GPU Pro 1~5:http://pan.baidu.com/s/1qWLSxAk
GPU Gems:
http://www.slideshare.net/DAMSIGNUP/so4-flexible-shadermanagmentandpostprocessing
其他书籍的索引:http://www.douban.com/doulist/1445745/
http://www.zhihu.com/question/26720808
0 0
原创粉丝点击