RigidBodyCombiner and color scaling problem

I find that when i make my models as children of a RigidBodyCombiner PathNode I get very weird results. The rgb values of the models seem to be scaled about in half. Also physically scaling the node using setScale also makes the model darker if the scale is less than 1 and brighter and if the scale is greater than 1. My workaround at the moment actually is making the model half as big in blender then using setScale(2.0). Is there something i’m missing, is this expected behavior?

I should mention that the problems go away when they are children of a regular PathNode

this sounds VERY strange. can you provide an minimal example demonstrating that behavior?

from direct.directbase.DirectStart import *
from pandac.PandaModules import *
import random
rbc = RigidBodyCombiner("rbc")
rbcnp = NodePath(rbc)
rbcnp.reparentTo(render)

for i in range(200):
    pos = Vec3(random.uniform(-100, 100),
               random.uniform(-100, 100),
               0)
    f = loader.loadModel("cube.egg")
    f.setScale(0.5)
    f.setPos(pos)
    f.reparentTo(rbcnp)
  
camera_light = PointLight('camera_light')
camera_light_node = camera.attachNewNode(camera_light)
render.setLight(camera_light_node)

rbc.collect()
run()

where this is cube.egg:

<CoordinateSystem> { Z-up }

<Material> Material {
  <Scalar> diffr {1.0}
  <Scalar> diffg {1.0}
  <Scalar> diffb {1.0}
  <Scalar> specr {0.25}
  <Scalar> specg {0.25}
  <Scalar> specb {0.25}
  <Scalar> shininess {12.5}
}
<Group> Cube {
  <Transform> {
    <Matrix4> {
      0.000000 1.000000 -0.000000 0.000000
      -1.000000 0.00000 0.000000 0.000000
      0.000000 0.000000 1.000000 0.000000
      0.00000 0.000000 0.000000 1.000000
    }
  }

  <VertexPool> Cube {
    <Vertex> 0 {
      -0.5 0.5 -0.5
    }
    <Vertex> 1 {
      0.5 0.5 -0.5
    }
    <Vertex> 2 {
      0.5 -0.5 -0.5
    }
    <Vertex> 3 {
      -0.5 -0.5 -0.5
    }
    <Vertex> 4 {
      -0.5 0.5 0.5
    }
    <Vertex> 5 {
      -0.5 -0.5 0.5
    }
    <Vertex> 6 {
      0.5 -0.5 0.5
    }
    <Vertex> 7 {
      0.5 0.5 0.5
    }
    <Vertex> 8 {
      -0.5 0.5 -0.5
    }
    <Vertex> 9 {
      -0.5 0.5 0.5
    }
    <Vertex> 10 {
      0.5 0.5 0.5
    }
    <Vertex> 11 {
      0.5 0.5 -0.5
    }
    <Vertex> 12 {
      0.5 0.5 -0.5
    }
    <Vertex> 13 {
      0.5 0.5 0.5
    }
    <Vertex> 14 {
      0.5 -0.5 0.5
    }
    <Vertex> 15 {
      0.5 -0.5 -0.5
    }
    <Vertex> 16 {
      0.5 -0.5 -0.5
    }
    <Vertex> 17 {
      0.5 -0.5 0.5
    }
    <Vertex> 18 {
      -0.5 -0.5 0.5
    }
    <Vertex> 19 {
      -0.5 -0.5 -0.5
    }
    <Vertex> 20 {
      -0.5 0.5 0.5
    }
    <Vertex> 21 {
      -0.5 0.5 -0.5
    }
    <Vertex> 22 {
      -0.5 -0.5 -0.5
    }
    <Vertex> 23 {
      -0.5 -0.5 0.5
    }
  }
  <Polygon> {
    <MRef> { Material }
    <Normal> { 0.000000 0.000000 -1.000000 }
    <VertexRef> { 0 1 2 3 <Ref> { Cube } }
  }
  <Polygon> {
    <MRef> { Material }
    <Normal> { 0.000000 0.000000 1.000000 }
    <VertexRef> { 4 5 6 7 <Ref> { Cube } }
  }
  <Polygon> {
    <MRef> { Material }
    <Normal> { -0.000000 1.000000 0.000000 }
    <VertexRef> { 8 9 10 11 <Ref> { Cube } }
  }
  <Polygon> {
    <MRef> { Material }
    <Normal> { 1.000000 0.000000 -0.000000 }
    <VertexRef> { 12 13 14 15 <Ref> { Cube } }
  }
  <Polygon> {
    <MRef> { Material }
    <Normal> { 0.000000 -1.000000 -0.000000 }
    <VertexRef> { 16 17 18 19 <Ref> { Cube } }
  }
  <Polygon> {
    <MRef> { Material }
    <Normal> { -1.000000 -0.000000 0.000000 }
    <VertexRef> { 20 21 22 23 <Ref> { Cube } }
  }
}

you should see 200 completely grey squares despite the diffuse color being white.

hm thats indeed strange. if i’d had to guess i would’ve said the vertex-normals are faulty. beeing scaled along with the rest of the node which should not happen. thought this is just a guess. drwr should be able to tell exactly what wrong. i’d concider this a bug.

whew, I thought i was just being crazy :smiley:. Good to know that this is something legitimate.

It’s not precisely a bug, but it is a limitation with the RigidBodyCombiner. The short answer: you can solve this by adding the line:

rbcnp.setAttrib(RescaleNormalAttrib.make(RescaleNormalAttrib.MNormalize))

The reason it happens is that scale operations scale normals as well as everything else, which will affect the lighting equation if the normal scale isn’t corrected later. Normally, when the scale is applied via the scene graph, Panda can detect the scale and compensate for it automatically, so you’re not aware of it happening. However, when the RigidBodyCombiner is used, it doesn’t render transforms in the same way. Instead, it converts the transforms on the scene graph into vertex-manipulation operations, and this automatic counter-scale by Panda is no longer so automatic. Thus, the scale of 0.5 gets applied to your normals and doesn’t get counterscaled, and your light brightness gets correspondingly reduced by half.

So, the solution is to apply a RescaleNormalAttrib to the node, which sets the rescale mode to MNormalize, which means to always normalize the normals before computing the lighting equation. This completely avoids any problems with scale and the normals. This is also a slightly more expensive render mode than the default, which is why it isn’t applied all the time; but in this case it’s still cheaper than not using the RigidBodyCombiner, so it’s worthwhile.

David