GLSL Programming/Unity

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

GLSL programming in the game engine Unity is considerably easier than GLSL programming for an OpenGL, OpenGL ES, or WebGL 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 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, Windows users have to use the command-line argument -force-opengl [1] when starting Unity in order to be able to use GLSL shaders; for example, by changing the Target setting in the properties of the desktop icon to: "C:\Program Files\Unity\Editor\Unity.exe" -force-opengl . (On MacOS X, OpenGL and therefore GLSL is used by default.) Note that GLSL shaders cannot be used in Unity applications running in web browsers on Windows.
  • Secondly, 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 [2] (Unity Basics, Building Scenes, Asset Import and Creation).
  • Furthermore, Unity supports a version of GLSL similar to version 1.0.x for OpenGL ES 2.0 (the specification is available at the “Khronos OpenGL ES API Registry”); however, Unity's shader documentation [3] focuses on shaders written in Unity's own “surface shader” format and Cg/HLSL [4]. There are only very few details documented that are specific to GLSL shaders [5]. Thus, this wikibook might also help to close some gaps in Unity's documentation. However, optimizations (see, for example, this blog) are usually not discussed.

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 GLSL 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]

Some additional tutorials about graphics programming in Unity without shaders are available in the Cg Programming wikibook.

Links[edit]

< GLSL Programming

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