Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
 public void FilterHits(List <GameObjectRayHit> hits)
 {
     hits.RemoveAll(item => !AllowedObjectTypes.Contains(item.HitObject.GetGameObjectType()) ||
                    IgnoreObjects.Contains(item.HitObject) || !LayerEx.IsLayerBitSet(_layerMask, item.HitObject.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 (var grabTarget in _grabTargets)
            {
                raycastFilter.IgnoreObjects.AddRange(grabTarget.GameObject.GetAllChildrenAndSelf());
            }

            SceneRaycastHit raycastHit = EditorScene.Get.Raycast(EditorCamera.Pickray, 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);
        }