Esempio n. 1
0
        private void DrawObjects(GameObject go)
        {
            Profiler.BeginSample("DrawColliders");

            if (transforms)
            {
                Profiler.BeginSample("transforms");
                DrawTools.DrawTransform(go.transform, 0.3f);
                Profiler.EndSample();
            }

            if (colliders)
            {
                Profiler.BeginSample("colliders");
                Collider[] comp = go.GetComponents <Collider>();
                for (int i = 0; i < comp.Length; i++)
                {
                    Collider baseCol = comp[i];

                    if (baseCol is BoxCollider)
                    {
                        Profiler.BeginSample("BoxCollider");
                        BoxCollider box = baseCol as BoxCollider;
                        DrawTools.DrawLocalCube(box.transform, box.size, Color.yellow, box.center);
                        Profiler.EndSample();
                    }

                    if (baseCol is SphereCollider)
                    {
                        Profiler.BeginSample("SphereCollider");
                        SphereCollider sphere = baseCol as SphereCollider;
                        DrawTools.DrawSphere(sphere.transform.TransformPoint(sphere.center), Color.red, sphere.radius);
                        Profiler.EndSample();
                    }

                    if (baseCol is CapsuleCollider)
                    {
                        Profiler.BeginSample("CapsuleCollider");
                        CapsuleCollider caps   = baseCol as CapsuleCollider;
                        Vector3         dir    = new Vector3(caps.direction == 0 ? 1 : 0, caps.direction == 1 ? 1 : 0, caps.direction == 2 ? 1 : 0);
                        Vector3         top    = caps.transform.TransformPoint(caps.center + caps.height * 0.5f * dir);
                        Vector3         bottom = caps.transform.TransformPoint(caps.center - caps.height * 0.5f * dir);
                        DrawTools.DrawCapsule(top, bottom, Color.green, caps.radius);
                        Profiler.EndSample();
                    }

                    if (baseCol is MeshCollider)
                    {
                        Profiler.BeginSample("MeshCollider");
                        MeshCollider mesh = baseCol as MeshCollider;
                        DrawTools.DrawLocalMesh(mesh.transform, mesh.sharedMesh, XKCDColors.ElectricBlue);
                        Profiler.EndSample();
                    }
                }
                Profiler.EndSample();
            }

            if (bounds && mode != Mode.UI)
            {
                Profiler.BeginSample("bounds");

                //DrawTools.DrawBounds(go.GetRendererBounds(), XKCDColors.Pink);

                //Renderer[] renderers = go.GetComponents<Renderer>();
                //for (int i = 0; i < renderers.Length; i++)
                //{
                //    Bounds bound = renderers[i].bounds;
                //    DrawTools.DrawLocalCube(renderers[i].transform, bound.size, XKCDColors.Pink, bound.center);
                //}

                MeshFilter[] mesh = go.GetComponents <MeshFilter>();
                for (int i = 0; i < mesh.Length; i++)
                {
                    DrawTools.DrawLocalCube(mesh[i].transform, mesh[i].mesh.bounds.size, XKCDColors.Pink, mesh[i].mesh.bounds.center);
                }
                Profiler.EndSample();
            }

            if (bounds && mode == Mode.UI)
            {
                Profiler.BeginSample("bounds");

                RectTransform[] rt = go.GetComponents <RectTransform>();
                for (int i = 0; i < rt.Length; i++)
                {
                    // TODO : search for the actual Canvas ?
                    DrawTools.DrawRectTransform(rt[i], UIMasterController.Instance.appCanvas, XKCDColors.GreenApple);
                }
                Profiler.EndSample();
            }

            if (meshes)
            {
                Profiler.BeginSample("meshes");
                MeshFilter[] mesh = go.GetComponents <MeshFilter>();

                for (int i = 0; i < mesh.Length; i++)
                {
                    Profiler.BeginSample("LocalMesh");
                    DrawTools.DrawLocalMesh(mesh[i].transform, mesh[i].sharedMesh, XKCDColors.Orange);
                    Profiler.EndSample();
                }
                Profiler.EndSample();
            }

            int count = go.transform.childCount;

            for (int i = 0; i < count; i++)
            {
                GameObject child = go.transform.GetChild(i).gameObject;

                if (!child.GetComponent <Part>() && child.name != "main camera pivot")
                {
                    DrawObjects(child);
                }
            }
            Profiler.EndSample();
        }
        private void OnRenderObjectEvent()
        {
            if (Camera.current != Camera.main || MapView.MapIsEnabled)
            {
                return;
            }

            if (CameraManager.Instance.currentCameraMode == CameraManager.CameraMode.IVA &&
                vessel == FlightGlobals.ActiveVessel)
            {
                return;
            }

            if (vessel != FlightGlobals.ActiveVessel)
            {
                if (Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, vessel.transform.position) >
                    PhysicsGlobals.Instance.VesselRangesDefault.subOrbital.unload)
                {
                    MarkersEnabled = false;
                    return;
                }
            }

            Profiler.BeginSample("FlightMarkersRenderDraw");

            DrawTools.DrawSphere(vessel.CoM, XKCDColors.Yellow, 1.0f * SphereScale);

            DrawTools.DrawSphere(vessel.rootPart.transform.position, XKCDColors.Green, 0.25f);

            _centerOfThrust = FindCenterOfThrust(vessel.rootPart);
            if (_centerOfThrust.Direction != Vector3.zero)
            {
                DrawTools.DrawSphere(_centerOfThrust.Position, XKCDColors.Magenta, 0.95f * SphereScale);
                DrawTools.DrawArrow(_centerOfThrust.Position, _centerOfThrust.Direction.normalized * ArrowLength, XKCDColors.Magenta);
            }

            if (vessel.staticPressurekPa > 0f)
            {
                _centerOfLift = FindCenterOfLift(vessel.rootPart, vessel.srf_velocity, vessel.altitude,
                                                 vessel.staticPressurekPa, vessel.atmDensity);
                _bodyLift = FindBodyLift(vessel.rootPart);
                _drag     = FindDrag(vessel.rootPart);

                var activeLift = LiftFlag.None;
                if (_centerOfLift.Total > CenterOfLiftCutoff)
                {
                    activeLift |= LiftFlag.SurfaceLift;
                }
                if (_bodyLift.Total > BodyLiftCutoff)
                {
                    activeLift |= LiftFlag.BodyLift;
                }

                var drawCombined = _combineLift && (activeLift & CombineFlags) == CombineFlags;

                if (drawCombined)
                {
                    _positionAvg.Reset();
                    _directionAvg.Reset();

                    if ((activeLift & LiftFlag.SurfaceLift) == LiftFlag.SurfaceLift)
                    {
                        _positionAvg.Add(_centerOfLift.Position);
                        _directionAvg.Add(_centerOfLift.Direction.normalized);
                    }

                    if ((activeLift & LiftFlag.BodyLift) == LiftFlag.BodyLift)
                    {
                        _positionAvg.Add(_bodyLift.Position);
                        _directionAvg.Add(_bodyLift.Direction.normalized);
                    }

                    DrawTools.DrawSphere(_positionAvg.Get(), XKCDColors.Purple, 0.9f * SphereScale);
                    DrawTools.DrawArrow(_positionAvg.Get(), _directionAvg.Get().normalized *ArrowLength, XKCDColors.Purple);
                }
                else
                {
                    if ((activeLift & LiftFlag.SurfaceLift) == LiftFlag.SurfaceLift)
                    {
                        DrawTools.DrawSphere(_centerOfLift.Position, XKCDColors.Blue, 0.9f * SphereScale);
                        DrawTools.DrawArrow(_centerOfLift.Position, _centerOfLift.Direction.normalized * ArrowLength, XKCDColors.Blue);
                    }

                    if ((activeLift & LiftFlag.BodyLift) == LiftFlag.BodyLift)
                    {
                        DrawTools.DrawSphere(_bodyLift.Position, XKCDColors.Cyan, 0.85f * SphereScale);
                        DrawTools.DrawArrow(_bodyLift.Position, _bodyLift.Direction.normalized * ArrowLength, XKCDColors.Cyan);
                    }
                }

                if (_drag.Total > DragCutoff)
                {
                    DrawTools.DrawSphere(_drag.Position, XKCDColors.Red, 0.8f * SphereScale);
                    DrawTools.DrawArrow(_drag.Position, _drag.Direction.normalized * ArrowLength, XKCDColors.Red);
                }
            }

            Profiler.EndSample();
        }