def __updateModel(self):
   if self.editModel:
     # create a image with the same size of the texture
     textureSize = (self.editTexture.getXSize(), self.editTexture.getYSize())
     
     # create a dummy node, where we setup the parameters for the background rendering
     loadPaintNode = NodePath(PandaNode('paintnode'))
     loadPaintNode.setShader(Shader.make(self.backgroundShader), 10001)
     loadPaintNode.setShaderInput('texsize', textureSize[0], textureSize[1], 0, 0)
     
     # copy the state onto the camera
     self.modelColorCam.node().setInitialState(loadPaintNode.getState())
     
     # the camera gets a special bitmask, to show/hide models from it
     self.modelColorCam.node().setCameraMask(BitMask32.bit(1))
     
     if False:
       # doesnt work, but would be nicer (not messing with the default render state)
       
       hiddenNode = NodePath(PandaNode('hiddennode'))
       hiddenNode.hide(BitMask32.bit(1))
       showTroughNode = NodePath(PandaNode('showtroughnode'))
       showTroughNode.showThrough(BitMask32.bit(1))
       
       self.modelColorCam.node().setTagStateKey('show-on-backrender-cam')
       self.modelColorCam.node().setTagState('False', hiddenNode.getState())
       self.modelColorCam.node().setTagState('True', showTroughNode.getState())
       
       render.setTag('show-on-backrender-cam', 'False')
       self.editModel.setTag('show-on-backrender-cam', 'True')
     else:
       # make only the model visible to the background camera
       render.hide(BitMask32.bit(1))
       self.editModel.showThrough(BitMask32.bit(1))
    def __init__(self,
                 scene=base.render,
                 ambient=0.2,
                 hardness=16,
                 fov=40,
                 near=10,
                 far=100):
        """Create an instance of this class to initiate shadows.
    Also, a shadow casting 'light' is created when this class is called.
    The first parameter is the nodepath in the scene where you
    want to apply your shadows on, by default this is render."""

        # Read and store the function parameters
        self.scene = scene
        self.__ambient = ambient
        self.__hardness = hardness

        # By default, mark every object as textured.
        self.flagTexturedObject(self.scene)

        # Create the buffer plus a texture to store the output in
        buffer = createOffscreenBuffer(-3)
        depthmap = Texture()
        buffer.addRenderTexture(depthmap, GraphicsOutput.RTMBindOrCopy,
                                GraphicsOutput.RTPColor)

        # Set the shadow filter if it is supported
        if (base.win.getGsg().getSupportsShadowFilter()):
            depthmap.setMinfilter(Texture.FTShadow)
            depthmap.setMagfilter(Texture.FTShadow)

        # Make the camera
        self.light = base.makeCamera(buffer)
        self.light.node().setScene(self.scene)
        self.light.node().getLens().setFov(fov)
        self.light.node().getLens().setNearFar(near, far)

        # Put a shader on the Light camera.
        lci = NodePath(PandaNode("lightCameraInitializer"))
        lci.setShader(loader.loadShader("caster.sha"))
        self.light.node().setInitialState(lci.getState())

        # Put a shader on the Main camera.
        mci = NodePath(PandaNode("mainCameraInitializer"))
        mci.setShader(loader.loadShader("softshadow.sha"))
        base.cam.node().setInitialState(mci.getState())

        # Set up the blurring buffers, one that blurs horizontally, the other vertically
        #blurXBuffer = makeFilterBuffer(buffer, "Blur X", -2, loader.loadShader("blurx.sha"))
        #blurYBuffer = makeFilterBuffer(blurXBuffer, "Blur Y", -1, loader.loadShader("blury.sha"))

        # Set the shader inputs
        self.scene.setShaderInput("light", self.light)
        #self.scene.setShaderInput("depthmap", blurYBuffer.getTexture())
        self.scene.setShaderInput("depthmap", buffer.getTexture())
        self.scene.setShaderInput("props", ambient, hardness, 0, 1)
    def __init__(self, scene=base.render, ambient=0.2, hardness=16, fov=40, near=10, far=100):
        """Create an instance of this class to initiate shadows.
    Also, a shadow casting 'light' is created when this class is called.
    The first parameter is the nodepath in the scene where you
    want to apply your shadows on, by default this is render."""

        # Read and store the function parameters
        self.scene = scene
        self.__ambient = ambient
        self.__hardness = hardness

        # By default, mark every object as textured.
        self.flagTexturedObject(self.scene)

        # Create the buffer plus a texture to store the output in
        buffer = createOffscreenBuffer(-3)
        depthmap = Texture()
        buffer.addRenderTexture(depthmap, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPColor)

        # Set the shadow filter if it is supported
        if base.win.getGsg().getSupportsShadowFilter():
            depthmap.setMinfilter(Texture.FTShadow)
            depthmap.setMagfilter(Texture.FTShadow)

        # Make the camera
        self.light = base.makeCamera(buffer)
        self.light.node().setScene(self.scene)
        self.light.node().getLens().setFov(fov)
        self.light.node().getLens().setNearFar(near, far)

        # Put a shader on the Light camera.
        lci = NodePath(PandaNode("lightCameraInitializer"))
        lci.setShader(loader.loadShader("caster.sha"))
        self.light.node().setInitialState(lci.getState())

        # Put a shader on the Main camera.
        mci = NodePath(PandaNode("mainCameraInitializer"))
        mci.setShader(loader.loadShader("softshadow.sha"))
        base.cam.node().setInitialState(mci.getState())

        # Set up the blurring buffers, one that blurs horizontally, the other vertically
        # blurXBuffer = makeFilterBuffer(buffer, "Blur X", -2, loader.loadShader("blurx.sha"))
        # blurYBuffer = makeFilterBuffer(blurXBuffer, "Blur Y", -1, loader.loadShader("blury.sha"))

        # Set the shader inputs
        self.scene.setShaderInput("light", self.light)
        # self.scene.setShaderInput("depthmap", blurYBuffer.getTexture())
        self.scene.setShaderInput("depthmap", buffer.getTexture())
        self.scene.setShaderInput("props", ambient, hardness, 0, 1)
예제 #4
0
    def setupGlowFilter(self):
        #create the shader that will determime what parts of the scene will glow
        glowShader=Shader.load(self.pandapath + "/data/glowShader.sha")
        
        # create the glow buffer. This buffer renders like a normal scene,
        # except that only the glowing materials should show up nonblack.
        glowBuffer=base.win.makeTextureBuffer("Glow scene", 512, 512)
        glowBuffer.setSort(-3)
        glowBuffer.setClearColor(Vec4(0,0,0,1))

        # We have to attach a camera to the glow buffer. The glow camera
        # must have the same frustum as the main camera. As long as the aspect
        # ratios match, the rest will take care of itself.
        self.glowCamera=base.makeCamera(glowBuffer, lens=base.cam.node().getLens())

        # Tell the glow camera to use the glow shader
        tempnode = NodePath(PandaNode("temp node"))
        tempnode.setShader(glowShader)
        self.glowCamera.node().setInitialState(tempnode.getState())

        # set up the pipeline: from glow scene to blur x to blur y to main window.
        blurXBuffer=self.makeFilterBuffer(glowBuffer,  "Blur X", -2, self.pandapath+"/data/XBlurShader.sha")
        blurYBuffer=self.makeFilterBuffer(blurXBuffer, "Blur Y", -1, self.pandapath+"/data/YBlurShader.sha")
        self.finalcard = blurYBuffer.getTextureCard()
        self.finalcard.reparentTo(render2d)
        self.finalcard.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd))
        
        render.setShaderInput('glow', Vec4(0,0,0,0),0)
        
        render.analyze()
예제 #5
0
  def __init__(self,manager,xml):
    self.surface = getWaterSurface(manager)
    self.surface.reparentTo(render)
    self.surface.hide(BitMask32.bit(1)) # Invisible to reflection camera (speedup)
    self.surface.hide(BitMask32.bit(2)) # Invisible to volumetric lighting camera (speedup)
    self.surface.hide(BitMask32.bit(3)) # Invisible to shadow cameras (speedup)
    self.surface.setShader(loader.loadShader(manager.get('paths').getConfig().find('shaders').get('path')+'/water.cg'))
    self.surface.setShaderInput('time', 0.0, 0.0, 0.0, 0.0)
    ntex = loader.loadTexture(manager.get('paths').getConfig().find('textures').get('path')+'/water-normal.png')
    ntex.setMinfilter(Texture.FTLinearMipmapLinear)
    self.surface.setShaderInput('normal', ntex)
    self.surface.setShaderInput('camera', base.cam)
    self.surface.setTransparency(TransparencyAttrib.MDual, 10)
    self.surface.setTwoSided(True)

    self.surface.setShaderInput('waveInfo', Vec4(0.4, 0.4, 0.4, 0))
    self.surface.setShaderInput('param2', Vec4(-0.015,0.005, 0.05, 0.05))
    self.surface.setShaderInput('param3', Vec4(0.7, 0.3, 0, 0))
    self.surface.setShaderInput('param4', Vec4(2.0, 0.5, 0.5, 0.0))
    #self.surface.setShaderInput('speed', Vec4(-.8, -.4, -.9, .3))
    self.surface.setShaderInput('speed', Vec4(0.2, -1.2, -0.2, -0.7))
    self.surface.setShaderInput('deepcolor', Vec4(0.0,0.3,0.5,1.0))
    self.surface.setShaderInput('shallowcolor', Vec4(0.0,1.0,1.0,1.0))
    self.surface.setShaderInput('reflectioncolor', Vec4(0.95,1.0,1.0,1.0))
    self.surface.hide()

    self.wbuffer = base.win.makeTextureBuffer('water', 512, 512)
    self.wbuffer.setClearColorActive(True)
    self.wbuffer.setClearColor(base.win.getClearColor())
    self.wcamera = base.makeCamera(self.wbuffer)
    if manager.get('sky') != None and manager.get('sky').model != None:
      self.sky = manager.get('sky').model.copyTo(self.wcamera)
      self.sky.setTwoSided(True)
      self.sky.setSz(self.sky, -1)
      self.sky.setClipPlaneOff(1)
      self.sky.show()
      self.sky.hide(BitMask32.bit(0)) # Hide for normal camera
      self.sky.hide(BitMask32.bit(2)) # Hide for volumetric lighting camera
      self.sky.hide(BitMask32.bit(3)) # Hide for shadow camera(s), if any
    else:
      self.sky = None
    self.wcamera.reparentTo(render)
    self.wcamera.node().setLens(base.camLens)
    self.wcamera.node().setCameraMask(BitMask32.bit(1))
    self.surface.hide(BitMask32.bit(1))
    wtexture = self.wbuffer.getTexture()
    wtexture.setWrapU(Texture.WMClamp)
    wtexture.setWrapV(Texture.WMClamp)
    wtexture.setMinfilter(Texture.FTLinearMipmapLinear)
    self.surface.setShaderInput('reflection', wtexture)
    self.wplane = Plane(Vec3(0, 0, 1), Point3(0, 0, 0))
    self.wplanenp = render.attachNewNode(PlaneNode('water', self.wplane))
    tmpnp = NodePath('StateInitializer')
    tmpnp.setClipPlane(self.wplanenp)
    tmpnp.setAttrib(CullFaceAttrib.makeReverse())
    self.wcamera.node().setInitialState(tmpnp.getState())

    #self.fog = Fog('UnderwaterFog')
    #self.fog.setColor(0.0,0.3,0.5)
    self.fogEnabled = False
    def renderSceneInto(self,
                        depthtex=None,
                        colortex=None,
                        auxtex=None,
                        auxbits=0,
                        textures=None):
        if textures:
            colortex = textures.get('color', None)
            depthtex = textures.get('depth', None)
            auxtex = textures.get('aux', None)

        if colortex == None:
            colortex = Texture('filter-base-color')
            colortex.setWrapU(Texture.WMClamp)
            colortex.setWrapV(Texture.WMClamp)

        texgroup = (depthtex, colortex, auxtex, None)
        (winx, winy) = self.getScaledSize(1, 1, 1)
        buffer = self.createBuffer('filter-base', winx, winy, texgroup)
        if buffer == None:
            return None

        cm = CardMaker('filter-base-quad')
        cm.setFrameFullscreenQuad()
        quad = NodePath(cm.generate())
        quad.setDepthTest(0)
        quad.setDepthWrite(0)
        quad.setTexture(colortex)
        quad.setColor(Vec4(1, 0.5, 0.5, 1))
        cs = NodePath('dummy')
        cs.setState(self.camstate)
        if auxbits:
            cs.setAttrib(AuxBitplaneAttrib.make(auxbits))

        self.camera.node().setInitialState(cs.getState())
        quadcamnode = Camera('filter-quad-cam')
        lens = OrthographicLens()
        lens.setFilmSize(2, 2)
        lens.setFilmOffset(0, 0)
        lens.setNearFar(-1000, 1000)
        quadcamnode.setLens(lens)
        quadcam = quad.attachNewNode(quadcamnode)
        self.region.setCamera(quadcam)
        dr = buffer.getDisplayRegion(0)
        self.setStackedClears(dr, self.rclears, self.wclears)
        if auxtex:
            dr.setClearActive(GraphicsOutput.RTPAuxRgba0, 1)
            dr.setClearValue(GraphicsOutput.RTPAuxRgba0,
                             Vec4(0.5, 0.5, 1.0, 0.0))

        self.region.disableClears()
        if self.isFullscreen():
            self.win.disableClears()

        dr.setCamera(self.camera)
        dr.setActive(1)
        self.buffers.append(buffer)
        self.sizes.append((1, 1, 1))
        return quad
    def __createBuffer(self):
        ''' create the buffer we render in the background into '''
        print "I: TexturePainter.__createBuffer"
        # the window has been modified
        if WindowManager.activeWindow:
            # on window resize there seems to be never a active window
            win = WindowManager.activeWindow.win
        else:
            win = base.win

        # get the window size
        self.windowSizeX = win.getXSize()
        self.windowSizeY = win.getYSize()

        # create a buffer in which we render the model using a shader
        self.paintMap = Texture()
        # 1.5.4 cant handle non power of 2 buffers
        self.modelColorBuffer = createOffscreenBuffer(
            -3, TEXTUREPAINTER_BACKGROUND_BUFFER_RENDERSIZE[0],
            TEXTUREPAINTER_BACKGROUND_BUFFER_RENDERSIZE[1]
        )  #self.windowSizeX, self.windowSizeY)
        self.modelColorBuffer.addRenderTexture(self.paintMap,
                                               GraphicsOutput.RTMBindOrCopy,
                                               GraphicsOutput.RTPColor)
        self.modelColorCam = base.makeCamera(self.modelColorBuffer,
                                             lens=base.cam.node().getLens(),
                                             sort=1)

        # Create a small buffer for the shader program that will fetch the point from the texture made
        # by the self.modelColorBuffer
        self.colorPickerImage = PNMImage()
        self.colorPickerTex = Texture()
        self.colorPickerBuffer = base.win.makeTextureBuffer(
            "color picker buffer", 2, 2, self.colorPickerTex, True)

        self.colorPickerScene = NodePath('color picker scene')

        self.colorPickerCam = base.makeCamera(self.colorPickerBuffer,
                                              lens=base.cam.node().getLens(),
                                              sort=2)
        self.colorPickerCam.reparentTo(self.colorPickerScene)
        self.colorPickerCam.setY(-2)

        cm = CardMaker('color picker scene card')
        cm.setFrameFullscreenQuad()
        pickerCard = self.colorPickerScene.attachNewNode(cm.generate())

        loadPicker = NodePath(PandaNode('pointnode'))
        loadPicker.setShader(Shader.make(COLOR_PICKER_SHADER), 10001)

        # Feed the paintmap from the paintBuffer to the shader and initial mouse positions
        self.colorPickerScene.setShaderInput('paintmap', self.paintMap)
        self.colorPickerScene.setShaderInput('mousepos', 0, 0, 0, 1)
        self.colorPickerCam.node().setInitialState(loadPicker.getState())
    def __updateModel(self):
        if self.editModel:
            # create a image with the same size of the texture
            textureSize = (self.editTexture.getXSize(),
                           self.editTexture.getYSize())

            # create a dummy node, where we setup the parameters for the background rendering
            loadPaintNode = NodePath(PandaNode('paintnode'))
            loadPaintNode.setShader(Shader.make(self.backgroundShader), 10001)
            loadPaintNode.setShaderInput('texsize', textureSize[0],
                                         textureSize[1], 0, 0)

            # copy the state onto the camera
            self.modelColorCam.node().setInitialState(loadPaintNode.getState())

            # the camera gets a special bitmask, to show/hide models from it
            self.modelColorCam.node().setCameraMask(BitMask32.bit(1))

            if False:
                # doesnt work, but would be nicer (not messing with the default render state)

                hiddenNode = NodePath(PandaNode('hiddennode'))
                hiddenNode.hide(BitMask32.bit(1))
                showTroughNode = NodePath(PandaNode('showtroughnode'))
                showTroughNode.showThrough(BitMask32.bit(1))

                self.modelColorCam.node().setTagStateKey(
                    'show-on-backrender-cam')
                self.modelColorCam.node().setTagState('False',
                                                      hiddenNode.getState())
                self.modelColorCam.node().setTagState(
                    'True', showTroughNode.getState())

                render.setTag('show-on-backrender-cam', 'False')
                self.editModel.setTag('show-on-backrender-cam', 'True')
            else:
                # make only the model visible to the background camera
                render.hide(BitMask32.bit(1))
                self.editModel.showThrough(BitMask32.bit(1))
 def __createBuffer(self):
   ''' create the buffer we render in the background into '''
   print "I: TexturePainter.__createBuffer"
   # the window has been modified
   if WindowManager.activeWindow:
     # on window resize there seems to be never a active window
     win = WindowManager.activeWindow.win
   else:
     win = base.win
   
   # get the window size
   self.windowSizeX = win.getXSize()
   self.windowSizeY = win.getYSize()
   
   # create a buffer in which we render the model using a shader
   self.paintMap = Texture()
   # 1.5.4 cant handle non power of 2 buffers
   self.modelColorBuffer = createOffscreenBuffer(-3, TEXTUREPAINTER_BACKGROUND_BUFFER_RENDERSIZE[0], TEXTUREPAINTER_BACKGROUND_BUFFER_RENDERSIZE[1]) #self.windowSizeX, self.windowSizeY)
   self.modelColorBuffer.addRenderTexture(self.paintMap, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPColor)
   self.modelColorCam = base.makeCamera(self.modelColorBuffer, lens=base.cam.node().getLens(), sort=1)
   
   # Create a small buffer for the shader program that will fetch the point from the texture made
   # by the self.modelColorBuffer
   self.colorPickerImage = PNMImage()
   self.colorPickerTex = Texture()
   self.colorPickerBuffer = base.win.makeTextureBuffer("color picker buffer", 2, 2, self.colorPickerTex, True)
   
   self.colorPickerScene = NodePath('color picker scene')
   
   self.colorPickerCam = base.makeCamera(self.colorPickerBuffer, lens=base.cam.node().getLens(), sort=2)
   self.colorPickerCam.reparentTo(self.colorPickerScene)
   self.colorPickerCam.setY(-2)
   
   cm = CardMaker('color picker scene card')
   cm.setFrameFullscreenQuad()
   pickerCard = self.colorPickerScene.attachNewNode(cm.generate())
   
   loadPicker = NodePath(PandaNode('pointnode'))
   loadPicker.setShader(Shader.make(COLOR_PICKER_SHADER), 10001)
   
   # Feed the paintmap from the paintBuffer to the shader and initial mouse positions
   self.colorPickerScene.setShaderInput('paintmap', self.paintMap)
   self.colorPickerScene.setShaderInput('mousepos', 0, 0, 0, 1)
   self.colorPickerCam.node().setInitialState(loadPicker.getState())
예제 #10
0
    def make_glow (self, data_dir = './src/core/sha'):
        """
        TODO: data_dir = current module directory?
        """
    
        glow_shader = Shader.load (data_dir + "/glow_shader.sha")
        
        # create the glow buffer. This buffer renders like a normal
        # scene, except that only the glowing materials should show up
        # nonblack.
        glow_buffer = base.win.makeTextureBuffer ("glow_scene", 512, 512)
        glow_buffer.setSort (-3)
        glow_buffer.setClearColor (Vec4 (0, 0, 0, 1))

        # We have to attach a camera to the glow buffer. The glow
        # camera must have the same frustum as the main camera. As
        # long as the aspect ratios match, the rest will take care of
        # itself.
        glow_camera = base.makeCamera (glow_buffer,
                                       lens = base.cam.node ().getLens ())

        # Tell the glow camera to use the glow shader
        temp_node = NodePath (PandaNode("temp_node"))
        temp_node.setShader (glow_shader)
        glow_camera.node ().setInitialState (temp_node.getState ())

        # set up the pipeline: from glow scene to blur x to blur y to
        # main window.
        blur_xbuffer = make_filter_buffer (glow_buffer, "blur_x", -2,
                                           data_dir + "/x_blur_shader.sha")
        blur_ybuffer = make_filter_buffer (blur_xbuffer, "blur_y", -1,
                                           data_dir + "/y_blur_shader.sha")

        final_card = blur_ybuffer.getTextureCard ()
        final_card.setAttrib (ColorBlendAttrib.make (ColorBlendAttrib.MAdd))

        self.glow_camera  = glow_camera
        self.glow_buffer  = glow_buffer
        self.blur_xbuffer = blur_xbuffer
        self.blur_ybuffer = blur_ybuffer
        self.final_card   = final_card
예제 #11
0
    def make_glow(self, data_dir='./src/core/sha'):
        """
        TODO: data_dir = current module directory?
        """

        glow_shader = Shader.load(data_dir + "/glow_shader.sha")

        # create the glow buffer. This buffer renders like a normal
        # scene, except that only the glowing materials should show up
        # nonblack.
        glow_buffer = base.win.makeTextureBuffer("glow_scene", 512, 512)
        glow_buffer.setSort(-3)
        glow_buffer.setClearColor(Vec4(0, 0, 0, 1))

        # We have to attach a camera to the glow buffer. The glow
        # camera must have the same frustum as the main camera. As
        # long as the aspect ratios match, the rest will take care of
        # itself.
        glow_camera = base.makeCamera(glow_buffer,
                                      lens=base.cam.node().getLens())

        # Tell the glow camera to use the glow shader
        temp_node = NodePath(PandaNode("temp_node"))
        temp_node.setShader(glow_shader)
        glow_camera.node().setInitialState(temp_node.getState())

        # set up the pipeline: from glow scene to blur x to blur y to
        # main window.
        blur_xbuffer = make_filter_buffer(glow_buffer, "blur_x", -2,
                                          data_dir + "/x_blur_shader.sha")
        blur_ybuffer = make_filter_buffer(blur_xbuffer, "blur_y", -1,
                                          data_dir + "/y_blur_shader.sha")

        final_card = blur_ybuffer.getTextureCard()
        final_card.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd))

        self.glow_camera = glow_camera
        self.glow_buffer = glow_buffer
        self.blur_xbuffer = blur_xbuffer
        self.blur_ybuffer = blur_ybuffer
        self.final_card = final_card
예제 #12
0
class ShadowCreator:

    def __init__(self):
        self.shadowBuffer = base.win.makeTextureBuffer('Shadow Buffer', 2048, 2048)
        self.shadowBuffer.setClearColorActive(True)
        self.shadowBuffer.setClearColor((0, 0, 0, 1))
        self.shadowCamera = base.makeCamera(self.shadowBuffer)
        self.shadowCamera.reparentTo(render)
        self.lens = base.camLens
        self.lens.setAspectRatio(1 / 1)
        self.shadowCamera.node().setLens(self.lens)
        self.shadowCamera.node().setCameraMask(BitMask32.bit(1))
        self.initial = NodePath('initial')
        self.initial.setColor(0.75, 0.75, 0.75, 1, 1)
        self.initial.setTextureOff(2)
        self.initial.setMaterialOff(2)
        self.initial.setLightOff(2)
        self.shadowCamera.node().setInitialState(self.initial.getState())
        self.shadowCamera.setPos(-10, 0, 20)
        self.shadowCamera.lookAt(0, 0, 0)
        self.filters = CommonFilters(self.shadowBuffer, self.shadowCamera)
        self.filters.setBlurSharpen(0.1)
        self.shadowTexture = self.shadowBuffer.getTexture()
        self.imageObject = OnscreenImage(image=self.shadowTexture, pos=(-0.75, 0, 0.75), scale=0.2)
예제 #13
0
    def __init__(self, objectPath, groundPath, lightPos=Vec3(0,0,1)):
        """ ShadowCaster::__init__
        objectPath is the shadow casting object
        groundPath is the shadow receiving object
        lightPos is the lights relative position to the objectPath
        """
        
        # uniq id-number for each shadow
        global shadowCasterObjectCounter
        shadowCasterObjectCounter += 1
        self.objectShadowId = shadowCasterObjectCounter
        
        # the object which will cast shadows
        self.objectPath = objectPath
        
        # get the objects bounds center and radius to
        # define the shadowrendering camera position and filmsize
        try:
            objectBoundRadius = self.objectPath.getBounds().getRadius()
            objectBoundCenter = self.objectPath.getBounds().getCenter()
        except:
            print "failed"
            objectBoundCenter = Point3( 0,0,0 )
            objectBoundRadius = 1
        
        lightPath = objectPath.attachNewNode('lightPath%i'%self.objectShadowId)
        # We can change this position at will to change the angle of the sun.
        lightPath.setPos( objectPath.getParent(), objectBoundCenter )
        self.lightPath = lightPath
        
        # the film size is the diameter of the object
        self.filmSize = objectBoundRadius * 2
        
        # Create an offscreen buffer to render the view of the avatar
        # into a texture.
        self.buffer = base.win.makeTextureBuffer(
            'shadowBuffer%i'%self.objectShadowId, self.texXSize, self.texYSize)

        # The background of this buffer--and the border of the
        # texture--is pure white.
        clearColor = VBase4(1, 1, 1, 1)
        self.buffer.setClearColor(clearColor)
        
        self.tex = self.buffer.getTexture()
        self.tex.setBorderColor(clearColor)
        self.tex.setWrapU(Texture.WMBorderColor)
        self.tex.setWrapV(Texture.WMBorderColor)

        # Set up a display region on this buffer, and create a camera.
        dr = self.buffer.makeDisplayRegion()
        self.camera = Camera('shadowCamera%i'%self.objectShadowId)
        self.cameraPath = self.lightPath.attachNewNode(self.camera)
        self.camera.setScene(self.objectPath)
        dr.setCamera(self.cameraPath)
        
        self.setLightPos( lightPos )
        
        # Use a temporary NodePath to define the initial state for the
        # camera.  The initial state will render everything in a
        # flat-shaded gray, as if it were a shadow.
        initial = NodePath('initial%i'%self.objectShadowId)
        initial.setColor( *SHADOWCOLOR )
        initial.setTextureOff(2)
        self.camera.setInitialState(initial.getState())
        
        # Use an orthographic lens for this camera instead of the
        # usual perspective lens.  An orthographic lens is better to
        # simulate sunlight, which is (almost) orthographic.  We set
        # the film size large enough to render a typical avatar (but
        # not so large that we lose detail in the texture).
        self.lens = OrthographicLens()
        self.lens.setFilmSize(self.filmSize, self.filmSize)
        self.camera.setLens(self.lens)
        
        # Finally, we'll need a unique TextureStage to apply this
        # shadow texture to the world.
        self.stage = TextureStage('shadow%i'%self.objectShadowId)

        # Make sure the shadowing object doesn't get its own shadow
        # applied to it.
        self.objectPath.setTextureOff(self.stage)
        
        # the object which will receive shadows
        self.setGround( groundPath )
예제 #14
0
    def __init__(self, manager, xml):
        self.updateTask = None

        self.sun = base.cam.attachNewNode('sun')
        loader.loadModel(
            manager.get('paths').getConfig().find('misc').get('path') +
            '/sphere').reparentTo(self.sun)
        self.sun.setScale(0.1)
        self.sun.setTwoSided(True)
        self.sun.setColorScale(10.0, 10.0, 10.0, 1.0, 10001)
        self.sun.setLightOff(1)
        self.sun.setShaderOff(1)
        self.sun.setFogOff(1)
        self.sun.setCompass()
        self.sun.setBin('background', 10)
        self.sun.setDepthWrite(False)
        self.sun.setDepthTest(False)
        # Workaround an annoyance in Panda. No idea why it's needed.
        self.sun.node().setBounds(OmniBoundingVolume())
        isa = xml.find('isa')
        inst = xml.find('instance')
        if isa != None or inst != None:
            if inst != None:
                orig = Vec3(float(inst.get('x',
                                           '0')), float(inst.get('y', '0')),
                            float(inst.get('z', '0')))
            else:
                level = manager.get(isa.get('source'))
                orig = Vec3(level.getByIsA(isa.get('name'))[0].getPos(render))
            orig.normalize()
            self.sun.setPos(orig)

        godrays = xml.find('godrays')
        if godrays != None:
            self.vlbuffer = base.win.makeTextureBuffer('volumetric-lighting',
                                                       base.win.getXSize() / 2,
                                                       base.win.getYSize() / 2)
            self.vlbuffer.setClearColor(Vec4(0, 0, 0, 1))
            cam = base.makeCamera(self.vlbuffer)
            cam.node().setLens(base.camLens)
            cam.reparentTo(base.cam)
            initstatenode = NodePath('InitialState')
            initstatenode.setColorScale(0, 0, 0, 1, 10000)
            initstatenode.setShaderOff(10000)
            initstatenode.setLightOff(10000)
            initstatenode.setMaterialOff(10000)
            initstatenode.setTransparency(TransparencyAttrib.MBinary, 10000)
            cam.node().setCameraMask(BitMask32.bit(2))
            cam.node().setInitialState(initstatenode.getState())
            self.vltexture = self.vlbuffer.getTexture()
            self.vltexture.setWrapU(Texture.WMClamp)
            self.vltexture.setWrapV(Texture.WMClamp)
            card = CardMaker('VolumetricLightingCard')
            card.setFrameFullscreenQuad()
            self.finalQuad = render2d.attachNewNode(card.generate())
            self.finalQuad.setAttrib(
                ColorBlendAttrib.make(ColorBlendAttrib.MAdd,
                                      ColorBlendAttrib.OIncomingColor,
                                      ColorBlendAttrib.OFbufferColor))

            self.finalQuad.setShader(
                Shader.load(
                    posixpath.join(
                        manager.get('paths').getConfig().find('shaders').get(
                            'path'), 'filter-vlight.cg')))
            self.finalQuad.setShaderInput('src', self.vltexture)
            self.finalQuad.setShaderInput(
                'vlparams', 32, 0.9 / 32.0, 0.97, 0.5
            )  # Note - first 32 is now hardcoded into shader for cards that don't support variable sized loops.
            self.finalQuad.setShaderInput('casterpos', 0.5, 0.5, 0, 0)
            # Last parameter to vlcolor is the exposure
            vlcolor = Vec4(float(godrays.get('r', '1')),
                           float(godrays.get('g', '1')),
                           float(godrays.get('b', '1')), 0.04)

            self.finalQuad.setShaderInput('vlcolor', vlcolor)
        else:
            self.finalQuad = None
예제 #15
0
파일: water.py 프로젝트: figo-sh/naith
    def __init__(self, manager, xml):
        self.surface = getWaterSurface(manager)
        self.surface.reparentTo(render)
        self.surface.hide(BitMask32.bit(1))  # Invisible to reflection camera (speedup)
        self.surface.hide(BitMask32.bit(2))  # Invisible to volumetric lighting camera (speedup)
        self.surface.hide(BitMask32.bit(3))  # Invisible to shadow cameras (speedup)
        self.surface.setShader(
            loader.loadShader(manager.get("paths").getConfig().find("shaders").get("path") + "/water.cg")
        )
        self.surface.setShaderInput("time", 0.0, 0.0, 0.0, 0.0)
        ntex = loader.loadTexture(manager.get("paths").getConfig().find("textures").get("path") + "/water-normal.png")
        ntex.setMinfilter(Texture.FTLinearMipmapLinear)
        self.surface.setShaderInput("normal", ntex)
        self.surface.setShaderInput("camera", base.cam)
        self.surface.setTransparency(TransparencyAttrib.MDual, 10)
        self.surface.setTwoSided(True)

        self.surface.setShaderInput("waveInfo", Vec4(0.4, 0.4, 0.4, 0))
        self.surface.setShaderInput("param2", Vec4(-0.015, 0.005, 0.05, 0.05))
        self.surface.setShaderInput("param3", Vec4(0.7, 0.3, 0, 0))
        self.surface.setShaderInput("param4", Vec4(2.0, 0.5, 0.5, 0.0))
        # self.surface.setShaderInput('speed', Vec4(-.8, -.4, -.9, .3))
        self.surface.setShaderInput("speed", Vec4(0.2, -1.2, -0.2, -0.7))
        self.surface.setShaderInput("deepcolor", Vec4(0.0, 0.3, 0.5, 1.0))
        self.surface.setShaderInput("shallowcolor", Vec4(0.0, 1.0, 1.0, 1.0))
        self.surface.setShaderInput("reflectioncolor", Vec4(0.95, 1.0, 1.0, 1.0))
        self.surface.hide()

        self.wbuffer = base.win.makeTextureBuffer("water", 512, 512)
        self.wbuffer.setClearColorActive(True)
        self.wbuffer.setClearColor(base.win.getClearColor())
        self.wcamera = base.makeCamera(self.wbuffer)
        if manager.get("sky") != None and manager.get("sky").model != None:
            self.sky = manager.get("sky").model.copyTo(self.wcamera)
            self.sky.setTwoSided(True)
            self.sky.setSz(self.sky, -1)
            self.sky.setClipPlaneOff(1)
            self.sky.show()
            self.sky.hide(BitMask32.bit(0))  # Hide for normal camera
            self.sky.hide(BitMask32.bit(2))  # Hide for volumetric lighting camera
            self.sky.hide(BitMask32.bit(3))  # Hide for shadow camera(s), if any
        else:
            self.sky = None
        self.wcamera.reparentTo(render)
        self.wcamera.node().setLens(base.camLens)
        self.wcamera.node().setCameraMask(BitMask32.bit(1))
        self.surface.hide(BitMask32.bit(1))
        wtexture = self.wbuffer.getTexture()
        wtexture.setWrapU(Texture.WMClamp)
        wtexture.setWrapV(Texture.WMClamp)
        wtexture.setMinfilter(Texture.FTLinearMipmapLinear)
        self.surface.setShaderInput("reflection", wtexture)
        self.wplane = Plane(Vec3(0, 0, 1), Point3(0, 0, 0))
        self.wplanenp = render.attachNewNode(PlaneNode("water", self.wplane))
        tmpnp = NodePath("StateInitializer")
        tmpnp.setClipPlane(self.wplanenp)
        tmpnp.setAttrib(CullFaceAttrib.makeReverse())
        self.wcamera.node().setInitialState(tmpnp.getState())

        # self.fog = Fog('UnderwaterFog')
        # self.fog.setColor(0.0,0.3,0.5)
        self.fogEnabled = False
    def __init__(self, filters):
        self.filters = filters
        self.updateTask = None
        self.finalQuad = None

        self.sun = base.cam.attachNewNode('sun')
        loader.loadModel("models/sphere").reparentTo(self.sun)
        self.sun.setScale(0.08)
        self.sun.setTwoSided(True)
        self.sun.setColorScale(1.0, 1.0, 1.0, 1.0, 10001)
        self.sun.setLightOff(1)
        self.sun.setShaderOff(1)
        self.sun.setFogOff(1)
        self.sun.setCompass()
        self.sun.setBin('background', 2)
        self.sun.setDepthWrite(False)
        self.sun.setDepthTest(False)
        # Workaround an annoyance in Panda. No idea why it's needed.
        self.sun.node().setBounds(OmniBoundingVolume())     

        direct = Vec4(2.0, 1.9, 1.8, 1) #bright for hdr
        #direct = Vec4(0.7, 0.65, 0.6, 1)
        self.dlight = DirectionalLight('dlight')
        self.dlight.setColor(direct)
        dlnp = self.sun.attachNewNode(self.dlight)
        render.setLight(dlnp)
        render.setShaderInput('dlight0', dlnp)

        self.setTime(700.0)

        pandaVolumetricLighting = False


        if pandaVolumetricLighting:
            self.filters.setVolumetricLighting( dlnp )
        else:
            self.vlbuffer = base.win.makeTextureBuffer('volumetric-lighting', base.win.getXSize() / 2, base.win.getYSize() / 2)
            self.vlbuffer.setClearColor(Vec4(0, 0, 0, 1))
            cam = base.makeCamera(self.vlbuffer)
            cam.node().setLens(base.camLens)
            cam.reparentTo(base.cam)
            initstatenode = NodePath('InitialState')
            initstatenode.setColorScale(0, 0, 0, 1, 10000)
            initstatenode.setShaderOff(10000)
            initstatenode.setFogOff(1)
            initstatenode.setLightOff(10000)
            initstatenode.setMaterialOff(10000)
            initstatenode.setTransparency(TransparencyAttrib.MBinary, 10000)
            cam.node().setCameraMask(BitMask32.bit(2))
            cam.node().setInitialState(initstatenode.getState())
            self.vltexture = self.vlbuffer.getTexture()
            self.vltexture.setWrapU(Texture.WMClamp)
            self.vltexture.setWrapV(Texture.WMClamp)
            card = CardMaker('VolumetricLightingCard')
            card.setFrameFullscreenQuad()
            self.finalQuad = render2d.attachNewNode(card.generate())
            self.finalQuad.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OIncomingColor, ColorBlendAttrib.OFbufferColor))
            self.finalQuad.setShader(Shader.load("shaders/filter-vlight.cg"))
            self.finalQuad.setShaderInput('src', self.vltexture)
            self.finalQuad.setShaderInput('vlparams', 32, 0.95 / 32.0, 0.985, 0.5) # Note - first 32 is now hardcoded into shader for cards that don't support variable sized loops.
            self.finalQuad.setShaderInput('casterpos', 0.5, 0.5, 0, 0)
            # Last parameter to vlcolor is the exposure
            vlcolor = Vec4(1, 0.99, 0.80, 0.03)
            self.finalQuad.setShaderInput('vlcolor', vlcolor)

        self.start()
예제 #17
0
    def renderSceneInto(self, depthtex=None, colortex=None, auxtex=None, auxbits=0, textures=None):

        """ Causes the scene to be rendered into the supplied textures
        instead of into the original window.  Puts a fullscreen quad
        into the original window to show the render-to-texture results.
        Returns the quad.  Normally, the caller would then apply a
        shader to the quad.

        To elaborate on how this all works:

        * An offscreen buffer is created.  It is set up to mimic
          the original display region - it is the same size,
          uses the same clear colors, and contains a DisplayRegion
          that uses the original camera.

        * A fullscreen quad and an orthographic camera to render
          that quad are both created.  The original camera is
          removed from the original window, and in its place, the
          orthographic quad-camera is installed.

        * The fullscreen quad is textured with the data from the
          offscreen buffer.  A shader is applied that tints the
          results pink.

        * Automatic shader generation NOT enabled.
          If you have a filter that depends on a render target from
          the auto-shader, you either need to set an auto-shader
          attrib on the main camera or scene, or, you need to provide
          these outputs in your own shader.

        * All clears are disabled on the original display region.
          If the display region fills the whole window, then clears
          are disabled on the original window as well.  It is
          assumed that rendering the full-screen quad eliminates
          the need to do clears.

        Hence, the original window which used to contain the actual
        scene, now contains a pink-tinted quad with a texture of the
        scene.  It is assumed that the user will replace the shader
        on the quad with a more interesting filter. """

        if (textures):
            colortex = textures.get("color", None)
            depthtex = textures.get("depth", None)
            auxtex = textures.get("aux", None)

        if (colortex == None):
            colortex = Texture("filter-base-color")
            colortex.setWrapU(Texture.WMClamp)
            colortex.setWrapV(Texture.WMClamp)

        texgroup = (depthtex, colortex, auxtex, None)

        # Choose the size of the offscreen buffer.

        (winx, winy) = self.getScaledSize(1,1,1)
        buffer = self.createBuffer("filter-base", winx, winy, texgroup)

        if (buffer == None):
            return None

        cm = CardMaker("filter-base-quad")
        cm.setFrameFullscreenQuad()
        quad = NodePath(cm.generate())
        quad.setDepthTest(0)
        quad.setDepthWrite(0)
        quad.setTexture(colortex)
        quad.setColor(Vec4(1,0.5,0.5,1))

        cs = NodePath("dummy")
        cs.setState(self.camstate)
        # Do we really need to turn on the Shader Generator?
        #cs.setShaderAuto()
        if (auxbits):
            cs.setAttrib(AuxBitplaneAttrib.make(auxbits))
        self.camera.node().setInitialState(cs.getState())

        quadcamnode = Camera("filter-quad-cam")
        lens = OrthographicLens()
        lens.setFilmSize(2, 2)
        lens.setFilmOffset(0, 0)
        lens.setNearFar(-1000, 1000)
        quadcamnode.setLens(lens)
        quadcam = quad.attachNewNode(quadcamnode)
        
        self.region.setCamera(quadcam)

        dr = buffer.getDisplayRegion(0)
        self.setStackedClears(dr, self.rclears, self.wclears)
        if (auxtex):
            dr.setClearActive(GraphicsOutput.RTPAuxRgba0, 1)
            dr.setClearValue(GraphicsOutput.RTPAuxRgba0, Vec4(0.5,0.5,1.0,0.0))
        self.region.disableClears()
        if (self.isFullscreen()):
            self.win.disableClears()
        dr.setCamera(self.camera)
        dr.setActive(1)

        self.buffers.append(buffer)
        self.sizes.append((1, 1, 1))

        return quad
예제 #18
0
    def renderSceneInto(self, depthtex=None, colortex=None, auxtex=None, auxbits=0, textures=None):

        """ Causes the scene to be rendered into the supplied textures
        instead of into the original window.  Puts a fullscreen quad
        into the original window to show the render-to-texture results.
        Returns the quad.  Normally, the caller would then apply a
        shader to the quad.

        To elaborate on how this all works:

        * An offscreen buffer is created.  It is set up to mimic
          the original display region - it is the same size,
          uses the same clear colors, and contains a DisplayRegion
          that uses the original camera.

        * A fullscreen quad and an orthographic camera to render
          that quad are both created.  The original camera is
          removed from the original window, and in its place, the
          orthographic quad-camera is installed.

        * The fullscreen quad is textured with the data from the
          offscreen buffer.  A shader is applied that tints the
          results pink.

        * Automatic shader generation NOT enabled.
          If you have a filter that depends on a render target from
          the auto-shader, you either need to set an auto-shader
          attrib on the main camera or scene, or, you need to provide
          these outputs in your own shader.

        * All clears are disabled on the original display region.
          If the display region fills the whole window, then clears
          are disabled on the original window as well.  It is
          assumed that rendering the full-screen quad eliminates
          the need to do clears.

        Hence, the original window which used to contain the actual
        scene, now contains a pink-tinted quad with a texture of the
        scene.  It is assumed that the user will replace the shader
        on the quad with a more interesting filter. """

        if (textures):
            colortex = textures.get("color", None)
            depthtex = textures.get("depth", None)
            auxtex = textures.get("aux", None)
            auxtex0 = textures.get("aux0", auxtex)
            auxtex1 = textures.get("aux1", None)
        else:
            auxtex0 = auxtex
            auxtex1 = None

        if (colortex == None):
            colortex = Texture("filter-base-color")
            colortex.setWrapU(Texture.WMClamp)
            colortex.setWrapV(Texture.WMClamp)

        texgroup = (depthtex, colortex, auxtex0, auxtex1)

        # Choose the size of the offscreen buffer.

        (winx, winy) = self.getScaledSize(1,1,1)
        buffer = self.createBuffer("filter-base", winx, winy, texgroup)

        if (buffer == None):
            return None

        cm = CardMaker("filter-base-quad")
        cm.setFrameFullscreenQuad()
        quad = NodePath(cm.generate())
        quad.setDepthTest(0)
        quad.setDepthWrite(0)
        quad.setTexture(colortex)
        quad.setColor(Vec4(1,0.5,0.5,1))

        cs = NodePath("dummy")
        cs.setState(self.camstate)
        # Do we really need to turn on the Shader Generator?
        #cs.setShaderAuto()
        if (auxbits):
            cs.setAttrib(AuxBitplaneAttrib.make(auxbits))
        self.camera.node().setInitialState(cs.getState())

        quadcamnode = Camera("filter-quad-cam")
        lens = OrthographicLens()
        lens.setFilmSize(2, 2)
        lens.setFilmOffset(0, 0)
        lens.setNearFar(-1000, 1000)
        quadcamnode.setLens(lens)
        quadcam = quad.attachNewNode(quadcamnode)
        
        self.region.setCamera(quadcam)

        self.setStackedClears(buffer, self.rclears, self.wclears)
        if (auxtex0):
            buffer.setClearActive(GraphicsOutput.RTPAuxRgba0, 1)
            buffer.setClearValue(GraphicsOutput.RTPAuxRgba0, Vec4(0.5, 0.5, 1.0, 0.0))
        if (auxtex1):
            buffer.setClearActive(GraphicsOutput.RTPAuxRgba1, 1)
        self.region.disableClears()
        if (self.isFullscreen()):
            self.win.disableClears()

        dr = buffer.makeDisplayRegion()
        dr.disableClears()
        dr.setCamera(self.camera)
        dr.setActive(1)

        self.buffers.append(buffer)
        self.sizes.append((1, 1, 1))

        return quad
예제 #19
0
파일: sun.py 프로젝트: figo-sh/naith
    def __init__(self, manager, xml):
        self.updateTask = None

        self.sun = base.cam.attachNewNode("sun")
        loader.loadModel(manager.get("paths").getConfig().find("misc").get("path") + "/sphere").reparentTo(self.sun)
        self.sun.setScale(0.1)
        self.sun.setTwoSided(True)
        self.sun.setColorScale(10.0, 10.0, 10.0, 1.0, 10001)
        self.sun.setLightOff(1)
        self.sun.setShaderOff(1)
        self.sun.setFogOff(1)
        self.sun.setCompass()
        self.sun.setBin("background", 10)
        self.sun.setDepthWrite(False)
        self.sun.setDepthTest(False)
        # Workaround an annoyance in Panda. No idea why it's needed.
        self.sun.node().setBounds(OmniBoundingVolume())
        isa = xml.find("isa")
        inst = xml.find("instance")
        if isa != None or inst != None:
            if inst != None:
                orig = Vec3(float(inst.get("x", "0")), float(inst.get("y", "0")), float(inst.get("z", "0")))
            else:
                level = manager.get(isa.get("source"))
                orig = Vec3(level.getByIsA(isa.get("name"))[0].getPos(render))
            orig.normalize()
            self.sun.setPos(orig)

        godrays = xml.find("godrays")
        if godrays != None:
            self.vlbuffer = base.win.makeTextureBuffer(
                "volumetric-lighting", base.win.getXSize() / 2, base.win.getYSize() / 2
            )
            self.vlbuffer.setClearColor(Vec4(0, 0, 0, 1))
            cam = base.makeCamera(self.vlbuffer)
            cam.node().setLens(base.camLens)
            cam.reparentTo(base.cam)
            initstatenode = NodePath("InitialState")
            initstatenode.setColorScale(0, 0, 0, 1, 10000)
            initstatenode.setShaderOff(10000)
            initstatenode.setLightOff(10000)
            initstatenode.setMaterialOff(10000)
            initstatenode.setTransparency(TransparencyAttrib.MBinary, 10000)
            cam.node().setCameraMask(BitMask32.bit(2))
            cam.node().setInitialState(initstatenode.getState())
            self.vltexture = self.vlbuffer.getTexture()
            self.vltexture.setWrapU(Texture.WMClamp)
            self.vltexture.setWrapV(Texture.WMClamp)
            card = CardMaker("VolumetricLightingCard")
            card.setFrameFullscreenQuad()
            self.finalQuad = render2d.attachNewNode(card.generate())
            self.finalQuad.setAttrib(
                ColorBlendAttrib.make(
                    ColorBlendAttrib.MAdd, ColorBlendAttrib.OIncomingColor, ColorBlendAttrib.OFbufferColor
                )
            )
            self.finalQuad.setShader(
                Shader.load(
                    posixpath.join(manager.get("paths").getConfig().find("shaders").get("path"), "filter-vlight.cg")
                )
            )
            self.finalQuad.setShaderInput("src", self.vltexture)
            self.finalQuad.setShaderInput(
                "vlparams", 32, 0.9 / 32.0, 0.97, 0.5
            )  # Note - first 32 is now hardcoded into shader for cards that don't support variable sized loops.
            self.finalQuad.setShaderInput("casterpos", 0.5, 0.5, 0, 0)
            # Last parameter to vlcolor is the exposure
            vlcolor = Vec4(
                float(godrays.get("r", "1")), float(godrays.get("g", "1")), float(godrays.get("b", "1")), 0.04
            )
            self.finalQuad.setShaderInput("vlcolor", vlcolor)
        else:
            self.finalQuad = None