Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Cg Programming/Unity

Cg programming in the game engine Unity is consid- 0.2 Tutorials


erably easier than Cg programming for an OpenGL or
Direct3D application. Import of meshes and images Note that the tutorials assume that you read them in the
(i.e. textures) is supported by a graphical user interface; order in which they are presented here, i.e. each tutorial
mipmaps and normal maps can be computed automati- will assume that you are familiar with the concepts and
cally; the most common vertex attributes and uniforms techniques introduced by previous tutorials. If you are
are predened; OpenGL and Direct3D states can be set new to Cg or Unity you should at least read through the
by very simple commands; etc. tutorials in the Basics section. (Now you are warned!)

0.1 Preliminaries 0.2.1 Basics

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

0.2.4 Basic Texturing 0.2.8 Non-Standard Vertex Transformations

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)

0.3 Missing Tutorials


0.2.6 Environment Mapping
0.3.1 Basic Features of Unity
Reecting Surfaces (about reection mapping)

Curved Glass (about refraction mapping) Fog


Lightmaps
Skyboxes (about rendering of environment maps as
background) Particles

Many Light Sources (about image-based lighting) Halos


Spherical Harmonic Lighting
0.2.7 Variations on Lighting Generate Cube Texture with ReadPixels

Brushed Metal (about anisotropic specular reec-


tion) 0.3.2 Features Related to RenderTexture

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)

Soft Shadows of Spheres (about rendering the um- Precision of Variables


bra and penumbra of spheres) Sampling of Textures
Toon Shading (about non-photorealistic rendering) Other Recommendations
0.4 Links 3

0.3.4 Other Topics

Z Priming
Chroma Keying

Illuminated Lines
Ambient Occlusion by Spheres (soft shadows of
spheres from skylight)
Water Waves (procedural normal mapping)

Curved Mirrors (raytraced reections)


Shadow Volumes without Stencil Buer

Animated Sprites

Procedural Textures (noise)


Glitter Eect

0.4 Links
Unity shader reference: http://docs.unity3d.com/
Manual/SL-Reference.html

Shaders in the Unify 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/

< 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 Text and image sources, contributors, and licenses


1.1 Text
Cg Programming/Unity Source: https://en.wikibooks.org/wiki/Cg_Programming/Unity?oldid=3107184 Contributors: Martin Kraus, Su-
perherosk123, Baozhenliang, BitBarrel and Anonymous: 1

1.2 Images

1.3 Content license


Creative Commons Attribution-Share Alike 3.0

You might also like