- Home /
Question by
g-pechorin · Jul 13, 2015 at 11:25 AM ·
shadercgcs
Can I read `CGPROGRAM` `#define` constants in `.cs` at runtime?
I have a DX9 shader doing a variant of monte-carlo sampling. It needs to be loaded from a .cs
behaviour. I want to extract the array size rather than hard-code it.
On Fridays it has a definition;
// i am a .shader file
// ...
#define splat_count 16
float4 splats[splat_count];
// ...
On Tuesdays we like to set it to;
// i am a .shader file too
// ...
#define splat_count 128
float4 splats[splat_count];
// ...
I'd like ms .cs
to say something like ...
// i am a .cs behaviour on the shaded object
// ...
private int splat_count;
void Start() {
splat_count = GetComponent<MeshRenderer> ().material.GetInt("splat_count");
for (int i = 0; i < splat_count; ++i) {
... load splat data ...
}
}
// ...
... but I am forced to do ...
// i am also a .cs behaviour on the shaded object
// ...
const int splat_count = 32; // oops! 32 is Wednesday's number ... I want to put the number in only one place ;)
void Start() {
for (int i = 0; i < splat_count; ++i) {
... load splat data ...
}
}
// ...
This is undesirable and annoying to my sense-of-smell.
Is there a way to;
read a constant out of a shader?
check a shader for a uniform by name?
perform some high-level wizard stuff and peel the value out?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220611231102im_/https://answers.unity.com/themes/thub/images/avi.jpg)