Exemple #1
0
        public bool Render(RenderTexture screen, float yawOffset, float pitchOffset)
        {
            if (isReferenceCamera && ourVessel.GetReferenceTransformPart() != referencePart)
            {
                CleanupCameraObjects();
                PointToReferenceCamera();
            }

            if (isReferenceClawCamera && clawModule.state != "Ready")
            {
                return(false);
            }

            if (!enabled)
            {
                return(false);
            }

            if (cameraPart == null || cameraPart.vessel != FlightGlobals.ActiveVessel || cameraTransform == null || cameraTransform.transform == null)
            {
                CleanupCameraObjects();
                return(false);
            }

            // Randomized camera flicker.
            if (flickerChance > 0 && flickerCounter == 0)
            {
                if (flickerChance > UnityEngine.Random.Range(0f, 1000f))
                {
                    flickerCounter = UnityEngine.Random.Range(1, flickerMaxTime);
                }
            }
            if (flickerCounter > 0)
            {
                flickerCounter--;
                return(false);
            }

            Quaternion rotation = cameraTransform.transform.rotation;

            if (isReferenceTransformCamera)
            {
                // Reference transforms of docking ports have the wrong orientation, so need an extra rotation applied before that.
                rotation *= referencePointRotation;
            }

            Quaternion offset = Quaternion.Euler(new Vector3(pitchOffset, yawOffset, 0.0f));

            rotation = rotation * offset;


            // This is a hack - FXCamera isn't always available, so I need to add and remove it in flight.
            // I don't know if there's a callback I can use to find when it's added, so brute force it for now.
            bool fxCameraExists = JUtil.DoesCameraExist(knownCameraNames[fxCameraIndex]);

            if (cameraObject[fxCameraIndex] == null)
            {
                if (fxCameraExists)
                {
                    CameraSetup(fxCameraIndex, knownCameraNames[fxCameraIndex]);
                }
            }
            else if (!fxCameraExists)
            {
                try
                {
                    UnityEngine.Object.Destroy(cameraObject[fxCameraIndex]);
                    // Analysis disable once EmptyGeneralCatchClause
                }
                catch
                {
                    // Yes, that's really what it's supposed to be doing.
                }
                finally
                {
                    cameraObject[fxCameraIndex] = null;
                }
            }

            for (int i = 0; i < cameraObject.Length; i++)
            {
                if (skipFarCamera && i == farCameraIndex)
                {
                    continue;
                }

                if (cameraObject[i] != null)
                {
                    // ScaledSpace camera and its derived cameras from Visual Enhancements mod are special - they don't move.
                    if (i >= 3)
                    {
                        cameraObject[i].transform.position = cameraTransform.transform.position;
                    }
                    cameraObject[i].targetTexture      = screen;
                    cameraObject[i].transform.rotation = rotation;
                    cameraObject[i].fieldOfView        = FOV;
                    cameraObject[i].Render();
                }
            }
            return(true);
        }