返回介绍

Material.SetMatrix 设置矩阵

发布于 2019-12-18 15:37:58 字数 5042 浏览 1371 评论 0 收藏 0

JavaScript => public function SetMatrix(propertyName: string, matrix: Matrix4x4): void;
JavaScript => public function SetMatrix(nameID: int, matrix: Matrix4x4): void;
C# => public void SetMatrix(string propertyName, Matrix4x4 matrix);
C# => public void SetMatrix(int nameID, Matrix4x4 matrix);

Description 描述

Set a named matrix for the shader.

为这个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.

这主要用于自定义着色器需要额外矩阵参数。矩阵参数不会将其公开在材质的检视面板中,但可以在脚本中通过SetMatrix 和 GetMatrix设置和查询。

See Also: GetMatrix, Shader.PropertyToID.

JavaScript:

#pragma strict
public var rotateSpeed: float = 30f;
public function Update() {
	// Construct a rotation matrix and set it for the shader
	var rot: var = Quaternion.Euler(0, 0, Time.time * rotateSpeed);
	var m: var = Matrix4x4.TRS(Vector3.zero, rot, Vector3.one);
	GetComponent.<Renderer>().material.SetMatrix("_TextureRotation", m);
}

JavaScript:

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

C#:

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
        var rot = Quaternion.Euler (0, 0, Time.time * rotateSpeed);
        var m = Matrix4x4.TRS (Vector3.zero, rot, Vector3.one);
        GetComponent<Renderer>().material.SetMatrix ("_TextureRotation", m);
    }
}

C#:

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

Material

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文