public void SetObjectLayerDuplicatable(int objectLayer, bool isDuplicatable)
 {
     if (isDuplicatable)
     {
         _duplicatableLayers = LayerEx.SetLayerBit(_duplicatableLayers, objectLayer);
     }
     else
     {
         _duplicatableLayers = LayerEx.ClearLayerBit(_duplicatableLayers, objectLayer);
     }
 }
 public void SetLayerSnapDestination(int objectLayer, bool isSnapDestination)
 {
     if (isSnapDestination)
     {
         _snapDestinationLayers = LayerEx.SetLayerBit(_snapDestinationLayers, objectLayer);
     }
     else
     {
         _snapDestinationLayers = LayerEx.ClearLayerBit(_snapDestinationLayers, objectLayer);
     }
 }
 public void SetLayerTransformable(int objectLayer, bool isTransformable)
 {
     if (isTransformable)
     {
         _transformableLayers = LayerEx.SetLayerBit(_transformableLayers, objectLayer);
     }
     else
     {
         _transformableLayers = LayerEx.ClearLayerBit(_transformableLayers, objectLayer);
     }
 }
 public void SetObjectLayerSelectable(int objectLayer, bool isSelectable)
 {
     if (isSelectable)
     {
         _selectableLayers = LayerEx.SetLayerBit(_selectableLayers, objectLayer);
     }
     else
     {
         _selectableLayers = LayerEx.ClearLayerBit(_selectableLayers, objectLayer);
     }
 }
        /// <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 void FilterHits(List <GameObjectRayHit> hits)
 {
     hits.RemoveAll(item => !AllowedObjectTypes.Contains(item.HitObject.GetGameObjectType()) ||
                    IgnoreObjects.Contains(item.HitObject) || !LayerEx.IsLayerBitSet(_layerMask, item.HitObject.layer));
 }
 public bool IsLayerSnapDestination(int objectLayer)
 {
     return(LayerEx.IsLayerBitSet(_snapDestinationLayers, objectLayer));
 }
 public void FilterOverlaps(List <GameObject> gameObjects)
 {
     gameObjects.RemoveAll(item => !AllowedObjectTypes.Contains(item.GetGameObjectType()) ||
                           IgnoreObjects.Contains(item) || !LayerEx.IsLayerBitSet(_layerMask, item.layer));
 }
        private bool IdentifyGrabSurface()
        {
            _grabSurfaceInfo.SurfaceType = GrabSurfaceType.Invalid;

            SceneRaycastFilter raycastFilter = new SceneRaycastFilter();

            raycastFilter.LayerMask = SharedSettings.SurfaceLayers;
            if ((SharedSettings.SurfaceFlags & ObjectGrabSurfaceFlags.Mesh) != 0)
            {
                raycastFilter.AllowedObjectTypes.Add(GameObjectType.Mesh);
            }
            if ((SharedSettings.SurfaceFlags & ObjectGrabSurfaceFlags.Terrain) != 0)
            {
                raycastFilter.AllowedObjectTypes.Add(GameObjectType.Terrain);
            }
            foreach (GrabTarget grabTarget in _grabTargets)
            {
                raycastFilter.IgnoreObjects.AddRange(grabTarget.GameObject.GetAllChildrenAndSelf());
            }

            IInputDevice    inputDevice = RTInputDevice.Get.Device;
            SceneRaycastHit raycastHit  = RTScene.Get.Raycast(inputDevice.GetRay(RTFocusCamera.Get.TargetCamera), SceneRaycastPrecision.BestFit, raycastFilter);

            if (!raycastHit.WasAnythingHit)
            {
                return(false);
            }

            _grabSurfaceInfo.SceneRaycastHit = raycastHit;
            if (raycastHit.WasAnObjectHit)
            {
                _grabSurfaceInfo.AnchorNormal = raycastHit.ObjectHit.HitNormal;
                _grabSurfaceInfo.AnchorPoint  = raycastHit.ObjectHit.HitPoint;
                _grabSurfaceInfo.AnchorPlane  = raycastHit.ObjectHit.HitPlane;

                GameObjectType hitObjectType = raycastHit.ObjectHit.HitObject.GetGameObjectType();
                if (hitObjectType == GameObjectType.Mesh)
                {
                    _grabSurfaceInfo.SurfaceType = GrabSurfaceType.Mesh;

                    int objectLayer = raycastHit.ObjectHit.HitObject.layer;
                    if (LayerEx.IsLayerBitSet(SharedSettings.SphericalMeshLayers, objectLayer))
                    {
                        _grabSurfaceInfo.SurfaceType = GrabSurfaceType.SphericalMesh;
                    }
                    else if (LayerEx.IsLayerBitSet(SharedSettings.TerrainMeshLayers, objectLayer))
                    {
                        _grabSurfaceInfo.SurfaceType = GrabSurfaceType.TerrainMesh;
                    }
                }
                else
                {
                    _grabSurfaceInfo.SurfaceType = GrabSurfaceType.UnityTerrain;
                }
            }
            else
            if (raycastHit.WasGridHit && (SharedSettings.SurfaceFlags & ObjectGrabSurfaceFlags.Grid) != 0)
            {
                _grabSurfaceInfo.AnchorNormal = raycastHit.GridHit.HitNormal;
                _grabSurfaceInfo.AnchorPoint  = raycastHit.GridHit.HitPoint;
                _grabSurfaceInfo.AnchorPlane  = raycastHit.GridHit.HitPlane;
                _grabSurfaceInfo.SurfaceType  = GrabSurfaceType.Grid;
            }

            return(true);
        }
 public bool IsLayerTransformable(int objectLayer)
 {
     return(LayerEx.IsLayerBitSet(_transformableLayers, objectLayer));
 }
 public bool IsObjectLayerDeletable(int objectLayer)
 {
     return(LayerEx.IsLayerBitSet(_deletableLayers, objectLayer));
 }
        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));
            }

            ObjectBounds.QueryConfig boundsQConfig = new ObjectBounds.QueryConfig();
            boundsQConfig.ObjectTypes = GameObjectType.Mesh | GameObjectType.Sprite;

            Vector3             overlapSizeAdd     = Vector3.one * snapConfig.SnapRadius * 2.0f;
            List <BoxFace>      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 (GameObject 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;
                }

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

                sortData.SrcObject = sourceObject;
                foreach (BoxFace srcSnapFace in allSnapFaces)
                {
                    BoxFaceAreaDesc srcAreaDesc   = sourceSnapData.GetWorldSnapAreaDesc(srcSnapFace);
                    OBB             srcAreaBounds = sourceSnapData.GetWorldSnapAreaBounds(srcSnapFace);
                    List <Vector3>  srcAreaPts    = srcAreaBounds.GetCenterAndCornerPoints();
                    sortData.SrcSnapFace = srcSnapFace;

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

                        sortData.DestObject = destObject;
                        foreach (BoxFace destSnapFace in allSnapFaces)
                        {
                            sortData.DestSnapFace = destSnapFace;
                            BoxFaceAreaDesc 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;
                                }
                            }

                            OBB            destAreaBounds = destSnapData.GetWorldSnapAreaBounds(destSnapFace);
                            List <Vector3> destAreaPts    = destAreaBounds.GetCenterAndCornerPoints();

                            foreach (Vector3 srcPt in srcAreaPts)
                            {
                                sortData.SnapPivot = srcPt;
                                foreach (Vector3 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));
        }