def __init__(self): base.disableMouse() base.cam.node().getLens().setNear(10.0) base.cam.node().getLens().setFar(200.0) camera.setPos(0, -50, 0) # Check video card capabilities. if (base.win.getGsg().getSupportsBasicShaders() == 0): addTitle( "Toon Shader: Video driver reports that shaders are not supported." ) return # Enable a 'light ramp' - this discretizes the lighting, # which is half of what makes a model look like a cartoon. # Light ramps only work if shader generation is enabled, # so we call 'setShaderAuto'. tempnode = NodePath(PandaNode("temp node")) tempnode.setAttrib(LightRampAttrib.makeSingleThreshold(0.5, 0.4)) tempnode.setShaderAuto() base.cam.node().setInitialState(tempnode.getState()) # Use class 'CommonFilters' to enable a cartoon inking filter. # This can fail if the video card is not powerful enough, if so, # display an error and exit. self.separation = 1 # Pixels self.filters = CommonFilters(base.win, base.cam) filterok = self.filters.setCartoonInk(separation=self.separation) if (filterok == False): addTitle( "Toon Shader: Video card not powerful enough to do image postprocessing" ) return # Post the instructions. self.title = addTitle( "Panda3D: Tutorial - Toon Shading with Normals-Based Inking") self.inst1 = addInstructions(0.95, "ESC: Quit") self.inst2 = addInstructions( 0.90, "Up/Down: Increase/Decrease Line Thickness") self.inst3 = addInstructions(0.85, "V: View the render-to-texture results") # Load a dragon model and animate it. self.character = Actor() self.character.loadModel('models/samples/cartoon/nik_dragon') self.character.reparentTo(render) self.character.loadAnims({'win': 'models/samples/cartoon/nik_dragon'}) self.character.loop('win') self.character.hprInterval(15, Point3(360, 0, 0)).loop() # Create a non-attenuating point light and an ambient light. plightnode = PointLight("point light") plightnode.setAttenuation(Vec3(1, 0, 0)) plight = render.attachNewNode(plightnode) plight.setPos(30, -50, 0) alightnode = AmbientLight("ambient light") alightnode.setColor(Vec4(0.8, 0.8, 0.8, 1)) alight = render.attachNewNode(alightnode) render.setLight(alight) render.setLight(plight) # Panda contains a built-in viewer that lets you view the # results of all render-to-texture operations. This lets you # see what class CommonFilters is doing behind the scenes. self.accept("v", base.bufferViewer.toggleEnable) self.accept("V", base.bufferViewer.toggleEnable) base.bufferViewer.setPosition("llcorner") self.accept("s", self.filters.manager.resizeBuffers) # These allow you to change cartooning parameters in realtime self.accept("escape", sys.exit, [0]) self.accept("arrow_up", self.increaseSeparation) self.accept("arrow_down", self.decreaseSeparation)
def __init__( self ): #The main initialization of our class #This creates the on screen title that is in every tutorial self.title = OnscreenText(text="Panda3D: Tutorial - Lighting", style=1, fg=(1,1,0,1), font = font, pos=(0.87,-0.95), scale = .07) #Creates labels used for onscreen instructions self.ambientText = self.makeStatusLabel(0) self.directionalText = self.makeStatusLabel(1) self.spotlightText = self.makeStatusLabel(2) self.pointLightText = self.makeStatusLabel(3) self.spinningText = self.makeStatusLabel(4) self.ambientBrightnessText = self.makeStatusLabel(5) self.directionalBrightnessText = self.makeStatusLabel(6) self.spotlightBrightnessText = self.makeStatusLabel(7) self.spotlightExponentText = self.makeStatusLabel(8) self.lightingPerPixelText = self.makeStatusLabel(9) self.disco = loader.loadModel("models/samples/disco_lights/disco_hall") self.disco.reparentTo(render) self.disco.setPosHpr(0, 50, -4, 90, 0, 0) # First we create an ambient light. All objects are affected by ambient # light equally #Create and name the ambient light self.ambientLight = render.attachNewNode( AmbientLight( "ambientLight" ) ) #Set the color of the ambient light self.ambientLight.node().setColor( Vec4( .1, .1, .1, 1 ) ) #add the newly created light to the lightAttrib # Now we create a directional light. Directional lights add shading from a # given angle. This is good for far away sources like the sun self.directionalLight = render.attachNewNode( DirectionalLight( "directionalLight" ) ) self.directionalLight.node().setColor( Vec4( .35, .35, .35, 1 ) ) # The direction of a directional light is set as a 3D vector self.directionalLight.node().setDirection( Vec3( 1, 1, -2 ) ) # Now we create a spotlight. Spotlights light objects in a given cone # They are good for simulating things like flashlights self.spotlight = camera.attachNewNode( Spotlight( "spotlight" ) ) self.spotlight.node().setColor( Vec4( .45, .45, .45, 1 ) ) #The cone of a spotlight is controlled by it's lens. This creates the lens self.spotlight.node().setLens( PerspectiveLens() ) #This sets the Field of View (fov) of the lens, in degrees for width and #height. The lower the numbers, the tighter the spotlight. self.spotlight.node().getLens().setFov( 16, 16 ) # Attenuation controls how the light fades with distance. The numbers are # The three values represent the three constants (constant, linear, and # quadratic) in the internal lighting equation. The higher the numbers the # shorter the light goes. self.spotlight.node().setAttenuation( Vec3( 1, 0.0, 0.0 ) ) # This exponent value sets how soft the edge of the spotlight is. 0 means a # hard edge. 128 means a very soft edge. self.spotlight.node().setExponent( 60.0 ) # Now we create three colored Point lights. Point lights are lights that # radiate from a single point, like a light bulb. Like spotlights, they # are given position by attaching them to NodePaths in the world self.redHelper = loader.loadModel('models/samples/disco_lights/sphere') self.redHelper.setColor( Vec4( 1, 0, 0, 1 ) ) self.redHelper.setPos( -6.5, -3.75, 0 ) self.redHelper.setScale(.25) self.redPointLight = self.redHelper.attachNewNode( PointLight( "redPointLight" ) ) self.redPointLight.node().setColor( Vec4( .35, 0, 0, 1 ) ) self.redPointLight.node().setAttenuation( Vec3( .1, 0.04, 0.0 ) ) #The green point light and helper self.greenHelper = loader.loadModel('models/samples/disco_lights/sphere') self.greenHelper.setColor( Vec4( 0, 1, 0, 1 ) ) self.greenHelper.setPos( 0, 7.5, 0 ) self.greenHelper.setScale(.25) self.greenPointLight = self.greenHelper.attachNewNode( PointLight( "greenPointLight" ) ) self.greenPointLight.node().setAttenuation( Vec3( .1, .04, .0 ) ) self.greenPointLight.node().setColor( Vec4( 0, .35, 0, 1 ) ) #The blue point light and helper self.blueHelper = loader.loadModel('models/samples/disco_lights/sphere') self.blueHelper.setColor( Vec4( 0, 0, 1, 1 ) ) self.blueHelper.setPos( 6.5, -3.75, 0 ) self.blueHelper.setScale(.25) self.bluePointLight = self.blueHelper.attachNewNode( PointLight( "bluePointLight" ) ) self.bluePointLight.node().setAttenuation( Vec3( .1, 0.04, 0.0 ) ) self.bluePointLight.node().setColor( Vec4( 0, 0, .35, 1 ) ) self.bluePointLight.node().setSpecularColor( Vec4( 1 ) ) #Create a dummy node so the lights can be spun with one command self.pointLightHelper = render.attachNewNode( "pointLightHelper" ) self.pointLightHelper.setPos(0, 50, 11) self.redHelper.reparentTo( self.pointLightHelper ) self.greenHelper.reparentTo( self.pointLightHelper ) self.blueHelper.reparentTo( self.pointLightHelper ) #Finally we store the lights on the root of the scene graph. #This will cause them to affect everything in the scene. render.setLight( self.ambientLight ) render.setLight( self.directionalLight ) render.setLight( self.spotlight ) render.setLight( self.redPointLight ) render.setLight( self.greenPointLight ) render.setLight( self.bluePointLight ) # Create and start interval to spin the lights, and a variable to # manage them. self.pointLightsSpin = self.pointLightHelper.hprInterval(6, Vec3(360, 0, 0)) self.pointLightsSpin.loop() self.arePointLightsSpinning = True # Per-pixel lighting is initially off self.perPixelEnabled = False # listen to keys for controlling the lights self.accept( "escape", sys.exit) self.accept( "a", self.toggleLights, [[self.ambientLight]] ) self.accept( "d", self.toggleLights, [[self.directionalLight]] ) self.accept( "s", self.toggleLights, [[self.spotlight]] ) self.accept( "p", self.toggleLights, [[self.redPointLight, self.greenPointLight, self.bluePointLight]] ) self.accept( "r", self.toggleSpinningPointLights ) self.accept( "l", self.togglePerPixelLighting ) self.accept( "z", self.addBrightness, [self.ambientLight, -.05] ) self.accept( "x", self.addBrightness, [self.ambientLight, .05] ) self.accept( "c", self.addBrightness, [self.directionalLight, -.05] ) self.accept( "v", self.addBrightness, [self.directionalLight, .05] ) self.accept( "b", self.addBrightness, [self.spotlight, -.05] ) self.accept( "n", self.addBrightness, [self.spotlight, .05] ) self.accept( "q", self.adjustSpotlightExponent, [self.spotlight, -1] ) self.accept( "w", self.adjustSpotlightExponent, [self.spotlight, 1] ) #Finally call the function that builds the instruction texts self.updateStatusLabel()
def __init__(self): base.disableMouse() base.cam.node().getLens().setNear(10.0) base.cam.node().getLens().setFar(200.0) camera.setPos(0, -50, 0) # Check video card capabilities. if (base.win.getGsg().getSupportsBasicShaders() == 0): addTitle("Toon Shader: Video driver reports that shaders are not supported.") return # Enable a 'light ramp' - this discretizes the lighting, # which is half of what makes a model look like a cartoon. # Light ramps only work if shader generation is enabled, # so we call 'setShaderAuto'. tempnode = NodePath(PandaNode("temp node")) tempnode.setAttrib(LightRampAttrib.makeSingleThreshold(0.5, 0.4)) tempnode.setShaderAuto() base.cam.node().setInitialState(tempnode.getState()) # Use class 'CommonFilters' to enable a cartoon inking filter. # This can fail if the video card is not powerful enough, if so, # display an error and exit. self.separation = 1 # Pixels self.filters = CommonFilters(base.win, base.cam) filterok = self.filters.setCartoonInk(separation=self.separation) if (filterok == False): addTitle("Toon Shader: Video card not powerful enough to do image postprocessing") return # Post the instructions. self.title = addTitle("Panda3D: Tutorial - Toon Shading with Normals-Based Inking") self.inst1 = addInstructions(0.95,"ESC: Quit") self.inst2 = addInstructions(0.90,"Up/Down: Increase/Decrease Line Thickness") self.inst3 = addInstructions(0.85,"V: View the render-to-texture results") # Load a dragon model and animate it. self.character = Actor() self.character.loadModel('models/samples/cartoon/nik_dragon') self.character.reparentTo(render) self.character.loadAnims({'win': 'models/samples/cartoon/nik_dragon'}) self.character.loop('win') self.character.hprInterval(15, Point3(360, 0,0)).loop() # Create a non-attenuating point light and an ambient light. plightnode = PointLight("point light") plightnode.setAttenuation(Vec3(1,0,0)) plight = render.attachNewNode(plightnode) plight.setPos(30,-50,0) alightnode = AmbientLight("ambient light") alightnode.setColor(Vec4(0.8,0.8,0.8,1)) alight = render.attachNewNode(alightnode) render.setLight(alight) render.setLight(plight) # Panda contains a built-in viewer that lets you view the # results of all render-to-texture operations. This lets you # see what class CommonFilters is doing behind the scenes. self.accept("v", base.bufferViewer.toggleEnable) self.accept("V", base.bufferViewer.toggleEnable) base.bufferViewer.setPosition("llcorner") self.accept("s", self.filters.manager.resizeBuffers) # These allow you to change cartooning parameters in realtime self.accept("escape", sys.exit, [0]) self.accept("arrow_up", self.increaseSeparation) self.accept("arrow_down", self.decreaseSeparation)
def __init__(self): # Check video card capabilities. if (base.win.getGsg().getSupportsBasicShaders() == 0): addTitle("Normal Mapping: Video driver reports that shaders are not supported.") return # Post the instructions self.title = addTitle("Panda3D: Tutorial - Normal Mapping (aka Bump Mapping)") self.inst1 = addInstructions(0.95, "Press ESC to exit") self.inst2 = addInstructions(0.90, "Move mouse to rotate camera") self.inst3 = addInstructions(0.85, "Left mouse button: Move forwards") self.inst4 = addInstructions(0.80, "Right mouse button: Move backwards") self.inst5 = addInstructions(0.75, "Enter: Turn normal maps Off") # Load the 'abstract room' model. This is a model of an # empty room containing a pillar, a pyramid, and a bunch # of exaggeratedly bumpy textures. self.room = loader.loadModel( \ "models/samples/normal_mapping/abstractroom") self.room.reparentTo(render) # Make the mouse invisible, turn off normal mouse controls base.disableMouse() props = WindowProperties() props.setCursorHidden(True) base.win.requestProperties(props) # Set the current viewing target self.focus = Vec3(55,-55,20) self.heading = 180 self.pitch = 0 self.mousex = 0 self.mousey = 0 self.last = 0 self.mousebtn = [0,0,0] # Start the camera control task: taskMgr.add(self.controlCamera, "camera-task") self.accept("escape", sys.exit, [0]) self.accept("mouse1", self.setMouseBtn, [0, 1]) self.accept("mouse1-up", self.setMouseBtn, [0, 0]) self.accept("mouse2", self.setMouseBtn, [1, 1]) self.accept("mouse2-up", self.setMouseBtn, [1, 0]) self.accept("mouse3", self.setMouseBtn, [2, 1]) self.accept("mouse3-up", self.setMouseBtn, [2, 0]) self.accept("enter", self.toggleShader) self.accept("j", self.rotateLight, [-1]) self.accept("k", self.rotateLight, [1]) self.accept("arrow_left", self.rotateCam, [-1]) self.accept("arrow_right", self.rotateCam, [1]) # Add a light to the scene. self.lightpivot = render.attachNewNode("lightpivot") self.lightpivot.setPos(0,0,25) self.lightpivot.hprInterval(10,Point3(360,0,0)).loop() plight = PointLight('plight') plight.setColor(Vec4(1, 1, 1, 1)) plight.setAttenuation(Vec3(0.7,0.05,0)) plnp = self.lightpivot.attachNewNode(plight) plnp.setPos(45, 0, 0) self.room.setLight(plnp) self.room.setShaderInput("light", plnp) # Add an ambient light alight = AmbientLight('alight') alight.setColor(Vec4(0.2, 0.2, 0.2, 1)) alnp = render.attachNewNode(alight) self.room.setLight(alnp) # create a sphere to denote the light sphere = loader.loadModel("models/samples/normal_mapping/sphere") sphere.reparentTo(plnp) # load and apply the shader. This is using panda's # built-in shader generation capabilities to create the # shader for you. However, if desired, you can supply # the shader manually. Change this line of code to: # self.room.setShader(Shader.load("bumpMapper.sha")) self.room.setShaderAuto() self.shaderenable = 1