polarCamera.py (7330B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\camera\polarCamera.py 2 import blue 3 import math 4 import geo2 5 import trinity 6 from carbon.common.lib.const import SEC 7 from carbon.common.script.util import mathCommon, mathUtil 8 from carbon.common.script.util.timerstuff import AutoTimer 9 from carbonui.camera.basicCamera import BasicCamera 10 11 class PolarCamera(BasicCamera): 12 13 def __init__(self): 14 BasicCamera.__init__(self) 15 self.poi = (0, 0, 0) 16 self.poiModifier = (0, 0, 0) 17 self.rotationSpeed = 0.0 18 self.minZoom = 0.0 19 self.maxZoom = 4.1 20 self.minPitch = 0.05 21 self.maxPitch = math.pi - 0.05 22 self.minRot = -math.pi 23 self.maxRot = math.pi 24 self.zoom = 0.75 25 self.distance = self.zoom 26 self.pitch = math.pi / 2 27 self.yaw = math.pi / 2 28 self.prevCameraDist = self.zoom 29 30 def PerformPick(self, x, y, ignoreEntID = -1): 31 return None 32 33 def GetRotationAsYaw(self): 34 return -self.yaw - math.pi / 2.0 35 36 def GetBasePointOfInterest(self): 37 return self.poi 38 39 def GetPointOfInterest(self): 40 return geo2.Vec3Add(self.poi, self.poiModifier) 41 42 def SetPointOfInterest(self, poi): 43 self.poi = poi 44 45 def AdjustPitch(self, delta): 46 self.SetPitch(self.pitch + delta) 47 48 def SetPitch(self, value): 49 self.pitch = max(min(value, self.maxPitch), self.minPitch) 50 51 def GetPitch(self): 52 return self.pitch 53 54 def SetPosition(self, position): 55 yaw, pitch, dist = self.PointToYawPitchDist(position) 56 self.SetYaw(yaw) 57 self.SetPitch(pitch) 58 self.SetZoom(dist) 59 60 def SetYawPitchDist(self, yaw, pitch, dist): 61 self.SetYaw(yaw) 62 self.SetPitch(pitch) 63 self.SetZoom(dist) 64 65 def _AssembleViewMatrix(self, cameraPOI, yaw, pitch, dist): 66 camera_pitch = math.pi / 2.0 - pitch 67 camera_yaw = math.pi / 2.0 - yaw 68 camera_roll = 0 69 camera_offset = self.YawPitchDistToPoint(yaw, pitch, dist) 70 camera_location = geo2.Vec3Add(camera_offset, cameraPOI) 71 matrix = mathCommon.ConvertTriToTupleMatrix(self.CreateViewMatrix(camera_location, (camera_yaw, camera_pitch, camera_roll))) 72 return (matrix, camera_location) 73 74 def AssembleViewMatrix(self, cameraPOI, yaw, pitch, dist, setInternals = False, instanceID = None): 75 matrix, camera_location = self._AssembleViewMatrix(cameraPOI, yaw, pitch, dist) 76 self.cameraPosition = camera_location 77 if setInternals: 78 self.poi = cameraPOI 79 self.SetYaw(yaw) 80 self.SetPitch(pitch) 81 self.viewMatrix.transform = matrix 82 83 def AssembleBaseViewMatrix(self, cameraPOI, yaw, pitch, dist): 84 matrix, trash = self._AssembleViewMatrix(cameraPOI, yaw, pitch, dist) 85 self.baseViewMatrix.transform = matrix 86 87 def CreateViewMatrix(self, location, rotation): 88 cameraTranslation = trinity.TriMatrix() 89 cameraRotation = trinity.TriMatrix() 90 x, y, z = location 91 cameraTranslation.Translation(x, y, z) 92 cameraRotation.RotationYawPitchRoll(rotation[0], rotation[1], rotation[2]) 93 cameraFinal = cameraRotation 94 cameraFinal.Multiply(cameraTranslation) 95 cameraFinal.Inverse() 96 return cameraFinal 97 98 def PointToYawPitchDist(self, pos): 99 upVector = (0, 1, 0) 100 if trinity.IsRightHanded(): 101 rotMatrix = geo2.MatrixLookAtRH(pos, self.poi, upVector) 102 else: 103 rotMatrix = geo2.MatrixLookAtLH(pos, self.poi, upVector) 104 rotMatrix = geo2.MatrixTranspose(rotMatrix) 105 quat = geo2.QuaternionRotationMatrix(rotMatrix) 106 yaw, pitch, roll = geo2.QuaternionRotationGetYawPitchRoll(quat) 107 yaw = math.pi / 2 - yaw 108 pitch = math.pi / 2 - pitch 109 return (yaw, pitch, geo2.Vec3Distance(pos, self.poi)) 110 111 def YawPitchDistToPoint(self, yaw, pitch, dist): 112 if trinity.IsRightHanded(): 113 position = (dist * math.sin(pitch) * math.cos(yaw), -dist * math.cos(pitch), dist * math.sin(pitch) * math.sin(yaw)) 114 else: 115 position = (-dist * math.sin(pitch) * math.cos(yaw), dist * math.cos(pitch), -dist * math.sin(pitch) * math.sin(yaw)) 116 return position 117 118 def SetYaw(self, value, ignoreUpdate = True): 119 self.yaw = value 120 if self.yaw < self.minRot: 121 self.yaw += math.pi * 2.0 122 elif self.yaw > self.maxRot: 123 self.yaw -= math.pi * 2.0 124 125 def GetYaw(self): 126 return self.yaw 127 128 def AdjustYaw(self, delta, maxRotate = None, ignoreUpdate = True): 129 if maxRotate: 130 if delta > maxRotate: 131 delta = maxRotate 132 if delta < -maxRotate: 133 delta = -maxRotate 134 self.SetYaw(self.yaw + delta, ignoreUpdate=ignoreUpdate) 135 136 def AdjustZoom(self, delta): 137 if delta < 0: 138 self.zoom = self.prevCameraDist 139 self.zoom += delta 140 if self.zoom < self.minZoom: 141 self.zoom = self.minZoom 142 self.zoom = min(self.zoom, self.maxZoom) 143 self.prevCameraDist = self.zoom 144 145 def SetZoom(self, zoom): 146 self.prevCameraDist = self.zoom = zoom 147 148 def VerifyMaxZoom(self): 149 raise NotImplementedError('VerifyMaxZoom must be implemented on derived camera classes') 150 151 def SmoothMove(self, targetPosition, targetRotation, targetTilt, targetDist, durationMS, callbackOnEnd = None): 152 sPos, sRot, sTilt, sDist = (self.GetPointOfInterest(), 153 self.yaw, 154 self.pitch, 155 self.zoom) 156 ePos, eRot, eTilt, eDist = (targetPosition, 157 targetRotation, 158 targetTilt, 159 targetDist) 160 eRot = sRot + mathCommon.GetLesserAngleBetweenYaws(sRot, eRot) 161 self._smoothMoveTimer = AutoTimer(50, self.UpdateSmoothMove, sPos, sRot, sTilt, sDist, ePos, eRot, eTilt, eDist, blue.os.GetWallclockTime(), float(durationMS), callbackOnEnd) 162 163 def UpdateSmoothMove(self, sPos, sRot, sTilt, sDist, ePos, eRot, eTilt, eDist, startTime, durationMS, callbackOnEnd): 164 ndt = blue.os.TimeDiffInMs(startTime, blue.os.GetWallclockTime()) / durationMS 165 ndt = max(0.0, min(1.0, ndt)) 166 self.AssembleViewMatrix((mathUtil.Lerp(sPos[0], ePos[0], ndt), mathUtil.Lerp(sPos[1], ePos[1], ndt), mathUtil.Lerp(sPos[2], ePos[2], ndt)), mathUtil.Lerp(sRot, eRot, ndt), mathUtil.Lerp(sTilt, eTilt, ndt), mathUtil.Lerp(sDist, eDist, ndt), True) 167 if ndt >= 1.0: 168 self._smoothMoveTimer = None 169 self.zoom = eDist 170 if callbackOnEnd: 171 callbackOnEnd() 172 173 def Update(self): 174 now = blue.os.GetWallclockTime() 175 frameTime = float(now - self.lastUpdateTime) / SEC 176 self.poiModifier = (0, 0, 0) 177 for priority, behavior in self.cameraBehaviors: 178 behavior.ProcessCameraUpdate(self, now, frameTime) 179 180 self.lastUpdateTime = now 181 if abs(self.rotationSpeed) > 0: 182 self.AdjustYaw(self.rotationSpeed * frameTime) 183 self.AssembleViewMatrix(self.GetPointOfInterest(), self.yaw, self.pitch, self.distance) 184 self.AssembleBaseViewMatrix(self.GetBasePointOfInterest(), self.yaw, self.pitch, self.distance) 185 186 def SetRotationSpeed(self, rotationSpeed): 187 self.rotationSpeed = rotationSpeed