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);
        }
        public static bool DoTurnHandle(this IChiselHandles handles, ref ChiselAABB box, string undoMessage = null)
        {
            var bounds = new Bounds();

            bounds.SetMinMax(box.Min, box.Max);
            var result = handles.DoTurnHandle(ref box, undoMessage);

            box.Min = bounds.min;
            box.Max = bounds.max;
            return(result);
        }
Exemple #3
0
        public static int CountPathedStairBrushes(UnsafeList <SegmentVertex> shapeVertices,
                                                  bool closedLoop,

                                                  ChiselAABB bounds,

                                                  float stepHeight,
                                                  float stepDepth,

                                                  float treadHeight,

                                                  float nosingDepth,

                                                  float plateauHeight,

                                                  StairsRiserType riserType,
                                                  float riserDepth,

                                                  StairsSideType leftSide,
                                                  StairsSideType rightSide,

                                                  float sideWidth,
                                                  float sideHeight,
                                                  float sideDepth)
        {
            var totalSubMeshCount = 0;

            for (int i = 0; i < shapeVertices.Length; i++)
            {
                if (i == 0 && !closedLoop)
                {
                    continue;
                }

                var segmentLeftSide  = (!closedLoop && i == 1) ? leftSide : StairsSideType.None;
                var segmentRightSide = (!closedLoop && i == shapeVertices.Length - 1) ? rightSide : StairsSideType.None;

                var description = new LineairStairsData(bounds, stepHeight, stepDepth, treadHeight,
                                                        nosingDepth, nosingWidth: 0, plateauHeight,
                                                        riserType, riserDepth,
                                                        segmentLeftSide, segmentRightSide,
                                                        sideWidth, sideHeight, sideDepth);
                totalSubMeshCount += description.subMeshCount;
            }
            return(totalSubMeshCount);
        }
Exemple #4
0
        // TODO: kind of broken, needs fixing
        public static bool GeneratePathedStairs(NativeList <ChiselBlobAssetReference <BrushMeshBlob> > brushMeshes,
                                                UnsafeList <SegmentVertex> shapeVertices,
                                                bool closedLoop,
                                                ChiselAABB bounds,

                                                float stepHeight,
                                                float stepDepth,

                                                float treadHeight,

                                                float nosingDepth,

                                                float plateauHeight,

                                                StairsRiserType riserType,
                                                float riserDepth,

                                                StairsSideType leftSide,
                                                StairsSideType rightSide,

                                                float sideWidth,
                                                float sideHeight,
                                                float sideDepth,
                                                in ChiselBlobAssetReference <NativeChiselSurfaceDefinition> surfaceDefinitionBlob,
 public static void RenderCylinder(this IChiselHandleRenderer renderer, ChiselAABB bounds, int segments)
 {
     renderer.RenderCylinder(new Bounds((bounds.Max + bounds.Min) * 0.5f, bounds.Max - bounds.Min), segments);
 }
 public static void RenderBoxMeasurements(this IChiselHandleRenderer renderer, ChiselAABB bounds)
 {
     renderer.RenderBoxMeasurements(new Bounds((bounds.Max + bounds.Min) * 0.5f, bounds.Max - bounds.Min));
 }
Exemple #7
0
 public static bool Intersects(this ChiselAABB left, ChiselAABB right, double epsilon)
 {
     return(((right.Max.x - left.Min.x) >= -epsilon) && ((left.Max.x - right.Min.x) >= -epsilon) &&
            ((right.Max.y - left.Min.y) >= -epsilon) && ((left.Max.y - right.Min.y) >= -epsilon) &&
            ((right.Max.z - left.Min.z) >= -epsilon) && ((left.Max.z - right.Min.z) >= -epsilon));
 }
Exemple #8
0
        // TODO: turn into job
        static void GetIntersectingPlanes(IntersectionType type,
                                          [NoAlias] ref ChiselBlobArray <float4> localPlanes,
                                          int localPlaneCount,
                                          [NoAlias] ref ChiselBlobArray <float3> vertices,
                                          ChiselAABB selfBounds,
                                          float4x4 treeToNodeSpaceInverseTransposed,
                                          [NoAlias] ref NativeArray <int> intersectingPlaneIndices,
                                          [NoAlias] out int intersectingPlaneLength,
                                          [NoAlias] out int intersectingPlanesAndEdgesLength)
        {
            NativeCollectionHelpers.EnsureMinimumSize(ref intersectingPlaneIndices, localPlanes.Length);
            if (type != IntersectionType.Intersection)
            {
                intersectingPlaneLength          = localPlaneCount;
                intersectingPlanesAndEdgesLength = localPlanes.Length;
                for (int i = 0; i < intersectingPlaneLength; i++)
                {
                    intersectingPlaneIndices[i] = i;
                }
                return;
            }

            var min = selfBounds.Min;
            var max = selfBounds.Max;

            //Debug.Log($"{localPlanes.Length}");

            intersectingPlaneLength          = 0;
            intersectingPlanesAndEdgesLength = 0;
            var verticesLength = vertices.Length;

            for (int i = 0; i < localPlanes.Length; i++)
            {
                // bring plane into local space of mesh, the same space as the bounds of the mesh

                var localPlane = localPlanes[i];

                // note: a transpose is part of this transformation
                var transformedPlane = math.mul(treeToNodeSpaceInverseTransposed, localPlane);
                //var normal            = transformedPlane.xyz; // only need the signs, so don't care about normalization
                //transformedPlane /= math.length(normal);      // we don't have to normalize the plane

                var corner = new float4((transformedPlane.x < 0) ? max.x : min.x,
                                        (transformedPlane.y < 0) ? max.y : min.y,
                                        (transformedPlane.z < 0) ? max.z : min.z,
                                        1.0f);
                float forward = math.dot(transformedPlane, corner);
                if (forward > kFatPlaneWidthEpsilon) // closest point is outside
                {
                    intersectingPlaneLength          = 0;
                    intersectingPlanesAndEdgesLength = 0;
                    return;
                }

                // do a bounds check
                corner = new float4((transformedPlane.x >= 0) ? max.x : min.x,
                                    (transformedPlane.y >= 0) ? max.y : min.y,
                                    (transformedPlane.z >= 0) ? max.z : min.z,
                                    1.0f);
                float backward = math.dot(transformedPlane, corner);
                if (backward < -kFatPlaneWidthEpsilon) // closest point is inside
                {
                    continue;
                }

                float minDistance = float.PositiveInfinity;
                float maxDistance = float.NegativeInfinity;
                int   onCount     = 0;
                for (int v = 0; v < verticesLength; v++)
                {
                    float distance = math.dot(transformedPlane, new float4(vertices[v], 1));
                    minDistance = math.min(distance, minDistance);
                    maxDistance = math.max(distance, maxDistance);
                    onCount    += (distance >= -kFatPlaneWidthEpsilon && distance <= kFatPlaneWidthEpsilon) ? 1 : 0;
                }

                // if all vertices are 'inside' this plane, then we're not truly intersecting with it
                if ((minDistance > kFatPlaneWidthEpsilon || maxDistance < -kFatPlaneWidthEpsilon))
                {
                    continue;
                }

                if (i < localPlaneCount)
                {
                    intersectingPlaneIndices[intersectingPlaneLength] = i;
                    intersectingPlaneLength++;
                    intersectingPlanesAndEdgesLength++;
                }
                else
                {
                    intersectingPlaneIndices[intersectingPlanesAndEdgesLength] = i;
                    intersectingPlanesAndEdgesLength++;
                }
            }
        }
Exemple #9
0
 public bool Equals(ChiselAABB other)
 {
     return(Min.Equals(other.Min) && Max.Equals(other.Max));
 }
Exemple #10
0
 public void Encapsulate(ChiselAABB aabb)
 {
     Min = math.min(Min, aabb.Min);
     Max = math.max(Max, aabb.Max);
 }