Creating a Randomly Colored Cube in Panda3D

  • Share this:

Code introduction


This function creates a randomly colored cube and adds it to the Panda3D scene. It uses the Panda3D geometry and node system to build the cube and sets the color of the cube.


Technology Stack : Panda3D

Code Type : Panda3D Application

Code Difficulty : Intermediate


                
                    
def generate_random_cube(color, size):
    from direct.showbase.ShowBase import ShowBase
    from panda3d.core import Geom, GeomNode, BitMask32
    from panda3d.core import Vec4

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

            # Create a cube
            cube = Geom.makeCube(size)
            cubeNode = GeomNode('Cube')
            cubeNode.addGeom(cube)
            cubeNode.setBin('fixed', 0)
            cubeNode.setDepthTest(True, BitMask32.allOff())

            # Set the color of the cube
            colorVec = Vec4(color[0], color[1], color[2], 1)
            cubeNode.setColor(colorVec)

            # Add the cube to the scene
            self.rootNode().addChild(cubeNode)

    app = RandomCubeApp()
    app.run()                
              
Tags: