public void SetMatrix (string name, Matrix4x4 value);
public void SetMatrix (int name, Matrix4x4 value);

参数

nameID属性名称 ID,使用 Shader.PropertyToID 获取。
name属性名称,例如“_CubemapRotation”。
value要设置的矩阵值。

描述

为着色器设置指定的矩阵。

这主要用于需要额外矩阵参数的自定义 着色器。矩阵参数不会在材质 Inspector 中公开, 但是可以在脚本中使用 SetMatrixGetMatrix 进行设置和查询。

另请参阅:GetMatrixMaterialsShaderLab documentationShader.PropertyToIDProperties in Shader Programs

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 = 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 } } }