简体   繁体   English

Unity Shader Graph - How to Change Parameters — 如何同时更改所有对象上的网格参数?

[英]Unity Shader Graph - How to Change Parameters — How to change a parameter of a mesh on all objects at the same time?

I created my first shader by following this video.我按照这个视频创建了我的第一个着色器。 Dissolve Shader Video Link溶解着色器视频链接

In a Coroutine,i took the Material's component from the object:在协程中,我从 object 中获取了 Material 的组件:

PalaceMaterial = palaceInstance.GetComponent <Renderer> () .material;

I set the parameter to 0:我将参数设置为0:

PalaceMaterial.SetFloat ("_ CutoffHeight", 0);

To have this effect, I had to modify the parameter "CuttoffHeight" gradually putting it in Update ()为了达到这个效果,我不得不逐渐修改参数“CuttoffHeight”,把它放到Update()中

 PalaceMaterial.SetFloat("_CutoffHeight", Mathf.MoveTowards(PalaceMaterial.GetFloat("_CutoffHeight"), CutoffHeight, 100 * Time.deltaTime));

I applied this to all objects.我将此应用于所有对象。 Every 2 seconds i instantiate an "invisible object" (CutoffHeight set to 0) and in Update() it change the value gradually.每 2 秒我实例化一个“不可见对象”(CutoffHeight 设置为 0),并在 Update() 中逐渐改变值。 Now I would like to change this parameter to all objects that use this shader simultaneously.现在我想将此参数更改为同时使用此着色器的所有对象。 I hope i was clear.我希望我很清楚。

Video of my project: Project , Dissolve_Effect我的项目视频: Project , Dissolve_Effect

I would like to change the "_CutoffHeight" parameter simultaneously in all the cubes that are in the first platform, then in the second one, etc. etc. Do you have any idea?我想同时在第一个平台中的所有立方体中更改“_CutoffHeight”参数,然后在第二个平台中,等等。你知道吗?

In general you shouldn't additionally use GetFloat but rather store the value locally and calculate it from there.一般来说,您不应该另外使用GetFloat而是将值存储在本地并从那里计算。


Then this already should work if multiple objects share the exact same material by using eg如果多个对象通过使用例如共享完全相同的材料,那么这已经可以工作了

GetComponent<Renderer>().sharedMaterial.SetFloat(...);

but this works only as long as you don't have multiple instances of the material in your renderers.但这仅在您的渲染器中没有材质的多个实例时才有效。


There is a way to set a property for all Shaders (which have that property) and their according instances (= Materials) I came across in this thread有一种方法可以为我在这个线程中遇到的所有着色器(具有该属性)及其相应的实例(=材质)设置一个属性

To do this you have to为此,您必须

  • Disable "exposed" for the according property.禁用相应属性的“暴露”。 Apparently it only works for properties that are not exposed in the Inspector.显然,它仅适用于检查器中未公开的属性。

  • Use the generated shader property ID you can find in the Inspector of the shader asset itself.使用您可以在着色器资产本身的 Inspector 中找到的生成着色器属性 ID。

    You can also retrieve it using Shader.PropertyToID您还可以使用Shader.PropertyToID检索它

    You can also change it to match your actual parameter name.您也可以更改它以匹配您的实际参数名称。

  • Use the global setter methods of the shader like Shader.SetGlobalXY使用着色器的全局设置方法,如Shader.SetGlobalXY

在此处输入图像描述

eg in order to set the Spec Brightness you would do例如,为了设置你会做的Spec Brightness

Shader.SetGlobalFloat("Vector1_DD757871", value);

So in your case you could probably have one single controller doing something like所以在你的情况下,你可能有一个 controller 做类似的事情

// Need this only if you have to get the initial value once
[SerializeField] private Material PalaceMaterial;

private float currentValue;
private string propertyId;

private void Awake()
{
    propertyId = Shader.PropertyToID("_CutoffHeight");
    currentValue = PalaceMaterial.GetFloat("_CutoffHeight");
}

private void Update()
{
    currentValue = Maths.MoveTowards(currentValue, CutoffHeight, 100 * Time.deltaTime);
    Shader.SetGlobalFloat(propertyId, currentValue);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM