예제 #1
0
 public static void GLDrawHull(ConvexHull3D h, Transform T, Color c, MaterialWrapper mat = null, Vector3 offset = default(Vector3))
 {
     foreach (var f in h.Faces)
     {
         var verts = f.ToArray();
         for (int i = 0; i < verts.Length; i++)
         {
             verts[i] = T.TransformDirection(verts[i] - offset) + T.position;
         }
         GLTriangles(verts, c, mat);
     }
 }
예제 #2
0
        public static void DrawHull(ConvexHull3D h, Transform T, Color c = default(Color), Material mat = null)
        {
            var verts = new List <Vector3>(h.Faces.Count * 3);
            var tris  = new List <int>(h.Faces.Count * 3);

            foreach (Face f in h.Faces)
            {
                verts.AddRange(f);
                tris.AddRange(new [] { 0 + tris.Count, 1 + tris.Count, 2 + tris.Count });
            }
            DrawMesh(verts.ToArray(), tris, T, c, mat ?? diffuse_material.New);
        }
 public static void GLDrawHull(ConvexHull3D h, Transform T, Color c, Vector3 offset = default(Vector3), bool filled = true)
 {
     foreach (var f in h.Faces)
     {
         var verts = f.ToArray();
         for (int i = 0; i < verts.Length; i++)
         {
             verts[i] = T.TransformDirection(verts[i] - offset) + T.position;
         }
         GLTriangles(verts, c, filled);
     }
 }
예제 #4
0
 //metric form vertices
 public Metric(Vector3[] verts, float m = 0f, int crew_capacity = 0, bool compute_hull = false) : this()
 {
     if (compute_hull)
     {
         hull = new ConvexHull3D(verts);
     }
     bounds        = initBounds(verts);
     bounds_volume = boundsVolume(bounds);
     bounds_area   = boundsArea(bounds);
     mass          = m;
     CrewCapacity  = crew_capacity;
 }
예제 #5
0
        public static void GLDrawHull2(ConvexHull3D h, Transform T, Color c, MaterialWrapper mat = null, Vector3 offset = default(Vector3))
        {
            float far;
            var   camera = GLBeginWorld(out far, mat);

            GL.Begin(GL.TRIANGLES);
            GL.Color(c);
            for (int i = 0, hFacesCount = h.Faces.Count; i < hFacesCount; i++)
            {
                var f     = h.Faces[i];
                var verts = new Vector3[3];
                for (int j = 0; j < 3; j++)
                {
                    verts[j] = T.TransformDirection(f[j] - offset) + T.position;
                }
                GL.Vertex(verts[0]);
                GL.Vertex(verts[1]);
                GL.Vertex(verts[2]);
            }
            GL.End();
            GL.PopMatrix();
            camera.farClipPlane = far;
        }
예제 #6
0
        public void Load(ConfigNode node)
        {
            if (!node.HasValue("bounds_center") ||
                !node.HasValue("bounds_size") ||
                !node.HasValue("crew_capacity") ||
                !node.HasValue("mass") ||
                !node.HasValue("cost"))
            {
                throw new KeyNotFoundException("Metric.Load: not all needed values are present in the config node.");
            }
            Vector3 _center = ConfigNode.ParseVector3(node.GetValue("bounds_center"));
            Vector3 _size   = ConfigNode.ParseVector3(node.GetValue("bounds_size"));

            bounds        = new Bounds(_center, _size);
            bounds_volume = boundsVolume(bounds);
            bounds_area   = boundsArea(bounds);
            CrewCapacity  = int.Parse(node.GetValue("crew_capacity"));
            mass          = float.Parse(node.GetValue("mass"));
            cost          = float.Parse(node.GetValue("cost"));
            if (node.HasNode("HULL"))
            {
                hull = ConvexHull3D.Load(node.GetNode("HULL"));
            }
        }
예제 #7
0
        Bounds partsBounds(IList <Part> parts, Transform refT, bool compute_hull, bool exclude_disabled = true)
        {
            //reset metric
            mass         = 0;
            cost         = 0;
            CrewCapacity = 0;
            Bounds b = default(Bounds);

            if (parts == null || parts.Count == 0)
            {
                Utils.Log("Metric.partsBounds: WARNING! No parts were provided.");
                return(b);
            }
            //calculate bounds and convex hull
            float          b_size      = 0;
            List <Vector3> hull_points = compute_hull ? new List <Vector3>() : null;

            for (int i = 0, partsCount = parts.Count; i < partsCount; i++)
            {
                Part p = parts[i];
                if (p == null)
                {
                    continue;
                }
                //EditorLogic.SortedShipList returns List<Part>{null} when all parts are deleted
                //check for weels; if it's a wheel, get all meshes under the wheel collider
                var wheel           = p.Modules.GetModule <ModuleWheelBase>();
                var wheel_transform = wheel != null &&
                                      wheel.Wheel != null &&
                                      wheel.Wheel.wheelCollider != null
                                          ? wheel.Wheel.wheelCollider.wheelTransform : null;
                //check for asteroids
                var is_asteroid = p.Modules.GetModule <ModuleAsteroid>() != null;
                //check for bad parts
                var pname    = p.partInfo != null ? p.partInfo.name : p.name;
                var bad_part = Utils.NameMatches(pname, AT_UtilsGlobals.Instance.BadPartsList);
                var part_rot = p.partTransform.rotation;
                if (bad_part)
                {
                    p.partTransform.rotation = Quaternion.identity;
                }
                foreach (var mesh in p.AllModelMeshes())
                {
                    //skip disabled objects
                    if (!mesh.Valid || exclude_disabled &&
                        (!mesh.r.enabled || !mesh.t.gameObject.activeInHierarchy))
                    {
                        continue;
                    }
                    //skip meshes from the blacklist
                    if (Utils.NameMatches(mesh.t.name, AT_UtilsGlobals.Instance.MeshesToSkipList))
                    {
                        continue;
                    }
                    Vector3[] verts;
                    if (bad_part)
                    {
                        verts = Utils.BoundCorners(mesh.r.bounds);
                        for (int j = 0, len = verts.Length; j < len; j++)
                        {
                            var v = p.partTransform.position + part_rot * (verts[j] - p.partTransform.position);
                            if (refT != null)
                            {
                                v = refT.InverseTransformPoint(v);
                            }
                            verts[j] = v;
                        }
                    }
                    else
                    {
                        if (is_asteroid || wheel_transform != null && mesh.t.IsChildOf(wheel_transform) ||
                            (compute_hull &&
                             Vector3.Scale(mesh.m.bounds.size, mesh.t.lossyScale).sqrMagnitude > b_size / 10))
                        {
                            verts = mesh.m.uniqueVertices();
                        }
                        else
                        {
                            verts = Utils.BoundCorners(mesh.m.bounds);
                        }
                        verts = refT != null?local2local(mesh.t, refT, verts) : local2world(mesh.t, verts);
                    }
                    updateBounds(ref b, verts);
                    if (compute_hull)
                    {
                        hull_points.AddRange(verts);
                        b_size = b.size.sqrMagnitude;
                    }
                }
                CrewCapacity += p.CrewCapacity;
                mass         += p.TotalMass();
                cost         += p.TotalCost();
                if (bad_part)
                {
                    p.partTransform.rotation = part_rot;
                }
            }
            if (compute_hull && hull_points.Count >= 4)
            {
                hull       = new ConvexHull3D(hull_points);
                _hull_mesh = null;
            }
            return(b);
        }
예제 #8
0
        Bounds partsBounds(List <Part> parts, Transform vT, bool compute_hull, bool exclude_disabled = true)
        {
            //reset metric
            mass         = 0;
            cost         = 0;
            CrewCapacity = 0;
            Bounds b = default(Bounds);

            if (parts == null || parts.Count == 0)
            {
                Utils.Log("Metric.partsBounds: WARNING! No parts were provided."); return(b);
            }
            //calculate bounds and convex hull
            float          b_size      = 0;
            List <Vector3> hull_points = compute_hull ? new List <Vector3>() : null;

            foreach (Part p in parts)
            {
                if (p == null)
                {
                    continue;                           //EditorLogic.SortedShipList returns List<Part>{null} when all parts are deleted
                }
                //if it's a wheel, get all meshes under the wheel collider
                var wheel        = p.Modules.GetModule <ModuleWheelBase>();
                var wheel_meshes = wheel != null && wheel.Wheel != null && wheel.Wheel.wheelCollider != null && wheel.Wheel.wheelCollider.wheelTransform != null?
                                   new HashSet <MeshFilter>(wheel.Wheel.wheelCollider.wheelTransform.GetComponentsInChildren <MeshFilter>()) : null;
                foreach (MeshFilter m in p.FindModelComponents <MeshFilter>())
                {
                    //skip disabled objects
                    if (m.gameObject == null || exclude_disabled && !m.gameObject.activeInHierarchy)
                    {
                        continue;
                    }
                    //skip meshes from the blacklist
                    bool skip_mesh = false;
                    for (int i = 0, count = MeshesToSkip.Count; i < count; i++)
                    {
                        string mesh_name = MeshesToSkip[i];
                        if (mesh_name == "")
                        {
                            continue;
                        }
                        skip_mesh = m.name.IndexOf(mesh_name, StringComparison.OrdinalIgnoreCase) >= 0;
                        if (skip_mesh)
                        {
                            break;
                        }
                    }
                    if (skip_mesh)
                    {
                        continue;
                    }
                    var       is_wheel = wheel_meshes != null && wheel_meshes.Contains(m);
                    Vector3[] verts;
                    if (compute_hull)
                    {
                        float m_size = Vector3.Scale(m.sharedMesh.bounds.size, m.transform.lossyScale).sqrMagnitude;
                        verts = local2local(m.transform, vT,
                                            (is_wheel || m_size > b_size / 10? uniqueVertices(m.sharedMesh) :
                                             Utils.BoundCorners(m.sharedMesh.bounds)));
                        hull_points.AddRange(verts);
                        b_size = b.size.sqrMagnitude;
                    }
                    else
                    {
                        verts = local2local(m.transform, vT, (is_wheel? uniqueVertices(m.sharedMesh) :
                                                              Utils.BoundCorners(m.sharedMesh.bounds)));
                    }
                    updateBounds(ref b, verts);
                }
                CrewCapacity += p.CrewCapacity;
                mass         += p.TotalMass();
                cost         += p.TotalCost();
            }
            if (compute_hull && hull_points.Count >= 4)
            {
                hull = new ConvexHull3D(hull_points);
            }
            return(b);
        }