Shader Variables Data Types in Unity
Shader Variables Data Types in Unity

Shader Variables Data Types in Unity

No alt text provided for this image


Why we use different variable types when writing shaders?

Shader code is for every pixel(or vertex), therefor efficient variable types are crucial for running time and memory. e.g.(an iPhone X have around 2.7 million pixels).

Also, when we are writing code in Unity Shader Lab, we are only writing enough code for the shader to compile.

No alt text provided for this image


Basic Data Types:

float
  • Size: (32 Bits) Highest Precision
  • Usage examples: World Positions, Texture coordinates
half
  • Size: (16 Bits) Half float
  • Usage examples: short vectors, directions, and dynamic colors
fixed
  • Size: (11 bits) Lowest float precision
  • Usage examples: regular colors and simple color operations
int
  • Size(32 Bits) the regular C# int
  • Usage examples: counters and array indices

Texture Data Types:


sampler2D
Textures are represented by sampler2D(data type)


  • Usage examples: regular 2D textures


samplerCUBE
Cube Maps are represented by samplerCUBE(data type)


  • Usage examples: for Cube maps

sampler2D and samplerCUBE have a half value (for low precision) and float value (for high precision).

Packed Arrays

for all the basic data types we can create a packed array

we can create a packed array for any of basic data type

The syntax of declaring a packed array is as the following:

Name of DataType LengthOfTheArray NameOfTheVariable = (VALUES);

fixed4 color= (0,0,1,1);

In regular coding, Access to values in an array is by using two square brackets that enclose the index> arr[1]=2

In Shader coding, Access to values in an array is by using(r,g,b,a) or (x,y,z,w)

fixed4 color= (0,0,1,1);

color.rgb >> (0,0,1)

Also, you can transfer values between variables:

fixed3 color2= color.rgb;

we are assigning the first three values of color into color2(which the capacity of three)

You can also swizzle values between two variables (Swizzling & Masking)

fixed3 color3= color.gbr

color3.rgb >> (0,1,0) > Green Color

A packed array can be initialized using a single value (Smearing)

fixed4 color4=1;

color4.rgb >>(1,1,1,1)

Packed Matrices

For data structures bigger than arrays there are packed matrices

No alt text provided for this image

Packed Matrices are used to store transformations, rotations, and scales, Image Source: The Amazing King

Packed matrices can be created for any basic type following this syntax:

NameOfDataType NumberOfRows x NumberOfColumns NameOfVariable;

float4x4 matrix;

Accessing values & Assigning values to the matrix follow this syntax:

m ._ RowNumberColumnNumber

Element at first row and first column

matrix._m00=1

Element at 2nd row and third column

matrix._m12=2

We start counting from zero & Row Number is before Column number

Packed Matrices support Chaining (storing matrix data in a packed array)

float4 pos= matrix._m00_m01_m02_m03;

You can store the whole matrix row also in the packed array

float4 pos=matrix[0];

Notes:

rgba structure can represent color using the Additive color model, however, it can be used for accessing values from arrays that represent data other than colors such as positions.

Additive Color Model

No alt text provided for this image


In Unity Editor Color Picker the default is RGB (0-255), you can change it to follow the shader RGB (0,1)

I am currently looking for a Unity Dev job (Remote or in the Bay Area).

If you know someone might be interested, my email is ah1053@stanford.edu

Rendering Pipeline in Unity

Introduction to Rendering Pipeline in Unity

Understanding Mesh Anatomy in Unity

In order to shade a model we need to understand its geometric structure

To view or add a comment, sign in

More articles by Ahmed Schrute

  • Genetic Evolution (Behavioral Traits) Simulation using Unity3D

    This is a simulation of Genetic Evolution with a focus on Behavioral traits passed between generation, In this…

  • Vertex Fragment Shader Structure

    This article is a continuum of shader coding articles using Shader lab in Unity, I highly recommend you visit my two…

  • Rendering Queues in Unity

    A lot of times, we need to render certain objects(Player, Gem …etc) to the front even there is an object overlaying our…

  • Normal Mapping

    If you have read the Lighting in Unity 1 and Lighting in Unity2, you have seen how important normal vectors are for…

    2 Comments
  • Light in Unity 2

    Intro to Blinn Phong Lighting model In the previous article, We have been through Lambert Lighting Model and how it…

  • Light in Unity 1

    Introduction to Lighting in Unity and Lambert Lighting Lighting in Computer Graphics is all about calculating the Pixel…

  • Understanding Mesh Anatomy in Unity

    In order to shade a model we need to understand its geometric structure Let’s take a cube as an example of a mesh model…

  • Rendering Pipeline In Unity

    Introduction to Rendering Pipeline in Unity Rendering Rendering is the process of drawing a scene on the computer…

  • Unity CPU Profiling

    Enhancing unity CPU performance I usually have a routine before I start inspecting the CPU usage tap which is opening…

Explore content categories