Ejemplo n.º 1
0
        private void CreateGameObjects()
        {
            const float BLEND_HORIZON_C0     = 0.259f;
            const float BLEND_HORIZON_C1     = 0.342f;
            const float BLEND_VERT_C0        = 0.966f;
            const float BLEND_VERT_C1        = 0.94f;
            const float BLEND_LEVEL_GUIDE_C0 = 0.984f;
            const float BLEND_LEVEL_GUIDE_C1 = 0.996f;

            float viewportSizeX = Screen.width * 0.5f;
            float viewportSizeY = Screen.height * 0.5f;

            {
                GameObject o   = new GameObject("KerbalFlightIndicators-CustomCamera");
                Camera     cam = o.AddComponent <Camera>();
                cam.orthographic          = true;
                cam.clearFlags            = CameraClearFlags.Nothing;
                cam.orthographicSize      = viewportSizeY;
                cam.aspect                = viewportSizeX / viewportSizeY;
                cam.cullingMask           = (1 << KFI_LAYER);
                cam.depth                 = drawInFrontOfCockpit ? 10 : 1;
                cam.farClipPlane          = 2.0f;
                cam.nearClipPlane         = 0.5f;
                o.transform.localPosition = new Vector3(0.5f, 0.5f, 0.0f);
                o.transform.localRotation = Quaternion.identity;
                CameraScript cs = cameraScript = o.AddComponent <CameraScript>();
            }

            {
                Shader shd = Shader.Find("KSP/Alpha/Unlit Transparent");

                GameObject o = markerParentObject = new GameObject("KerbalFlightIndicators-MarkerParent");

                markerEnabling = cameraScript.markerEnabling = new bool[(int)Markers.COUNT];
                markerScripts  = cameraScript.markerScripts = new MarkerScript[(int)Markers.COUNT];
                for (int i = 0; i < (int)Markers.COUNT; ++i)
                {
                    Material mat = new Material(shd);
                    mat.mainTexture = marker_atlas;
                    Rect       uv = atlas_uv[i];
                    RectOffset px = atlas_px[i];
                    mat.mainTextureScale  = new Vector2(uv.width, uv.height);
                    mat.mainTextureOffset = new Vector2(uv.xMin, uv.yMin);

                    o      = GameObject.CreatePrimitive(PrimitiveType.Quad);
                    o.name = "KerbalFlightIndicators-" + Enum.GetName(typeof(Markers), i);

                    Component[] colliders = o.GetComponents <Collider>();
                    foreach (var c in colliders)
                    {
                        Component.DestroyImmediate(c); // DON'T WANT OUR GUI ELEMENTS COLLIDING WITH STUFF!
                    }

                    MeshRenderer mr = o.GetComponent <MeshRenderer>();
                    mr.material = mat;
                    o.layer     = KFI_LAYER;

                    float        zlevel = 1.0f;
                    MarkerScript ms     = markerScripts[i] = o.AddComponent <MarkerScript>();
                    if (i == (int)Markers.Heading || i == (int)Markers.LevelGuide || i == (int)Markers.Reverse)
                    {
                        ms.baseColor = attitudeColor;
                    }
                    else if (i == (int)Markers.Prograde || i == (int)Markers.Retrograde)
                    {
                        ms.baseColor = progradeColor;
                        zlevel       = 1.1f;
                    }
                    else
                    {
                        ms.baseColor = horizonColor;
                        zlevel       = 1.2f;
                    }
                    if (i == (int)Markers.Horizon)
                    {
                        ms.SetBlendConstants(BLEND_HORIZON_C0, BLEND_HORIZON_C1);
                    }
                    else if (i == (int)Markers.Vertical)
                    {
                        ms.SetBlendConstants(BLEND_VERT_C0, BLEND_VERT_C1);
                    }
                    else if (i == (int)Markers.LevelGuide)
                    {
                        ms.SetBlendConstants(BLEND_LEVEL_GUIDE_C0, BLEND_LEVEL_GUIDE_C1);
                    }

                    o.transform.parent        = markerParentObject.transform;
                    o.transform.localPosition = new Vector3(0.0f, 0.0f, zlevel);
                    o.transform.localRotation = Quaternion.identity;
                    o.transform.localScale    = new Vector3((px.right - px.left) * displayScaleFactor, (px.bottom - px.top) * displayScaleFactor, 1.0f);

                    markerEnabling[i] = true;
                }
            }
            OnGuiVisibilityChange();
        }