Why render.setShaderAuto() causes directional lighting not to work

    def setupLightning(self):
        dlight = DirectionalLight('sun')
        dlight.setColor((1,1,1,1))
        dlight.setShadowCaster(True,512,512)
        lens = PerspectiveLens()
        dlight.setLens(lens)
        alight = AmbientLight('shadow')
        alight.setColor((0.5,0.5,0.5,1))
        alnp = render.attachNewNode(alight)
        render.setLight(alnp)
        dlnp = render.attachNewNode(dlight)
        dlnp.setHpr(45,-45,0)
        render.setLight(dlnp)
        render.setShaderAuto()

изображение
tried to make shadows

You need to adjust the frustum of the light.

Here are several ways, you can choose the best option for your case.

On the contrary, I suspect that you’ll find that your box is lit–it’s just that you have your light angled such that it lights all three of the visible faces equally, making them all equally bright.

If you were to rotate the camera around the box, I think that you’d find the other faces to be darker–or, alternatively, if you were to adjust the angles of your light (e.g. to (15, -45, 0)) you would find the visible faces to have varying intensities of lighting.

I’m not sure of why you’re not seeing a shadow–that matter I’ll leave for others to figure out, I think!

there are no shadows, only ambient light

Have you moved the camera around the cube?

Trying your code in a simple program of my own, I found that while there were no shadows, the faces of my cube that were not facing the light were dimmer–which is what I was saying.

That is, there’s lighting, but not shadows.

forgot to change self.cube.setLight to render.setLight before posting code here, here is screenshot with ambient light 0,1 0,1 0,1 (directional light is still 1 1 1)

изображение

I don’t understand why we are talking about lighting here, while the topic of the post is shadows. To have shadows, you need to adjust the frustum at the light source.

Ah, I see! Indeed, it does look as though the light isn’t working, then!

Well, the code that you posted seems to work (aside from applying the lights to “render”, as you noted), so perhaps the problem is elsewhere–could you show us the rest of your code, please?

The thread-title also mentions lighting, and the cube is rather uniformly lit. My impression then is that both lighting and shadows are problems here.

i adjusted it in this way so it should work i think?

    def setupLightning(self):
        sun = DirectionalLight('sun')
        sun.setColor((1,1,1,1))
        sun.setShadowCaster(True,512,512)
        sun.getLens().set_film_size(100)
        sun.getLens().set_near_far(1,100)
        sun.showFrustum()
        alight = AmbientLight('shadow')
        alight.setColor((0.1,0.1,0.1,1))
        alnp = render.attachNewNode(alight)
        render.setLight(alnp)
        dlnp = render.attachNewNode(sun)
        dlnp.setHpr(45,-45,0)
        dlnp.setPos(30,-30,30)
        render.setLight(dlnp)
        render.setShaderAuto()

it might be kind of a mess, but here it is (im new panda3d)

from direct.showbase.ShowBase import ShowBase
from panda3d.core import loadPrcFile, CollisionBox, CollisionNode, CollisionHandlerQueue, CollisionTraverser, CollisionHandlerPusher, NodePath, ClockObject, BitMask32,Vec3,WindowProperties,DirectionalLight,Material,AmbientLight,PerspectiveLens
from panda3d.physics import ActorNode, PhysicsManager, ForceNode, LinearVectorForce
from panda3d.bullet import BulletWorld, BulletPlaneShape, BulletRigidBodyNode, BulletBoxShape, BulletDebugNode, BulletCharacterControllerNode, BulletCapsuleShape, ZUp
from direct.showbase.InputStateGlobal import inputState
from direct.gui.OnscreenImage import OnscreenImage, TransparencyAttrib

loadPrcFile('settings.prc')

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)

        self.plane = self.loader.loadModel("plane.glb")

        self.enableParticles()

        self.object_z_speed = 0
    
        taskMgr.add(self.update, 'update')

        self.world = BulletWorld()
        self.world.setGravity((0,0,-9.81))

        plane_shape = BulletBoxShape((5, 5, 0.5))
        
        brbnode = BulletRigidBodyNode('Ground')
        self.worldNP = render.attachNewNode(brbnode)
        brbnode.addShape(plane_shape)

        plane_np = render.attachNewNode(brbnode)
        plane_np.setPos(0, 0, 0)
        self.plane.reparentTo(plane_np)
        self.plane.setPos(0,0,0)
        self.world.attachRigidBody(brbnode)

        debugNode = BulletDebugNode('Debug')
        debugNode.showWireframe(True)
        debugNode.showConstraints(True)
        debugNode.showBoundingBoxes(True)
        debugNode.showNormals(True)
        self.debugNP = render.attachNewNode(debugNode)
        self.world.setDebugNode(self.debugNP.node())

        height = 1.85
        radius = 0.4
        shape = BulletCapsuleShape(radius, height - 2*radius, ZUp)

        self.playerNode = BulletCharacterControllerNode(shape, 0.4, 'Player')
        self.playerNP = self.worldNP.attachNewNode(self.playerNode)
        self.playerNP.setPos(-2, 0, 14)
        self.playerNP.setCollideMask(BitMask32.allOn())
        #self.camera.reparentTo(self.playerNP)
        self.camera.setPos(0,0,0.75)

        self.world.attachCharacter(self.playerNP.node())

        cube_shape = BulletBoxShape((0.5, 0.5, 0.5))
        cube_brbnode = BulletRigidBodyNode('Box')
        cube_brbnode.setMass(1.0)
        cube_brbnode.addShape(cube_shape)

        self.cube_np = render.attachNewNode(cube_brbnode)
        self.cube_np.setPos(0, 0, 5)

        self.world.attachRigidBody(cube_brbnode)

        self.cube = self.loader.loadModel("cube.glb")
        self.cube.reparentTo(self.cube_np)

        self.accept('f3', self.switchDebug)
        self.accept('escape', self.releaseMouse)
        self.accept('mouse1', self.captureMouse)

        inputState.watchWithModifiers('forward', 'w')
        inputState.watchWithModifiers('reverse', 's')
        inputState.watchWithModifiers('left', 'a')
        inputState.watchWithModifiers('right', 'd')
        inputState.watchWithModifiers('jump', 'space')

        self.setupCamera()
        self.captureMouse()

        self.playerH = 0
        self.playerP = 0
        self.mouseMultiplier = 0.1

        self.setupSkybox()
        self.setupLightning()

    def captureMouse(self):
        self.mouse_ctrl = True
        md = self.win.getPointer(0)
        self.lastMouseX = md.getX()
        self.lastMouseY = md.getY()

        properties = WindowProperties()
        properties.setCursorHidden(True)
        self.win.requestProperties(properties)

    def processInput(self):
        speed = Vec3(0, 0, 0)
        omega = 0.0
        if inputState.isSet('forward'): speed.setY( 3.0)
        if inputState.isSet('reverse'): speed.setY(-3.0)
        if inputState.isSet('left'):    speed.setX(-3.0)
        if inputState.isSet('right'):   speed.setX( 3.0)
        if inputState.isSet('jump'):    self.doJump()

        props = self.win.getProperties()
        windowCenterX = int(props.getXSize() / 2)
        windowCenterY = int(props.getYSize() / 2)

        md = self.win.getPointer(0)
        mouseX = md.getX()
        mouseY = md.getY()


        if self.mouse_ctrl:
            self.win.movePointer(0,windowCenterX,windowCenterY)
            self.playerH = -(mouseX-windowCenterX)*self.mouseMultiplier*75
            self.playerP -= (mouseY-windowCenterY)*self.mouseMultiplier
            if self.playerP < -90:
                self.playerP = -90
            elif self.playerP > 90:
                self.playerP = 90
            self.camera.setP(self.playerP)

        omega = self.playerH
        square_root = ((speed.getX()**2)+(speed.getY()**2))**0.5
        if speed.getY() != 0 or speed.getX() != 0:
            speed = Vec3(speed.getX()/square_root*3,speed.getY()/square_root*3,0)
        self.playerNode.setAngularMovement(omega)
        self.playerNode.setLinearMovement(speed, True)


    def setupLightning(self):
        sun = DirectionalLight('sun')
        sun.setColor((1,1,1,1))
        sun.setShadowCaster(True,512,512)
        sun.getLens().set_film_size(100)
        sun.getLens().set_near_far(1,100)
        sun.showFrustum()
        alight = AmbientLight('shadow')
        alight.setColor((0.1,0.1,0.1,1))
        alnp = render.attachNewNode(alight)
        render.setLight(alnp)
        dlnp = render.attachNewNode(sun)
        dlnp.setHpr(45,-45,0)
        dlnp.setPos(30,-30,30)
        render.setLight(dlnp)
        render.setShaderAuto()

    def setupSkybox(self):
        skybox = loader.loadModel('skybox.glb')
        skybox.setScale(500)
        skybox.setBin('background',0)
        skybox.setDepthWrite(0)
        skybox.setLightOff()
        skybox.reparentTo(self.playerNP)

    def doJump(self):
        self.playerNode.setJumpSpeed(5)
        self.playerNode.doJump()

    def setupCamera(self):
        #self.disableMouse()
        self.camLens.setFov(90)
        self.camLens.setNear(0.1)

        crosshair = OnscreenImage(image = 'crosshair.png', pos = (0, 0, 0), scale = 0.03)
        crosshair.setTransparency(TransparencyAttrib.MAlpha)

    def move(self,dir):
        self.dir = dir

    def update(self, task):
        dt = globalClock.getDt()
        self.world.doPhysics(dt)
        self.processInput()
        return task.cont

    def switchDebug(self):
        if self.debugNP.isHidden():
            self.debugNP.show()
            self.setFrameRateMeter(True)
        else:
            self.debugNP.hide()
            self.setFrameRateMeter(False)

    def releaseMouse(self):
        self.mouse_ctrl = False
        properties = WindowProperties()
        properties.setCursorHidden(False)
        self.win.requestProperties(properties)




app = MyApp()
app.run()

Hmm… The funny thing is, if I try that code (with my own models substituted into it) I see both lighting and shadows!

It’s possible that your skybox is interfering with your lighting–what happens if you comment out the call to “setupSkybox”?

tried, skybox not interfering with lightning (nothing changes)

just commented out loadPrcFile(‘settings.prc’) (with only one line “model-cache-dir” because some dude in guide said i need this setting to make my pixel textures not blurred) and this abomination appeared

The plane may have problems with the material, as well as the two-sided rendering of the cube. Here is a simple example, I think you should put your model in this code and see what happens.

from direct.showbase.ShowBase import ShowBase
from panda3d.core import AmbientLight, DirectionalLight

class Game(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        
        model = loader.loadModel("box")
        model.reparentTo(render)
        
        model1 = loader.loadModel("box")
        model1.reparentTo(render)
        model1.setScale(10, 10, 1)
        model1.setPos(-4, -4, -1)
        

        alight = AmbientLight("alight")
        alight.setColor((0.2, 0.2, 0.2, 1))
        alnp = render.attachNewNode(alight)

        slight = DirectionalLight('slight')
        slight.getLens().set_film_size(10)
        slight.getLens().set_near_far(0.1, 17)
        slight.setColor((1,1,1,1))
        slight.show_frustum()
        slight.setShadowCaster(True, 1024, 1024)
        
        slnp = render.attachNewNode(slight)
        slnp.setPos(0, 10, 5)
        slnp.setHpr(180, -20, 0)

        render.setShaderAuto()
        render.setLight(alnp)
        render.setLight(slnp)

game = Game()
game.run()
1 Like

oof

It looks like you used two-sided rendering of the material in a blender. I don’t know exactly where this option is located, but it should be disabled.

1 Like

cube rendered fine in panda3d with 2 sided rendering in blender before (and without setting)

wait, what you mean two sided rendering? you mean this setting?
изображение
i just made a cube and applied a texture to it, nothing else

maybe problem in this setting, maybe it broke something?
изображение

What is your version of the Blender. It’s just that this option is enabled for some reason in some versions

3.6.0