Sorry for double-post, but again I find myself doing something wrong and in need of help.
I try to connect this with a deferred renderer, but something is wrong. I see all that I should in the buffer viewer, but thereâs no effect on screen
My code so far:
from panda3d.core import *
from direct.filter.FilterManager import *
import random
class DeferredRenderer():
def __init__(self, base, scene_mask=1, light_mask=2, SSLR=False):
render.setShaderAuto()
# Camera setup
self.light_cam = base.makeCamera(base.win)
self.light_cam.reparentTo(base.cam)
base.cam.node().getLens().setNearFar(1.0, 5000.0)
self.scene_mask = BitMask32(scene_mask)
self.light_mask = BitMask32(light_mask)
base.cam.node().setCameraMask(self.scene_mask)
self.light_cam.node().setCameraMask(self.light_mask)
base.cam.node().getDisplayRegion(0).setSort(1)
self.light_cam.node().getDisplayRegion(0).setSort(2)
base.win.setSort(3)
self.light_cam.node().getDisplayRegion(0).setClearColor(Vec4(.0, .0, .0, 1))
self.light_cam.node().getDisplayRegion(0).setClearColorActive(1)
#Buffers creation
self.manager = FilterManager(base.win, base.cam)
self.depth = Texture()
self.albedo = Texture()
self.normal = Texture()
final_quad=self.manager.renderSceneInto(colortex = self.albedo,
depthtex = self.depth,
auxtex = self.normal,
auxbits = AuxBitplaneAttrib.ABOAuxNormal)
if SSLR:
ssr_filter, ssr_quad = self.make_filter_buffer(base.win,
'ssr_buffer', 1, 'ssr_zfar.sha',
texture = None,
inputs = [("albedo", self.albedo),
("depth", self.depth),
("normal", self.normal),
("mcamera", base.cam)
]
)
final_quad.setShader(Shader.load("mix.sha"))
final_quad.setShaderInput("albedo", self.albedo)
final_quad.setShaderInput("reflection", ssr_filter.getTexture())
#light list
self.lights=[]
#flickering lights
self.flicker={}
#geometry list
self.geometery=[]
taskMgr.doMethodLater(.1, self.update,'renderer_update')
def make_filter_buffer(self, srcbuffer, name, sort, prog, texture = None, inputs = None):
filterBuffer=base.win.makeTextureBuffer(name, 512, 512)
filterBuffer.setSort(sort)
filterBuffer.setClearColor(Vec4(1,0,0,1))
blurCamera=base.makeCamera2d(filterBuffer)
blurScene=NodePath("Filter scene %i" % sort)
blurCamera.node().setScene(blurScene)
shader = loader.loadShader(prog)
card = srcbuffer.getTextureCard()
if texture:
card.setTexture(texture)
card.reparentTo(blurScene)
card.setShader(shader)
if inputs:
for name, val in inputs:
card.setShaderInput(name, val)
return filterBuffer, card
def update(self,task):
for light in self.flicker:
self.doFlicker(light)
return task.again
def addGeometry(self, node):
if isinstance(node, basestring):
node=loader.loadModel(node)
node.reparentTo(render)
node.hide(self.light_mask)
node.setShader(Shader.load("gshader.sha"))
self.geometery.append(node)
#return the index of the node to remove it later
return self.geometery.index(self.geometery[-1])
def removeGeometry(self, geometryID):
if self.geometery[geometryID]:
self.geometery[geometryID].removeNode()
self.geometery[geometryID]=None
def addLight(self, color, model="volume/sphere", pos=None, radius=1.0, diffuse=Vec3(1,1,1), specular=Vec3(.8,.8,.8), attenuation=Vec3(0.1,0.2,0.003)):
#light geometry
if isinstance(model, basestring):
model=loader.loadModel(model)
self.lights.append(model)
self.lights[-1].reparentTo(render)
if pos:
self.lights[-1].setPos(pos)
self.lights[-1].setColor(color)
self.lights[-1].setScale(radius)
self.lights[-1].reparentTo(render)
self.lights[-1].setAttrib(DepthTestAttrib.make(RenderAttrib.MLess))
self.lights[-1].setAttrib(CullFaceAttrib.make(CullFaceAttrib.MCullCounterClockwise))
#self.lights[-1].setAttrib(CullFaceAttrib.make(CullFaceAttrib.MCullClockwise)) #??
self.lights[-1].setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OOne, ColorBlendAttrib.OOne))
self.lights[-1].setAttrib(DepthWriteAttrib.make(DepthWriteAttrib.MOff))
self.lights[-1].hide(self.scene_mask)
#light shader
self.lights[-1].setShader(loader.loadShader("def_light.cg"))
self.lights[-1].setShaderInput("albedo", self.albedo)
self.lights[-1].setShaderInput("depth", self.depth)
self.lights[-1].setShaderInput("normal", self.normal)
self.lights[-1].setShaderInput("Kd", diffuse)
self.lights[-1].setShaderInput("Ks",specular)
self.lights[-1].setShaderInput("att_params",attenuation)
self.lights[-1].setShaderInput("light_radius", radius)
self.lights[-1].setShaderInput("camera",base.camera)
#return the index of the light to remove it later
return self.lights.index(self.lights[-1])
def removeLight(self, lightID):
if self.lights[lightID]:
self.lights[lightID].removeNode()
self.lights[lightID]=None
def setFlicker(self, lightID, min_attenuation, max_attenuation):
self.flicker[lightID]=[min_attenuation, max_attenuation]
def doFlicker(self, lightID):
const=random.uniform(self.flicker[lightID][0][0], self.flicker[lightID][1][0])
lin=random.uniform(self.flicker[lightID][0][1], self.flicker[lightID][1][1])
quad=random.uniform(self.flicker[lightID][0][2], self.flicker[lightID][1][2])
self.lights[lightID].setShaderInput("att_params",(const, lin, quad))