Unity - スクリプティング API: MaterialPropertyBlock.AddVector ­
言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

MaterialPropertyBlock.AddVector

public void AddVector(int nameID, Vector4 value);

Description

Vector型のマテリアルプロパティを追加します

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Mesh aMesh;
    public Material aMaterial = new Material(Shader.Find("VertexLit"));
    void Update() {
        MaterialPropertyBlock materialProperty = new MaterialPropertyBlock();
        materialProperty.Clear();
        materialProperty.AddVector("_Color", new Vector4(1, 0, 0, 0.5F));
        Graphics.DrawMesh(aMesh, new Vector3(0, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
        materialProperty.Clear();
        materialProperty.AddVector("_Color", new Vector4(0, 1, 0, 0.5F));
        Graphics.DrawMesh(aMesh, new Vector3(5, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
        materialProperty.Clear();
        materialProperty.AddVector("_Color", new Vector4(0, 0, 1, 0.5F));
        Graphics.DrawMesh(aMesh, new Vector3(-5, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
    }
}

/nameID/ でこの関数を呼び出すほうが高速になります。もし、繰り返し同じ名前のプロパティを追加する場合は、 Shader.PropertyToID関数から/nameID/を取得して利用した方が高速に動作します。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Mesh aMesh;
    public Material aMaterial = new Material(Shader.Find("VertexLit"));
    void Update() {
        MaterialPropertyBlock materialProperty = new MaterialPropertyBlock();
        int tagID = Shader.PropertyToID("_Color");
        materialProperty.Clear();
        materialProperty.AddVector(tagID, new Vector4(1, 0, 0, 0.5F));
        Graphics.DrawMesh(aMesh, new Vector3(0, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
        materialProperty.Clear();
        materialProperty.AddVector(tagID, new Vector4(0, 1, 0, 0.5F));
        Graphics.DrawMesh(aMesh, new Vector3(5, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
        materialProperty.Clear();
        materialProperty.AddVector(tagID, new Vector4(0, 0, 1, 0.5F));
        Graphics.DrawMesh(aMesh, new Vector3(-5, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
    }
}