/// <summary>
        /// Renders a layer mask field which can be used to mask/unmask different
        /// combinations of layers.
        /// </summary>
        public static int LayerMaskField(GUIContent content, int layerMask)
        {
            // Store all layer names for easy access
            List <string> allLayerNames = LayerEx.GetAllLayerNames();

            // We first need to build a mask that is mapped to each element in 'allLayerNames'.
            // A 0 bit indicates that the layer with the same index as the bit position is masked.
            int indexMask = 0;

            for (int layerNameIndex = 0; layerNameIndex < allLayerNames.Count; ++layerNameIndex)
            {
                // If the layer is set inside the layer mask, set the bit in the index mask also
                string layerName = allLayerNames[layerNameIndex];
                if (LayerEx.IsLayerBitSet(layerMask, LayerMask.NameToLayer(layerName)))
                {
                    indexMask |= (1 << layerNameIndex);
                }
            }

            // Now we need to show the mask field to the user and use the returned index mask
            // to rebuild the actual layer mask.
            int resultMask   = layerMask;
            int newIndexMask = EditorGUILayout.MaskField(content, indexMask, allLayerNames.ToArray());

            for (int layerNameIndex = 0; layerNameIndex < allLayerNames.Count; ++layerNameIndex)
            {
                // Sync the index mask with the layer mask
                string layerName = allLayerNames[layerNameIndex];
                if (((newIndexMask >> layerNameIndex) & 0x1) != 0)
                {
                    resultMask = LayerEx.SetLayerBit(resultMask, LayerMask.NameToLayer(layerName));
                }
                else
                {
                    resultMask = LayerEx.ClearLayerBit(resultMask, LayerMask.NameToLayer(layerName));
                }
            }

            return(resultMask);
        }
        public static SnapResult CalculateSnapResult(GameObject root, Config snapConfig)
        {
            if (snapConfig.IgnoreDestObjects == null)
            {
                snapConfig.IgnoreDestObjects = new List <GameObject>();
            }

            List <GameObject> sourceObjects = root.GetAllChildrenAndSelf();

            if (sourceObjects.Count > MaxSourceObjects)
            {
                return(new SnapResult(SnapFailReson.MaxObjectsExceeded));
            }

            List <GameObject> sourceMeshObjects   = root.GetMeshObjectsInHierarchy();
            List <GameObject> sourceSpriteObjects = root.GetSpriteObjectsInHierarchy();

            if (sourceMeshObjects.Count == 0 && sourceSpriteObjects.Count == 0)
            {
                return(new SnapResult(SnapFailReson.InvalidSourceObjects));
            }

            var boundsQConfig = new ObjectBounds.QueryConfig();

            boundsQConfig.ObjectTypes = GameObjectType.Mesh | GameObjectType.Sprite;

            Vector3             overlapSizeAdd     = Vector3.one * snapConfig.SnapRadius * 2.0f;
            var                 allSnapFaces       = BoxMath.AllBoxFaces;
            bool                tryMatchAreas      = (snapConfig.Prefs & Prefs.TryMatchArea) != 0;
            bool                foundMatchingAreas = false;
            List <SnapSortData> sortedSnapData     = new List <SnapSortData>(10);
            SnapSortData        sortData           = new SnapSortData();

            foreach (var sourceObject in sourceObjects)
            {
                OBB overlapOBB = ObjectBounds.CalcWorldOBB(sourceObject, boundsQConfig);
                overlapOBB.Size = overlapOBB.Size + overlapSizeAdd;

                List <GameObject> nearbyObjects = RTScene.Get.OverlapBox(overlapOBB);
                nearbyObjects.RemoveAll(item => item.transform.IsChildOf(root.transform) ||
                                        snapConfig.IgnoreDestObjects.Contains(item) || !LayerEx.IsLayerBitSet(snapConfig.DestinationLayers, item.layer));
                if (nearbyObjects.Count == 0)
                {
                    continue;
                }

                var sourceSnapData = Object2ObjectSnapDataDb.Get.GetObject2ObjectSnapData(sourceObject);
                if (sourceSnapData == null)
                {
                    continue;
                }

                sortData.SrcObject = sourceObject;
                foreach (var srcSnapFace in allSnapFaces)
                {
                    var srcAreaDesc   = sourceSnapData.GetWorldSnapAreaDesc(srcSnapFace);
                    var srcAreaBounds = sourceSnapData.GetWorldSnapAreaBounds(srcSnapFace);
                    var srcAreaPts    = srcAreaBounds.GetCenterAndCornerPoints();
                    sortData.SrcSnapFace = srcSnapFace;

                    foreach (var destObject in nearbyObjects)
                    {
                        var destSnapData = Object2ObjectSnapDataDb.Get.GetObject2ObjectSnapData(destObject);
                        if (destSnapData == null)
                        {
                            continue;
                        }

                        sortData.DestObject = destObject;
                        foreach (var destSnapFace in allSnapFaces)
                        {
                            sortData.DestSnapFace = destSnapFace;
                            var destAreaDesc = destSnapData.GetWorldSnapAreaDesc(destSnapFace);

                            sortData.FaceAreasMatch = false;

                            if (tryMatchAreas && destAreaDesc.AreaType == srcAreaDesc.AreaType)
                            {
                                sortData.FaceAreaDiff = Mathf.Abs(destAreaDesc.Area - srcAreaDesc.Area);
                                if (sortData.FaceAreaDiff <= 1000.0f)
                                {
                                    sortData.FaceAreasMatch = true;
                                }
                            }

                            var destAreaBounds = destSnapData.GetWorldSnapAreaBounds(destSnapFace);
                            var destAreaPts    = destAreaBounds.GetCenterAndCornerPoints();

                            foreach (var srcPt in srcAreaPts)
                            {
                                sortData.SnapPivot = srcPt;
                                foreach (var destPt in destAreaPts)
                                {
                                    sortData.SnapDistance = (destPt - srcPt).magnitude;
                                    if (sortData.SnapDistance < snapConfig.SnapRadius)
                                    {
                                        sortData.SnapDest = destPt;
                                        sortedSnapData.Add(sortData);

                                        if (sortData.FaceAreasMatch)
                                        {
                                            foundMatchingAreas = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (sortedSnapData.Count != 0)
            {
                if (!tryMatchAreas || !foundMatchingAreas)
                {
                    sortedSnapData.Sort(delegate(SnapSortData s0, SnapSortData s1)
                    {
                        return(s0.SnapDistance.CompareTo(s1.SnapDistance));
                    });
                }
                else
                {
                    while (true)
                    {
                        if (!sortedSnapData[0].FaceAreasMatch)
                        {
                            sortedSnapData.RemoveAt(0);
                        }
                        else
                        {
                            break;
                        }
                    }

                    sortedSnapData.Sort(delegate(SnapSortData s0, SnapSortData s1)
                    {
                        return(s0.FaceAreaDiff.CompareTo(s1.FaceAreaDiff));
                    });
                }

                return(new SnapResult(sortedSnapData[0].SnapPivot, sortedSnapData[0].SnapDest, sortedSnapData[0].SnapDistance));
            }
            return(new SnapResult(SnapFailReson.NoDestinationFound));
        }
 public bool IsLayerSnapDestination(int objectLayer)
 {
     return(LayerEx.IsLayerBitSet(_snapDestinationLayers, objectLayer));
 }
 public bool IsLayerTransformable(int objectLayer)
 {
     return(LayerEx.IsLayerBitSet(_transformableLayers, objectLayer));
 }
Exemplo n.º 5
0
 public void FilterOverlaps(List <GameObject> gameObjects)
 {
     gameObjects.RemoveAll(item => !AllowedObjectTypes.Contains(item.GetGameObjectType()) ||
                           IgnoreObjects.Contains(item) || !LayerEx.IsLayerBitSet(_layerMask, item.layer));
 }
 public void FilterHits(List <GameObjectRayHit> hits)
 {
     hits.RemoveAll(item => !AllowedObjectTypes.Contains(item.HitObject.GetGameObjectType()) ||
                    IgnoreObjects.Contains(item.HitObject) || !LayerEx.IsLayerBitSet(_layerMask, item.HitObject.layer));
 }