I want to make a mirror in my game.
Here is my code.
def __init__(self):
ShowBase.__init__(self)
# tempnode = NodePath(PandaNode("temp node"))
# tempnode.setShaderAuto()
# self.cam.node().setInitialState(tempnode.getState())
# self.render.setShaderAuto()
self.cam.setPos(0, -6, 1)
self.player = Actor()
self.player.loadModel("Models/akai")
self.player.reparentTo(self.render)
# self.player.loadAnims({"Idle": "Models/idle"})
# self.player.loop("Idle")
self.player.loadAnims({"Walk": "Models/walk"})
self.player.loop("Walk")
self.world = self.loader.loadModel('environment')
self.world.setPos(0, 0, -.05)
self.world.reparentTo(self.render)
self.refPanel = self.loader.loadModel('Models/plane')
self.refPanel.reparentTo(self.render)
self.refPanel.setPos(0, 10, 0)
self.rig = NodePath('rig')
self.buffer = self.win.makeCubeMap('env', 2048, self.rig)
self.rig.reparentTo(self.refPanel)
self.refPanel.setTexGen(TextureStage.getDefault(),
TexGenAttrib.MWorldCubeMap)
self.refPanel.setTexture(self.buffer.getTexture())
The reflection has no problem.
But it seems that the image in the mirror is too big.
And…the image in the mirror seems get a little distortion.
Any help please?
Just like this.
It seems that the size of image in mirror can be affected by the distance between model and camera.
H3LLB0Y
September 27, 2023, 11:11pm
3
Hi SoNewBee,
Apologies if you’ve moved on, but if someone else finds this thread this might be of use.
I’ve recently stumbled upon this MirrorDemo.py
in the Panda3D GitHub Repo which might be useful for you.
"""This file demonstrates one way to create a mirror effect in Panda.
Call :func:`setupMirror()` to create a mirror in the world that reflects
everything in front of it.
The approach taken here is to create an offscreen buffer with its own
camera that renders its view into a texture, which is then applied to
the mirror geometry. The mirror's camera is repositioned each frame
with a task to keep it always on the opposite side of the mirror from
the main camera.
This demonstrates the basic interface for offscreen
render-to-a-texture in Panda. Similar approaches can be used for
related effects, such as a remote spy camera presenting its view onto
a closed-circuit television screen.
In this example the mirror itself is always perfectly flat--it's just
a single polygon, after all--but small distortions of the mirror
surface are possible, like a funhouse mirror. However, the reflection
itself is always basically planar; for more accurate convex
reflections, you will need to use a sphere map or a cube map."""
This file has been truncated. show original
It seems to take a slightly different approach to yours, but perhaps can be adapted into your project.
1 Like