예제 #1
0
        public bool RenderCamera(RenderTexture screen, float cameraAspect)
        {
            // Just in case.
            if (!HighLogic.LoadedSceneIsFlight)
            {
                return(false);
            }

            if (cameras.Count < 1)
            {
                return(false);
            }

            var activeCamera = cameras[currentCamera];

            if (string.IsNullOrEmpty(activeCamera.cameraTransform))
            {
                return(false);
            }

            if (cameraObject == null)
            {
                cameraObject = new FlyingCamera(part, screen, cameraAspect);
                cameraObject.PointCamera(activeCamera.cameraTransform, activeCamera.currentFoV);
            }

            cameraObject.FOV = activeCamera.currentFoV;

            // Negate pitch - the camera object treats a negative pitch as "up"
            if (cameraObject.Render(activeCamera.currentYaw, -activeCamera.currentPitch))
            {
                ITargetable target = FlightGlobals.fetch.VesselTarget;

                bool drawSomething = ((gizmoTexture != null && target != null && showTargetIcon) || homeCrosshairMaterial.color.a > 0);

                if (drawSomething)
                {
                    GL.PushMatrix();
                    GL.LoadPixelMatrix(0, screen.width, screen.height, 0);
                }

                if (gizmoTexture != null && target != null && showTargetIcon)
                {
                    // Figure out which direction the target is.
                    Vector3 targetDisplacement = target.GetTransform().position - cameraObject.GetTransform().position;
                    targetDisplacement.Normalize();

                    // Transform it using the active camera's rotation.
                    var targetDisp = GetNormalizedScreenPosition(activeCamera, targetDisplacement, cameraAspect);

                    var iconCenter = new Vector2(screen.width * targetDisp.x, screen.height * targetDisp.y);

                    // Apply some clamping values to force the icon to stay on screen
                    iconCenter.x = Math.Max(iconPixelSize * 0.5f, iconCenter.x);
                    iconCenter.x = Math.Min(screen.width - iconPixelSize * 0.5f, iconCenter.x);
                    iconCenter.y = Math.Max(iconPixelSize * 0.5f, iconCenter.y);
                    iconCenter.y = Math.Min(screen.height - iconPixelSize * 0.5f, iconCenter.y);

                    var position = new Rect(iconCenter.x - iconPixelSize * 0.5f, iconCenter.y - iconPixelSize * 0.5f, iconPixelSize, iconPixelSize);

                    Graphics.DrawTexture(position, gizmoTexture, GizmoIcons.GetIconLocation(GizmoIcons.IconType.TARGETPLUS), 0, 0, 0, 0, iconMaterial);
                }

                if (homeCrosshairMaterial.color.a > 0)
                {
                    // Mihara: Reference point cameras are different enough to warrant it.
                    var cameraForward   = cameraObject.GetTransformForward();
                    var crossHairCenter = GetNormalizedScreenPosition(activeCamera, cameraForward, cameraAspect);
                    crossHairCenter.x *= screen.width;
                    crossHairCenter.y *= screen.height;
                    crossHairCenter.x  = Math.Max(iconPixelSize * 0.5f, crossHairCenter.x);
                    crossHairCenter.x  = Math.Min(screen.width - iconPixelSize * 0.5f, crossHairCenter.x);
                    crossHairCenter.y  = Math.Max(iconPixelSize * 0.5f, crossHairCenter.y);
                    crossHairCenter.y  = Math.Min(screen.height - iconPixelSize * 0.5f, crossHairCenter.y);

                    float zoomAdjustedIconSize = iconPixelSize * Mathf.Tan(Mathf.Deg2Rad * activeCamera.fovLimits.y * 0.5f) / Mathf.Tan(Mathf.Deg2Rad * activeCamera.currentFoV * 0.5f);

                    homeCrosshairMaterial.SetPass(0);
                    GL.Begin(GL.LINES);
                    GL.Vertex3(crossHairCenter.x - zoomAdjustedIconSize * 0.5f, crossHairCenter.y, 0.0f);
                    GL.Vertex3(crossHairCenter.x + zoomAdjustedIconSize * 0.5f, crossHairCenter.y, 0.0f);
                    GL.Vertex3(crossHairCenter.x, crossHairCenter.y - zoomAdjustedIconSize * 0.5f, 0.0f);
                    GL.Vertex3(crossHairCenter.x, crossHairCenter.y + zoomAdjustedIconSize * 0.5f, 0.0f);
                    GL.End();
                }

                if (drawSomething)
                {
                    GL.PopMatrix();
                }

                return(true);
            }
            return(false);
        }
예제 #2
0
        public bool RenderCamera(RenderTexture screen, float cameraAspect)
        {
            // Just in case.
            if (HighLogic.LoadedSceneIsEditor)
            {
                return(false);
            }

            if (cameras.Count < 1)
            {
                return(false);
            }

            var activeCamera = cameras[currentCamera];

            if (string.IsNullOrEmpty(activeCamera.cameraTransform))
            {
                return(false);
            }

            if (cameraObject == null)
            {
                cameraObject = new FlyingCamera(part, cameraAspect);
                cameraObject.PointCamera(activeCamera.cameraTransform, activeCamera.currentFoV);
            }

            cameraObject.FOV = activeCamera.currentFoV;

            if (rentexWidth == 0)
            {
                rentexWidth  = screen.width;
                rentexHeight = screen.height;

                // Note to self: when rentex dims != screen dims, the FOV seems to be wrong (like FOV is smaller).
            }
            if (renderTex == null)
            {
                renderTex = new RenderTexture(rentexWidth, rentexHeight, screen.depth);
                renderTex.Create();
            }

            // Negate pitch - the camera object treats a negative pitch as "up"
            if (cameraObject.Render(renderTex, activeCamera.currentYaw, -activeCamera.currentPitch))
            {
                if (cameraEffectMaterial != null)
                {
                    cameraEffectMaterial.SetVector("_ImageDims", new Vector4((float)renderTex.width, (float)renderTex.height, 1.0f / (float)renderTex.width, 1.0f / (float)renderTex.height));

                    for (int i = 0; i < ceVariables.Count; ++i)
                    {
                        float value = ceVariables[i].value.AsFloat();
                        cameraEffectMaterial.SetFloat(ceVariables[i].variable, value);
                    }

                    Graphics.Blit(renderTex, screen, cameraEffectMaterial);
                }
                else
                {
                    Graphics.Blit(renderTex, screen);
                }
                renderTex.DiscardContents();

                ITargetable target = FlightGlobals.fetch.VesselTarget;

                bool drawSomething = ((gizmoTexture != null && target != null && showTargetIcon) || homeCrosshairMaterial.color.a > 0);

                if (drawSomething)
                {
                    GL.PushMatrix();
                    GL.LoadPixelMatrix(0, screen.width, screen.height, 0);
                }

                if (gizmoTexture != null && target != null && showTargetIcon)
                {
                    // Figure out which direction the target is.
                    Vector3 targetDisplacement = target.GetTransform().position - cameraObject.GetTransform().position;
                    targetDisplacement.Normalize();

                    // Transform it using the active camera's rotation.
                    var targetDisp = GetNormalizedScreenPosition(activeCamera, targetDisplacement, cameraAspect);

                    var iconCenter = new Vector2(screen.width * targetDisp.x, screen.height * targetDisp.y);

                    // Apply some clamping values to force the icon to stay on screen
                    iconCenter.x = Math.Max(iconPixelSize * 0.5f, iconCenter.x);
                    iconCenter.x = Math.Min(screen.width - iconPixelSize * 0.5f, iconCenter.x);
                    iconCenter.y = Math.Max(iconPixelSize * 0.5f, iconCenter.y);
                    iconCenter.y = Math.Min(screen.height - iconPixelSize * 0.5f, iconCenter.y);

                    var position = new Rect(iconCenter.x - iconPixelSize * 0.5f, iconCenter.y - iconPixelSize * 0.5f, iconPixelSize, iconPixelSize);

                    Graphics.DrawTexture(position, gizmoTexture, GizmoIcons.GetIconLocation(GizmoIcons.IconType.TARGETPLUS), 0, 0, 0, 0, iconMaterial);
                }

                if (homeCrosshairMaterial.color.a > 0)
                {
                    // Mihara: Reference point cameras are different enough to warrant it.
                    var cameraForward   = cameraObject.GetTransformForward();
                    var crossHairCenter = GetNormalizedScreenPosition(activeCamera, cameraForward, cameraAspect);
                    crossHairCenter.x *= screen.width;
                    crossHairCenter.y *= screen.height;
                    crossHairCenter.x  = Math.Max(iconPixelSize * 0.5f, crossHairCenter.x);
                    crossHairCenter.x  = Math.Min(screen.width - iconPixelSize * 0.5f, crossHairCenter.x);
                    crossHairCenter.y  = Math.Max(iconPixelSize * 0.5f, crossHairCenter.y);
                    crossHairCenter.y  = Math.Min(screen.height - iconPixelSize * 0.5f, crossHairCenter.y);

                    float zoomAdjustedIconSize = iconPixelSize * Mathf.Tan(Mathf.Deg2Rad * activeCamera.fovLimits.y * 0.5f) / Mathf.Tan(Mathf.Deg2Rad * activeCamera.currentFoV * 0.5f);

                    homeCrosshairMaterial.SetPass(0);
                    GL.Begin(GL.LINES);
                    GL.Vertex3(crossHairCenter.x - zoomAdjustedIconSize * 0.5f, crossHairCenter.y, 0.0f);
                    GL.Vertex3(crossHairCenter.x + zoomAdjustedIconSize * 0.5f, crossHairCenter.y, 0.0f);
                    GL.Vertex3(crossHairCenter.x, crossHairCenter.y - zoomAdjustedIconSize * 0.5f, 0.0f);
                    GL.Vertex3(crossHairCenter.x, crossHairCenter.y + zoomAdjustedIconSize * 0.5f, 0.0f);
                    GL.End();
                }

                if (drawSomething)
                {
                    GL.PopMatrix();
                }

                return(true);
            }
            else if (skipMissingCameras)
            {
                // This will handle cameras getting ejected while in use.
                SelectNextCamera();
            }

            return(false);
        }