Exemple #1
0
        public static Bounds CalculateBounds(ChiselGeneratorComponent generator)
        {
            if (!generator.TopTreeNode.Valid)
            {
                return(ChiselHierarchyItem.EmptyBounds);
            }

            var modelMatrix = ChiselNodeHierarchyManager.FindModelTransformMatrixOfTransform(generator.hierarchyItem.Transform);
            var minMax      = new ChiselAABB {
            };
            var boundsCount = 0;

            s_FoundBrushes.Clear();
            ChiselGeneratedComponentManager.GetAllTreeBrushes(generator, s_FoundBrushes);
            foreach (var brush in s_FoundBrushes)
            {
                if (!brush.Valid)
                {
                    continue;
                }

                var transformation = modelMatrix * (Matrix4x4)brush.NodeToTreeSpaceMatrix;
                var childBounds    = brush.Bounds;
                var size           = childBounds.Max - childBounds.Min;
                var magnitude      = math.lengthsq(size);
                if (float.IsInfinity(magnitude) ||
                    float.IsNaN(magnitude))
                {
                    var center   = ((float4)transformation.GetColumn(3)).xyz;
                    var halfSize = size * 0.5f;
                    childBounds = new ChiselAABB {
                        Min = center - halfSize, Max = center + halfSize
                    };
                }
                if (magnitude != 0)
                {
                    if (boundsCount == 0)
                    {
                        minMax = childBounds;
                    }
                    else
                    {
                        minMax.Encapsulate(childBounds);
                    }
                    boundsCount++;
                }
            }
            if (boundsCount == 0)
            {
                return(ChiselHierarchyItem.EmptyBounds);
            }
            var bounds = new Bounds();

            bounds.SetMinMax(minMax.Min, minMax.Max);
            return(bounds);
        }