GLSL Programming/Unity
GLSL programming in the game engine Unity is considerably easier than GLSL programming for an OpenGL, OpenGL ES, or WebGL application. Import of meshes and images (i.e. textures) is supported by a graphical user interface; mipmaps and normal maps can be computed automatically; the most common vertex attributes and uniforms are predefined; OpenGL states can be set by very simple commands; etc.
Preliminaries
[edit | edit source]A free version of Unity can be downloaded for Windows and MacOS at Unity's download page. All of the included tutorials work with the free version. Three points should be noted:
- First, Windows users have to use the command-line argument
-force-opengl
[1] when starting Unity in order to be able to use GLSL shaders; for example, by changing theTarget
setting in the properties of the desktop icon to:"C:\Program Files\Unity\Editor\Unity.exe" -force-opengl
. (On MacOS X, OpenGL and therefore GLSL is used by default.) Note that GLSL shaders cannot be used in Unity applications running in web browsers on Windows. - Secondly, this part assumes that readers are somewhat familiar with Unity. If this is not the case, readers should consult the first three sections of Unity's User Guide [2] (Unity Basics, Building Scenes, Asset Import and Creation).
- Furthermore, Unity supports a version of GLSL similar to version 1.0.x for OpenGL ES 2.0 (the specification is available at the “Khronos OpenGL ES API Registry”); however, Unity's shader documentation [3] focuses on shaders written in Unity's own “surface shader” format and Cg/HLSL [4]. There are only very few details documented that are specific to GLSL shaders [5]. Thus, this wikibook might also help to close some gaps in Unity's documentation. However, optimizations (see, for example, this blog) are usually not discussed.
Tutorials
[edit | edit source]Note that the tutorials assume that you read them in the order in which they are presented here, i.e. each tutorial will assume that you are familiar with the concepts and techniques introduced by previous tutorials. If you are new to GLSL or Unity you should at least read through the tutorials in the “Basics” section. (Now you are warned!)
Basics
[edit | edit source]- Minimal Shader (about shaders, materials, and game objects)
- RGB Cube (about varyings)
- Debugging of Shaders (about attributes)
- Shading in World Space (about uniforms)
Transparent Surfaces
[edit | edit source]- 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
[edit | edit source]- 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
[edit | edit source]- 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
[edit | edit source]- 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
[edit | edit source]- 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
[edit | edit source]- 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
[edit | edit source]- 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
[edit | edit source]Some additional tutorials about graphics programming in Unity without shaders are available in the Cg Programming wikibook.
Links
[edit | edit source]- 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