private void updateCameraMode(byte controlMode) { Debug.Log("Camera update called"); oldCameraModeControl = receivedCameraControlMode; needToUpdateCamera = false; // Switch based on the CameraControlBits values switch (controlMode) { // See if the control bit is a camera mode setting bit - need to add validity checking, to make sure the modes can be entered case CameraControlBits.FlightMode: cameraManager.SetCameraFlight(); break; case CameraControlBits.IVAMode: cameraManager.SetCameraIVA(); break; case CameraControlBits.PlanetaryMode: PlanetariumCamera.fetch.Activate(); break; // If it is not a camera mode, it must be a mode for the (hopefully) current camera default: // Based on the current camera mode, change where the CameraControl value gets sent switch (cameraManager.currentCameraMode) { // Flight Camera case CameraManager.CameraMode.Flight: Debug.Log("Camera can have it's mode changed"); Debug.Log("Control mode: " + controlMode.ToString()); int currentFlightMode = FlightCamera.CamMode; int maxEnum = Enum.GetNames(typeof(FlightCamera.Modes)).Length - 1; Debug.Log("Max Enum Value: " + maxEnum.ToString()); // Switch based on the operation to perform on the flight camera switch (controlMode) { case CameraControlBits.Auto: FlightCamera.SetMode(FlightCamera.Modes.AUTO); printCameraMode("Auto"); break; case CameraControlBits.Free: FlightCamera.SetMode(FlightCamera.Modes.FREE); printCameraMode("Free"); break; case CameraControlBits.Orbital: FlightCamera.SetMode(FlightCamera.Modes.ORBITAL); printCameraMode("Orbital"); break; case CameraControlBits.Chase: FlightCamera.SetMode(FlightCamera.Modes.CHASE); printCameraMode("Chase"); break; case CameraControlBits.Locked: FlightCamera.SetMode(FlightCamera.Modes.LOCKED); printCameraMode("Locked"); break; case CameraControlBits.NextCameraModeState: printCameraMode("Next"); // If the current flight camera mode is at the end of the list, wrap around to the start if (currentFlightMode == maxEnum) { FlightCamera.SetMode((FlightCamera.Modes) 0); } else { FlightCamera.SetMode((FlightCamera.Modes)currentFlightMode + 1); } break; case CameraControlBits.PreviousCameraModeState: printCameraMode("Previous"); // If the current flight camera mode is at the start of the list, wrap around to the end if (currentFlightMode == 0) { FlightCamera.SetMode((FlightCamera.Modes)maxEnum); } else { // Hack to get around the issue of orbital mode not being available below a certain altitude around SOIs. FlightCamera.Modes autoMode = FlightCamera.GetAutoModeForVessel(FlightGlobals.ActiveVessel); // If the Flight Camera mode tries to go backwards to orbital mode, and orbital mode is not available, set the camera mode back two places. if (((FlightCamera.Modes)(FlightCamera.CamMode - 1)) == FlightCamera.Modes.ORBITAL && autoMode != FlightCamera.Modes.ORBITAL) { FlightCamera.SetMode((FlightCamera.Modes)(currentFlightMode - 2)); } // Otherwise just change the mode back by one else { FlightCamera.SetMode((FlightCamera.Modes)currentFlightMode - 1); } } break; default: printCameraMode("No flight camera state to match the control bits:("); break; } break; /* End Flight Camera Mode */ case CameraManager.CameraMode.IVA: break; default: Debug.Log(String.Format("Simpit control for the camera mode: {0} is unsupported", cameraManager.currentCameraMode.ToString())); break; } /* End of current camera mode switch */ break; } /* End of control mode switch */ }
void Update() { if (ADJUST_LOOK.GetKeyDown()) { adjustLook = true; } if (ADJUST_LOOK.GetKeyUp()) { adjustLook = false; } if (InternalCamera.Instance != null && InternalCamera.Instance.isActive) //checking if camera is IVA { if (!isIVA) { isIVA = true; } } else { if (isIVA) { isIVA = false; } } #region Chase(locked) mode if (FlightCamera.fetch.mode == FlightCamera.Modes.LOCKED && !MapView.MapIsEnabled && !FlightGlobals.ActiveVessel.isEVA) { Vector3 lookVector = Quaternion.Inverse(FlightGlobals.ActiveVessel.transform.rotation) * FlightGlobals.ActiveVessel.GetSrfVelocity(); //lookvector X is left/right velocity, Y is forward velocity, Z is up/down velocity Vector3 forwardVector = new Vector3(0f, 1f, 0f); Quaternion pitchAngleQ = Quaternion.FromToRotation(forwardVector, lookVector); //float lerpRate = Mathf.Clamp((float) FlightGlobals.ActiveVessel.srf_velocity.magnitude / 50f, 0f, 1f); float lerpRate; if (Time.time - timeCheck < 2) { lerpRate = 0.1f; } else { lerpRate = 1; } if (adjustLook) { snapHeading = FlightCamera.fetch.camHdg - (0 - pitchAngleQ.Roll()); snapPitch = FlightCamera.fetch.camPitch - (0 + pitchAngleQ.Pitch()); } if (defaultOn && defaultFiredMode != "CHASE") { enableChase = true; defaultFiredMode = "CHASE"; ScreenMessages.RemoveMessage(chaseOn); ScreenMessages.RemoveMessage(chaseOff); ScreenMessages.RemoveMessage(freeChaseOn); ScreenMessages.RemoveMessage(freeChaseOff); ScreenMessages.PostScreenMessage(chaseOn); timeCheck = Time.time; snapHeading = 0; snapPitch = defaultAngle * Mathf.Deg2Rad; //FlightCamera.fetch.SetFoV(setFov); } if (ENABLE_CHASE.GetKeyDown()) { if (enableChase) { ScreenMessages.RemoveMessage(chaseOn); ScreenMessages.RemoveMessage(chaseOff); ScreenMessages.RemoveMessage(freeChaseOn); ScreenMessages.RemoveMessage(freeChaseOff); ScreenMessages.PostScreenMessage(chaseOff); FlightCamera.fetch.SetFoV(FlightCamera.fetch.fovDefault); } if (!enableChase) { ScreenMessages.RemoveMessage(chaseOn); ScreenMessages.RemoveMessage(chaseOff); ScreenMessages.RemoveMessage(freeChaseOn); ScreenMessages.RemoveMessage(freeChaseOff); ScreenMessages.PostScreenMessage(chaseOn); timeCheck = Time.time; snapHeading = 0; snapPitch = defaultAngle * Mathf.Deg2Rad; //FlightCamera.fetch.SetFoV(setFov); } enableChase = !enableChase; } if (enableChase && FlightGlobals.ActiveVessel != null && !adjustLook) //runs continuously while in chase cam on an active vessel when not adjusting look angle { if (FlightGlobals.ActiveVessel.srf_velocity.magnitude > 0.5) { targetPitch = (0 + pitchAngleQ.Pitch() + snapPitch); FlightCamera.fetch.camPitch = Mathf.Lerp(FlightCamera.fetch.camPitch, targetPitch, lerpRate); targetHeading = (0 - pitchAngleQ.Roll() + snapHeading); FlightCamera.fetch.camHdg = Mathf.Lerp(FlightCamera.fetch.camHdg, targetHeading, lerpRate); } } } if (FlightCamera.fetch.mode != FlightCamera.Modes.LOCKED && enableChase) //runs once on switching out of chase cam { //setCam = false; enableChase = false; } #endregion #region Free chase mode if (FlightCamera.fetch.mode == FlightCamera.Modes.FREE && !MapView.MapIsEnabled && !FlightGlobals.ActiveVessel.isEVA) { Vessel v = FlightGlobals.ActiveVessel; Quaternion vesselRot = getSrfRotation(v); float headingRads = vesselRot.eulerAngles.y * Mathf.Deg2Rad; float pitchRads = ((vesselRot.eulerAngles.x > 180f) ? (360.0f - vesselRot.eulerAngles.x) : -vesselRot.eulerAngles.x) * Mathf.Deg2Rad; //cred to r4m0n float rollDegrees = ((vesselRot.eulerAngles.z > 180f) ? (vesselRot.eulerAngles.z - 360.0f) : vesselRot.eulerAngles.z); Vector3 lookVector = Quaternion.Inverse(FlightGlobals.ActiveVessel.transform.rotation) * FlightGlobals.ActiveVessel.GetSrfVelocity(); //lookvector X is left/right velocity, Y is forward velocity, Z is up/down velocity Vector3 forwardVector = new Vector3(0f, 1f, 0f); Quaternion rollAdjust = Quaternion.AngleAxis(rollDegrees, forwardVector); lookVector = rollAdjust * lookVector; Quaternion viewAngleQ = Quaternion.FromToRotation(forwardVector, lookVector); if (defaultOn && defaultFiredMode != "FREE") { enableFreeChase = true; defaultFiredMode = "FREE"; ScreenMessages.RemoveMessage(chaseOn); ScreenMessages.RemoveMessage(chaseOff); ScreenMessages.RemoveMessage(freeChaseOn); ScreenMessages.RemoveMessage(freeChaseOff); ScreenMessages.PostScreenMessage(freeChaseOn); snapHeading = 0; snapPitch = defaultAngle * Mathf.Deg2Rad; //FlightCamera.fetch.SetFoV(setFov); timeCheck = Time.time; } if (ENABLE_CHASE.GetKeyDown()) { if (enableFreeChase) { ScreenMessages.RemoveMessage(chaseOn); ScreenMessages.RemoveMessage(chaseOff); ScreenMessages.RemoveMessage(freeChaseOn); ScreenMessages.RemoveMessage(freeChaseOff); ScreenMessages.PostScreenMessage(freeChaseOff); FlightCamera.fetch.SetFoV(FlightCamera.fetch.fovDefault); } if (!enableFreeChase) { ScreenMessages.RemoveMessage(chaseOn); ScreenMessages.RemoveMessage(chaseOff); ScreenMessages.RemoveMessage(freeChaseOn); ScreenMessages.RemoveMessage(freeChaseOff); ScreenMessages.PostScreenMessage(freeChaseOn); snapHeading = 0; snapPitch = defaultAngle * Mathf.Deg2Rad; //FlightCamera.fetch.SetFoV(setFov); timeCheck = Time.time; } enableFreeChase = !enableFreeChase; } if (adjustLook) { if (!autoSnap) { if (!vtolMode) { snapPitch = FlightCamera.fetch.camPitch - (0 - pitchRads + (1f * viewAngleQ.Pitch())); //adjusted snapHeading = FlightCamera.fetch.camHdg - (headingRads - (1f * viewAngleQ.Roll())); //adjusted } else { snapPitch = FlightCamera.fetch.camPitch - (0 + Mathf.Clamp(viewAngleQ.Pitch() / 10, 0, 90f)); snapHeading = FlightCamera.fetch.camHdg - (headingRads - (viewAngleQ.Roll() / 10)); } if (Mathf.Abs(snapHeading) < 5 * Mathf.Deg2Rad) //snap heading straight if close enough { snapHeading = 0; } } else { snapHeading = 0; snapPitch = defaultAngle * Mathf.Deg2Rad; timeCheck = Time.time; } } float lerpRate; if (Time.time - timeCheck < 1 || v.srfSpeed < 90) { lerpRate = 0.1f; } else { lerpRate = 1; } if (enableFreeChase && FlightGlobals.ActiveVessel != null && !adjustLook) { if (FlightGlobals.ActiveVessel.srf_velocity.magnitude > 0.5) { if (lookVector.y < 35 && FlightGlobals.ActiveVessel.srfSpeed < 50)//vtol mode when fwd velocity is less than 50m/s { if (!vtolMode) { vtolMode = true; } lerpRate = 0.1f; FlightCamera.fetch.camPitch = Mathf.LerpAngle(FlightCamera.fetch.camPitch, (0 + Mathf.Clamp(viewAngleQ.Pitch() / 10, 0, 90f) + snapPitch), lerpRate); FlightCamera.fetch.camHdg = (Mathf.LerpAngle(FlightCamera.fetch.camHdg * Mathf.Rad2Deg, ((headingRads - (viewAngleQ.Roll() / 10) + snapHeading) * Mathf.Rad2Deg), lerpRate)) * Mathf.Deg2Rad; } else { if (vtolMode) { vtolMode = false; } FlightCamera.fetch.camPitch = Mathf.LerpAngle(FlightCamera.fetch.camPitch, 0 - pitchRads + viewAngleQ.Pitch() + snapPitch, lerpRate); //doesn't really need to be LerpAngle unless converted to degrees. FlightCamera.fetch.camHdg = (Mathf.LerpAngle(FlightCamera.fetch.camHdg * Mathf.Rad2Deg, (headingRads - viewAngleQ.Roll() + snapHeading) * Mathf.Rad2Deg, lerpRate)) * Mathf.Deg2Rad; } } } } if (FlightCamera.fetch.mode != FlightCamera.Modes.FREE && enableFreeChase) { enableFreeChase = false; } #endregion if (FlightCamera.fetch.mode == FlightCamera.Modes.AUTO) { if (disableAuto) { FlightCamera.fetch.setMode(FlightCamera.GetAutoModeForVessel(FlightGlobals.ActiveVessel)); if (defaultOn && FlightCamera.fetch.mode == FlightCamera.Modes.FREE) { enableFreeChase = true; } snapHeading = 0; snapPitch = defaultAngle * Mathf.Deg2Rad; } if (defaultFiredMode != "AUTO") { ScreenMessages.RemoveMessage(chaseOn); ScreenMessages.RemoveMessage(chaseOff); ScreenMessages.RemoveMessage(freeChaseOn); ScreenMessages.RemoveMessage(freeChaseOff); } } #region IVA /* * if(isIVA && !MapView.MapIsEnabled) * { * if(enableIVASnap) * { * adjustLookIVA = InternalCamera.Instance.mouseLocked; * if(adjustLookIVA) * { * if(SET_IVA_SNAP.GetKeyDown()) * { * snapIVARotation = InternalCamera.Instance.camera.transform.localRotation; * } * if(ADJUST_LOOK.GetKeyUp()) * { * InternalCamera.Instance.UnlockMouse(); * } * } * else * { * InternalCamera.Instance.camera.transform.localRotation = snapIVARotation; * } * } * } */ #endregion }
void StationaryCamera() { Debug.Log("flightCamera position init: " + flightCamera.transform.position); if (FlightGlobals.ActiveVessel != null) { hasDied = false; vessel = FlightGlobals.ActiveVessel; cameraUp = -FlightGlobals.getGeeForceAtPosition(vessel.GetWorldPos3D()).normalized; if (FlightCamera.fetch.mode == FlightCamera.Modes.ORBITAL || (FlightCamera.fetch.mode == FlightCamera.Modes.AUTO && FlightCamera.GetAutoModeForVessel(vessel) == FlightCamera.Modes.ORBITAL)) { cameraUp = Vector3.up; } flightCamera.transform.parent = cameraParent.transform; flightCamera.setTarget(null); cameraParent.transform.position = vessel.transform.position + vessel.rigidbody.velocity * Time.fixedDeltaTime; manualPosition = Vector3.zero; hasTarget = (camTarget != null) ? true : false; Vector3 rightAxis; if (referenceMode == ReferenceModes.Surface && vessel.horizontalSrfSpeed > 2) { rightAxis = Quaternion.AngleAxis(-90, vessel.srf_velocity) * cameraUp; } else { rightAxis = Quaternion.AngleAxis(-90, vessel.obt_velocity) * cameraUp; } if (flightCamera.autoMode == FlightCamera.Modes.FREE) { Vector3 cameraUpRef = cameraUp; Vector3.OrthoNormalize(ref rightAxis, ref cameraUpRef); } if (autoFlybyPosition) { setPresetOffset = false; Vector3 velocity = vessel.srf_velocity; if (referenceMode == ReferenceModes.Orbit) { velocity = vessel.obt_velocity; } Vector3 clampedVelocity = Mathf.Clamp((float)vessel.srfSpeed, 0, maxRelV) * velocity.normalized; float clampedSpeed = clampedVelocity.magnitude; float sideDistance = Mathf.Clamp(20 + (clampedSpeed / 10), 20, 150); float distanceAhead = Mathf.Clamp(4 * clampedSpeed, 30, 3500); flightCamera.transform.rotation = Quaternion.LookRotation(vessel.transform.position - flightCamera.transform.position, cameraUp); if (referenceMode == ReferenceModes.Surface && vessel.srfSpeed > 4) { flightCamera.transform.position = vessel.transform.position + (distanceAhead * vessel.srf_velocity.normalized); } else if (referenceMode == ReferenceModes.Orbit && vessel.obt_speed > 4) { flightCamera.transform.position = vessel.transform.position + (distanceAhead * vessel.obt_velocity.normalized); } else { flightCamera.transform.position = vessel.transform.position + (distanceAhead * vessel.vesselTransform.up); } if (flightCamera.mode == FlightCamera.Modes.FREE || FlightCamera.GetAutoModeForVessel(vessel) == FlightCamera.Modes.FREE) { flightCamera.transform.position += (sideDistance * rightAxis) + (15 * cameraUp); } else if (flightCamera.mode == FlightCamera.Modes.ORBITAL || FlightCamera.GetAutoModeForVessel(vessel) == FlightCamera.Modes.ORBITAL) { flightCamera.transform.position += (sideDistance * FlightGlobals.getUpAxis()) + (15 * Vector3.up); } } else if (manualOffset) { setPresetOffset = false; float sideDistance = manualOffsetRight; float distanceAhead = manualOffsetForward; flightCamera.transform.rotation = Quaternion.LookRotation(vessel.transform.position - flightCamera.transform.position, cameraUp); if (referenceMode == ReferenceModes.Surface && vessel.srfSpeed > 4) { flightCamera.transform.position = vessel.transform.position + (distanceAhead * vessel.srf_velocity.normalized); } else if (referenceMode == ReferenceModes.Orbit && vessel.obt_speed > 4) { flightCamera.transform.position = vessel.transform.position + (distanceAhead * vessel.obt_velocity.normalized); } else { flightCamera.transform.position = vessel.transform.position + (distanceAhead * vessel.vesselTransform.up); } if (flightCamera.mode == FlightCamera.Modes.FREE || FlightCamera.GetAutoModeForVessel(vessel) == FlightCamera.Modes.FREE) { flightCamera.transform.position += (sideDistance * rightAxis) + (manualOffsetUp * cameraUp); } else if (flightCamera.mode == FlightCamera.Modes.ORBITAL || FlightCamera.GetAutoModeForVessel(vessel) == FlightCamera.Modes.ORBITAL) { flightCamera.transform.position += (sideDistance * FlightGlobals.getUpAxis()) + (manualOffsetUp * Vector3.up); } } else if (setPresetOffset) { flightCamera.transform.position = presetOffset; //setPresetOffset = false; } initialVelocity = vessel.srf_velocity; initialOrbit = new Orbit(); initialOrbit.UpdateFromStateVectors(vessel.orbit.pos, vessel.orbit.vel, FlightGlobals.currentMainBody, Planetarium.GetUniversalTime()); initialUT = Planetarium.GetUniversalTime(); isStationaryCamera = true; isDefault = false; SetDoppler(); AddAtmoAudioControllers(); } else { Debug.Log("CameraTools: Stationary Camera failed. Active Vessel is null."); } resetPositionFix = flightCamera.transform.position; Debug.Log("flightCamera position post init: " + flightCamera.transform.position); }