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.

PreliminariesEdit

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.

TutorialsEdit

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!)

BasicsEdit

Transparent SurfacesEdit

Basic LightingEdit

Basic TexturingEdit

Textures in 3DEdit

Environment MappingEdit

Variations on LightingEdit

Non-Standard Vertex TransformationsEdit

Image Effects and Compute ShadersEdit

MiscellaneousEdit

Missing TutorialsEdit

Basic Features of UnityEdit

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

Optimizing ShadersEdit

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

Other TopicsEdit

  • [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

LinksEdit

< Cg Programming

Unless stated otherwise, all example source code on this page is granted to the public domain.