How to define an array of floats in Shader properties?

五迷三道 提交于 2019-12-05 16:43:11

Apparently not.

I didn't think so, as I'd never seen it, but I thought I'd take a search around and I ran across this thread where the person answering the question says this (emphasis mine):

You can set arrays from script using SetFloatArray, SetVectorArray, and SetColorArray as of 5.4, but you can't define an array in the properties. Basically this means you can still set the value and have it defined in the CGPROGRAM block to be used, but it won't be serialized / saved by the material asset or show up in the material editor. It's an odd omission, especially since texture arrays are supported as properties (though texture arrays are a specific type of texture sampler rather than an array of texture properties like color arrays).

So you would be able to use it in the calculation, but you would only be able to modify the value via MonoBehaviour script (and would need to).

You can't use properties for float[]. But you can use them as variables in your shader and set them from script:

In your shader:

CGPROGRAM
int _SegmentsCount = 0;
float _Segments[1000];

void surf (Input IN, inout SurfaceOutput o) {
    for (int i = 0; i < _SegmentsCount; i++) {
        // This is obsviously just an example,
        // avoid loops in shaders if you can help it.
    }
}

ENDCG

Then in your script:

float [] array = new float[] { 0.25f, 0.75f };
material.SetFloatArray("_Segments", array);
material.SetInt("_SegmentsCount", 2);

renderer.material = material;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!