How to apply bump-mapping into a 3d model

Hello Panda3D :D.
I do have a question regarding 3d models, I have a model with no texture or material, I was able to add the texture in the code, but now I want to make it bump-mapping, and I didn’t know how. So in that case what should we do to add a bump mapping into a 3d model which has no texture mapping?

I’ve tried the following code, but it did not work:

from panda3d.core import Texture, TextureStage

# Load diffuse texture and create a TextureStage
diffuse_tex = loader.loadTexture('path/to/diffuse_texture.png')
diffuse_stage = TextureStage('diffuse-stage')
diffuse_stage.setMode(TextureStage.MModulate)

# Load normal texture and create a TextureStage
normal_tex = loader.loadTexture('path/to/normal_texture.png')
normal_stage = TextureStage('normal-stage')
normal_stage.setMode(TextureStage.MNormal)

# Create a GeomNode and apply textures
geom_node = my_model_node.find('**/+GeomNode')
if geom_node:
    # Apply diffuse texture
    geom_node.setTexGen(diffuse_stage, TexGenAttrib.MWorldPosition)
    geom_node.setTexture(diffuse_stage, diffuse_tex)

    # Apply normal texture
    geom_node.setTexGen(normal_stage, TexGenAttrib.MWorldPosition)
    geom_node.setTexture(normal_stage, normal_tex)

I think that the main thing that’s missing might simply be that in Panda, normal-mapping is provided via shaders. As a result, seeing the effect of your normal-map might call for either activating the automatic shader-generator, or applying a shader of your own that implements normal-mapping.

The former (the automatic shader-generator) is perhaps the simpler option. Presuming that you have no other shaders in effect, you should be able to apply the shader-generator by calling “setShaderAuto()” on either the NodePath that you want to have so shaded, or on a NodePath above it in the scene-graph (e.g. a “world” or “level” node, or even just “render”). Like so:

    myNodePath.setShaderAuto()

You should find more details in the following manual-page:
https://docs.panda3d.org/1.10/python/programming/shaders/shader-generator

[edit] Note removed due to being inaccurate. My apologies!

1 Like
from direct.showbase.ShowBase import ShowBase
from panda3d.core import TextureStage, PointLight, NodePath

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)

        diffuse_texture = base.loader.load_texture("brick_d.jpg")
        diffuse_stage = TextureStage('diffuse-stage')
        diffuse_stage.set_mode(TextureStage.M_modulate)

        normal_texture = base.loader.load_texture("brick_n.jpg")
        normal_stage = TextureStage('normal-stage')
        normal_stage.set_mode(TextureStage.M_normal)

        model = base.loader.load_model("test.egg")
        model.set_texture(diffuse_stage, diffuse_texture, 1)
        model.set_texture(normal_stage, normal_texture, 1)
        model.reparent_to(render)

        plight = PointLight('my plight')
        plight_np = NodePath(plight)
        plight_np.set_pos(0, -5, 5)
        plight_np.reparent_to(render)
        render.set_light(plight_np)

        render.set_shader_auto()

app = MyApp()
app.run()

test.egg (6.8 KB)

I think the problem is that in your model there are no tangents and binormals that are necessary to calculate the illumination of the relief, and also a light source is needed.

2 Likes

Thank you guys for helping me :slight_smile:
Well @serega-kkz, do you have any idea of how to create those binormals and tangents in Panda3d?

If you have procedural generation…

This is usually exported from a 3D editor, and usually in tangent space.

All aspects of shading are explained very well here

2 Likes