(5.2) Unity code of computer graphics calls Shader and modifies property values

Straight to the point:

In the C# code, first reference the material ball (Material), and then use the methods provided by the material ball, such as:

public void SetBuffer(string name, ComputeBuffer value);
        //
        // 摘要:
        //     Sets a named buffer value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name.
        //
        //   value:
        //     The ComputeBuffer or GraphicsBuffer value to set.
        public void SetBuffer(int nameID, ComputeBuffer value);
        //
        // 摘要:
        //     Sets a named buffer value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name.
        //
        //   value:
        //     The ComputeBuffer or GraphicsBuffer value to set.
        public void SetBuffer(string name, GraphicsBuffer value);
        //
        // 摘要:
        //     Sets a named buffer value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name.
        //
        //   value:
        //     The ComputeBuffer or GraphicsBuffer value to set.
        public void SetBuffer(int nameID, GraphicsBuffer value);
        //
        // 摘要:
        //     Sets a named color value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_Color".
        //
        //   value:
        //     Color value to set.
        public void SetColor(string name, Color value);
        //
        // 摘要:
        //     Sets a named color value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_Color".
        //
        //   value:
        //     Color value to set.
        public void SetColor(int nameID, Color value);
        //
        // 摘要:
        //     Sets a color array property.
        //
        // 参数:
        //   name:
        //     Property name.
        //
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   values:
        //     Array of values to set.
        public void SetColorArray(int nameID, Color[] values);
        //
        // 摘要:
        //     Sets a color array property.
        //
        // 参数:
        //   name:
        //     Property name.
        //
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   values:
        //     Array of values to set.
        public void SetColorArray(string name, Color[] values);
        public void SetColorArray(string name, List<Color> values);
        public void SetColorArray(int nameID, List<Color> values);
        //
        // 摘要:
        //     Sets a ComputeBuffer or GraphicsBuffer as a named constant buffer for the material.
        //
        // 参数:
        //   name:
        //     The name of the constant buffer to override.
        //
        //   value:
        //     The ComputeBuffer to override the constant buffer values with, or null to remove
        //     binding.
        //
        //   offset:
        //     Offset in bytes from the beginning of the buffer to bind. Must be a multiple
        //     of SystemInfo.constantBufferOffsetAlignment, or 0 if that value is 0.
        //
        //   size:
        //     The number of bytes to bind.
        //
        //   nameID:
        //     The shader property ID of the constant buffer to override.
        public void SetConstantBuffer(int nameID, ComputeBuffer value, int offset, int size);
        //
        // 摘要:
        //     Sets a ComputeBuffer or GraphicsBuffer as a named constant buffer for the material.
        //
        // 参数:
        //   name:
        //     The name of the constant buffer to override.
        //
        //   value:
        //     The ComputeBuffer to override the constant buffer values with, or null to remove
        //     binding.
        //
        //   offset:
        //     Offset in bytes from the beginning of the buffer to bind. Must be a multiple
        //     of SystemInfo.constantBufferOffsetAlignment, or 0 if that value is 0.
        //
        //   size:
        //     The number of bytes to bind.
        //
        //   nameID:
        //     The shader property ID of the constant buffer to override.
        public void SetConstantBuffer(string name, GraphicsBuffer value, int offset, int size);
        //
        // 摘要:
        //     Sets a ComputeBuffer or GraphicsBuffer as a named constant buffer for the material.
        //
        // 参数:
        //   name:
        //     The name of the constant buffer to override.
        //
        //   value:
        //     The ComputeBuffer to override the constant buffer values with, or null to remove
        //     binding.
        //
        //   offset:
        //     Offset in bytes from the beginning of the buffer to bind. Must be a multiple
        //     of SystemInfo.constantBufferOffsetAlignment, or 0 if that value is 0.
        //
        //   size:
        //     The number of bytes to bind.
        //
        //   nameID:
        //     The shader property ID of the constant buffer to override.
        public void SetConstantBuffer(int nameID, GraphicsBuffer value, int offset, int size);
        //
        // 摘要:
        //     Sets a ComputeBuffer or GraphicsBuffer as a named constant buffer for the material.
        //
        // 参数:
        //   name:
        //     The name of the constant buffer to override.
        //
        //   value:
        //     The ComputeBuffer to override the constant buffer values with, or null to remove
        //     binding.
        //
        //   offset:
        //     Offset in bytes from the beginning of the buffer to bind. Must be a multiple
        //     of SystemInfo.constantBufferOffsetAlignment, or 0 if that value is 0.
        //
        //   size:
        //     The number of bytes to bind.
        //
        //   nameID:
        //     The shader property ID of the constant buffer to override.
        public void SetConstantBuffer(string name, ComputeBuffer value, int offset, int size);
        //
        // 摘要:
        //     Sets a named float value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   value:
        //     Float value to set.
        //
        //   name:
        //     Property name, e.g. "_Glossiness".
        public void SetFloat(int nameID, float value);
        //
        // 摘要:
        //     Sets a named float value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   value:
        //     Float value to set.
        //
        //   name:
        //     Property name, e.g. "_Glossiness".
        public void SetFloat(string name, float value);
        public void SetFloatArray(string name, List<float> values);
        public void SetFloatArray(int nameID, List<float> values);
        //
        // 摘要:
        //     Sets a float array property.
        //
        // 参数:
        //   name:
        //     Property name.
        //
        //   nameID:
        //     Property name ID. Use Shader.PropertyToID to get this ID.
        //
        //   values:
        //     Array of values to set.
        public void SetFloatArray(string name, float[] values);
        //
        // 摘要:
        //     Sets a float array property.
        //
        // 参数:
        //   name:
        //     Property name.
        //
        //   nameID:
        //     Property name ID. Use Shader.PropertyToID to get this ID.
        //
        //   values:
        //     Array of values to set.
        public void SetFloatArray(int nameID, float[] values);
        //
        // 摘要:
        //     Sets a named integer value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   value:
        //     Integer value to set.
        //
        //   name:
        //     Property name, e.g. "_SrcBlend".
        public void SetInt(int nameID, int value);
        //
        // 摘要:
        //     Sets a named integer value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   value:
        //     Integer value to set.
        //
        //   name:
        //     Property name, e.g. "_SrcBlend".
        public void SetInt(string name, int value);
        //
        // 摘要:
        //     Sets a named matrix for the shader.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_CubemapRotation".
        //
        //   value:
        //     Matrix value to set.
        public void SetMatrix(int nameID, Matrix4x4 value);
        //
        // 摘要:
        //     Sets a named matrix for the shader.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_CubemapRotation".
        //
        //   value:
        //     Matrix value to set.
        public void SetMatrix(string name, Matrix4x4 value);
        //
        // 摘要:
        //     Sets a matrix array property.
        //
        // 参数:
        //   name:
        //     Property name.
        //
        //   values:
        //     Array of values to set.
        //
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        public void SetMatrixArray(int nameID, Matrix4x4[] values);
        public void SetMatrixArray(int nameID, List<Matrix4x4> values);
        //
        // 摘要:
        //     Sets a matrix array property.
        //
        // 参数:
        //   name:
        //     Property name.
        //
        //   values:
        //     Array of values to set.
        //
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        public void SetMatrixArray(string name, Matrix4x4[] values);
        public void SetMatrixArray(string name, List<Matrix4x4> values);
        //
        // 摘要:
        //     Sets an override tag/value on the material.
        //
        // 参数:
        //   tag:
        //     Name of the tag to set.
        //
        //   val:
        //     Name of the value to set. Empty string to clear the override flag.
        public void SetOverrideTag(string tag, string val);
        //
        // 摘要:
        //     Activate the given pass for rendering.
        //
        // 参数:
        //   pass:
        //     Shader pass number to setup.
        //
        // 返回结果:
        //     If false is returned, no rendering should be done.
        [FreeFunction("MaterialScripting::SetPass", HasExplicitThis = true)]
        public bool SetPass(int pass);
        //
        // 摘要:
        //     Enables or disables a Shader pass on a per-Material level.
        //
        // 参数:
        //   passName:
        //     Shader pass name (case insensitive).
        //
        //   enabled:
        //     Flag indicating whether this Shader pass should be enabled.
        [FreeFunction("MaterialScripting::SetShaderPassEnabled", HasExplicitThis = true)]
        public void SetShaderPassEnabled(string passName, bool enabled);
        //
        // 摘要:
        //     Sets a named texture.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_MainTex".
        //
        //   value:
        //     Texture to set.
        //
        //   element:
        //     Optional parameter that specifies the type of data to set from the RenderTexture.
        public void SetTexture(int nameID, RenderTexture value, RenderTextureSubElement element);
        //
        // 摘要:
        //     Sets a named texture.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_MainTex".
        //
        //   value:
        //     Texture to set.
        //
        //   element:
        //     Optional parameter that specifies the type of data to set from the RenderTexture.
        public void SetTexture(string name, RenderTexture value, RenderTextureSubElement element);
        //
        // 摘要:
        //     Sets a named texture.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_MainTex".
        //
        //   value:
        //     Texture to set.
        //
        //   element:
        //     Optional parameter that specifies the type of data to set from the RenderTexture.
        public void SetTexture(int nameID, Texture value);
        //
        // 摘要:
        //     Sets a named texture.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_MainTex".
        //
        //   value:
        //     Texture to set.
        //
        //   element:
        //     Optional parameter that specifies the type of data to set from the RenderTexture.
        public void SetTexture(string name, Texture value);
        //
        // 摘要:
        //     Sets the placement offset of texture propertyName.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, for example: "_MainTex".
        //
        //   value:
        //     Texture placement offset.
        public void SetTextureOffset(int nameID, Vector2 value);
        //
        // 摘要:
        //     Sets the placement offset of texture propertyName.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, for example: "_MainTex".
        //
        //   value:
        //     Texture placement offset.
        public void SetTextureOffset(string name, Vector2 value);
        //
        // 摘要:
        //     Sets the placement scale of texture propertyName.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_MainTex".
        //
        //   value:
        //     Texture placement scale.
        public void SetTextureScale(int nameID, Vector2 value);
        //
        // 摘要:
        //     Sets the placement scale of texture propertyName.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_MainTex".
        //
        //   value:
        //     Texture placement scale.
        public void SetTextureScale(string name, Vector2 value);
        //
        // 摘要:
        //     Sets a named vector value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_WaveAndDistance".
        //
        //   value:
        //     Vector value to set.
        public void SetVector(int nameID, Vector4 value);
        //
        // 摘要:
        //     Sets a named vector value.
        //
        // 参数:
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        //
        //   name:
        //     Property name, e.g. "_WaveAndDistance".
        //
        //   value:
        //     Vector value to set.
        public void SetVector(string name, Vector4 value);
        //
        // 摘要:
        //     Sets a vector array property.
        //
        // 参数:
        //   name:
        //     Property name.
        //
        //   values:
        //     Array of values to set.
        //
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        public void SetVectorArray(int nameID, Vector4[] values);
        //
        // 摘要:
        //     Sets a vector array property.
        //
        // 参数:
        //   name:
        //     Property name.
        //
        //   values:
        //     Array of values to set.
        //
        //   nameID:
        //     Property name ID, use Shader.PropertyToID to get it.
        public void SetVectorArray(string name, Vector4[] values);
        public void SetVectorArray(int nameID, List<Vector4> values);
        public void SetVectorArray(string name, List<Vector4> values);

I use material.SetColor("_Color", Color.red);

The structure is SetColor(shader attribute name, attribute value setting);

_Color is a property defined in Properties{} in the shader, the name must be constant, and then it is assigned a value.

Shader code:

Shader "GFSudy/Myshader1"
{
    //属性 可以在编辑器中查看和修改
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
		Pass{
			CGPROGRAM  // 插入Cg代码的开始

			//定义一个结构体
			struct my_struct{
				int a;
			}; //结尾一定要有分号

			//定义一个函数
			float sum(float a,float b)
			{
				return a + b;
			}


			fixed4 _Color;  //首先定义同样名字的变量 才可以访问到
			sampler2D _MainTex;

			#pragma vertex my_vert // 把my_vert作为顶点Shader的入口
			// 如何获取上一个模块的参数呢?: 语义绑定bind  输入输出
			float4 my_vert(float4 pos : POSITION) : POSITION
			{
				return UnityObjectToClipPos(pos);
			}

			#pragma fragment my_frag // 把my_frag作为片元Shader的入口
			fixed4 my_frag():COLOR
			{
				//return fixed4(1.0,0.0,0.0,1.0);  //这里直接把颜色设置为了红色
				return _Color;  //这里使用属性里的颜色
			}


			ENDCG	// 插入Cg代码的结束
		}
    }
    FallBack "Diffuse"
}

C# code:

using UnityEngine;

public class ShaderCtrl : MonoBehaviour
{
    public Material material;
    // Start is called before the first frame update
    void Start()
    {
        material.SetColor("_Color", Color.red);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Note here that the script must be bound (associated) with the material ball (Material).

Guess you like

Origin blog.csdn.net/u013774978/article/details/130095435