CG Programming Unity
CG Programming Unity
A free version of Unity can be downloaded for Windows Minimal Shader (about shaders, materials, and game
and MacOS at Unitys download page. All of the included objects)
tutorials work with the free version. Three points should
RGB Cube (about vertex output parameters)
be noted:
Debugging of Shaders (about vertex input parame-
First, this part is about vertex and fragment shaders ters)
in Unity. This kind of shader programming is par-
ticularly exible and allows you to program very ef- Shading in World Space (about uniforms)
cient shaders, which is often important when ren-
dering performance is limited, e.g., on mobile de-
vices. Its also very useful for image eects and to 0.2.2 Transparent Surfaces
understand how GPUs work in general. Program-
Cutaways (about discarding fragments and triangle-
ming vertex and fragment shaders is less useful if
face culling)
the standard lighting of Unity should be taken into
account. (In that case, Surface Shaders are prefer- Transparency (about blending)
able in Unity.)
Order-Independent Transparency (about order-
Second, this part assumes that readers are some- independent blending)
what familiar with Unity. If this is not the case,
readers should consult the rst three sections of the Silhouette Enhancement (about transforming nor-
Unity Manual (Unity Basics, Asset Workow and mal vectors)
The Main Windows).
Third, Unity doesn't distinguish between Cg (the 0.2.3 Basic Lighting
shading language by Nvidia) and DirectX 9-style
HLSL (the shading language in Direct3D 9) since Diuse Reection (about per-vertex diuse lighting
the two languages are very similar; thus, most of and multiple light sources of dierent kinds)
these tutorials also apply to DirectX 9-style HLSL.
Specular Highlights (about per-vertex lighting)
Furthermore, Cg is documented by Nvidias Cg
Tutorial and Nvidias Cg Language Specication. Two-Sided Surfaces (about two-sided per-vertex
However, these descriptions are missing the details lighting)
specic to Unity. On the other hand, Unitys shader
documentation focuses on Unitys ShaderLab syn- Smooth Specular Highlights (about per-pixel light-
tax and assumes that readers are familiar with Cg. ing)
Thus, learning Cg programming in Unity without Two-Sided Smooth Surfaces (about two-sided per-
prior knowledge of Cg can be rather dicult. This pixel lighting)
wikibook tries to close this gap by providing an in-
troduction to Cg programming in Unity without re- Multiple Lights (about for-loops for handling multi-
quiring prior knowledge of Cg. ple light sources in one shader pass)
1
2
Textured Spheres (about texturing a sphere) Screen Overlays (about a direct transformation from
object space to screen space)
Lighting Textured Surfaces (about textures for dif-
fuse lighting) Billboards (about view-aligned projection of ob-
jects)
Glossy Textures (about gloss mapping)
Nonlinear Deformations (about vertex blending)
Transparent Textures (about using alpha textures for Shadows on Planes (about projecting shadows onto
discarding fragments, alpha testing, and blending) planes)
Layers of Textures (about multitexturing) Mirrors (about mirroring objects and stenciling with
the alpha buer)
0.2.5 Textures in 3D
0.2.9 Miscellaneous
Lighting of Bumpy Surfaces (about normal map-
ping) Rotations (about Euler angles and quaternions)
Projection of Bumpy Surfaces (about parallax map- Projection for Virtual Reality (about o-axis per-
ping) spective projection)
Cookies (about projective texture mapping for shap- Bzier Curves (about quadratic Bzier curves and
ing light) splines)
Hermite Curves (about cubic Hermite curves and
Projectors (about projective texture mapping for splines)
projectors)
Specular Highlights at Silhouettes (about the Fresnel [stub:] Water Reection and Refraction (about Wa-
factor for specular reection) ter)
Diuse Reection of Skylight (about hemisphere Image Processing (Unitys grab pass)
lighting) Shadow Maps
Translucent Surfaces (about diuse and forward-
scattered transmission of backlight) 0.3.3 Optimizing Shaders
Translucent Bodies (about diuse lighting with re- Per-Vertex vs. Per-Fragment Computations
duced contrast and transmission of diuse backlight
at silhouettes) Blending and discard (and Alpha Test)
Z Priming
Chroma Keying
Illuminated Lines
Ambient Occlusion by Spheres (soft shadows of
spheres from skylight)
Water Waves (procedural normal mapping)
Animated Sprites
0.4 Links
Unity shader reference: http://docs.unity3d.com/
Manual/SL-Reference.html
< Cg Programming
Unless stated otherwise, all example source code on this
page is granted to the public domain.
4 1 TEXT AND IMAGE SOURCES, CONTRIBUTORS, AND LICENSES
1.2 Images