GLSL Programming/Unity
在 Unity 环境下进行 GLSL 开发比起使用编写独立的 OpenGL, OpenGL ES 或者 WebGL 应用程序无疑会更为简单。可以直接导入 mesh,图片资源(如 Textures);mipmap 和 normal map 会被自动计算;大部分 GLSL vertex attributes 和 uniforms 变量都是预定义好的,可以直接用;一些 OpenGL 状态变量可以直接设置。等等。
准备工作
编辑可以在 Unity's download page上下载到免费版本的 Unity,有多种平台版本,Windows,MacOS,Linux等。下面陈述的所有实例都可以在免费版本上实作运行。有几点需要注意:
- 第一,Windows 用户启动 Unity 开发环境时需要传入如下参数
-force-opengl
,以便可以在 Unity 开发环境中使用 GLSL shader [1]。Windows 默认情况下使用 DirectX。可以参考如下做法避免每次都要从命令终端启动 Unity :右键单击 Unity 桌面图标,编辑属性选项,例如"C:\Program Files\Unity\Editor\Unity.exe" -force-opengl
(在MacOS 上默认使用的就是 OpenGL)。另外Windows 下还不支持 Web 浏览器里面使用 GLSL shader
- 第二,我们假设读者朋友们已经熟悉 Unity 开发环境,如果答案是否定的,那么推荐先阅读一下 Unity User Guide [2]部分前三节(Unity Basics, Building Scenes, Asset Import and Creation)。
另外,Unity 是支持 GLSL 1.0.x 版本的,也就是 OpenGL ES 2.0(请参考“Khronos OpenGL ES API Registry”),然而 Unity 官方 shader [3]文档内容主要关注于 Unity 自己的 “Surface Shader”和 Cg/HLSL[4],仅仅用很少的文字来描述 GLSL 部分[5]。所以这一份 wikibook 也许可以作为 Unity 环境下 GLSL 开发的补充,不过关于优化部分并没有过多涉及 (请参考这篇博客)。
示例教程
编辑以下示例教程内容具有关联性,后面的章节一般会依赖于前面的部分,所以请按照顺序来阅读。如果您还没接触过 GLSL 或者 Unity,至少应该首先阅读“Basic”部分!
Basics
编辑- Minimal Shader (关于 着色器,材质,游戏物体(gameObject))
- RGB Cube (about varyings)
- Debugging of Shaders (about attributes)
- Shading in World Space (about uniforms)
Transparent Surfaces
编辑- Cutaways (about discarding fragments and triangle-face culling)
- Transparency (about blending)
- Order-Independent Transparency (about order-independent blending)
- Silhouette Enhancement (about transforming normal vectors)
Basic Lighting
编辑- Diffuse Reflection (about per-vertex diffuse lighting and multiple light sources of different kinds)
- Specular Highlights (about per-vertex lighting)
- Two-Sided Surfaces (about two-sided per-vertex lighting)
- Smooth Specular Highlights (about per-pixel lighting)
- Two-Sided Smooth Surfaces (about two-sided per-pixel lighting)
- Multiple Lights (about for-loops for handling multiple light sources in one shader pass)
Basic Texturing
编辑- Textured Spheres (about texturing a sphere)
- Lighting Textured Surfaces (about textures for diffuse lighting)
- Glossy Textures (about gloss mapping)
- Transparent Textures (about using alpha textures for discarding fragments, alpha testing, and blending)
- Layers of Textures (about multitexturing)
Textures in 3D
编辑- Lighting of Bumpy Surfaces (about normal mapping)
- Projection of Bumpy Surfaces (about parallax mapping)
- Cookies (about projective texture mapping for shaping light)
- Light Attenuation (about texture mapping for light attenuation and lookup tables in general)
- Projectors (about projective texture mapping for projectors)
Environment Mapping
编辑- Reflecting Surfaces (about reflection mapping)
- Curved Glass (about refraction mapping)
- Skyboxes (about rendering of environment maps as background)
- Many Light Sources (about image-based lighting)
Variations on Lighting
编辑- Brushed Metal (about anisotropic specular reflection)
- Specular Highlights at Silhouettes (about the Fresnel factor for specular reflection)
- Diffuse Reflection of Skylight (about hemisphere lighting)
- Translucent Surfaces (about diffuse and forward-scattered transmission of backlight)
- Translucent Bodies (about diffuse lighting with reduced contrast and transmission of diffuse backlight at silhouettes)
- Soft Shadows of Spheres (about rendering the umbra and penumbra of spheres)
- Toon Shading (about non-photorealistic rendering)
Non-Standard Vertex Transformations
编辑- Screen Overlays (about a direct transformation from object space to screen space)
- Billboards (about view-aligned projection of objects)
- Nonlinear Deformations (about vertex blending)
- Shadows on Planes (about projecting shadows onto planes)
- Mirrors (about mirroring objects and stenciling with the alpha buffer)
Graphics without Shaders
编辑关于 tutorials about graphics programming in Unity without shaders 的一些资料可以在 Cg Programming wikibook 上面找到.
参考链接
编辑- Unity shader reference: http://unity3d.com/support/documentation/Components/SL-Reference.html
- Unity shaders in Cg (including built-in shaders): http://www.unifycommunity.com/wiki/index.php?title=Shaders#Unity_3.x_Shaders
- Unity user forum about shaders: http://forum.unity3d.com/forums/16-ShaderLab
- Blog entry about optimizing shaders of iOS: http://aras-p.info/blog/2011/02/01/ios-shader-tricks-or-its-2001-all-over-again/
- Blog entry about a SIGGRAPH presentation on shader optimization: http://blogs.unity3d.com/2011/08/18/fast-mobile-shaders-talk-at-siggraph/
- Utility for analyzing performance of shaders on PowerVR GPUs (e.g. all iOS devices): http://www.imgtec.com/powervr/insider/powervr-pvrshaman.asp