Using light ramps

The lighting system of Panda3D allows you to pull off some additional tricks to create some dramatic effects with scene lights. In this recipe, you will learn how to use light ramps to modify the lights affect on the models and actors in your game scenes.

Getting ready

In this recipe we will extend the code created in Adding lights and shadows found in this chapter. Please review this recipe before proceeding if you haven't done so yet.

How to do it...

Light ramps can be used like this:

  1. Open Application.py and add and modify the existing code as highlighted:
    from direct.showbase.ShowBase import ShowBase
    from direct.actor.Actor import Actor
    from panda3d.core import *
    from direct.interval.IntervalGlobal import *
    class Application(ShowBase):
    def __init__(self):
    ShowBase.__init__(self)
    self.panda = Actor("panda", {"walk": "panda-walk"})
    self.panda.reparentTo(render)
    self.panda.loop("walk")
    cm = CardMaker("plane")
    cm.setFrame(-10, 10, -10, 10)
    plane = render.attachNewNode(cm.generate())
    plane.setP(270)
    self.cam.setPos(0, -40, 6)
    ambLight = AmbientLight("ambient")
    ambLight.setColor(Vec4(0.3, 0.2, 0.2, 1.0))
    ambNode = render.attachNewNode(ambLight)
    render.setLight(ambNode)
    dirLight = DirectionalLight("directional")
    dirLight.setColor(Vec4(0.3, 0.9, 0.3, 1.0))
    dirNode = render.attachNewNode(dirLight)
    dirNode.setHpr(60, 0, 90)
    render.setLight(dirNode)
    pntLight = PointLight("point")
    pntLight.setColor(Vec4(3.9, 3.9, 3.8, 1.0))
    pntNode = render.attachNewNode(pntLight)
    pntNode.setPos(0, 0, 15)
    self.panda.setLight(pntNode)
    sptLight = Spotlight("spot")
    sptLens = PerspectiveLens()
    sptLight.setLens(sptLens)
    sptLight.setColor(Vec4(1.0, 0.4, 0.4, 1.0))
    sptLight.setShadowCaster(True)
    sptNode = render.attachNewNode(sptLight)
    sptNode.setPos(-10, -10, 20)
    sptNode.lookAt(self.panda)
    render.setLight(sptNode)
    render.setShaderAuto()
    Panda3Dlight ramps, usingself.activeRamp = 0
    toggle = Func(self.toggleRamp)
    switcher = Sequence(toggle, Wait(3))
    switcher.loop()
    def toggleRamp(self):
    if self.activeRamp == 0:
    render.setAttrib(LightRampAttrib.makeDefault())
    elif self.activeRamp == 1:
    render.setAttrib(LightRampAttrib.makeHdr0())
    elif self.activeRamp == 2:
    render.setAttrib(LightRampAttrib.makeHdr1())
    elif self.activeRamp == 3:
    render.setAttrib(LightRampAttrib.makeHdr2())
    elif self.activeRamp == 4:
    render.setAttrib(LightRampAttrib.makeSingleThreshold(0.1, 0.3))
    elif self.activeRamp == 5:
    render.setAttrib(LightRampAttrib.makeDoubleThreshold(0, 0.1, 0.3, 0.8))
    self.activeRamp += 1
    if self.activeRamp > 5:
    self.activeRamp = 0
    
  2. Press F6 to start the sample and see it switch through the available light ramps as shown in this screenshot:
How to do it...

How it works...

The original lighting equation that is used by Panda3D to calculate the final screen color of a lit pixel limits color intensities to values within a range from zero to one. By using light ramps we are able to go beyond these limits or even define our own ones to create dramatic effects just like the ones we can see in the sample program.

In the sample code, we increase the lighting intensity and add a method that switches between the available light ramps, beginning with LightRampAttrib.makeDefault() which sets the default clamping thresholds for the lighting calculations.

Then, the high dynamic range ramps are enabled one after another. These light ramps allow you to have a higher range of color intensities that go beyond the standard range between zero and one. These high intensities are then mapped back into the displayable range, allocating different amounts of values within it to displaying brightness.

By using makeHdr0(), we allocate a quarter of the displayable range to brightness values that are greater than one. With makeHdr1() it is a third and with makeHdr2() we are causing Panda3D to use half of the color range for overly bright values. This doesn't come without any side effects, though. By increasing the range used for high intensities, we are decreasing the range of color intensities available for displaying colors that are within the limits of 0 and 1, thus losing contrast and making the scene look grey and washed out.

Finally, with the makeSingleThreshold() and makeDoubleThreshold() methods, we are able to create very interesting lighting effects. With a single threshold, lighting values below the given limit will be ignored, while anything that exceeds the threshold will be set to the intensity given in the second parameter of the method.

The double threshold system works analogous to the single threshold, but lighting intensity will be normalized to two possible values, depending on which of the two thresholds was exceeded.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset