动态改变Shader的属性 [英] Dynamically change property of Shader

查看:136
本文介绍了动态改变Shader的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Unity 中的游戏对象上有一个着色器,我们称之为 FooShader.在 FooShader 中有一个名为 _fooVal 的公共属性,我想实时更新.

I have a shader on a gameObject in Unity, Lets call it FooShader. In FooShader there is a public property called _fooVal I want to update in real time.

我在同一个游戏对象上创建了一个空白的 C# 脚本.我的第一个问题是如何引用这个 FooShader 着色器?然后一旦我有一个正确的引用更改它的 _fooVal 属性?

I've created a blank C# script on the same gameObject. My first problem is how do I reference this FooShader shader? And then once I have a correct reference change its _fooVal property?

我已经尝试使用以下方法来获取对着色器的引用,但它不起作用.

I've tried using the following to get a reference to the Shader but it doesn't work.

gameObject.material._fooVal

推荐答案

如果我正确理解您的问题,您无法直接访问着色器中的变量,您必须使用材质对象上的方法.尝试使用以下代码获取渲染器组件并在其着色器中设置(例如)浮点数的值:

If I'm understanding your question correctly, you cannot access the variables in a shader directly, you have to use the methods on the material object. Try the following code to get your renderer component and set the value of (for instance) a float in its shader:

Renderer rend = GetComponent<Renderer>();
rend.material.SetFloat("_fooVal", 2.0f /* Your value here */);

这是 Unity3D 材料参考 - 还有 SetIntSetColorSetVector 和其他方法,具体取决于您尝试更新的变量类型.

Here is the Unity3D reference on materials - there are also SetInt, SetColor, SetVector, and other methods depending on what type of variable you are trying to update.

这篇关于动态改变Shader的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆