Book Image

Unity 3 Game Development HOTSHOT

By : Jate Wittayabundit
Book Image

Unity 3 Game Development HOTSHOT

By: Jate Wittayabundit

Overview of this book

<p>Unity 3d is the game engine of choice for creating professional looking games at no cost. Its combination of powerful tools and outstanding community support make it the natural choice for experienced and aspiring game developers. <br /><br />Unity3D Game Development Hotshot will show you how to exploit the full array of Unity3Dtechnology in order to create an advanced gaming experience for the user. It has eight exciting and challenging projects with step- by-step explanations, diagrams, screenshots, and downloadable materials.<br /><br />Every project is designed to push your Unity skills to the very limits and beyond. You will create a hero/heroine for a role playing game. Create a menu for the RPG game allowing you to customize your character with powerups, armor, and weapons. You will shade, model, rig, and animate your hero/heroine. The end result will be a&nbsp; character on the level of Final Fantasy, far superior to a simple sprite.<br /><br />Now for some damage - rocket launchers! Typically the most powerful weapons in any first person shooter, you will create a rocket launcher that has fire and smoke particles and most importantly causes splash damage for that all important area effect. Create AI controlled enemies for your hero/heroine to eliminate with the rocket launcher. Forge&nbsp; a destructible&nbsp; interactive world so if the rocket launchers miss their target they will at least cause significant damage to the surrounding environment. Learn to save and load your game so you can take a break from the action for life’s necessities like going to the bathroom. Incorporate social gaming by uploading scores online so everyone can see the carnage.</p>
Table of Contents (19 chapters)
Unity 3 Game Development HOTSHT
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Cg/HLSL programming


This section presents a brief description of how to access the shader Properties in Cg/HLSL programming, and the data types and common methods used in Cg/HLSL programming.

Accessing shader properties in Cg/HLSL

Shader can be declared with properties in a Properties block. If you want to access some of those properties in a Cg/HLSL shader program, you need to declare a Cg/HLSL variable with the same name and a matching type.

Example:

Properties {
    _MainTex ("Texture", 2D) = "white" {}
  }

  SubShader {
    ......
    CGPROGRAM
    sampler2D _MainTex;
    ...

Property types to Cg/HLSL variable types are as follows:

  • Color and Vector properties map to float4 variables.

  • Range and Float properties map to float variables.

  • Texture properties map to sampler2D variables for regular (2D) textures. CUBE and RECT textures map to samplerCUBE and samplerRECT variables, respectively.

Data type

Cg/HLSL has six basic data types. Some of them are the same as in C, while others are especially added for GPU programming. These types are:

Date type

Description

float

A 32-bit floating point number (high precision floating point.

Generally 32 bits, just like float type in regular programming languages).

half

A 16-bit floating point number (medium-precision floating point. Generally 16 bits, with a range of -60000 to +60000 and 3.3 decimal digits of precision)

int

A 32-bit integer.

fixed

A 12-bit fixed point number (low-precision fixed point. Generally 11 bits, with a range of -2.0 to +2.0 and 1/256th precision).

bool

A Boolean variable (FALSE = 0, TRUE = 1).

sampler*

Represents a texture object (sampler1D, sampler2D, sampler3D, samplerCUBE, samplerRECT).

Cg/HLSL also features vector and matrix data types that are based on the basic data types, such as float3 and float4x4. Such data types are quite common when dealing with 3D graphics programming. Cg/HLSL also has struct and array data types, which work in a similar way to their C equivalents.

Common methods to create shaders

Method

Description

dot( a, b )

Dot product of two vectors.

cross( A , B )

Cross product of vectors A and B; A and B must be three-component vectors.

max( a, b )

Maximum of a and b.

min( a , b )

Minimum of a and b.

floor( x )

Get largest integer not greater than x.

round( x )

Get closest integer to x.

ceil( x )

Get smallest integer not less than x.

pow( x , y )

Computes x raised to the power y.

normalize( v )

Returns a vector of length 1 that points in the same direction as vector v.

saturate( x )

Clamps x to the [0, 1] range.

tex2D(sampler, x )

2D texture lookup (sampled data at the location indicated by the texture coordinate set in the sampler object).

The preceding methods are the common methods that you can use to create your shader with Cg/HLSL. There are a lot of methods that you can also use in Cg/HLSL.

For more details, you can refer to the following site:

http://http.developer.nvidia.com/CgTutorial/cg_tutorial_appendix_e.html.

Tip

Note that UnpackNormal( x ) is the method that is provided by Unity to unpack the normal or bump texture, which you can find in the UnityCG.cginc file inside Unity {unity install path}/Data/CGIncludes/UnityCG.cginc on Windows, and in /Applications/Unity/Unity.app/Contents/CGIncludes/UnityCG.cginc on Mac.