Proper Use of TextureStage::set_color()?


#1

I’ve been trying to change the colour of a multitexture on a model during runtime by calling the set_color method on the stage by assigning a button to get colour values to set. Example:

void CharacterGen::enter() {
  make_ui_elements();
  layer1 = new TextureStage("layer1");
  layer1->set_sort(1);
  layer1->set_mode(TextureStage::Mode::M_blend);
  set_model();
}

void CharacterGen::make_ui_elements() {
  apply_button = new PGButton("ApplyButton");
  apply_button->setup("Apply");

  apply_np = m_a->window->get_aspect_2d().attach_new_node(apply_button);
  apply_np.set_scale(0.05);
  apply_np.set_pos(0, 0, -0.3);

  m_a->framework.define_key(apply_button->get_click_event(MouseButton::one()),
                            "button press",
                            &CharacterGen::apply_color,
                            (void*)this);
}

void CharacterGen::set_model() {
  player_char = m_a->window->load_model(
    m_a->framework.get_models(),
    "../media/models/panda-model");

  tex1 = TexturePool::get_global_ptr()->load_texture("../media/textures/cookie_cutter.png", 0, false);

  player_char.set_texture(layer1, tex1);

  player_char.reparent_to(m_a->window->get_render());
}

void CharacterGen::apply_color(const Event *e, void* data) {
  CharacterGen* chargen=(CharacterGen*) data;

  chargen->layer1->set_color(LVector4f(
                               chargen->slider1->get_value() / 255,
                               chargen->slider2->get_value() / 255,
                               chargen->slider3->get_value() / 255,
                               1));
//where layer1 is of type TextureStage*, sliders PGSliderBar* etc
}

I removed some code because I thought it was unrelated, but essentially I call that apply_color() function after the model is made and the TextureStage/Texture is applied with a button. However, I noticed that the colour does not change! It stays black or whatever I initialise it at. Further, if I add some code to apply_color() and tell it to redo “player_char.set_texture(layer1, tex1);”, it works! Buuuut I notice that for every new colour input to the TextureStage that it leaks memory for some reason.

I’m not sure if I’m trying to use it in the wrong way, but the manual suggests that it should work (though doesn’t exactly specify if it works at runtime or not). Am I doing something wrong or is this a bug?


#2

Hi, welcome to the forums! :slight_smile:

It should work. You should be able to change the color on an active texture stage without needing to do anything else. A quick test on my end confirms this.

Which version of Panda are you using? Are you perchance enabling the shader generator using set_shader_auto()? The shader generator previously was unable to deal with runtime changes of some settings, but this issue is no longer present in recent development builds.


#3

I’m using the latest stable (1.9.4) and yeah, I had set_shader_auto() somewhere. I never would have figured out that had something to do with it. Thanks for the speedy reply!


#4

Great. I would encourage you to try out development builds of Panda3D, which have this issue resolved.