Cg Programming/Unity

Cg programming in the game engine Unity is considerably easier than Cg programming for an OpenGL or Direct3D 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 and Direct3D states can be set by very simple commands; etc.

Preliminaries
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. A few points should be noted:
 * First, this part is about vertex and fragment shaders in the standard 3D built-in pipeline of Unity. This kind of shader programming is particularly flexible and allows you to program very efficient shaders, which is often important when rendering performance is limited, e.g., on mobile devices. It's also very useful for image effects and to understand how GPUs work in general. Programming vertex and fragment shaders is less useful if the standard lighting of Unity should be taken into account. (In that case, Surface Shaders and Shader Graph are preferable in Unity.)
 * Second, this part assumes that readers are somewhat familiar with Unity. If this is not the case, readers should consult the first few sections of the Unity Manual (Unity Basics, Asset Workflow and The Main Windows).
 * Third, Unity doesn't distinguish between Cg (the shading language by Nvidia) and DirectX 9-style HLSL (the shading language in Direct3D 9) since the two languages are very similar; thus, most of these tutorials also apply to DirectX 9-style HLSL.
 * Furthermore, Cg is documented by Nvidia's Cg Tutorial and Nvidia's Cg Language Specification. However, these descriptions are missing the details specific to Unity. On the other hand, Unity's shader documentation focuses on Unity's ShaderLab syntax and assumes that readers are familiar with Cg. Thus, learning Cg programming in Unity without prior knowledge of Cg can be rather difficult. This wikibook tries to close this gap by providing an introduction to Cg programming in Unity without requiring prior knowledge of Cg.

Tutorials
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 Cg or Unity you should at least read through the tutorials in the “Basics” section. (Now you are warned!)

Basics

 * Minimal Shader (about shaders, materials, and game objects)
 * RGB Cube (about vertex output parameters)
 * Debugging of Shaders (about vertex input parameters)
 * 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)
 * Displacement Maps (about texture lookups in vertex shaders)
 * Cookies (about projective texture mapping for shaping light)


 * 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)
 * Outlining Objects (about enlarging objects to render an outline using the stencil buffer)
 * Nonlinear Deformations (about vertex blending)
 * Shadows on Planes (about projecting shadows onto planes)

Image Effects and Compute Shaders

 * Minimal Image Effect (about basic image post-processing with fragment shaders)
 * Computing Image Effects (about using compute shaders for image effects)
 * Computing Color Histograms (about computing a color histogram with a compute shader)
 * Computing the Brightest Pixel (about finding the brightest pixel with a compute shader)

Miscellaneous

 * Bézier Curves (about quadratic Bézier curves and splines)
 * Hermite Curves (about cubic Hermite curves and splines)
 * Rotations (about Euler angles and quaternions)
 * Projection for Virtual Reality (about off-axis perspective projection)
 * Mirrors (about rendering plane mirrors with render textures)
 * Portals (about rendering a portal or magic lens into another scene using the stencil buffer)

Basic Features of Unity

 * Fog
 * Lightmaps
 * Particles
 * Halos
 * Spherical Harmonic Lighting
 * Generate Cube Texture with ReadPixels
 * Unity Graphic pipeline

Optimizing Shaders

 * Per-Vertex vs. Per-Fragment Computations
 * Blending and discard (and Alpha Test)
 * Precision of Variables
 * Sampling of Textures
 * Other Recommendations

Other Topics

 * [stub:] Water Reflection and Refraction (about Water)
 * Shadow Maps
 * Z Priming
 * Chroma Keying
 * Illuminated Lines
 * Ambient Occlusion by Spheres (soft shadows of spheres from skylight)
 * Water Waves (procedural normal mapping)
 * Curved Mirrors (raytraced reflections)
 * Shadow Volumes without Stencil Buffer
 * Animated Sprites
 * Procedural Textures (noise)
 * Glitter Effect

Links

 * Unity shader reference: http://docs.unity3d.com/Manual/SL-Reference.html
 * Shaders in the Unity Community Wiki: http://wiki.unity3d.com/index.php/Shaders
 * Unity user forum about shaders: http://forum.unity3d.com/forums/16-ShaderLab
 * Video tutorials on graphics: http://unity3d.com/learn/tutorials/topics/graphics
 * Blog entry about a SIGGRAPH presentation on shader optimization: http://blogs.unity3d.com/2011/08/18/fast-mobile-shaders-talk-at-siggraph/