You can download this code by clicking the button below.
This code is now available for download.
The code creates a randomly colored cube and adds it to the Panda3D scene. It uses Panda3D's geometry, materials, color, and scene management features.
Technology Stack : The code uses Panda3D's geometry, materials, color, and scene management features.
Code Type : The type of code
Code Difficulty :
def create_random_cube(size, color):
from direct.showbase.ShowBase import ShowBase
from panda3d.core import CullFace, GeomNode, Geom, VertexFormat
from panda3d.graphics import GraphicsStateGuardian
from panda3d.core import Material
class RandomCubeApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.size = size
self.color = color
self.create_cube()
def create_cube(self):
# Define the vertex format
vertex_format = VertexFormat.get_default()
vertex_format.add_vertex_data(3) # Add position data
# Create the geometry
cube = Geom()
cube.add_vertices(8, vertex_format)
# Define the vertices of the cube
vertices = [
(-size/2, -size/2, -size/2),
(size/2, -size/2, -size/2),
(size/2, size/2, -size/2),
(-size/2, size/2, -size/2),
(-size/2, -size/2, size/2),
(size/2, -size/2, size/2),
(size/2, size/2, size/2),
(-size/2, size/2, size/2)
]
# Add vertices to the geometry
for i, vertex in enumerate(vertices):
cube.add_vertex(vertex)
# Create a geom node
geom_node = GeomNode('cube_geom')
geom_node.addGeom(cube)
# Create material
material = Material('cube_material')
material.setShade(Material.ShadeSmooth)
material.setAmbient(color)
geom_node.setMaterial(material)
# Set the culling face
gsg = GraphicsStateGuardian.getGlobalStateGuardian()
gsg.setCulling(CullFace.CullNone)
# Add the geom node to the scene
self.rootNode().addChild(geom_node)
# Run the application
app = RandomCubeApp()
app.run()