vertex color for hlsl shaders

Started by
6 comments, last by Bulma 18 years, 10 months ago
Hi! i created a 3d model in softimage XSI, painted some vertex colors on it and exportet it as .x model. How can I now use these vertex colors in a HLSL programmed vertex shader?? I tried all the Vertex Shader Inpout Semantics - but it didn't work . . . can someone help me? thx!
Advertisement
The right semantic is COLOR0-COLORn; depending on how the shader is implemented. I don't know if the X-format supports any kind of vertex declaration, however.

Greetz,

Illco
hi Illco!
.x should support vertex colors . . . but i don't know how to use them . . .

when i want to load them with this shader:


////VertexShader:

float4 view_position;
float4 light0;
float4x4 view_proj_matrix;

struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 View : TEXCOORD0;
float3 Normal: TEXCOORD1;
float3 Light1: TEXCOORD2;
float4 vertexcol: COLOR0;
float3 gradient: TEXCOORD5;
};
VS_OUTPUT main( float4 inPos : POSITION,
float3 inNorm : NORMAL,
float4 Col: COLOR )
{
VS_OUTPUT Out = (VS_OUTPUT) 0;

// Output transformed vertex position:
Out.Pos = mul( view_proj_matrix, inPos );

Out.Normal = inNorm;

// Compute the view vector:
Out.View = normalize( view_position - inPos );

// Compute light vector
Out.Light1 = normalize(light0 - inPos); // Light 1

// create gradient
Out.gradient = inPos.y;

//create vertexcolormap
Out.vertexcol=Col;

return Out;
}


/////Pixel Shader

float4 color1;
float4 color2;
float4 main( float3 View: TEXCOORD0,
float3 Normal: TEXCOORD1,
float3 Light1: TEXCOORD2,
float4 vertexcol: COLOR0,
float3 gradient: TEXCOORD5 ) : COLOR
{
// Normalize input normal vector:
float3 norm = normalize (Normal);

float incidence = 1-dot (norm, normalize(View));

float lighting = (dot (normalize (Light1), norm) * 0.5 + 0.5)-0.45;

float4 normals = float4(norm,0.0);
float4 viewdirect = float4(View, 0.0);
float4 Gradient = (float4(gradient,0.0)/4+1)/2;


//return lighting;
//return lerp(color2, color1, Gradient);
//return lerp(color2, color1, Gradient)+lighting;
//return incidence;
return vertexcol;
}




then i get that:

http://neo.cycovery.com/skullcolor.jpg
Maybe try a simpler shader first -- one which just puts on the vertex color and nothing else. Then you can see if the color component is transferred well. With DX a shader 'simple.fx' comes, which pretty much shows how this is done. Then use a graphical editor like EffectEdit (DX) or FX Composer (NVidia); this allows you to see the difference each change makes to the visuals.

Greetz,

Illco
My kneejerk reaction is that

Quote:float4 Col: COLOR )

(from the vertex shader function declaration)

should be

Quote:float4 Col: COLOR0 )
so that leads me to another question about those input output Semantics!

What is the difference between COLOR and COLORn ? or TEXCOORD and TEXCOORDn ?

I found out, that the first uv coordinates from a model for example are stored in TEXCOORD0 . . . but when i generate an vertex output semantic which refers to TEXCOORD0 - what happens then? will the uv coordinates of the 3d model get overrided? what if I don't output a TEXCOORD0? can i then use the original uv coordinates of the model direclty in the Pixelshader?

and what means the :COLOR in this construction:


float4 main( float3 View: TEXCOORD3,
float3 Normal: TEXCOORD1,
float3 Light1: TEXCOORD2,
float4 vertexcol: COLOR0,
float3 gradient: TEXCOORD5,
float2 uv: TEXCOORD0 ) : COLOR
{
//mainfunction of the pixelshader
}
if there's no number on the end of the semantic then it's simply the 0'th one. COLOR == COLOR0, TEXCOORD == TEXCOORD0, etc...

Ok, the actual uv float2 texture coordinate for the model gets sent into the vertex shader with semantic TEXCOORD0 (or just TEXCOORD if you prefer). Now you can take that and return the structure of variables with an included float2 texcoord: TEXCOORDn where n is any number within the max number of texture coordinates... into the pixel shader.

You sent float3 View : TEXCOORD0; to the pixel shader, but the pixel shader is using float2 uv: TEXCOORD0 as the parameter. The semantics matched, your treating view as your uv texture coordinates.

Do this

in the vertex shader, return an included float2 tcoord: TEXCOORDk in the struct. Then have the pixel shader accept a parameter of float2 texcoord: TEXCOORDk, as long as k is the same and within the max number of texture coordinates.
Quote:Original post by genesys
and what means the :COLOR in this construction:


float4 main( float3 View: TEXCOORD3,
float3 Normal: TEXCOORD1,
float3 Light1: TEXCOORD2,
float4 vertexcol: COLOR0,
float3 gradient: TEXCOORD5,
float2 uv: TEXCOORD0 ) : COLOR
{
//mainfunction of the pixelshader
}


It is an output semantic (return value) of pixel shader. It could also be DEPTH if your output is depth value.
Bulma

This topic is closed to new replies.

Advertisement