Cg Programming/Unity

From Wikibooks, open books for an open world
Jump to: navigation, search

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[edit]

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, 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 [1] (Unity Basics, Building Scenes, Asset Import and Creation).
  • Second, Unity doesn't distinguish between Cg (the shading language by Nvidia) and HLSL (the shading language in Direct3D) since the two languages are very similar; thus, most of these tutorials also apply to 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 [2] focuses on shaders written in Unity's own “surface shader” format, while the documentation of shaders in Cg/HLSL is very limited [3]. 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[edit]

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[edit]

Transparent Surfaces[edit]

Basic Lighting[edit]

Basic Texturing[edit]

Textures in 3D[edit]

Environment Mapping[edit]

Variations on Lighting[edit]

Non-Standard Vertex Transformations[edit]

Graphics without Shaders[edit]

Missing Tutorials[edit]

Basic Features of Unity[edit]

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

Features of Unity Pro[edit]

  • Image Processing (Unity's grab pass)
  • Shadow Maps

Optimizing Shaders[edit]

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

Other Topics[edit]

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

< Cg Programming

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