Legacy Documentation: Version 5.4
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Material.SetMatrix

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public function SetMatrix(propertyName: string, matrix: Matrix4x4): void;
public void SetMatrix(string propertyName, Matrix4x4 matrix);
public function SetMatrix(nameID: int, matrix: Matrix4x4): void;
public void SetMatrix(int nameID, Matrix4x4 matrix);

Parameters

propertyName Property name, e.g. "_CubemapRotation".
nameID Property name ID, use Shader.PropertyToID to get it.
matrix Matrix value to set.

Description

Set a named matrix for the shader.

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.

See Also: GetMatrix, Materials, ShaderLab documentation, Shader.PropertyToID, Properties in Shader Programs.

#pragma strict
public var rotateSpeed: float = 30f;
public function Update() {
	// Construct a rotation matrix and set it for the shader
	var rot: Quaternion = Quaternion.Euler(0, 0, Time.time * rotateSpeed);
	var m: Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rot, Vector3.one);
	GetComponent.<Renderer>().material.SetMatrix("_TextureRotation", m);
}
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); } }
// 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 = mul(UNITY_MATRIX_MVP, 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 } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961