public void OnCreated(CSGBrush component)
 {
     if (!component || UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
     {
         return;
     }
     InternalCSGModelManager.OnCreated(component);
 }
        public static bool FindSurfaceIntersection(CSGBrush brush, Matrix4x4 modelTransformation, Int32 surfaceIndex, Vector2 screenPos, out LegacySurfaceIntersection intersection)
        {
            var worldRay  = HandleUtility.GUIPointToWorldRay(screenPos);
            var rayStart  = worldRay.origin;
            var rayVector = (worldRay.direction * (Camera.current.farClipPlane - Camera.current.nearClipPlane));
            var rayEnd    = rayStart + rayVector;

            return(FindSurfaceIntersection(brush, modelTransformation, surfaceIndex, rayStart, rayEnd, out intersection));
        }
Пример #3
0
        public static bool FindBrushIntersection(CSGBrush brush, Vector3 modelTranslation, int texGenIndex, Vector2 screenPos, out BrushIntersection intersection)
        {
            var worldRay  = HandleUtility.GUIPointToWorldRay(screenPos);
            var rayStart  = worldRay.origin;
            var rayVector = (worldRay.direction * (Camera.current.farClipPlane - Camera.current.nearClipPlane));
            var rayEnd    = rayStart + rayVector;

            return(FindBrushIntersection(brush, modelTranslation, texGenIndex, rayStart, rayEnd, out intersection));
        }
Пример #4
0
        public static void SetPivotToLocalCenter(CSGBrush brush)
        {
            if (!brush)
            {
                return;
            }

            var localCenter = BoundsUtilities.GetLocalCenter(brush);
            var worldCenter = brush.transform.localToWorldMatrix.MultiplyPoint(localCenter);

            SetPivot(brush, worldCenter);
        }
Пример #5
0
 public void Select(CSGBrush brush, int polygonIndex)
 {
     for (var i = Brushes.Length - 1; i >= 0; i--)
     {
         if (Brushes[i] == brush)
         {
             var polygons = States[i].Selection.Polygons;
             for (int j = 0; j < polygons.Length; j++)
             {
                 if (j == polygonIndex)
                 {
                     polygons[j] = SelectState.Selected;
                 }
                 else
                 {
                     polygons[j] = SelectState.None;
                 }
             }
         }
     }
 }
Пример #6
0
        public static void SetPivot(CSGBrush brush, Vector3 newCenter)
        {
            if (!brush)
            {
                return;
            }

            var transform  = brush.transform;
            var realCenter = transform.position;
            var difference = newCenter - realCenter;

            if (difference.sqrMagnitude < MathConstants.ConsideredZero)
            {
                return;
            }

            transform.position += difference;

            GeometryUtility.MoveControlMeshVertices(brush, -difference);
            SurfaceUtility.TranslateSurfacesInWorldSpace(brush, -difference);
            ControlMeshUtility.RebuildShape(brush);
        }
        public static bool SetBrushCubeMesh(CSGBrush brush, UnityEngine.Vector3 size)
        {
            if (!brush)
            {
                return(false);
            }

            ControlMesh controlMesh;
            Shape       shape;

            BrushFactory.CreateCubeControlMesh(out controlMesh, out shape, size);

            brush.ControlMesh = controlMesh;
            brush.Shape       = shape;
            if (brush.ControlMesh != null)
            {
                brush.ControlMesh.SetDirty();
            }
            if (brush.Shape != null)
            {
                ShapeUtility.EnsureInitialized(brush.Shape);
            }
            return(true);
        }
        public static bool FindBrushIntersection(CSGBrush brush, Matrix4x4 modelTransformation, Vector3 rayStart, Vector3 rayEnd, out LegacyBrushIntersection intersection)
        {
            intersection = null;
            if (!brush || InternalCSGModelManager.External.RayCastIntoBrush == null)
            {
                return(false);
            }

            if (!InternalCSGModelManager.External.RayCastIntoBrush(brush.brushNodeID,
                                                                   rayStart,
                                                                   rayEnd,
                                                                   modelTransformation,
                                                                   out intersection,
                                                                   false))
            {
                return(false);
            }

            if (BrushTraits.IsSurfaceSelectable(brush, intersection.surfaceIndex))
            {
                return(false);
            }
            return(true);
        }
 public static bool SetBrushCubeMesh(CSGBrush brush)
 {
     return(SetBrushCubeMesh(brush, Vector3.one));
 }
Пример #10
0
        public static bool FindTwinEdgeOnOtherBrush(CSGBrush brush, Int32 halfEdgeIndex, out CSGBrush twinBrush, out Int32 twinHalfEdgeIndex)
        {
            twinBrush         = null;
            twinHalfEdgeIndex = -1;
            if (InternalCSGModelManager.External.FindTwinEdgeOnOtherBrush == null ||
                !brush ||
                brush.ControlMesh == null ||
                halfEdgeIndex < 0 ||
                halfEdgeIndex >= brush.ControlMesh.Edges.Length)
            {
                return(false);
            }

            var twinIndex     = brush.ControlMesh.Edges[halfEdgeIndex].TwinIndex;
            var polygonIndex1 = brush.ControlMesh.Edges[halfEdgeIndex].PolygonIndex;
            var polygonIndex2 = brush.ControlMesh.Edges[twinIndex].PolygonIndex;

            var surfaceIndex1 = brush.ControlMesh.Polygons[polygonIndex1].TexGenIndex;
            var surfaceIndex2 = brush.ControlMesh.Polygons[polygonIndex2].TexGenIndex;

            Int32 outInstanceId;
            Int32 outBrushID;

            if (!InternalCSGModelManager.External.FindTwinEdgeOnOtherBrush(brush.brushID,
                                                                           surfaceIndex1,
                                                                           surfaceIndex2,
                                                                           out outInstanceId,
                                                                           out outBrushID,
                                                                           out surfaceIndex1,
                                                                           out surfaceIndex2) ||
                outInstanceId == 0)
            {
                return(false);
            }

            var outBrush = EditorUtility.InstanceIDToObject(outInstanceId) as CSGBrush;

            if (!outBrush)
            {
                Debug.Log("instanceid " + outInstanceId + " for found brush not found");
                return(false);
            }

            if (outBrush.brushID != outBrushID)
            {
                Debug.Log("outBrush.brushID != out_brushID");
                return(false);
            }

            var controlMesh = outBrush.ControlMesh;
            var polygons    = controlMesh.Polygons;
            var edges       = controlMesh.Edges;

            for (var p = 0; p < polygons.Length; p++)
            {
                if (polygons[p].TexGenIndex != surfaceIndex1)
                {
                    continue;
                }

                var edgeIndices = polygons[p].EdgeIndices;
                for (var e = 0; e < edgeIndices.Length; e++)
                {
                    var edgeIndex        = edgeIndices[e];
                    var twinPolygonIndex = edges[edges[edgeIndex].TwinIndex].PolygonIndex;

                    if (polygons[twinPolygonIndex].TexGenIndex != surfaceIndex2)
                    {
                        continue;
                    }

                    twinBrush         = outBrush;
                    twinHalfEdgeIndex = e;
                    return(true);
                }
            }
            return(false);
        }
Пример #11
0
        public static bool FindMultiWorldIntersection(Vector3 rayStart, Vector3 rayEnd, out BrushIntersection[] intersections, float growDistance = 0.0f, bool ignoreInvisibleSurfaces = true, bool ignoreUnrenderables = true)
        {
            intersections = null;
            if (InternalCSGModelManager.External == null ||
                InternalCSGModelManager.External.RayCastIntoModelMulti == null)
            {
                return(false);
            }

            var foundIntersections = new Dictionary <CSGNode, BrushIntersection>();

            ignoreInvisibleSurfaces = ignoreInvisibleSurfaces && !CSGSettings.ShowInvisibleSurfaces;
            for (var g = 0; g < InternalCSGModelManager.Models.Length; g++)
            {
                var model = InternalCSGModelManager.Models[g];
                if (!model ||
                    !model.isActiveAndEnabled)
                {
                    continue;
                }

                if (ignoreUnrenderables && !ModelTraits.WillModelRender(model) &&
                    !Selection.Contains(model.gameObject.GetInstanceID()))
                {
                    continue;
                }

                BrushIntersection[] modelIntersections;
                var translation = model.transform.position;
                if (!InternalCSGModelManager.External.RayCastIntoModelMulti(model,
                                                                            rayStart - translation,
                                                                            rayEnd - translation,
                                                                            ignoreInvisibleSurfaces,
                                                                            growDistance,
                                                                            out modelIntersections))
                {
                    continue;
                }

                for (var i = 0; i < modelIntersections.Length; i++)
                {
                    var      intersection = modelIntersections[i];
                    CSGBrush brush        = null;
                    for (var b = 0; b < InternalCSGModelManager.Brushes.Length; b++)
                    {
                        if (InternalCSGModelManager.Brushes[b].brushID != intersection.brushID)
                        {
                            continue;
                        }

                        brush = InternalCSGModelManager.Brushes[b];
                        break;
                    }

                    if (BrushTraits.IsSurfaceSelectable(brush, intersection.surfaceIndex))
                    {
                        continue;
                    }

                    intersection.brush              = brush;
                    intersection.model              = model;
                    intersection.worldIntersection += translation;
                    intersection.plane.Translate(translation);
                    var currentNode = GetTopMostGroupForNode(intersection.brush);

                    BrushIntersection other;
                    if (foundIntersections.TryGetValue(currentNode, out other) &&
                        other.distance <= intersection.distance)
                    {
                        continue;
                    }

                    foundIntersections[currentNode] = modelIntersections[i];
                }
            }

            if (foundIntersections.Count == 0)
            {
                return(false);
            }

            var sortedIntersections = foundIntersections.Values.ToArray();

            Array.Sort(sortedIntersections, (x, y) => (x.distance < y.distance) ? -1 : 0);
            intersections = sortedIntersections;
            return(true);
        }