Пример #1
0
    def __init__(self, x1, y1, x2, y2, z):
        Att_base.__init__(self, False, "Water1")
        # Water surface
        maker = CardMaker( 'water' )
        maker.setFrame( x1, x2, y1, y2 )

        self.waterNP = render.attachNewNode(maker.generate())
        self.waterNP.setHpr(0,-90,0)
        self.waterNP.setPos(0,0,z)
        self.waterNP.setTransparency(TransparencyAttrib.MAlpha )
        self.waterNP.setShader(loader.loadShader( 'Shaders/water1.sha' ))

        # Reflection plane
        self.waterPlane = Plane( Vec3( 0, 0, z+1 ), Point3( 0, 0, z ) )

        planeNode = PlaneNode( 'waterPlane' )
        planeNode.setPlane( self.waterPlane )

        # Buffer and reflection camera
        self.buffer = base.win.makeTextureBuffer( 'waterBuffer', 512, 512 )
        self.buffer.setClearColor( Vec4( 0, 0, 0, 1 ) )

        cfa = CullFaceAttrib.makeReverse( )
        rs = RenderState.make(cfa)

        self.watercamNP = base.makeCamera( self.buffer )
        self.watercamNP.reparentTo(render)

        sa = ShaderAttrib.make()
        sa = sa.setShader(loader.loadShader('Shaders/splut3Clipped.sha') )

        self.cam = self.watercamNP.node()
        self.cam.getLens( ).setFov( base.camLens.getFov( ) )
        self.cam.getLens().setNear(1)
        self.cam.getLens().setFar(5000)
        self.cam.setInitialState( rs )
        self.cam.setTagStateKey('Clipped')
        self.cam.setTagState('True', RenderState.make(sa))


        # ---- water textures ---------------------------------------------

        # reflection texture, created in realtime by the 'water camera'
        tex0 = self.buffer.getTexture( )
        tex0.setWrapU(Texture.WMClamp)
        tex0.setWrapV(Texture.WMClamp)
        ts0 = TextureStage( 'reflection' )
        self.waterNP.setTexture( ts0, tex0 )

        # distortion texture
        tex1 = loader.loadTexture('Textures/water.png')
        ts1 = TextureStage('distortion')
        self.waterNP.setTexture(ts1, tex1)
Пример #2
0
    def __init__(self, parent):
        self.parent = parent
        self.baseNode = self.parent.attachNewNode(self.__class__.__name__)

        x1 = -200
        y1 = -200
        x2 = 200
        y2 = 200
        z = 0.0

        #waterNP = NodePath("Water Node Parent")
        #waterNP.reparentTo(parent)

        #sn = self.drawSquare(-100, -100, 0, 100, 100, 0);
        #sn.reparentTo(self.baseNode)
        water = Square(self.baseNode, Point3(x1, y1, 0.2), Point3(x2, y2, 0.2),
                       Vec4(0.0, 0.0, 0.5, 0.5))
        wNp = water.draw()
        t1 = loader.loadTexture('assets/textures/wave.png')
        t1.setWrapU(Texture.WMRepeat)
        t1.setWrapV(Texture.WMRepeat)
        wNp.setTexture(t1)

        # Water Shader from
        # http://www.panda3d.org/forums/viewtopic.php?p=70853&sid=53d92b5ae1683bd9458f21d6026ad36e
        # anim: vx, vy, scale, skip
        # distort: offset, strength, refraction factor (0 = perfect mirror,
        #   1 = total refraction), refractivity
        anim = (.022, -.012, 2.5, 0)
        distort = (.1, 2, .5, .45)

        self.buffer = base.win.makeTextureBuffer('waterBuffer', 512, 512)
        self.watercamNP = base.makeCamera(self.buffer)

        # Create water surface using a card
        # The surface will be centered and
        maker = CardMaker('water')  # Water surface
        maker.setFrame(x1, x2, y1, y2)
        self.waterNP = self.baseNode.attachNewNode(maker.generate())
        self.waterNP.setPosHpr((0, 0, z), (0, -90, 0))
        self.waterNP.setTransparency(TransparencyAttrib.MAlpha)
        self.waterNP.setTwoSided(True)

        # Attach the water shader to the water shader surface
        waterShader = Shader.load("shaders/water.sha")
        self.waterNP.setShader(waterShader)
        self.waterNP.setShaderInput('wateranim', anim)
        self.waterNP.setShaderInput('waterdistort', distort)
        self.waterNP.setShaderInput('time', 0)

        self.waterPlane = Plane((0, 0, z + 1), (0, 0, z))  # Reflection plane
        PlaneNode('waterPlane').setPlane(self.waterPlane)

        self.buffer.setClearColor((0, 0, 0.5, 1))  # buffer

        self.watercamNP.reparentTo(self.baseNode)  # reflection camera
        cam = self.watercamNP.node()
        cam.getLens().setFov(base.camLens.getFov())
        cam.getLens().setNearFar(1, 5000)
        cam.setInitialState(RenderState.make(CullFaceAttrib.makeReverse()))
        cam.setTagStateKey('Clipped')
        cam.setTagState(
            'True',
            RenderState.make(ShaderAttrib.make().setShader(
                loader.loadShader('shaders/splut3Clipped.sha'))))

        tex0 = self.buffer.getTexture()  # reflection texture, created in
        # realtime by the 'water camera'
        tex0.setWrapU(Texture.WMClamp)
        tex0.setWrapV(Texture.WMClamp)
        self.waterNP.setTexture(TextureStage('reflection'), tex0)
        self.waterNP.setTexture(
            TextureStage('distortion'),
            loader.loadTexture(
                'assets/textures/water.png'))  # distortion texture

        self.task = taskMgr.add(self.update, 'waterUpdate', sort=50)
Пример #3
0
	def __init__(self, world, x1, y1, x2, y2, z):
		self.world = world
		logging.info(('setting up water plane at z=' + str(z)))
		
		# Water surface
		maker = CardMaker('water')
		maker.setFrame(x1, x2, y1, y2)
		
		self.waterNP = render.attachNewNode(maker.generate())
		self.waterNP.setHpr(0, -90, 0)
		self.waterNP.setPos(0, 0, z)
		self.waterNP.setTransparency(TransparencyAttrib.MAlpha)
		self.waterNP.setShader(loader.loadShader('shaders/water.sha'))
		self.waterNP.setShaderInput('wateranim', Vec4(0.03, -0.015, 64.0, 0)) # vx, vy, scale, skip
		# offset, strength, refraction factor (0=perfect mirror, 1=total refraction), refractivity
		self.waterNP.setShaderInput('waterdistort', Vec4(0.4, 4.0, 0.25, 0.45))
		self.waterNP.setShaderInput('time', 0)
		
		# Reflection plane	
		self.waterPlane = Plane(Vec3(0, 0, z + 1), Point3(0, 0, z))
		planeNode = PlaneNode('waterPlane')
		planeNode.setPlane(self.waterPlane)
		
		# Buffer and reflection camera
		buffer = base.win.makeTextureBuffer('waterBuffer', 512, 512)
		buffer.setClearColor(Vec4(0, 0, 0, 1))
		
		cfa = CullFaceAttrib.makeReverse()
		rs = RenderState.make(cfa)
		
		self.watercamNP = base.makeCamera(buffer)
		self.watercamNP.reparentTo(render)
		
		#sa = ShaderAttrib.make()
		#sa = sa.setShader(loader.loadShader('shaders/splut3Clipped.sha') )
		
		cam = self.watercamNP.node()
		cam.getLens().setFov(base.camLens.getFov())
		cam.getLens().setNear(1)
		cam.getLens().setFar(5000)
		cam.setInitialState(rs)
		cam.setTagStateKey('Clipped')
		#cam.setTagState('True', RenderState.make(sa))
		
		# ---- water textures -----------------------------------------------
		
		#reflection texture, created in realtime by the 'water camera'
		tex0 = buffer.getTexture()
		tex0.setWrapU(Texture.WMClamp)
		tex0.setWrapV(Texture.WMClamp)
		ts0 = TextureStage('reflection')
		self.waterNP.setTexture(ts0, tex0)
		
		# distortion texture
		tex1 = loader.loadTexture('textures/water.png')
		ts1 = TextureStage('distortion')
		self.waterNP.setTexture(ts1, tex1)
		
		# ---- Fog --- broken
		min = Point3(x1, y1, -999.0)
		max = Point3(x2, y2, z)
		boundry = BoundingBox(min, max
		self.waterFog = Fog('waterFog')
		self.waterFog.setBounds(boundry)
		colour = (0.2, 0.5, 0.8)
		self.waterFog.setColor(*colour)
		self.waterFog.setExpDensity(0.05)
		render.attachNewNode(self.waterFog)
		#render.setFog(world.waterFog)
		taskMgr.add(self.update, "waterTask")
		
	def update(self, task):
		# update matrix of the reflection camera
		mc = base.camera.getMat()
		mf = self.waterPlane.getReflectionMat()
		self.watercamNP.setMat(mc * mf)
		self.waterNP.setShaderInput('time', task.time)
		self.waterNP.setX(self.world.ralph.getX())
		self.waterNP.setY(self.world.ralph.getY())
		return task.cont
Пример #4
0
    def __init__(self, _base):
        
        self._base = _base
        
        self._base.cam.node().setActive(0)
        
        # window properties
        self.winprops = WindowProperties.size(base.win.getXSize(),base.win.getYSize())
        self.props = FrameBufferProperties()
        self.props.setDepthBits(1)
        self.props.setColorBits(1) 
        self.props.setAlphaBits(1) 
        
        # buffers creation
        self.geom_buffer = self.make_FBO("geom buffer",3)
        self.light_buffer = self.make_FBO("light buffer",0)
        self.transparency_buffer = self.make_FBO("transparency buffer",0)
        
        # cameras creation
        self.geom_cam = self._base.makeCamera(self.geom_buffer)
        self.light_cam = self._base.makeCamera(self.light_buffer)
        self.transparency_cam = self._base.makeCamera(self.transparency_buffer)
        
        # cameras setup
        self.geom_cam.node().getDisplayRegion(0).setClearColorActive(1) 
        self.geom_cam.node().getDisplayRegion(0).setClearColor(Vec4(1,0,0,1)) 
        self.geom_cam.node().getDisplayRegion(0).setClearDepthActive(1) 
        
        self.light_cam.node().getDisplayRegion(0).setClearColorActive(1) 
        self.geom_cam.node().getDisplayRegion(0).setClearColor(Vec4(0,1,0,1)) 
        self.light_cam.node().getDisplayRegion(0).setClearDepthActive(1) 
        
        self.transparency_cam.node().getDisplayRegion(0).setClearColorActive(1) 
        self.transparency_cam.node().getDisplayRegion(0).setClearDepthActive(1) 
        
        self.geom_cam.node().getLens().setNear(0.1)
        self.light_cam.node().getLens().setNear(0.1)
        self.transparency_cam.node().getLens().setNear(0.1)
        
        self.geom_cam.node().getLens().setFov(90)
        self.light_cam.node().getLens().setFov(90)
        self._base.cam.node().getLens().setFov(90)
        self.transparency_cam.node().getLens().setFov(90)
                
        # cameras masks
        self.geom_mask = BitMask32(1)
        self.light_mask = BitMask32(2)    
        self.transparency_mask = BitMask32(4) 
        
        self.light_cam.node().setCameraMask(self.light_mask)
        self.geom_cam.node().setCameraMask(self.geom_mask)
        self.transparency_cam.node().setCameraMask(self.transparency_mask)
        
        RENDER_NODES['LIGHTS'].hide(self.geom_mask)
        RENDER_NODES['GEOMS'].hide(self.light_mask)
        RENDER_NODES['TRANSPS'].hide(self.geom_mask)
        
        # link cameras
        self.geom_cam.node().setScene(RENDER_NODES['GEOMS'])
        self.light_cam.node().setScene(RENDER_NODES['LIGHTS'])
        self.transparency_cam.node().setScene(RENDER_NODES['TRANSPS'])
        
        # buffers rendering order
        self.light_buffer.setSort(0)
        self.geom_buffer.setSort(-50)
        self.transparency_buffer.setSort(50)
        self._base.win.setSort(-100)   

        # shadows cube creation
        self.shadow_cube = RENDER_NODES['GEOMS'].attachNewNode("cubemap")
        self.shadow_cube.setPos(0, 0, 0)
        buffer_cube_shadow = self._base.win.makeCubeMap("cube 1", 512, self.shadow_cube, camera_mask = self.geom_mask, to_ram = False, fbp = self.props)
        self.shadow_cube_texture = buffer_cube_shadow.getTexture()

        cameras = []
        for camera in self.shadow_cube.getChildren():
            cameras.append(camera)
            camera.node().getLens().setFov(90)
            camera.node().getLens().setNearFar(0.1 ,10.0)
            camera.node().setCameraMask(self.geom_mask)
            cameraInit = NodePath(PandaNode("cube camera initiator"))
            cameraInit.setShader(Shader.load('shaders/point_lights_cube.sha'))
            cameraInit.setShaderInput("light2", self.shadow_cube)
            cameraInit.setShaderInput("lightCamera", camera)
            cameraInit.setShaderInput("lensFar", Vec4(camera.node().getLens().getFar()))
            cameraInit.setShaderInput("transparency", Vec4(1))
            camera.node().setInitialState(cameraInit.getState())
            
        # reflections cube creation                    
        self.reflection_cube = RENDER_NODES['GEOMS'].attachNewNode("cubemap pivot")
        self.buffer_reflection_cube = self._base.win.makeCubeMap("cube 2", 512, self.reflection_cube, camera_mask = self.geom_mask, to_ram = False, fbp = self.props)
        self.reflection_cube_texture = self.buffer_reflection_cube.getTexture()

        for camera in self.reflection_cube.getChildren():
            camera.node().getLens().setNearFar(0.1 ,500.0)

        # shader rendering normal maps
        normal_shader = ShaderAttrib.make()
        normal_shader = normal_shader.setShader(Shader.load(CG_NORMALMAP))

        # shader rendering light
        light_shader = ShaderAttrib.make()
        light_shader = light_shader.setShader(Shader.load(CG_LIGHT))
        
        # link states with cameras
        self.geom_cam.node().setTagStateKey(SHADER_TAGS['g-buffer'])
        self.geom_cam.node().setTagState("True", RenderState.make(normal_shader))
        self.light_cam.node().setTagStateKey(SHADER_TAGS['compute light'])
        self.light_cam.node().setTagState("True", RenderState.make(light_shader))
        
        # render textures creation
        self.albedo_map = Texture()
        self.normal_map = Texture()
        self.depth_map = Texture()  
        self.specular_map = Texture()
        self.misc_map = Texture()
        self.light_map = Texture()
        self.transparency_map = Texture() 
        self.transparency_depth_map = Texture()
             
        # render textures   
        self.transparency_buffer.addRenderTexture(self.transparency_map, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPColor)
        self.transparency_buffer.addRenderTexture(self.transparency_depth_map, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPDepth)
        
        self.geom_buffer.addRenderTexture(self.normal_map, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPColor)
        self.geom_buffer.addRenderTexture(self.depth_map, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPDepth)
        self.geom_buffer.addRenderTexture(self.albedo_map, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPAuxRgba0)      
        self.geom_buffer.addRenderTexture(self.specular_map, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPAuxRgba1)
        self.geom_buffer.addRenderTexture(self.misc_map, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPAuxRgba2)
        
        self.light_buffer.addRenderTexture(self.light_map, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPColor)  
 
        taskMgr.add(self.update,'update reflections cubemap')
Пример #5
0
    def __init__(self, world, x1, y1, x2, y2, z):
        self.world = world
        logging.info(('setting up water plane at z=' + str(z)))

        # Water surface
        maker = CardMaker('water')
        maker.setFrame(x1, x2, y1, y2)

        self.waterNP = render.attachNewNode(maker.generate())
        self.waterNP.setHpr(0, -90, 0)
        self.waterNP.setPos(0, 0, z)
        self.waterNP.setTransparency(TransparencyAttrib.MAlpha)
        self.waterNP.setShader(loader.loadShader('shaders/water.sha'))
        self.waterNP.setShaderInput('wateranim',
                                    Vec4(0.03, -0.015, 64.0,
                                         0))  # vx, vy, scale, skip
        # offset, strength, refraction factor (0=perfect mirror, 1=total refraction), refractivity
        self.waterNP.setShaderInput('waterdistort', Vec4(0.4, 4.0, 0.25, 0.45))
        self.waterNP.setShaderInput('time', 0)

        # Reflection plane
        self.waterPlane = Plane(Vec3(0, 0, z + 1), Point3(0, 0, z))
        planeNode = PlaneNode('waterPlane')
        planeNode.setPlane(self.waterPlane)

        # Buffer and reflection camera
        buffer = base.win.makeTextureBuffer('waterBuffer', 512, 512)
        buffer.setClearColor(Vec4(0, 0, 0, 1))

        cfa = CullFaceAttrib.makeReverse()
        rs = RenderState.make(cfa)

        self.watercamNP = base.makeCamera(buffer)
        self.watercamNP.reparentTo(render)

        #sa = ShaderAttrib.make()
        #sa = sa.setShader(loader.loadShader('shaders/splut3Clipped.sha') )

        cam = self.watercamNP.node()
        cam.getLens().setFov(base.camLens.getFov())
        cam.getLens().setNear(1)
        cam.getLens().setFar(5000)
        cam.setInitialState(rs)
        cam.setTagStateKey('Clipped')
        #cam.setTagState('True', RenderState.make(sa))

        # ---- water textures ---------------------------------------------

        # reflection texture, created in realtime by the 'water camera'
        tex0 = buffer.getTexture()
        tex0.setWrapU(Texture.WMClamp)
        tex0.setWrapV(Texture.WMClamp)
        ts0 = TextureStage('reflection')
        self.waterNP.setTexture(ts0, tex0)

        # distortion texture
        tex1 = loader.loadTexture('textures/water.png')
        ts1 = TextureStage('distortion')
        self.waterNP.setTexture(ts1, tex1)

        # ---- Fog --- broken
        min = Point3(x1, y1, -999.0)
        max = Point3(x2, y2, z)
        boundry = BoundingBox(min, max)
        self.waterFog = Fog('waterFog')
        self.waterFog.setBounds(boundry)
        colour = (0.2, 0.5, 0.8)
        self.waterFog.setColor(*colour)
        self.waterFog.setExpDensity(0.05)
        render.attachNewNode(self.waterFog)
        #render.setFog(world.waterFog)
        taskMgr.add(self.update, "waterTask")
Пример #6
0
 def Destroy(self):
     base.graphicsEngine.removeWindow(self.buffer)
     self.cam.setInitialState(RenderState.makeEmpty())
     self.watercamNP.removeNode()
     self.waterNP.clearShader()
     self.waterNP.removeNode()
Пример #7
0
 def removeNode(self):
     self.cam.setInitialState(RenderState.makeEmpty())
     self.watercamNP.removeNode()
     self.waterNP.clearShader()
     self.waterNP.removeNode()
Пример #8
0
    def __init__(self, parent):
        self.parent = parent
        self.baseNode = self.parent.attachNewNode(self.__class__.__name__)
        
        x1 = -200
        y1 = -200
        x2 =  200
        y2 =  200
        z  =  0.0
        
        #waterNP = NodePath("Water Node Parent")
        #waterNP.reparentTo(parent)
        
        #sn = self.drawSquare(-100, -100, 0, 100, 100, 0);
        #sn.reparentTo(self.baseNode)
        water = Square(self.baseNode,
                       Point3(x1, y1, 0.2),
                       Point3(x2, y2, 0.2),
                       Vec4(0.0,0.0,0.5,0.5))
        wNp = water.draw()
        t1 = loader.loadTexture( 'assets/textures/wave.png' )
        t1.setWrapU(Texture.WMRepeat)
        t1.setWrapV(Texture.WMRepeat)
        wNp.setTexture(t1)
        
        # Water Shader from
        # http://www.panda3d.org/forums/viewtopic.php?p=70853&sid=53d92b5ae1683bd9458f21d6026ad36e
        # anim: vx, vy, scale, skip
        # distort: offset, strength, refraction factor (0 = perfect mirror,
        #   1 = total refraction), refractivity
        anim = ( .022, -.012, 2.5, 0 )
        distort = ( .1, 2, .5, .45 )
        
        self.buffer = base.win.makeTextureBuffer( 'waterBuffer', 512, 512 )
        self.watercamNP = base.makeCamera( self.buffer )
        
        # Create water surface using a card
        # The surface will be centered and 
        maker = CardMaker( 'water' ) # Water surface
        maker.setFrame( x1, x2, y1, y2 )
        self.waterNP = self.baseNode.attachNewNode( maker.generate() )
        self.waterNP.setPosHpr( ( 0, 0, z ), ( 0, -90, 0 ) )
        self.waterNP.setTransparency( TransparencyAttrib.MAlpha )
        self.waterNP.setTwoSided(True)
        
        # Attach the water shader to the water shader surface
        waterShader = Shader.load("shaders/water.sha")
        self.waterNP.setShader(waterShader)
        self.waterNP.setShaderInput('wateranim',    anim )
        self.waterNP.setShaderInput('waterdistort', distort )
        self.waterNP.setShaderInput('time',         0 )
        
        self.waterPlane = Plane( ( 0, 0, z + 1 ), ( 0, 0, z ) ) # Reflection plane
        PlaneNode( 'waterPlane' ).setPlane( self.waterPlane )
        
        self.buffer.setClearColor( ( 0, 0, 0.5, 1 ) ) # buffer

        self.watercamNP.reparentTo( self.baseNode ) # reflection camera
        cam = self.watercamNP.node()
        cam.getLens().setFov( base.camLens.getFov() )
        cam.getLens().setNearFar( 1, 5000 )
        cam.setInitialState( RenderState.make( CullFaceAttrib.makeReverse() ) )
        cam.setTagStateKey( 'Clipped' )
        cam.setTagState('True', RenderState.make(
          ShaderAttrib.make().setShader(
            loader.loadShader( 'shaders/splut3Clipped.sha' ) ) ) )
        
        tex0 = self.buffer.getTexture() # reflection texture, created in
                                             # realtime by the 'water camera'
        tex0.setWrapU( Texture.WMClamp ); tex0.setWrapV( Texture.WMClamp )
        self.waterNP.setTexture( TextureStage( 'reflection' ), tex0 )
        self.waterNP.setTexture( TextureStage( 'distortion' ),
          loader.loadTexture( 'assets/textures/water.png' ) ) # distortion texture
        
        self.task = taskMgr.add( self.update, 'waterUpdate', sort = 50 )