6.1.10 Providing data to shaders

Any data declared as a uniform in the Pass block is available to both vertex and fragment shaders.

A uniform can be considered as a type of global constant variable because it cannot be modified inside the shader.
You can supply this uniform to the shader in the following ways:
The Properties block enables you to define uniforms interactively in the Inspector. Any variable declared in the Properties block appears in the material inspector listed with the variable name.
The following code shows the Properties block of the shader example associated to material ctSphereMat:
Properties
{
	_AmbientColor ("Ambient Color", Color) = (0.2,0.2,0.2,1.0)
	_MainTex ("Base (RGB)", 2D) = "white" {}
}
		
The variables _AmbientColor and _MainTex declared in the Properties block with the names Ambient Color and Base (RGB) respectively appear in the Material Inspector with those names.
The following figure shows Properties in Material Inspector:
Figure 6-5 Properties in Material Inspector

Passing data to the shader by means of the Properties block is very useful especially when you are in the development stage of the shader because you can change the data interactively and see the effect at run time.
You can put the following types of variables in the Properties block:
The Properties block is not a useful way of passing data if for example, data is required from a previous calculation or data is required to be passed at specific point in time.
An alternative method of passing data to the shaders is programmatically from a script.
The material class exposes several methods that you can use to pass data associated with a material to a shader. The following table lists the most common methods:

Table 6-2 Common methods for passing data associated with a material to a shader

Method
SetColor (propertyName: string, color: Color);
SetFloat (propertyName: string, value: float);
SetInt (propertyName: string, value: int);
SetMatrix (propertyName: string, matrix: Matrix4x4);
SetVector (propertyName: string, vector: Vector4);
SetTexture (propertyName: string, texture: Texture);
In the following code, immediately before the main camera renders the scene, a secondary camera shwCam renders the shadows to a texture to be combined with the main camera render pass.
For the shadow texture projection process the vertices must be transformed in a convenient manner. The shadow camera projection matrix (shwCam.projectionMatrix), world to local transformation matrix (shwCam.transform.worldToLocalMatrix), and the rendered shadow texture (m_ShadowsTexture) are passed to the shader.
These values are available in the shader as uniforms with the names _ShwCamProjMat, _ShwCamViewMat and m_ShadowsTexture.
The following code shows how matrices and textures are sent to the shader by means of materials contained in the list shwMats.
// Called before object is rendered. 

public void OnWillRenderObject()
{
	// Perform different checks.
	...
	CreateShadowsTexture();
	// Set up shadows camera shwCam.
	...
	// Pass matrices to the shader
	for(int i = 0; i < shwMats.Count; i++)
	{
		shwMats[i].SetMatrix("_ShwCamProjMat", shwCam.projectionMatrix);
		shwMats[i].SetMatrix("_ShwCamViewMat", shwCam.transform.worldToLocalMatrix);
	}
	// Render shadows texture
	shwCam.Render();
	for(int i = 0; i < shwMats.Count; i++)
	{
		shwMats[i].SetTexture( "_ShadowsTex", m_ShadowsTexture );
	}
	s_RenderingShadows = false;
}
		
Non-ConfidentialPDF file icon PDF versionARM 100140_0201_00_en
Copyright © 2014, 2015 ARM. All rights reserved.