Example #1
0
        private Bounds SetBoundsFromMeshes()
        {
            Vector3 upper = Vector3.one * float.NegativeInfinity, lower = Vector3.one * float.PositiveInfinity;

            for (int i = 0; i < meshDataList.Count; ++i)
            {
                GeometryMesh geoMesh = meshDataList[i];

                if (!geoMesh.valid)
                {
                    continue;
                }

                upper = Vector3.Max(upper, geoMesh.bounds.max);
                lower = Vector3.Min(lower, geoMesh.bounds.min);
            }
            Bounds overallBounds = new Bounds((upper + lower) * 0.5f, upper - lower);

            float tmpTestBounds = overallBounds.center.x + overallBounds.center.y + overallBounds.center.z +
                                  overallBounds.extents.x + overallBounds.extents.y + overallBounds.extents.z;

            if (float.IsNaN(tmpTestBounds) || float.IsInfinity(tmpTestBounds))
            {
                Debug.Log("Overall bounds error in " + part.partInfo.title + " " + meshDataList.Count + " meshes");
                _valid = false;
            }
            else
            {
                _valid = true;
            }

            return(overallBounds);
        }
Example #2
0
        internal void RebuildAllMeshData()
        {
            if (!(HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor))
            {
                return;
            }

            partTransform = part.partTransform;
            List <Transform> meshTransforms = part.PartModelTransformList();
            List <MeshData>  geometryMeshes = CreateMeshListFromTransforms(ref meshTransforms);

            meshDataList = new List <GeometryMesh>();

            Matrix4x4 worldToVesselMatrix;

            if (HighLogic.LoadedSceneIsFlight)
            {
                worldToVesselMatrix = vessel.vesselTransform.worldToLocalMatrix;
            }
            else
            {
                worldToVesselMatrix = EditorLogic.RootPart.partTransform.worldToLocalMatrix;
            }
            for (int i = 0; i < meshTransforms.Count; i++)
            {
                MeshData     m       = geometryMeshes[i];
                GeometryMesh geoMesh = new GeometryMesh(m, meshTransforms[i], worldToVesselMatrix, this);
                meshDataList.Add(geoMesh);
            }
            //UpdateTransformMatrixList(worldToVesselMatrix);
            overallMeshBounds = part.GetPartOverallMeshBoundsInBasis(worldToVesselMatrix);
        }
        internal void RebuildAllMeshData()
        {
            if (!(HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor))
            {
                return;
            }

            _ready = false;

            while (_meshesToUpdate > 0) //if the previous transform order hasn't been completed yet, wait here to let it
            {
                if (this == null)
                {
                    return;
                }
            }

            partTransform = part.partTransform;
            List <Transform> meshTransforms = part.PartModelTransformList();

            meshTransforms.RemoveAll(IgnoredPredicate);
            List <MeshData> geometryMeshes = CreateMeshListFromTransforms(ref meshTransforms);

            meshDataList = new List <GeometryMesh>();

            Matrix4x4 worldToVesselMatrix;

            if (HighLogic.LoadedSceneIsFlight)
            {
                worldToVesselMatrix = vessel.vesselTransform.worldToLocalMatrix;
            }
            else
            {
                worldToVesselMatrix = EditorLogic.RootPart.partTransform.worldToLocalMatrix;
            }
            for (int i = 0; i < meshTransforms.Count; ++i)
            {
                MeshData m = geometryMeshes[i];
                if (m.vertices.Length <= 0)
                {
                    geometryMeshes.RemoveAt(i);
                    meshTransforms.RemoveAt(i);
                    --i;
                    continue;
                }
                GeometryMesh geoMesh = new GeometryMesh(m, meshTransforms[i], worldToVesselMatrix, this);
                meshDataList.Add(geoMesh);
            }

            _meshesToUpdate = 0;
            _started        = true;

            //UpdateTransformMatrixList(worldToVesselMatrix);
            //overallMeshBounds = part.GetPartOverallMeshBoundsInBasis(worldToVesselMatrix);
        }
Example #4
0
        public void UpdateTransformMatrixList(Matrix4x4 worldToVesselMatrix)
        {
            if (meshDataList != null)
            {
                _ready = false;
                while (_meshesToUpdate > 0) //if the previous transform order hasn't been completed yet, wait here to let it
                {
                    if (this == null)
                    {
                        return;
                    }
                }
                _ready = false;

                _meshesToUpdate = meshDataList.Count;
                for (int i = 0; i < meshDataList.Count; ++i)
                {
                    GeometryMesh mesh = meshDataList[i];
                    if (mesh.TrySetThisToVesselMatrixForTransform())
                    {
                        ThreadPool.QueueUserWorkItem(mesh.MultithreadTransformBasis, worldToVesselMatrix);
                        //mesh.TransformBasis(worldToVesselMatrix);
                    }
                    else
                    {
                        Debug.Log("A mesh on " + part.partInfo.title + " did not exist and was removed");
                        meshDataList.RemoveAt(i);
                        --i;
                        lock (this)
                            --_meshesToUpdate;
                    }

                    /*if (mesh.TrySetThisToVesselMatrixForTransform())
                     * {
                     *  mesh.TransformBasis(worldToVesselMatrix);
                     * }
                     * else
                     * {
                     *  meshDataList.RemoveAt(i);
                     *  i--;
                     * }*/
                }
            }
            if (crossSectionAdjusters != null)
            {
                for (int i = 0; i < crossSectionAdjusters.Count; i++)
                {
                    ICrossSectionAdjuster adjuster = crossSectionAdjusters[i];
                    adjuster.SetThisToVesselMatrixForTransform();
                    adjuster.TransformBasis(worldToVesselMatrix);
                    adjuster.UpdateArea();
                }
            }
            //overallMeshBounds = part.GetPartOverallMeshBoundsInBasis(worldToVesselMatrix);
        }
        private (Bounds, Bounds) SetBoundsFromMeshes()
        {
            if (meshDataList.Count == 0)
            {
                // If the mesh is empty, try rebuilding it. This can happen when a part was added to the editor but not
                // the ship before adding it to the ship
                RebuildAllMeshData();
            }

            Vector3 upper = Vector3.one * float.NegativeInfinity, lower = Vector3.one * float.PositiveInfinity;
            Vector3 upperLocal = Vector3.one * float.NegativeInfinity,
                    lowerLocal = Vector3.one * float.PositiveInfinity;

            foreach (GeometryMesh geoMesh in meshDataList)
            {
                if (!geoMesh.valid)
                {
                    continue;
                }

                upper = Vector3.Max(upper, geoMesh.bounds.max);
                lower = Vector3.Min(lower, geoMesh.bounds.min);

                Bounds meshBounds = GeometryMesh.TransformBounds(geoMesh.meshLocalBounds, geoMesh.meshLocalToPart);
                upperLocal = Vector3.Max(upperLocal, meshBounds.max);
                lowerLocal = Vector3.Min(lowerLocal, meshBounds.min);
            }

            var overallBounds = new Bounds((upper + lower) * 0.5f, upper - lower);
            var localBounds   = new Bounds((upperLocal + lowerLocal) * 0.5f, upperLocal - lowerLocal);

            float tmpTestBounds = overallBounds.center.x +
                                  overallBounds.center.y +
                                  overallBounds.center.z +
                                  overallBounds.extents.x +
                                  overallBounds.extents.y +
                                  overallBounds.extents.z;

            if (float.IsNaN(tmpTestBounds) || float.IsInfinity(tmpTestBounds))
            {
                FARLogger.Info("Overall bounds error in " + part.partInfo.title + " " + meshDataList.Count + " meshes");
                Valid = false;
            }
            else
            {
                Valid = true;
            }

            return(localBounds, overallBounds);
        }
Example #6
0
        public void UpdateTransformMatrixList(Matrix4x4 worldToVesselMatrix)
        {
            if (meshDataList != null)
            {
                _ready = false;
                //if the previous transform order hasn't been completed yet, wait here to let it
                while (_meshesToUpdate > 0)
                {
                    if (this == null)
                    {
                        return;
                    }
                }
                _ready = false;

                _meshesToUpdate = meshDataList.Count;
                for (int i = 0; i < meshDataList.Count; ++i)
                {
                    GeometryMesh mesh = meshDataList[i];
                    if (mesh.TrySetThisToVesselMatrixForTransform())
                    {
                        mesh.TransformBasis(worldToVesselMatrix);
                    }
                    else
                    {
                        FARLogger.Info("A mesh on " + part.partInfo.title + " did not exist and was removed");
                        meshDataList.RemoveAt(i);
                        --i;
                        lock (this)
                        {
                            --_meshesToUpdate;
                        }
                    }
                }
            }

            if (crossSectionAdjusters == null)
            {
                return;
            }
            foreach (ICrossSectionAdjuster adjuster in crossSectionAdjusters)
            {
                adjuster.SetThisToVesselMatrixForTransform();
                adjuster.TransformBasis(worldToVesselMatrix);
                adjuster.UpdateArea();
            }
        }
Example #7
0
        private Bounds SetBoundsFromMeshes()
        {
            Vector3 upper = new Vector3(1, 1, 1) * float.NegativeInfinity, lower = new Vector3(1, 1, 1) * float.PositiveInfinity;

            for (int i = 0; i < meshDataList.Count; i++)
            {
                GeometryMesh geoMesh = meshDataList[i];
                upper.x = Math.Max(upper.x, geoMesh.bounds.max.x);
                upper.y = Math.Max(upper.y, geoMesh.bounds.max.y);
                upper.z = Math.Max(upper.z, geoMesh.bounds.max.z);

                lower.x = Math.Min(lower.x, geoMesh.bounds.min.x);
                lower.y = Math.Min(lower.y, geoMesh.bounds.min.y);
                lower.z = Math.Min(lower.z, geoMesh.bounds.min.z);
            }
            Bounds overallBounds = new Bounds((upper + lower) * 0.5f, upper - lower);

            return(overallBounds);
        }
Example #8
0
        public void UpdateTransformMatrixList(Matrix4x4 worldToVesselMatrix)
        {
            _ready = false;
            if (meshDataList != null)
            {
                _meshesToUpdate = meshDataList.Count;
                for (int i = 0; i < meshDataList.Count; ++i)
                {
                    GeometryMesh mesh = meshDataList[i];
                    if (mesh.TrySetThisToVesselMatrixForTransform())
                    {
                        ThreadPool.QueueUserWorkItem(mesh.MultithreadTransformBasis, worldToVesselMatrix);
                        //mesh.TransformBasis(worldToVesselMatrix);
                    }
                    else
                    {
                        meshDataList.RemoveAt(i);
                        --i;
                        lock (this)
                            --_meshesToUpdate;
                    }

                    /*if (!meshDataList[i].TryTransformBasis(worldToVesselMatrix))
                     * {
                     *  meshDataList.RemoveAt(i);
                     *  i--;
                     * }*/
                }
            }
            if (crossSectionAdjusters != null)
            {
                for (int i = 0; i < crossSectionAdjusters.Count; i++)
                {
                    ICrossSectionAdjuster adjuster = crossSectionAdjusters[i];
                    adjuster.SetThisToVesselMatrixForTransform();
                    adjuster.TransformBasis(worldToVesselMatrix);
                    adjuster.UpdateArea();
                }
            }
            overallMeshBounds = part.GetPartOverallMeshBoundsInBasis(worldToVesselMatrix);
        }
        private void UpdateFromMesh(GeometryMesh mesh, Part part)
        {
            if (mesh.bounds.size.x < elementSize && mesh.bounds.size.y < elementSize && mesh.bounds.size.z < elementSize)
            {
                CalculateVoxelShellFromTinyMesh(mesh.bounds.min, mesh.bounds.max, part);
                return;
            }

            /*Vector3[] vertsVoxelSpace = new Vector3[vertices.Length];
            for (int i = 0; i < vertsVoxelSpace.Length; i++)
            {
                vertsVoxelSpace[i] = transform.MultiplyPoint3x4(mesh.vertices[i]);
            }*/

            for (int a = 0; a < mesh.triangles.Length; a += 3)
            {
                Vector3 vert1, vert2, vert3;

                vert1 = mesh.vertices[mesh.triangles[a]];
                vert2 = mesh.vertices[mesh.triangles[a + 1]];
                vert3 = mesh.vertices[mesh.triangles[a + 2]];

                CalculateVoxelShellForTriangle(vert1, vert2, vert3, part, mesh.invertXYZ);
            }
        }
 public VoxelShellParams(Part part, GeometryMesh mesh)
 {
     this.part = part;
     this.mesh = mesh;
 }