(五·二)计算机图形学 之 Unity代码调用Shader并修改属性值

这篇具有很好参考价值的文章主要介绍了(五·二)计算机图形学 之 Unity代码调用Shader并修改属性值。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

直接划重点:

在C#代码中,要先引用材质球(Material),然后通过材质球提供的方法比如:

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);

我这里使用material.SetColor("_Color", Color.red);

结构是SetColor(shader属性名称,属性值设置);

_Color是在shader中,Properties{}里定义好的属性,名称要一直,然后就是给他赋值。

Shader代码:

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#代码:

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()
    {
        
    }
}

这里注意的是,要先给脚本绑定(关联)好材质球(Material)。文章来源地址https://www.toymoban.com/news/detail-617060.html

到了这里,关于(五·二)计算机图形学 之 Unity代码调用Shader并修改属性值的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【计算机图形学】二维图形裁剪算法

    Cohen-Sutherland算法 Cohen-Sutherland是最早最流行的算法。 核心思想:通过 编码测试 来减少计算交点的次数。(编码算法) 1. 区域码: 线段端点以区域赋值以四位二进制码。 编码顺序:四位从右到左分别为:左边界、右边界、下边界、上边界。 编码值:落在相应位置为1,否则

    2024年02月02日
    浏览(38)
  • 初识计算机图形学

    笔记来源:【老奇】阴差阳错 撼动世界的游戏引擎 详见本人博客: 1.Transformation 2.梳理从MVP变换到光栅化的过程 MVP变换将空间中3D物体投影到2D屏幕 详见本人博客: 1.Rasterization(光栅化) 2.梳理从MVP变换到光栅化的过程 场景是一个个由三角面组成的模型 将模型投射到像素就

    2024年01月21日
    浏览(39)
  • 计算机图形学——大作业

    绘制一个简单的三维场景,可以是室内:卧室,办公室,教室,也可以是室外:运动场,公园等,加上光照效果,简单的纹理映射,透视投影;不能过于简单;可以加动画、鼠标和键盘交互。     上交材料: project和word文档(具体内容展示,思路和心得) 首先初始化窗口,

    2024年02月11日
    浏览(37)
  • 计算机图形与图像技术

    可以使用Python、Java等语言。 下图中,图中各事物比例失调 如何使用代码去掉某个人(不允许使用抠图工具)?         像素(Pixel)是“图像元素”的缩写, 指的是图像的最小单位 。 它是构成数码图像或屏幕显示图像的基本单元,代表了图像中的一个小点或一个小方块

    2024年02月07日
    浏览(39)
  • 计算机图形学 | 变换与观察

    华中科技大学《计算机图形学》课程 MOOC地址:计算机图形学(HUST) 回顾几何阶段 整体流程: 这其中存在3种变换: 坐标系的变换 模型本身的运动 观察者的运动 几何变换 以上各种变换都可以通过以下变换的复合来计算: 平移 比例 旋转 对称 错切 图形的几何变换是指对图

    2023年04月27日
    浏览(35)
  • 【计算机图形学01】坐标变换

             将坐标变换为标准化设备坐标,接着再转化为屏幕坐标的过程通常是分步进行的,也就是类似于流水线那样子。在流水线中,物体的顶点在最终转化为屏幕坐标之前还会被变换到多个坐标系统(Coordinate System)。将物体的坐标变换到几个 过渡 坐标系(Intermediate Coor

    2024年02月10日
    浏览(31)
  • 计算机图形学(三) -- 3D 变换

    同样引入齐次坐标: 3D 点 = ( x , y , z , 1 ) T (x, y, z, 1)^T ( x , y , z , 1 ) T 3D 向量 = ( x , y , z , 0 ) T (x, y, z, 0)^T ( x , y , z , 0 ) T 通常, ( x , y , z , w ) (x, y, z, w) ( x , y , z , w ) (w != 0) 表示一个坐标为 ( x / w , y / w , z / w ) (x/w, y/w, z/w) ( x / w , y / w , z / w ) 的 3D 点 用一个 4x4 的矩阵来表示

    2024年02月08日
    浏览(28)
  • 【计算机图形学】曲线和曲面

    模块5 曲线和曲面 一 实验目的 编写曲线和曲面的算法 二 实验内容 1 :绘制Bezier曲线,并采用自行设计输入和交互修改数据点的方式。 实验结果如下图所示: 第一步:输入特征多边形的顶点个数,并按照顺序输入顶点的坐标。 第二步:点击左键生成bezier曲线(白色部分)和

    2024年02月06日
    浏览(34)
  • 计算机图形学 期末复习笔记

    目录 第一章-导论 1. 计算机图形学的定义 2. 计算机图形学的应用领域 2.1 计算机图形学与其他学科的关系 3. 图形显示器的发展及其工作原理 3.1 阴极射线管(CRT) 3.2 随机扫描显示器 3.3 直视存储管显示器 3.4 光栅扫描显示器 4. 图形软件标准的形成 5. 三维图形渲染管线 第二章

    2024年02月12日
    浏览(35)
  • 【计算机图形学】三维图形投影和消隐(三视图构造)

    模块4-1 三维图形投影和消隐 一 实验目的 编写三维图形各种变换的投影算法 二 实验内容 1 :自行选择三维物体(不能选长方体),建立坐标系,给定点的三维坐标值,建立边表结构。完成三视图。 实验结果如下图所示: 左上显示为主视图,右上显示为侧视图,右下显示为

    2024年02月01日
    浏览(69)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包