Hi guys:
I’m trying to learn how to write my own shader using cg. But I can not access the shader properties in my cg program. I’m doing what the following page told me, but it did not work.
And here is my code:
Shader “CGTest_PureColor” {
Properties {
_Color (“Main Color”, Color) = (0.5,0.5,0.5,1)
}
SubShader {
Pass{
CGPROGRAM
#pragma fragment frag
#include “UnityCG.cginc”
uniform float4 _Color;
float4 frag() : COLOR
{
float4 c = _Color;
return _Color;
}
ENDCG
}
}
}
It’s very simple, I only want to color all the primitive to _Color. But whatever the “Main Color” is in inspector, it always be black.
I have to correct something here. The function frag() is actually look like this:
float4 frag() : COLOR
{
float4 c = _Color;
return c;
}
Then I change the float4 to half4, it still does not work.
But if I use solid color, like this:
half4 frag() : COLOR
{
half4 c = half4(1, 0, 0, 1);
return c;
}
It does work. The primitive becomes red.
Why is this? Can anyone give me an explanation? Thx.
That is weird. It looks like you need an input structure for it to work, even if it’s just position. This works as you’d expect:
Shader "CGTest_PureColor" {
Properties {
_Color ("Main Color", Color) = (0.5,0.5,0.5,1)
}
SubShader {
Pass {
CGPROGRAM
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : POSITION;
};
uniform float4 _Color;
float4 frag(v2f v) : COLOR {
return _Color;
}
ENDCG
}
}
}