def OrbitUpdateThread(self): try: while True: if self.orbitTarget is None: break vLookAt = self.GetLookAtDirection() currPitch = self.GetAngleLookAtToUpDirection() self.eyePosition = geo2.Subtract(self.eyePosition, self.atPosition) yawLeft = self.orbitTarget[0] if yawLeft: yaw = self.kOrbitSpeed * yawLeft / blue.os.fps if math.fabs(yawLeft) < self.kOrbitStopAngle: yaw = yawLeft rotYaw = geo2.MatrixRotationAxis(self.upDirection, yaw) self.eyePosition = geo2.Vec3Transform(self.eyePosition, rotYaw) self.orbitTarget[0] -= yaw targetPitch = self.orbitTarget[1] pitchLeft = currPitch - targetPitch if pitchLeft: pitch = self.kOrbitSpeed * pitchLeft / blue.os.fps if math.fabs(pitchLeft) < self.kOrbitStopAngle: pitch = pitchLeft axis = geo2.Vec3Cross(vLookAt, self.upDirection) rotPitch = geo2.MatrixRotationAxis(axis, pitch) self.eyePosition = geo2.Vec3Transform(self.eyePosition, rotPitch) self.eyePosition = geo2.Add(self.eyePosition, self.atPosition) if not pitchLeft and not yawLeft: break blue.synchro.Yield() finally: self.orbitUpdateThread = None self.orbitTarget = None
def Rotate(self, x = 0, y = 0): xAxis = self.GetXAxis() yAxis = self.GetYAxis() self.atPosition = geo2.Subtract(self.atPosition, self.eyePosition) rotY = geo2.MatrixRotationAxis(xAxis, y) self.atPosition = geo2.Vec3Transform(self.atPosition, rotY) rotX = geo2.MatrixRotationAxis(yAxis, x) self.atPosition = geo2.Vec3Transform(self.atPosition, rotX) self.atPosition = geo2.Add(self.atPosition, self.eyePosition)
def _UpdateRotateOffset(self): if self._rotateOffset != (0.0, 0.0): yaw, pitch = self._rotateOffset rotMat = geo2.MatrixRotationAxis(self.upDirection, -yaw) eyeAtVec = geo2.Vec3Subtract(self._atPosition, self._eyePosition) vec = geo2.Vec3Transform(eyeAtVec, rotMat) axis = geo2.Vec3Normalize(geo2.Vec3Cross(vec, self.upDirection)) rotMat = geo2.MatrixRotationAxis(axis, -pitch) vec = geo2.Vec3Transform(vec, rotMat) offset = geo2.Vec3Subtract(vec, eyeAtVec) self._AddToAtOffset(offset)
def SetupScene(self, hangarScene): for obj in hangarScene.objects: if hasattr(obj, 'PlayAnimationEx'): obj.PlayAnimationEx('NormalLoop', 0, 0.0, 1.0) for obj in hangarScene.objects: if '_Traffic_' in obj.name: obj.RebuildBoosterSet() trafficStartEndArea = {} for obj in hangarScene.objects: if obj.__bluetype__ == 'trinity.EveStation2': for loc in obj.locators: if 'Traffic_Start_' in loc.name or 'Traffic_End_' in loc.name: trafficStartEndArea[loc.name] = geo2.Vec3Transform( (0.0, 0.0, 0.0), loc.transform) if len(trafficStartEndArea) == 6: for obj in hangarScene.objects: if '_Traffic_' in obj.name: obj.display = False obj.translationCurve = trinity.TriVectorCurve() obj.translationCurve.keys.append(trinity.TriVectorKey()) obj.translationCurve.keys.append(trinity.TriVectorKey()) obj.rotationCurve = trinity.TriRotationCurve() if random.randint(0, 1) == 0: obj.rotationCurve.value = geo2.QuaternionRotationSetYawPitchRoll( 0.5 * math.pi, 0.0, 0.0) else: obj.rotationCurve.value = geo2.QuaternionRotationSetYawPitchRoll( -0.5 * math.pi, 0.0, 0.0) uthreadObj = uthread.new(self.AnimateTraffic, obj, trafficStartEndArea) uthreadObj.context = 'HangarTraffic::SetupScene' self.threadList.append(uthreadObj)
def SetPitch(self, pitch): pitch = self.ClampPitch(pitch) axis = geo2.Vec3Cross(self.upDirection, self.GetLookAtDirection()) rotMat = geo2.MatrixRotationAxis(axis, pitch) vec = (0, self.GetZoomDistance(), 0) self._eyePosition = geo2.Vec3Add(self._atPosition, geo2.Vec3Transform(vec, rotMat))
def SetYaw(self, yaw): rotMat = geo2.MatrixRotationY(yaw - math.pi) eyePos = geo2.Vec3Subtract(self._eyePosition, self._atPosition) x = math.sqrt(eyePos[0]**2 + eyePos[2]**2) vec = (0, eyePos[1], x) self._eyePosition = geo2.Vec3Add(geo2.Vec3Transform(vec, rotMat), self._atPosition)
def PickObject(self, x, y): if self.sceneManager.GetActiveScene() != self.renderScene: return rescale = 1.0 / 10000.0 projection = trinity.TriProjection() projection.PerspectiveFov(trinity.GetFieldOfView(), trinity.GetAspectRatio(), trinity.GetFrontClip(), trinity.GetBackClip()) view = trinity.TriView() view.transform = trinity.GetViewTransform() scaling, rotation, translation = geo2.MatrixDecompose( self.transform.worldTransform) pZ = geo2.Vec3Transform((0, 0, 1), self.transform.worldTransform) surfaceNormal = geo2.Subtract(pZ, translation) cameraZ = geo2.Vector(view.transform[0][2], view.transform[1][2], view.transform[2][2]) if geo2.Vec3Dot(surfaceNormal, cameraZ) < 0: return self.renderObject.translation = geo2.Vec3Scale(translation, rescale) self.renderObject.rotation = rotation self.renderObject.scaling = geo2.Vec3Scale(scaling, rescale) scaling, rotation, translation = geo2.MatrixDecompose(view.transform) translation = geo2.Vec3Scale(translation, rescale) view.transform = geo2.MatrixTransformation(None, None, scaling, None, rotation, translation) return self.renderObject.PickObject(x, y, projection, view, trinity.device.viewport)
def fset(self, rotationQuaternion): rotationMatrix = geo2.MatrixRotationQuaternion(rotationQuaternion) transformedVec = geo2.Vec3Transform((1.0, 0.0, 0.0), rotationMatrix) cameraDistance = geo2.Vec3Length(self._eyePositionCurrent) self._eyePositionCurrent = geo2.Vec3Scale(transformedVec, cameraDistance) self._eyePosition = self._eyePositionCurrent
def Update(self): speedFactor = 0.2 diff = geo2.Vec3Subtract(self.pointOfInterest, self._pointOfInterestCurrent) diffLength = geo2.Vec3Length(diff) if diffLength > 0.001: self._pointOfInterestCurrent = geo2.Vec3Add(self._pointOfInterestCurrent, geo2.Vec3Scale(diff, speedFactor)) else: self._pointOfInterestCurrent = self.pointOfInterest if abs(self._yawSpeed) > 0.0001: yawChange = self._yawSpeed * speedFactor rotYaw = geo2.MatrixRotationAxis(self.upVector, yawChange) self._eyePositionCurrent = geo2.Vec3Transform(self._eyePositionCurrent, rotYaw) self._yawSpeed -= yawChange else: self._yawSpeed = 0.0 if abs(self._pitchSpeed) > 0.0001: pitchChange = self._pitchSpeed * speedFactor viewVectorNormalized = geo2.Vec3Normalize(self._eyePositionCurrent) axis = geo2.Vec3Cross(viewVectorNormalized, self.upVector) rotPitch = geo2.MatrixRotationAxis(axis, pitchChange) self._eyePositionCurrent = geo2.Vec3Transform(self._eyePositionCurrent, rotPitch) self._pitchSpeed -= pitchChange else: self._pitchSpeed = 0.0 if self._panSpeed: panDistance = geo2.Vec3Length(self._panSpeed) if panDistance > 0.001: toMove = geo2.Vec3Scale(self._panSpeed, 0.95) self.pointOfInterest = geo2.Add(self.pointOfInterest, toMove) self._panSpeed -= toMove else: self._panSpeed = None cameraDistance = self.GetZoomDistance() cameraDistanceDiff = self._translationFromPOI - cameraDistance if math.fabs(cameraDistanceDiff) > 0.001: usedDist = cameraDistanceDiff * 0.1 viewVectorNormalized = geo2.Vec3Normalize(self._eyePositionCurrent) newDistance = min(self.maxDistance, max(self.minDistance, cameraDistance + usedDist)) self._eyePositionCurrent = geo2.Vec3Scale(viewVectorNormalized, newDistance) self.translationFromParent = newDistance self.UpdateProjection() self.UpdateView() if self.callback: self.callback()
def GetCorrectCameraXandYFactors(self, position, poi): cameraZ = geo2.Vec3Normalize(geo2.Subtract(position, poi)) cameraX = geo2.Vec3Cross(cameraZ, (0.0, 1.0, 0.0)) cameraY = geo2.Vec3Cross(cameraZ, cameraX) cameraMatrix = ((cameraX[0], cameraY[0], cameraZ[0], 0.0), (cameraX[1], cameraY[1], cameraZ[1], 0.0), (cameraX[2], cameraY[2], cameraZ[2], 0.0), (0.0, 0.0, 0.0, 1.0)) offset = geo2.Subtract(self.focus, poi) res = geo2.Vec3Transform(offset, cameraMatrix) return (res[0], res[1])
def _UpdateYaw(self): yawRemaining = self.orbitTarget[0] if yawRemaining: if math.fabs(yawRemaining) < self.kOrbitStopAngle: yaw = yawRemaining yawRemaining = None else: yaw = yawRemaining * self._GetOrbitSpeed() rotYaw = geo2.MatrixRotationAxis(self.upDirection, yaw) self.SetEyePosition(geo2.Vec3Transform(self._eyePosition, rotYaw)) self.orbitTarget[0] -= yaw return yawRemaining
def PickObject(self, obj, rayOrigin, rayDirection, areaName = None): if obj.placeableRes is None: return model = obj.placeableRes.visualModel if model is None: return pickingGeometry = None pickingMesh = None pickingArea = None pickingCount = None for mesh in model.meshes: for area in mesh.transparentAreas: if areaName is None or area.name == areaName: pickingGeometry = mesh.geometry pickingMesh = mesh.meshIndex pickingArea = area.index pickingCount = area.count break for area in mesh.opaquePrepassAreas: if areaName is None or area.name == areaName: pickingGeometry = mesh.geometry pickingMesh = mesh.meshIndex pickingArea = area.index pickingCount = area.count break if pickingGeometry is not None: world = ((obj.transform._11, obj.transform._12, obj.transform._13, obj.transform._14), (obj.transform._21, obj.transform._22, obj.transform._23, obj.transform._24), (obj.transform._31, obj.transform._32, obj.transform._33, obj.transform._34), (obj.transform._41, obj.transform._42, obj.transform._43, obj.transform._44)) worldInv = geo2.MatrixInverse(world) origin = geo2.Vec3Transform(rayOrigin, worldInv) direction = geo2.Vec3TransformNormal(rayDirection, worldInv) for area in range(pickingCount): result = pickingGeometry.GetRayAreaIntersection(origin, direction, pickingMesh, pickingArea + area, trinity.TriGeometryCollisionResultFlags.ANY) if result is not None: return result
def _UpdatePitch(self, currPitch, vLookAt): targetPitch = self.orbitTarget[1] pitchRemaining = currPitch - targetPitch if pitchRemaining: if math.fabs(pitchRemaining) < self.kOrbitStopAngle: pitch = pitchRemaining pitchRemaining = None else: pitch = pitchRemaining * self._GetOrbitSpeed() axis = geo2.Vec3Cross(vLookAt, self.upDirection) rotPitch = geo2.MatrixRotationAxis(axis, pitch) self.SetEyePosition(geo2.Vec3Transform(self._eyePosition, rotPitch)) return pitchRemaining
def ProjectWorldToCamera(self, vec): return geo2.Vec3Transform(vec, self.viewMatrix.transform)
def _GetTrackEyeOffsetDirection(self): pitch = math.pi - self.GetPitch() rotMat = geo2.MatrixRotationAxis(self.GetZAxis(), -pitch) return geo2.Vec3Transform(self.GetYAxis(), rotMat)
def Update(self): pointOfInterestOverrideValue = self.GetPointOfInterestOverrideValue() if pointOfInterestOverrideValue is not None: self._pointOfInterest = pointOfInterestOverrideValue elif self.followMarker: followMarker = self.followMarker() if followMarker: markerPosition = followMarker.GetDisplayPosition() if markerPosition is not None: self._pointOfInterest = markerPosition speedFactor = 0.4 diff = geo2.Vec3Subtract(self._pointOfInterest, self._pointOfInterestCurrent) diffLength = geo2.Vec3Length(diff) if diffLength > 0.001: addVector = geo2.Vec3ScaleD(diff, speedFactor) newPosition = geo2.Vec3Add(self._pointOfInterestCurrent, addVector) if geo2.Vec3Equal(newPosition, self._pointOfInterestCurrent): newPosition = self._pointOfInterest self._pointOfInterestCurrent = newPosition else: self._pointOfInterestCurrent = self._pointOfInterest if abs(self._yawSpeed) > 0.0001: yawChange = self._yawSpeed * speedFactor rotYaw = geo2.MatrixRotationAxis(self.upVector, yawChange) self._eyePositionCurrent = geo2.Vec3Transform( self._eyePositionCurrent, rotYaw) currentPositionN = geo2.Vec3Normalize(self._eyePositionCurrent) self._eyePosition = geo2.Vec3Scale( currentPositionN, geo2.Vec3Length(self._eyePosition)) self._yawSpeed -= yawChange else: self._yawSpeed = 0.0 if abs(self._pitchSpeed) > 0.0001: pitchChange = self._pitchSpeed * speedFactor viewVectorNormalized = geo2.Vec3Normalize(self._eyePositionCurrent) axis = geo2.Vec3Cross(viewVectorNormalized, self.upVector) rotPitch = geo2.MatrixRotationAxis(axis, pitchChange) self._eyePositionCurrent = geo2.Vec3Transform( self._eyePositionCurrent, rotPitch) currentPositionN = geo2.Vec3NormalizeD(self._eyePositionCurrent) self._eyePosition = geo2.Vec3ScaleD( currentPositionN, geo2.Vec3Length(self._eyePosition)) self._pitchSpeed -= pitchChange else: self._pitchSpeed = 0.0 setCameraDistance = geo2.Vec3Length(self._eyePosition) currentCameraDistance = geo2.Vec3Length(self._eyePositionCurrent) cameraDistanceDiff = setCameraDistance - currentCameraDistance if math.fabs(cameraDistanceDiff) > 0.001: usedDist = cameraDistanceDiff * speedFactor * 0.5 viewVectorNormalized = geo2.Vec3NormalizeD( self._eyePositionCurrent) newDistance = min( self.maxDistance, max(self.minDistance, currentCameraDistance + usedDist)) self._eyePositionCurrent = geo2.Vec3ScaleD(viewVectorNormalized, newDistance) self.UpdateProjection() self.UpdateView() if self.callback: self.callback()
def CreateMiniCollisionObject(name, miniballs, minicapsules, miniboxes): t = trinity.EveRootTransform() sphere = blue.resMan.LoadObject('res:/Model/Global/Miniball.red') capsule = blue.resMan.LoadObject('res:/Model/Global/Minicapsule.red') box = blue.resMan.LoadObject('res:/Model/Global/Minibox.red') if len(miniballs) > 0: for miniball in miniballs: mball = sphere.CopyTo() mball.translation = (miniball.x, miniball.y, miniball.z) r = miniball.radius * 2 mball.scaling = (r, r, r) t.children.append(mball) for minicapsule in minicapsules: mcapsule = capsule.CopyTo() pointA = (minicapsule.ax, minicapsule.ay, minicapsule.az) pointB = (minicapsule.bx, minicapsule.by, minicapsule.bz) dir = geo2.Vec3Subtract(pointA, pointB) length = geo2.Vec3Length(dir) mcapsule.translation = geo2.Vec3Scale(geo2.Vec3Add(pointA, pointB), 0.5) radius = minicapsule.radius pitch = math.acos(dir[1] / length) yaw = math.atan2(dir[0], dir[2]) quat = geo2.QuaternionRotationSetYawPitchRoll(yaw, pitch, 0) mcapsule.rotation = quat for each in mcapsule.children: if each.name == 'Cylinder': height = length * each.scaling[1] rscaling = geo2.Vec3Scale((each.scaling[0], each.scaling[2]), radius) each.scaling = (rscaling[0], height, rscaling[1]) else: each.scaling = geo2.Vec3Scale(each.scaling, radius) each.translation = geo2.Vec3Scale(each.translation, length) t.children.append(mcapsule) for minibox in miniboxes: mbox = box.CopyTo() corner = (minibox.c0, minibox.c1, minibox.c2) xAxis = (minibox.x0, minibox.x1, minibox.x2) yAxis = (minibox.y0, minibox.y1, minibox.y2) zAxis = (minibox.z0, minibox.z1, minibox.z2) xyzAxis = geo2.Vec3Add(xAxis, geo2.Vec3Add(yAxis, zAxis)) center = geo2.Vec3Add(corner, geo2.Vec3Scale(xyzAxis, 0.5)) xNormalized = geo2.Vec3Normalize(xAxis) yNormalized = geo2.Vec3Normalize(yAxis) zNormalized = geo2.Vec3Normalize(zAxis) rotationMatrix = ((xNormalized[0], xNormalized[1], xNormalized[2], 0), (yNormalized[0], yNormalized[1], yNormalized[2], 0), (zNormalized[0], zNormalized[1], zNormalized[2], 0), (0, 0, 0, 1)) rot = geo2.QuaternionRotationMatrix(rotationMatrix) mbox.translation = center mbox.scaling = geo2.Vec3Transform( (1, 1, 1), geo2.MatrixScaling(geo2.Vec3Length(xAxis), geo2.Vec3Length(yAxis), geo2.Vec3Length(zAxis))) mbox.rotation = geo2.QuaternionNormalize(rot) t.children.append(mbox) t.name = name return t