Question by
Xylord · Apr 24, 2016 at 01:48 AM ·
unity 5shadershadersshader programmingvertex shader
Very basic shader problem
Hey guys,
I'm trying to get started with shaders, and the very first thing I'm trying to do isn't working.
I just want to vary pixel color with vertex position.
Here is the shader :
Shader "Custom/FirstAttempt" {
Properties {
_Tint("Tint", Color) = (1, 1, 1, 1)
}
SubShader {
Pass {
CGPROGRAM
float4 _Tint;
#pragma vertex MyVertexProgram
#pragma fragment MyFragmentProgram
#include "UnityCG.cginc"
float4 MyVertexProgram(
float4 position : POSITION,
out float3 localPosition : TEXCOORD0
) : SV_POSITION {
localPosition = position.xyz;
return mul(UNITY_MATRIX_MVP, position);
}
float4 MyFragmentProgram(
float3 localPosition : TEXCOORD0
) : SV_TARGET {
return float4(localPosition, 1);
}
ENDCG
}
}
}
But all I'm getting with my material using this shader is a black sphere, as if localPosition was always 0.
Anyone would know what my mistake could be?
shad.png
(10.8 kB)
Comment
Basically re-learned shaders from a better documentation, apparently. It works now!
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
fixed3 color : COLOR0;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
o.color = v.vertex;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return fixed4(i.color + 0.5, 1);
}