from direct.showbase.ShowBase import ShowBase from panda3d.core import WindowProperties from panda3d.core import Vec3 from direct.task import Task from panda3d.core import DirectionalLight from panda3d.core import Point3 from win32api import GetSystemMetrics import pyautogui import math

class MyApp(ShowBase): def init(self): ShowBase.init(self)

    self.scene = self.loader.loadModel('models/environment')
    self.scene.reparentTo(self.render)

    self.scene.setScale(0.25, 0.25, 0.25)
    self.scene.setPos(-8, 42, 0)

    self.disableMouse()
    self.camera.setPos(0, -10, 0)
    self.camera.lookAt(0, 0, 0)

    self.target = Point3(0, 0, 0)
    self.distance = 5
    self.angleH = 0
    self.angleP = 0
    self.speedH = 0
    self.speedP = 0

    self.accept('mouse1', self.startDrag)
    self.accept('mouse3', self.stopDrag)

    self.isDragging = False
    self.lastPosition = None

    self.taskMgr.add(self.rotateCameraTask, 'rotateCameraTask')

    self.lastPosition = self.win.getPointer(0)
    pyautogui.mouseUp(button='middle')
    pyautogui.mouseUp(button='left')
    pyautogui.mouseUp(button='right')

def startDrag(self):
    self.isDragging = True
    self.lastPosition = self.win.getPointer(0)
    wp = WindowProperties()
    wp.setCursorHidden(True)
    self.win.requestProperties(wp)
    pyautogui.mouseDown(button='middle')

def stopDrag(self):
    if self.isDragging == True:
        try:
            pyautogui.mouseUp(button='middle')
        except:
            pass
        self.isDragging = False
        wp = WindowProperties()
        wp.setCursorHidden(False)
        self.win.requestProperties(wp)
        self.win.movePointer(0, self.win.getXSize() // 2, self.win.getYSize() // 2)
    else:
        self.startDrag()

def rotateCameraTask(self, task):
    if self.isDragging:
        currentPosition = self.win.getPointer(0)
        deltaX = self.lastPosition.getX() - currentPosition.getX()
        deltaY = self.lastPosition.getY() - currentPosition.getY()

        self.speedH = deltaX * 0.1
        self.speedP = deltaY * 0.1

        self.angleH += self.speedH
        self.angleP += self.speedP

        self.camera.setPos(self.target + Vec3(self.distance * math.sin(math.radians(self.angleH)) * math.cos(math.radians(self.angleP)),
                                              self.distance * math.cos(math.radians(self.angleH)) * math.cos(math.radians(self.angleP)),
                                              self.distance * math.sin(math.radians(self.angleP))))
        self.camera.lookAt(self.target)
        self.lastPosition = currentPosition

    return task.cont

app = MyApp() app.run()

Panda3D 摄像机控制 - 鼠标拖动旋转

原文地址: https://www.cveoy.top/t/topic/nYVc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录