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