shader not respecting setColor()

i pasted the sample shader from the manual (http://panda3d.org/manual/index.php/Shader_Basics) into a file called shader_test.sha, like so:

//Cg

void vshader(float4 vtx_position : POSITION,
  float4 vtx_color: COLOR,
  out float4 l_position : POSITION,
  out float4 l_color0 : COLOR0,
  uniform float4x4 mat_modelproj)
{
  l_position=mul(mat_modelproj, vtx_position);
  l_color0=vtx_color;
}

void fshader(
  float4 l_color0 : COLOR0,
  out float4 o_color : COLOR)
{
  o_color=float4(l_color0[1],l_color0[0], l_color0[2],l_color0[3]);
}

next, i created a red card:

import direct.directbase.directStart
from pandac.PandaModules import *
cm = CardMaker('card')
c = render.attachNewNode(cm.generate())
c.setColor(1,0,0,1)

and positioned the camera so i could see the card. finally, i applied the shader:

s = loader.loadShader('shader_test.sha')
c.setShader(s)

and the card turned white (instead of green). why is this? the scene graph still thinks the card is red:

>>> c.getColor()
VBase4(1,0,0,1)

jeremy

What happens if you call setColor on the cardmaker instead of the NodePath?

Lemme explain:
CardMaker.setColor sets the vertex color of the vertices on the card. You can read this out using vtx_color0.
NodePath.setColor adds a color attrib to the node, which you can read out using attr_color.

In the shader, you’re reading out vtx_color0, which is white in this case indeed. To read out the color attrib, use attr_color. Or, work with vertex colors in the first place (which is probably better unless you dynamically plan to change the color every frame or so).

i had one success and one failure.

first, i left the shader unchanged and called setColor() on the CardMaker. i got a red card that turned white when the shader was applied. so that was a failure.

but when i modified the shader to use attr_color on a NodePath with setColor() applied, i got a red card that changed to green when the shader was applied. yay!

I had a similar issue. I recently updated my build of panda and found that vertex colors seemed to be getting lost on some models when using shaders. It seems that panda now optimizes away vertex colors and turns them into node colors when it finds geometry that is one solid color during the egg loading process. It used to turn these solid colors into vertex colors.

In general, when trying to use vertex colors in shaders it will be neccesary to look for ‘attr_color’ as well now.