Unity Shader(给Shader传递用户数据)
通过代码修改Shader中的属性
前面我有讲到可以定义各种类型的属性,然后在面板上可以进行更改。但是其实我们也可以在代码中进行修改。
Properties
{
_Color("I am Color", Color) = (1,1,1,1)//依次为RGBA(0-1)
_Int("I am Int", Int) = 1
}
public Material material;
void Start()
{
material.SetInt("_Int", 200);
material.SetColor("_Color", new Color(0, 0, 0, 0));
}
在Pass中自定义数据类型然后赋值
Shader "Unlit/NewUnlitShader"
{
Properties
{
_Color("I am Color", Color) = (1,1,1,1)//依次为RGBA(0-1)
_Int("I am Int", Int) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float b2;
RWStructuredBuffer<float> b5;
ENDCG
}
}
}
public class NewBehaviourScript : MonoBehaviour
{
public Material material;
ComputeBuffer b5Buffer;
void Start()
{
material.SetInt("_Int", 200);
material.SetColor("_Color", new Color(0, 0, 0, 0));
material.SetFloat("float", 998);
b5Buffer = new ComputeBuffer(24, 4);
material.SetBuffer("b5", b5Buffer);
}
}
|