Random Lighting and Auto-Destruction for Panda3D Models

  • Share this:

Code introduction


This function creates a random directional light and ambient light applied to a Panda3D model, and destroys the light after a random period of time.


Technology Stack : Panda3D, Python, random, panda3d.core, panda3d.task

Code Type : The type of code

Code Difficulty :


                
                    
def panda3d_random_lighting(model, intensity=1.0):
    from panda3d.core import LightNode, DirectionalLight, AmbientLight
    from panda3d.task import Task

    # Create a new light node
    light_node = LightNode('random_light')
    light_node.set_light(LightNode.LN_point)
    
    # Create a directional light with random direction and intensity
    light = DirectionalLight()
    light.set_direction((random.uniform(-1, 1), random.uniform(-1, 1), random.uniform(-1, 1)))
    light.set_intensity(intensity)
    light_node.set_light(light)
    
    # Create an ambient light for a bit of overall lighting
    ambient_light = AmbientLight()
    ambient_light.set_intensity(0.5)
    light_node.set_light(ambient_light)
    
    # Attach the light node to the model
    model.set_light(light_node)
    
    # Schedule a task to remove the light after a random time
    task = Task.renderTask(model)
    task.addTask(10, light_node.destroy)
    
    return model