Setting Self-Illumin/Diffuse Shader properties in C#

How do you set the properties of the Self-Illumin/Diffuse Shader? So far I have:

renderer.material.mainTexture = m_Texture;
renderer.material.shader = Shader.Find(“Self-Illumin/Diffuse”);

which works, but now I want to set the Main Color, Base (RGB) Gloss (A) and Illumin (A) with code.

All help appreciated.

Can you use this

renderer.material.SetColor (“_SpecColor”, Color.red);

or

RGB:
renderer.material.color.r
renderer.material.color.g
renderer.material.color.b
Alpha:
renderer.material.color.a

This is what I’ve been trying to do, and here’s what I’ve seen so far:

gameObject.GetComponent<Renderer>( ).material.SetColor( "Main Color", new Color( 42.0f / 255.0f, 40.0f / 255.0f, 120.0f / 255.0f ) );

Debug.Log( "material " + gameObject.GetComponent<Renderer>( ).material.name + " has " + ( gameObject.GetComponent<Renderer>( ).material.HasProperty( "Main Color" ) ? "" : "NOT " ) + "property Main Color" );

As you can see, here I simply want to assign to the Color property called “Main Color” the colour (42,40,120).
I never got this to work so I tried to check if the property actually exists with this name, hence my debug output which gives this:

material blue (Instance) has property Main Color

So the property DOES exist with this name, (and I assume) sees its value set to (42,40,120), and yet, nothing happens at all, the colour stays like it is in the prefab.

However, as the game is running, would I try to change the “Main Color” property on this particular instance via the Inspector, it WILL change.

Also, if I do this:

gameObject.GetComponent<Renderer>( ).material.color = new Color( 42.0f / 255.0f, 40.0f / 255.0f, 120.0f / 255.0f );

Then it works and the colour does change. But it seems that changing a shader/material property via the code isn’t quite working, unless I missed something.
And I will very soon need to be able to change some material/shader properties via the code, ones that aren’t directly member of the Material structure, like Material.color or Material.mainTexture are.

Help!

Cheers.

Is it like this??

gameObject.GetComponent<Renderer>( ).material.SetColor("_Color", new Color (0.2f, 0.3f, 0.4f));

“_Color” is the main color.

Yup, eriksen, thanks, that worked.
BUT, this material is using the ‘Bumped Specular’ shader which I can’t edit and see how things are named in there.
All I know is:

  • that in the Inspector, it’s called ‘Main Color’ (or there’s a property called that)
  • that when I ask in code if the property ‘Main Color’ exists, it says YES
  • that when I changed this property directly in the inspector it DOES change the color
  • that doing the same thing in the code DOESN’T work

Is the ‘Main Color’ property implicitly called ‘_Color’ internally?
Any other pleasant surprise like this?

Thanks!

take a look here:

Does that mean the ‘Main Color’ property found in the ‘Bumped Specular’ shader actually points to ‘_Color’?

I still don’t get why we should see exposed this property (‘Main Color’), be able to succesfully use it to change the material main colour via the Inspector, but not via code… unless you call it ‘_Color’ instead of ‘Main Color’.

Bearing in mind that none of those:

“_Color”
“_SpecColor”
"_Emission
“_ReflectColor”

appear in the inspector’s fields for that material, as opposed to:

“Main Color”
“Specular Color”
“Shininess”
“Base (RGB) Gloss (A)”
“Bumpmap (RGB)”

which do appear.

To sum up, I dislike the fact that you can’t succesfully use in the code the public properties you can see exposed in the material’s inspector.
And I’m worried that if I were to add my own property to one of my own shaders, I would stumble upon the same issue (being unable to change the value passed to the shader from the code), but then there wouldn’t be any ‘default’ properties that would do the same thing.

I’m pretty sure that it does.

That is properly to make it more understandable for non-programmers that they use the expression “Main Color” in the Inspector and not “_Color”

I see.
Surely though, that can’t apply to all properties. I’m thinking about properties I would add myself to the shader. This won’t find a default property as it is quite specific to the very shader I’m working with. I do hope that if I was to try and change it in the code by calling the property’s name I would have set myself, it would work.

But… I still consider as an error the fact that calling in the code the ‘Main Color’ property - which DO exist, I made that clear - doesn’t work like it should :twisted:

When you make your own shader a properties would look like this:

   Properties {
        _Color ("Main Color", Color) = (1,1,1,0.5)
        _SpecColor ("Spec Color", Color) = (1,1,1,1)
        _Emission ("Emmisive Color", Color) = (0,0,0,0)
        _Shininess ("Shininess", Range (0.01, 1)) = 0.7
        _MainTex ("Base (RGB)", 2D) = "white" { }
    }

So as you can se the “_Color” is the Internal name and “Main Color” is just the Inspector name.

Ah, thanks! That certainly put some light on the issue!

Not having the source code of this shader wasn’t really helping ^^

I see now that there is an ‘exposed name’ and an ‘internal name’ for each property. That certainly isn’t convenient when you don’t have the source of a shader and are trying to use it in the code :stuck_out_tongue:

So why do we see the ‘exposed names’ as existing properties when we can’t actualy use them for anything?
(I’m thinking about Material.HasProperty returning ‘true’ when passed the ‘exposed name’ as parameter)

I don’t know why the Material.HasProperty looks for the Inspector name.
Does it work if you use material.HasProperty(“_Color”)?

Maybe it just looks for both internal and inspector name.

It certainly does.
What got me confused in the first place was that it returned true when you ask it for the Inspector name, which wrongfully tends to make you confident that you can safely use that property using said name.