# SetMatrix

> Sets a named matrix for the shader.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.7/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## SetMatrix(string, Matrix4x4)

Sets a named matrix for the shader.

```csharp
public void SetMatrix(string name, Matrix4x4 value)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): Property name, e.g. "\_CubemapRotation".**** (\[Matrix4x4]\(/engine/6000.7/script-reference/unityengine/matrix4x4)): Matrix value to set.

### Remarks

This is mostly used with custom shaders that need extra matrix parameters. Matrix parameters are not exposed in the material inspector, but can be set and queried with `SetMatrix` and `GetMatrix` from scripts.

Additional Resources: [Material.GetMatrix](/engine/6000.7/script-reference/unityengine/material/getmatrix.md), [Materials](/engine/6000.7/manual/materials-and-shaders/materials.md), [ShaderLab documentation](/engine/6000.7/script-reference/unityengine/shaders.md), [Shader.PropertyToID](/engine/6000.7/script-reference/unityengine/shader/propertytoid.md), [Properties in Shader Programs](/engine/6000.7/manual/materials-and-shaders/shaders/writing-custom/shader-writing/writing-shader-change-properties/sl-properties-in-programs.md).

### Examples

```csharp
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    // Attach to an object that has a Renderer component,
    // and use material with the shader below.
    public float rotateSpeed = 30f;
    public void Update()
    {
        // Construct a rotation matrix and set it for the shader
        Quaternion rot = Quaternion.Euler(0, 0, Time.time * rotateSpeed);
        Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, rot, Vector3.one);
        GetComponent<Renderer>().material.SetMatrix("_TextureRotation", m);
    }
}
```

```csharp
// Use this shader on an object together with the above example script.
// The shader transforms texture coordinates with a matrix set from a script.
Shader "RotatingTexture"
{
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
            };

            float4x4 _TextureRotation;

            v2f vert (float4 pos : POSITION, float2 uv : TEXCOORD0)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(pos);
                o.uv = mul(_TextureRotation, float4(uv,0,1)).xy;
                return o;
            }

            sampler2D _MainTex;
            fixed4 frag (v2f i) : SV_Target
            {
                return tex2D(_MainTex, i.uv);
            }
            ENDCG
        }
    }
}
```

## SetMatrix(int, Matrix4x4)

Sets a named matrix for the shader.

```csharp
public void SetMatrix(int nameID, Matrix4x4 value)
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Property name ID, use [Shader.PropertyToID](/engine/6000.7/script-reference/unityengine/shader/propertytoid.md) to get it.**** (\[Matrix4x4]\(/engine/6000.7/script-reference/unityengine/matrix4x4)): Matrix value to set.

### Remarks

This is mostly used with custom shaders that need extra matrix parameters. Matrix parameters are not exposed in the material inspector, but can be set and queried with `SetMatrix` and `GetMatrix` from scripts.

Additional Resources: [Material.GetMatrix](/engine/6000.7/script-reference/unityengine/material/getmatrix.md), [Materials](/engine/6000.7/manual/materials-and-shaders/materials.md), [ShaderLab documentation](/engine/6000.7/script-reference/unityengine/shaders.md), [Shader.PropertyToID](/engine/6000.7/script-reference/unityengine/shader/propertytoid.md), [Properties in Shader Programs](/engine/6000.7/manual/materials-and-shaders/shaders/writing-custom/shader-writing/writing-shader-change-properties/sl-properties-in-programs.md).

### Examples

```csharp
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    // Attach to an object that has a Renderer component,
    // and use material with the shader below.
    public float rotateSpeed = 30f;
    public void Update()
    {
        // Construct a rotation matrix and set it for the shader
        Quaternion rot = Quaternion.Euler(0, 0, Time.time * rotateSpeed);
        Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, rot, Vector3.one);
        GetComponent<Renderer>().material.SetMatrix("_TextureRotation", m);
    }
}
```

```csharp
// Use this shader on an object together with the above example script.
// The shader transforms texture coordinates with a matrix set from a script.
Shader "RotatingTexture"
{
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
            };

            float4x4 _TextureRotation;

            v2f vert (float4 pos : POSITION, float2 uv : TEXCOORD0)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(pos);
                o.uv = mul(_TextureRotation, float4(uv,0,1)).xy;
                return o;
            }

            sampler2D _MainTex;
            fixed4 frag (v2f i) : SV_Target
            {
                return tex2D(_MainTex, i.uv);
            }
            ENDCG
        }
    }
}
```
