public void Begin(List <GameObject> grabbedObjects)
        {
            if (_grabSettings == null || grabbedObjects == null || grabbedObjects.Count == 0 || IsActive)
            {
                return;
            }

            _grabbedObjects = new List <GameObject>(grabbedObjects);
            MouseCursorRayHit cursorRayHit = GetCursorRayHit();

            if (!cursorRayHit.WasAnythingHit)
            {
                return;
            }

            _surfaceHitPoint = cursorRayHit.WasAnObjectHit ? cursorRayHit.ClosestObjectRayHit.HitPoint : cursorRayHit.GridCellRayHit.HitPoint;

            _state = State.Grabbing;
            _rayHitMask.ObjectCollectionMask.Mask(_grabbedObjects);

            foreach (var grabbedObject in grabbedObjects)
            {
                if (grabbedObject != null)
                {
                    _objectToPivotDir.Add(grabbedObject, grabbedObject.transform.position - _surfaceHitPoint);
                }
            }
        }
        public void Begin(List <GameObject> grabbedObjects)
        {
            if (_grabSettings == null || grabbedObjects == null || grabbedObjects.Count == 0 || IsActive)
            {
                return;
            }

            _grabbedObjects = Octave3DWorldBuilder.ActiveInstance.GetRoots(grabbedObjects);
            foreach (var grabbedObj in _grabbedObjects)
            {
                _rayHitMask.ObjectCollectionMask.Mask(grabbedObj.GetAllChildrenIncludingSelf());
            }

            MouseCursorRayHit cursorRayHit = GetCursorRayHit();

            if (!cursorRayHit.WasAnythingHit)
            {
                return;
            }

            _surfaceHitPoint = cursorRayHit.WasAnObjectHit ? cursorRayHit.ClosestObjectRayHit.HitPoint : cursorRayHit.GridCellRayHit.HitPoint;
            _state           = State.Moving;

            foreach (var grabbedObject in _grabbedObjects)
            {
                if (grabbedObject != null)
                {
                    _objectToPivotDir.Add(grabbedObject, grabbedObject.transform.position - _surfaceHitPoint);
                }
            }
        }
        public void FromMouseCursorRayHit(MouseCursorRayHit cursorRayHit)
        {
            _isValid = false;

            if (!cursorRayHit.WasAnythingHit)
            {
                return;
            }
            else
            if (cursorRayHit.WasAnObjectHit && !ObjectSnapSettings.Get().IgnoreObjectsWhenSnapping)
            {
                GameObjectRayHit gameObjectRayHit = FindClosestHitObjectWhichCanBeUsedAsSnapSurface(cursorRayHit);
                if (gameObjectRayHit != null)
                {
                    if (!cursorRayHit.WasACellHit)
                    {
                        ExtractData(gameObjectRayHit);
                    }
                    else
                    {
                        if (gameObjectRayHit.HitEnter <= cursorRayHit.GridCellRayHit.HitEnter)
                        {
                            ExtractData(gameObjectRayHit);
                        }
                    }
                }
            }

            // If we didn't manage to build the surface up until now, we will give it one last chance
            // using the cursor grid cell ray hit.
            if (!_isValid && cursorRayHit.WasACellHit)
            {
                ExtractData(cursorRayHit.GridCellRayHit);
            }
        }
Exemple #4
0
        public void SnapXZGridToCursorPickPoint(bool snapToClosestTopOrBottom)
        {
            MouseCursor.Instance.PushObjectMask(null);
            MouseCursor.Instance.PushObjectPickMaskFlags(MouseCursorObjectPickFlags.ObjectBox | MouseCursorObjectPickFlags.ObjectTerrain);
            MouseCursorRayHit cursorRayHit = MouseCursor.Instance.GetRayHit();

            MouseCursor.Instance.PopObjectPickMaskFlags();
            MouseCursor.Instance.PopObjectMask();

            if (cursorRayHit.WasAnythingHit)
            {
                Vector3 snapDestPoint;
                if (cursorRayHit.WasAnObjectHit)
                {
                    snapDestPoint = cursorRayHit.ClosestObjectRayHit.HitPoint;
                    UndoEx.RecordForToolAction(XZSnapGrid);

                    if (snapToClosestTopOrBottom)
                    {
                        Box     objectWorldBox       = cursorRayHit.ClosestObjectRayHit.HitObject.GetWorldBox();
                        Vector3 fromCenterToHitPoint = snapDestPoint - objectWorldBox.Center;
                        if (Vector3.Dot(fromCenterToHitPoint, Vector3.up) > 0.0f)
                        {
                            snapDestPoint = objectWorldBox.Center + Vector3.up * objectWorldBox.Extents.y;
                        }
                        else
                        {
                            snapDestPoint = objectWorldBox.Center - Vector3.up * objectWorldBox.Extents.y;
                        }
                    }

                    XZSnapGrid.SnapToPoint(snapDestPoint);
                }
            }
        }
        public void Perform()
        {
            MouseCursorRayHit cursorRayHit = ObjectEraser.Get().GetMouseCursorRayHit();

            if (cursorRayHit.WasAnObjectHit)
            {
                List <GameObject> objectsToErase = cursorRayHit.GetAllObjectsSortedByHitDistance();
                objectsToErase = ObjectEraser.Get().FilterObjectsWhichCanBeErased(objectsToErase);
                if (objectsToErase.Count == 0)
                {
                    return;
                }

                objectsToErase = new List <GameObject> {
                    objectsToErase[0]
                };

                bool eraseEntireHierarchy = !AllShortcutCombos.Instance.EraseIndividualObjects.IsActive();
                if (eraseEntireHierarchy)
                {
                    ObjectErase.EraseObjectHierarchiesInObjectCollection(objectsToErase);
                }
                else
                {
                    ObjectErase.EraseGameObjectCollection(objectsToErase);
                }
            }
        }
        public static List <GameObject> ReplaceSelectedObjectsPrefabOnMouseClick()
        {
            MouseCursorRayHit cursorRayHit = ObjectSelection.Get().GetObjectPickedByCursor();

            if (cursorRayHit.WasAnObjectHit && !ObjectSelection.Get().IsGameObjectSelected(cursorRayHit.ClosestObjectRayHit.HitObject))
            {
                GameObject hitObject = cursorRayHit.ClosestObjectRayHit.HitObject;
                hitObject = Octave3DWorldBuilder.ActiveInstance.GetRoot(hitObject);
                if (hitObject == null)
                {
                    return(new List <GameObject>());
                }

                GameObject newPrefabForSelectedObjects = hitObject.GetSourcePrefab();
                if (newPrefabForSelectedObjects == null)
                {
                    return(new List <GameObject>());
                }

                List <GameObject> allSelectedObjects = ObjectSelection.Get().GetAllSelectedGameObjects();
                ObjectSelection.Get().RemoveGameObjectCollectionFromSelection(allSelectedObjects);
                List <GameObject> newObjects = ObjectActions.ReplaceGameObjectHierarchyCollectionPrefab(allSelectedObjects, newPrefabForSelectedObjects);
                newObjects.RemoveAll(item => item == null);

                return(newObjects);
            }

            return(new List <GameObject>());
        }
        public MouseCursorRayHit GetObjectPickedByCursor()
        {
            MouseCursor.Instance.PushObjectPickMaskFlags(MouseCursorObjectPickFlags.ObjectTerrain);
            MouseCursorRayHit cursorRayHit = MouseCursor.Instance.GetRayHit();

            MouseCursor.Instance.PopObjectPickMaskFlags();

            return(cursorRayHit);
        }
        public void AcquireCursorRayHit(ObjectMask objectMask, MouseCursorObjectPickFlags cursorPickMaskFlags)
        {
            MouseCursor.Instance.PushObjectMask(objectMask);
            MouseCursor.Instance.PushObjectPickMaskFlags(cursorPickMaskFlags);

            _cursorRayHit = MouseCursor.Instance.GetRayHit();

            MouseCursor.Instance.PopObjectMask();
            MouseCursor.Instance.PopObjectPickMaskFlags();
        }
        private void AcquireCurorRayHit()
        {
            MouseCursor.Instance.PushObjectPickMaskFlags(GetCursorPickMaskFlags());
            MouseCursor.Instance.PushObjectMask(DecorPaintObjectPlacement.Get().DecorPaintMask);

            _cursorRayHit = MouseCursor.Instance.GetRayHit();

            MouseCursor.Instance.PopObjectMask();
            MouseCursor.Instance.PopObjectPickMaskFlags();
        }
Exemple #10
0
        private MouseCursorRayHit GetCursorRayHit()
        {
            MouseCursor.Instance.PushObjectPickMaskFlags(MouseCursorObjectPickFlags.ObjectTerrain | MouseCursorObjectPickFlags.ObjectMesh);
            MouseCursor.Instance.PushObjectMask(_rayCastObjectMask);
            MouseCursorRayHit cursorRayHit = MouseCursor.Instance.GetRayHit();

            MouseCursor.Instance.PopObjectMask();
            MouseCursor.Instance.PopObjectPickMaskFlags();

            return(cursorRayHit);
        }
        private MouseCursorRayHit GetCursorRayHit()
        {
            MouseCursor.Instance.PushObjectPickMaskFlags(MouseCursorObjectPickFlags.ObjectBox | MouseCursorObjectPickFlags.ObjectSprite);
            MouseCursor.Instance.PushObjectMask(_rayHitMask);
            MouseCursorRayHit cursorRayHit = MouseCursor.Instance.GetRayHit();

            MouseCursor.Instance.PopObjectPickMaskFlags();
            MouseCursor.Instance.PopObjectMask();

            return(cursorRayHit);
        }
Exemple #12
0
        public static List <GameObject> ReplaceSelectedObjectsPrefabOnMouseClick()
        {
            MouseCursorRayHit cursorRayHit = ObjectSelection.Get().GetObjectPickedByCursor();

            if (cursorRayHit.WasAnObjectHit && !ObjectSelection.Get().IsGameObjectSelected(cursorRayHit.ClosestObjectRayHit.HitObject))
            {
                GameObject hitObject = cursorRayHit.ClosestObjectRayHit.HitObject;
                hitObject = Octave3DWorldBuilder.ActiveInstance.GetRoot(hitObject);
                if (hitObject == null)
                {
                    return(new List <GameObject>());
                }

                GameObject newPrefabForSelectedObjects = hitObject.GetSourcePrefab();
                if (newPrefabForSelectedObjects == null)
                {
                    List <GameObject> allSelectedObjects = ObjectSelection.Get().GetAllSelectedGameObjects();
                    ObjectSelection.Get().RemoveGameObjectCollectionFromSelection(allSelectedObjects);

                    List <GameObject> selectedRoots = Octave3DWorldBuilder.ActiveInstance.GetRoots(allSelectedObjects);
                    var newObjects = new List <GameObject>();
                    foreach (var root in selectedRoots)
                    {
                        Transform  rootTransform = root.transform;
                        GameObject newObject     = GameObjectExtensions.CloneAsWorkingObject(hitObject, hitObject.transform.parent, true);
                        if (newObject != null)
                        {
                            Transform objectTransform = newObject.transform;
                            objectTransform.position = rootTransform.position;
                            objectTransform.rotation = rootTransform.rotation;
                            objectTransform.SetWorldScale(rootTransform.lossyScale);
                            newObject.SetSelectedHierarchyWireframeHidden(ObjectPlacementSettings.Get().HideWireframeWhenPlacingObjects);

                            UndoEx.DestroyObjectImmediate(root);
                            newObjects.Add(newObject);
                        }
                    }

                    return(newObjects);
                }
                else
                {
                    List <GameObject> allSelectedObjects = ObjectSelection.Get().GetAllSelectedGameObjects();
                    ObjectSelection.Get().RemoveGameObjectCollectionFromSelection(allSelectedObjects);
                    List <GameObject> newObjects = ObjectActions.ReplaceGameObjectHierarchyCollectionPrefab(allSelectedObjects, newPrefabForSelectedObjects);
                    newObjects.RemoveAll(item => item == null);

                    return(newObjects);
                }
            }

            return(new List <GameObject>());
        }
        private MouseCursorRayHit GetCursorRayHit()
        {
            if (_sourceObjects == null || _state == ObjectVertexSnapSessionState.SnapToDestination)
            {
                MouseCursor.Instance.PushObjectMask(_objectMask);
                MouseCursor.Instance.PushObjectPickMaskFlags(MouseCursorObjectPickFlags.ObjectBox | MouseCursorObjectPickFlags.ObjectTerrain);
                MouseCursorRayHit cursorRayHit = MouseCursor.Instance.GetRayHit();
                MouseCursor.Instance.PopObjectPickMaskFlags();
                MouseCursor.Instance.PopObjectMask();

                return(cursorRayHit);
            }
            else
            {
                return(MouseCursor.Instance.GetRayHitForMeshAndSpriteObjects(_sourceObjects));
            }
        }
        public override void Perform()
        {
            if (Event.current.InvolvesLeftMouseButton())
            {
                Event.current.DisableInSceneView();

                MouseCursorRayHit cursorRayHit = ObjectSelection.Get().GetObjectPickedByCursor();
                if (cursorRayHit.WasAnObjectHit)
                {
                    GameObject closestHitObject = cursorRayHit.SortedObjectRayHits[0].HitObject;
                    if (cursorRayHit.WasLightObjectHit || cursorRayHit.WasParticleSystemHit)
                    {
                        closestHitObject = cursorRayHit.GetClosestHitParticleSystemOrLightObject();
                    }

                    // Note: If append mode is enabled, we will toggle the selected state of the picked object. Otherwise,
                    //       we will clear the current selection and select only the picked game object.
                    if (AllShortcutCombos.Instance.EnableAppendObjectsToSelection.IsActive())
                    {
                        ToggleObjectSelectedState(closestHitObject);
                    }
                    else
                    {
                        ClearSelectionAndSelectObject(closestHitObject);
                    }

                    if (Event.current.clickCount == 2)
                    {
                        UndoEx.RecordForToolAction(ObjectSelection.Get());
                        ObjectSelectionActions.SelectAllObjectsWithSamePrefabAsObject(closestHitObject);
                        ObjectSelection.Get().ObjectSelectionGizmos.OnObjectSelectionUpdatedUsingMouseClick();
                    }
                }
                else
                {
                    ClearSelectionWhenNoObjectPicked();
                }
            }
        }
        public void FromMouseCursorRayHit(MouseCursorRayHit cursorRayHit)
        {
            _isValid = false;

            if (!cursorRayHit.WasAnythingHit)
            {
                return;
            }

            if (cursorRayHit.WasAnObjectHit)
            {
                GameObjectRayHit objectRayHit = FindClosestHitObjectWhichCanBeUsedAsPaintSurface(cursorRayHit);
                if (objectRayHit != null && ValidateGameObjectRayHit(objectRayHit))
                {
                    ExtractData(objectRayHit);
                }
            }

            if (!_isValid && cursorRayHit.WasACellHit)
            {
                ExtractData(cursorRayHit.GridCellRayHit);
            }
        }
Exemple #16
0
        private void CreateSurfaceColliderProjector()
        {
            MouseCursorRayHit brushCursorRayHit = _workingBrushCircle.CursorRayHit;

            if (brushCursorRayHit.WasAnObjectHit)
            {
                GameObjectRayHit objectRayHit = brushCursorRayHit.ClosestObjectRayHit;
                if (objectRayHit.WasMeshHit)
                {
                    _surfaceColliderProjector = new PointsOnColliderProjector(objectRayHit.ObjectMeshHit.HitCollider, _workingBrushCircle.Plane);
                }
                else
                if (objectRayHit.WasTerrainHit)
                {
                    _surfaceColliderProjector = new PointsOnColliderProjector(objectRayHit.ObjectTerrainHit.HitCollider, _workingBrushCircle.Plane);
                }
                else
                if (objectRayHit.WasSpriteHit)
                {
                    _surfaceColliderProjector = new PointsOnColliderProjector(objectRayHit.ObjectSpriteHit.HitCollider, _workingBrushCircle.Plane);
                }
            }
        }
        private GameObjectRayHit FindClosestHitObjectWhichCanBeUsedAsPaintSurface(MouseCursorRayHit cursorRayHit)
        {
            ObjectMask decorPaintMask = DecorPaintObjectPlacement.Get().DecorPaintMask;

            List <GameObjectRayHit> gameObjectHits = cursorRayHit.SortedObjectRayHits;

            for (int hitIndex = 0; hitIndex < gameObjectHits.Count; ++hitIndex)
            {
                GameObject hitObject = gameObjectHits[hitIndex].HitObject;

                if (!hitObject.HasMesh() && !hitObject.HasTerrain() && !hitObject.IsSprite())
                {
                    continue;
                }
                if (decorPaintMask.IsGameObjectMasked(hitObject))
                {
                    continue;
                }

                return(gameObjectHits[hitIndex]);
            }

            return(null);
        }
Exemple #18
0
        private GameObjectRayHit FindClosestHitObjectWhichCanBeUsedAsSnapSurface(MouseCursorRayHit cursorRayHit)
        {
            ObjectMask objectSnapMask = ObjectSnapping.Get().ObjectSnapMask;

            List <GameObjectRayHit> gameObjectHits = cursorRayHit.SortedObjectRayHits;

            for (int hitIndex = 0; hitIndex < gameObjectHits.Count; ++hitIndex)
            {
                GameObject hitObject = gameObjectHits[hitIndex].HitObject;

                if (!hitObject.HasMesh())
                {
                    continue;
                }
                if (objectSnapMask.IsGameObjectMasked(hitObject))
                {
                    continue;
                }

                return(gameObjectHits[hitIndex]);
            }

            return(null);
        }
Exemple #19
0
 private void AcquireMouseCursorRayHit()
 {
     MouseCursor.Instance.PushObjectPickMaskFlags(MouseCursorObjectPickFlags.ObjectMesh | MouseCursorObjectPickFlags.ObjectTerrain);
     _cursorRayHit = MouseCursor.Instance.GetRayHit();
     MouseCursor.Instance.PopObjectPickMaskFlags();
 }
 private void AcquireCursorRayHitForTerrainObject(GameObject terrainObject)
 {
     _cursorRayHit = MouseCursor.Instance.GetCursorRayHitForTerrainObject(terrainObject);
 }
        public void UpdateForMouseMovement()
        {
            if (!_isActive)
            {
                return;
            }

            if (MouseButtonStates.Instance.IsMouseButtonDown(MouseButton.Left))
            {
                _state = ObjectVertexSnapSessionState.SnapToDestination;
            }
            else
            {
                _state = ObjectVertexSnapSessionState.SelectSourceVertex;
            }

            if (_state == ObjectVertexSnapSessionState.SelectSourceVertex)
            {
                if (_sourceObjects == null || _sourceObjects.Count == 0)
                {
                    _objectMask.ObjectCollectionMask.UnmaskAll();
                    MouseCursorRayHit cursorRayHit = GetCursorRayHit();
                    if (cursorRayHit.WasAnObjectHit)
                    {
                        GameObjectRayHit objectRayHit = cursorRayHit.ClosestObjectRayHit;
                        MeshRayHit       meshRayHit   = objectRayHit.ObjectMeshHit;
                        if (meshRayHit != null)
                        {
                            Octave3DMesh octaveMesh = meshRayHit.HitMesh;

                            Triangle3D sourceTriangle = octaveMesh.GetTriangle(meshRayHit.HitTriangleIndex);
                            sourceTriangle.TransformPoints(objectRayHit.HitObject.transform.localToWorldMatrix);

                            _sourceVertex = sourceTriangle.GetPointClosestToPoint(meshRayHit.HitPoint);
                            _sourceObject = objectRayHit.HitObject;
                            _objectMask.ObjectCollectionMask.Mask(_sourceObject.transform.root.gameObject.GetAllChildrenIncludingSelf());
                        }
                        else
                        {
                            SpriteRenderer spriteRenderer = objectRayHit.HitObject.GetComponent <SpriteRenderer>();
                            if (spriteRenderer != null)
                            {
                                _sourceObject = objectRayHit.HitObject;
                                _sourceVertex = Vector3Extensions.GetClosestPointToPoint(objectRayHit.ObjectBoxHit.HitBox.GetCenterAndCornerPoints(), objectRayHit.HitPoint);
                                _objectMask.ObjectCollectionMask.Mask(_sourceObject.transform.root.gameObject.GetAllChildrenIncludingSelf());
                            }
                        }
                    }
                }
                else
                {
                    MouseCursorRayHit cursorRayHit = GetCursorRayHit();
                    if (cursorRayHit.WasAnObjectHit)
                    {
                        GameObjectRayHit objectRayHit = cursorRayHit.ClosestObjectRayHit;
                        MeshRayHit       meshRayHit   = objectRayHit.ObjectMeshHit;
                        if (meshRayHit != null)
                        {
                            Octave3DMesh octaveMesh = meshRayHit.HitMesh;

                            Triangle3D sourceTriangle = octaveMesh.GetTriangle(meshRayHit.HitTriangleIndex);
                            sourceTriangle.TransformPoints(objectRayHit.HitObject.transform.localToWorldMatrix);
                            _sourceVertex = sourceTriangle.GetPointClosestToPoint(meshRayHit.HitPoint);
                        }
                        else
                        {
                            SpriteRenderer spriteRenderer = objectRayHit.HitObject.GetComponent <SpriteRenderer>();
                            if (spriteRenderer != null)
                            {
                                _sourceVertex = Vector3Extensions.GetClosestPointToPoint(objectRayHit.ObjectBoxHit.HitBox.GetCenterAndCornerPoints(), objectRayHit.HitPoint);
                            }
                        }
                    }

                    foreach (var parent in _sourceParents)
                    {
                        _objectMask.ObjectCollectionMask.Mask(parent.GetAllChildrenIncludingSelf());
                    }
                }
            }
            else
            {
                MouseCursorRayHit cursorRayHit = GetCursorRayHit();
                if (cursorRayHit.WasAnythingHit)
                {
                    bool useGridCellHit = false;
                    if (!cursorRayHit.WasAnObjectHit)
                    {
                        useGridCellHit = true;
                    }
                    else
                    if (cursorRayHit.WasAnObjectHit && cursorRayHit.WasACellHit)
                    {
                        float gridCellHitEnter = cursorRayHit.GridCellRayHit.HitEnter;
                        float objectHitEnter   = cursorRayHit.ClosestObjectRayHit.HitEnter;
                        if (gridCellHitEnter < Mathf.Max(0.0f, (objectHitEnter - 1e-3f)))
                        {
                            useGridCellHit = true;
                        }
                    }

                    if (useGridCellHit)
                    {
                        XZGridCell       hitCell  = cursorRayHit.GridCellRayHit.HitCell;
                        XZOrientedQuad3D cellQuad = hitCell.Quad;

                        _destinationObject   = null;
                        _destinationGridCell = hitCell;

                        _snapPosition = cellQuad.GetPointClosestToPoint(cursorRayHit.GridCellRayHit.HitPoint, true);
                        SnapToDestination();
                    }
                    else
                    {
                        GameObjectRayHit objectRayHit = cursorRayHit.ClosestObjectRayHit;
                        MeshRayHit       meshRayHit   = objectRayHit.ObjectMeshHit;
                        if (meshRayHit != null)
                        {
                            _destinationObject = objectRayHit.HitObject;
                            Triangle3D destinationTriangle = meshRayHit.HitMesh.GetTriangle(meshRayHit.HitTriangleIndex);
                            destinationTriangle.TransformPoints(_destinationObject.transform.localToWorldMatrix);
                            _destinationGridCell = null;

                            _snapPosition = destinationTriangle.GetPointClosestToPoint(meshRayHit.HitPoint);
                            SnapToDestination();
                        }
                        else
                        {
                            SpriteRenderer spriteRenderer = objectRayHit.HitObject.GetComponent <SpriteRenderer>();
                            if (spriteRenderer != null)
                            {
                                _destinationGridCell = null;
                                _destinationObject   = objectRayHit.HitObject;

                                _snapPosition = Vector3Extensions.GetClosestPointToPoint(objectRayHit.ObjectBoxHit.HitBox.GetCenterAndCornerPoints(), objectRayHit.HitPoint);
                                SnapToDestination();
                            }
                        }
                    }
                }
            }
        }
        public void Update()
        {
            if (IsActive)
            {
                _currentCursorRayHit = GetCursorRayHit();
                if (!_currentCursorRayHit.WasAnythingHit)
                {
                    return;
                }

                if (_currentCursorRayHit.WasAnObjectHit)
                {
                    GameObjectExtensions.RecordObjectTransformsForUndo(_grabbedObjects);
                    GameObjectRayHit objectRayHit = _currentCursorRayHit.ClosestObjectRayHit;
                    _surfaceHitPoint = objectRayHit.HitPoint;
                    foreach (var grabbedObject in _grabbedObjects)
                    {
                        if (grabbedObject == null)
                        {
                            continue;
                        }

                        Transform objectTransform = grabbedObject.transform;
                        objectTransform.position = objectRayHit.HitPoint + _objectToPivotDir[grabbedObject];
                        if (objectRayHit.WasTerrainHit)
                        {
                            Ray ray = new Ray(grabbedObject.GetHierarchyWorldOrientedBox().Center + Vector3.up, -Vector3.up);
                            GameObjectRayHit sitPointHit = null;
                            if (objectRayHit.HitObject.RaycastTerrainReverseIfFail(ray, out sitPointHit))
                            {
                                if (_grabSettings.AlignAxis)
                                {
                                    AxisAlignment.AlignObjectAxis(grabbedObject, _grabSettings.AlignmentAxis, sitPointHit.HitNormal);
                                }
                                grabbedObject.PlaceHierarchyOnPlane(new Plane(sitPointHit.HitNormal, sitPointHit.HitPoint));
                                if (!_grabSettings.EmbedInSurfaceWhenNoAlign || _grabSettings.AlignAxis)
                                {
                                    objectTransform.position += _grabSettings.OffsetFromSurface * sitPointHit.HitNormal;
                                }
                                if (_grabSettings.EmbedInSurfaceWhenNoAlign && !_grabSettings.AlignAxis)
                                {
                                    grabbedObject.EmbedInSurfaceByVertex(-Vector3.up, objectRayHit.HitObject);
                                }
                            }
                        }
                        else
                        if (objectRayHit.WasMeshHit)
                        {
                            Ray ray = new Ray(grabbedObject.GetHierarchyWorldOrientedBox().Center + objectRayHit.HitNormal * 2.0f, -objectRayHit.HitNormal);
                            GameObjectRayHit sitPointHit = null;
                            if (objectRayHit.HitObject.RaycastMeshReverseIfFail(ray, out sitPointHit))
                            {
                                if (_grabSettings.AlignAxis)
                                {
                                    AxisAlignment.AlignObjectAxis(grabbedObject, _grabSettings.AlignmentAxis, sitPointHit.HitNormal);
                                }
                                grabbedObject.PlaceHierarchyOnPlane(new Plane(sitPointHit.HitNormal, sitPointHit.HitPoint));
                                if (!_grabSettings.EmbedInSurfaceWhenNoAlign || _grabSettings.AlignAxis)
                                {
                                    objectTransform.position += _grabSettings.OffsetFromSurface * sitPointHit.HitNormal;
                                }
                                if (_grabSettings.EmbedInSurfaceWhenNoAlign && !_grabSettings.AlignAxis)
                                {
                                    grabbedObject.EmbedInSurfaceByVertex(-sitPointHit.HitNormal, objectRayHit.HitObject);
                                }
                            }
                        }
                    }
                }
                else
                if (_currentCursorRayHit.WasACellHit)
                {
                    GameObjectExtensions.RecordObjectTransformsForUndo(_grabbedObjects);
                    GridCellRayHit cellRayHit = _currentCursorRayHit.GridCellRayHit;
                    _surfaceHitPoint = cellRayHit.HitPoint;

                    foreach (var grabbedObject in _grabbedObjects)
                    {
                        if (grabbedObject == null)
                        {
                            continue;
                        }

                        Transform objectTransform = grabbedObject.transform;
                        objectTransform.position = cellRayHit.HitPoint + _objectToPivotDir[grabbedObject];

                        if (_grabSettings.AlignAxis)
                        {
                            AxisAlignment.AlignObjectAxis(grabbedObject, _grabSettings.AlignmentAxis, cellRayHit.HitNormal);
                        }
                        grabbedObject.PlaceHierarchyOnPlane(new Plane(cellRayHit.HitNormal, cellRayHit.HitPoint));
                        if (!_grabSettings.EmbedInSurfaceWhenNoAlign || _grabSettings.AlignAxis)
                        {
                            objectTransform.position += _grabSettings.OffsetFromSurface * cellRayHit.HitNormal;
                        }
                    }
                }
            }
        }
        public void Update()
        {
            if (IsActive)
            {
                if (AllShortcutCombos.Instance.GrabRotateSelection.IsActive())
                {
                    _state = State.Rotating;
                }
                else if (AllShortcutCombos.Instance.GrabScaleSelection.IsActive())
                {
                    if (_state != State.Scaling)
                    {
                        _objectToWorldScale.Clear();
                        foreach (var grabbedObject in _grabbedObjects)
                        {
                            if (grabbedObject != null)
                            {
                                _objectToWorldScale.Add(grabbedObject, grabbedObject.transform.lossyScale);
                            }
                        }
                        _cursorPosAtScaleBegin = MouseCursor.Instance.Position;
                        _state = State.Scaling;
                    }
                }
                else
                {
                    // Need to reset the anchor relationships because the cursor was moved without
                    // the objects following it.
                    if (_state == State.Rotating || _state == State.Scaling)
                    {
                        _objectToPivotDir.Clear();
                        foreach (var grabbedObject in _grabbedObjects)
                        {
                            if (grabbedObject != null)
                            {
                                _objectToPivotDir.Add(grabbedObject, grabbedObject.transform.position - _surfaceHitPoint);
                            }
                        }
                    }
                    _state = State.Moving;
                }

                _currentCursorRayHit = GetCursorRayHit();
                if (!_currentCursorRayHit.WasAnythingHit)
                {
                    return;
                }

                if (_currentCursorRayHit.WasAnythingHit)
                {
                    if (_currentCursorRayHit.WasAnObjectHit)
                    {
                        _surfaceHitPoint = _currentCursorRayHit.ClosestObjectRayHit.HitPoint;
                    }
                    else
                    {
                        _surfaceHitPoint = _currentCursorRayHit.GridCellRayHit.HitPoint;
                    }
                }

                if (_state == State.Moving || _objectToSurfaceInfo.Count == 0)
                {
                    if (_currentCursorRayHit.WasAnObjectHit)
                    {
                        GameObjectExtensions.RecordObjectTransformsForUndo(_grabbedObjects);
                        GameObjectRayHit objectRayHit = _currentCursorRayHit.ClosestObjectRayHit;
                        foreach (var grabbedObject in _grabbedObjects)
                        {
                            if (grabbedObject == null)
                            {
                                continue;
                            }

                            Transform objectTransform = grabbedObject.transform;
                            objectTransform.position = objectRayHit.HitPoint + _objectToPivotDir[grabbedObject];
                            if (objectRayHit.WasTerrainHit)
                            {
                                Ray ray = new Ray(grabbedObject.GetHierarchyWorldOrientedBox().Center + Vector3.up, -Vector3.up);
                                GameObjectRayHit sitPointHit = null;
                                if (objectRayHit.HitObject.RaycastTerrainReverseIfFail(ray, out sitPointHit))
                                {
                                    Plane surfacePlane = new Plane(sitPointHit.HitNormal, sitPointHit.HitPoint);
                                    if (_grabSettings.AlignAxis)
                                    {
                                        AxisAlignment.AlignObjectAxis(grabbedObject, _grabSettings.AlignmentAxis, sitPointHit.HitNormal);
                                    }
                                    grabbedObject.PlaceHierarchyOnPlane(surfacePlane);
                                    if (!_grabSettings.EmbedInSurfaceWhenNoAlign || _grabSettings.AlignAxis)
                                    {
                                        objectTransform.position += _grabSettings.OffsetFromSurface * sitPointHit.HitNormal;
                                    }
                                    if (_grabSettings.EmbedInSurfaceWhenNoAlign && !_grabSettings.AlignAxis)
                                    {
                                        grabbedObject.EmbedInSurfaceByVertex(-Vector3.up, objectRayHit.HitObject);
                                    }

                                    ObjectSurfaceInfo surfaceInfo = new ObjectSurfaceInfo();
                                    surfaceInfo.SurfacePlane = surfacePlane;
                                    surfaceInfo.SitPoint     = sitPointHit.HitPoint;
                                    SetObjectSurfaceInfo(grabbedObject, surfaceInfo);
                                }
                            }
                            else
                            if (objectRayHit.WasMeshHit)
                            {
                                Ray ray = new Ray(grabbedObject.GetHierarchyWorldOrientedBox().Center + objectRayHit.HitNormal * 2.0f, -objectRayHit.HitNormal);
                                GameObjectRayHit sitPointHit = null;
                                if (objectRayHit.HitObject.RaycastMeshReverseIfFail(ray, out sitPointHit))
                                {
                                    Plane surfacePlane = new Plane(sitPointHit.HitNormal, sitPointHit.HitPoint);
                                    if (_grabSettings.AlignAxis)
                                    {
                                        AxisAlignment.AlignObjectAxis(grabbedObject, _grabSettings.AlignmentAxis, sitPointHit.HitNormal);
                                    }
                                    grabbedObject.PlaceHierarchyOnPlane(surfacePlane);
                                    if (!_grabSettings.EmbedInSurfaceWhenNoAlign || _grabSettings.AlignAxis)
                                    {
                                        objectTransform.position += _grabSettings.OffsetFromSurface * sitPointHit.HitNormal;
                                    }
                                    if (_grabSettings.EmbedInSurfaceWhenNoAlign && !_grabSettings.AlignAxis)
                                    {
                                        grabbedObject.EmbedInSurfaceByVertex(-sitPointHit.HitNormal, objectRayHit.HitObject);
                                    }

                                    ObjectSurfaceInfo surfaceInfo = new ObjectSurfaceInfo();
                                    surfaceInfo.SurfacePlane = surfacePlane;
                                    surfaceInfo.SitPoint     = sitPointHit.HitPoint;
                                    SetObjectSurfaceInfo(grabbedObject, surfaceInfo);
                                }
                            }
                        }
                    }
                    else
                    if (_currentCursorRayHit.WasACellHit)
                    {
                        GameObjectExtensions.RecordObjectTransformsForUndo(_grabbedObjects);
                        GridCellRayHit cellRayHit = _currentCursorRayHit.GridCellRayHit;
                        foreach (var grabbedObject in _grabbedObjects)
                        {
                            if (grabbedObject == null)
                            {
                                continue;
                            }

                            Transform objectTransform = grabbedObject.transform;
                            objectTransform.position = cellRayHit.HitPoint + _objectToPivotDir[grabbedObject];

                            Plane   surfacePlane = new Plane(cellRayHit.HitNormal, cellRayHit.HitPoint);
                            Vector3 sitPoint     = surfacePlane.ProjectPoint(grabbedObject.GetWorldOrientedBox().Center);

                            if (_grabSettings.AlignAxis)
                            {
                                AxisAlignment.AlignObjectAxis(grabbedObject, _grabSettings.AlignmentAxis, cellRayHit.HitNormal);
                            }
                            grabbedObject.PlaceHierarchyOnPlane(surfacePlane);
                            if (!_grabSettings.EmbedInSurfaceWhenNoAlign || _grabSettings.AlignAxis)
                            {
                                objectTransform.position += _grabSettings.OffsetFromSurface * cellRayHit.HitNormal;
                            }

                            ObjectSurfaceInfo surfaceInfo = new ObjectSurfaceInfo();
                            surfaceInfo.SurfacePlane = surfacePlane;
                            surfaceInfo.SitPoint     = sitPoint;
                            SetObjectSurfaceInfo(grabbedObject, surfaceInfo);
                        }
                    }
                }
                else
                if (_state == State.Rotating)
                {
                    GameObjectExtensions.RecordObjectTransformsForUndo(_grabbedObjects);
                    float rotationSensitivity = _grabSettings.RotationSensitivity;
                    foreach (var grabbedObject in _grabbedObjects)
                    {
                        if (!_objectToSurfaceInfo.ContainsKey(grabbedObject))
                        {
                            continue;
                        }

                        var         surfaceInfo = _objectToSurfaceInfo[grabbedObject];
                        OrientedBox worldOOBB   = grabbedObject.GetHierarchyWorldOrientedBox();
                        grabbedObject.RotateHierarchyBoxAroundPoint(MouseCursor.Instance.OffsetSinceLastMouseMove.x * rotationSensitivity, _grabSettings.AlignAxis ? surfaceInfo.SurfacePlane.normal : Vector3.up, worldOOBB.Center);
                    }
                }
                else
                if (_state == State.Scaling)
                {
                    GameObjectExtensions.RecordObjectTransformsForUndo(_grabbedObjects);
                    Vector2 currentCursorPos           = MouseCursor.Instance.Position;
                    Vector2 cursorOffsetFromScaleBegin = currentCursorPos - _cursorPosAtScaleBegin;

                    float scaleFactor = 1.0f + _grabSettings.ScaleSensitivity * cursorOffsetFromScaleBegin.x;
                    foreach (var grabbedObject in _grabbedObjects)
                    {
                        if (!_objectToSurfaceInfo.ContainsKey(grabbedObject) ||
                            !_objectToWorldScale.ContainsKey(grabbedObject))
                        {
                            continue;
                        }

                        var surfaceInfo = _objectToSurfaceInfo[grabbedObject];
                        grabbedObject.SetHierarchyWorldScaleByPivotPoint(_objectToWorldScale[grabbedObject] * scaleFactor, surfaceInfo.SitPoint);
                    }
                }
            }
        }
Exemple #24
0
        public void UpdateForMouseMovement()
        {
            if (_state == State.Inactive)
            {
                return;
            }

            if (MouseButtonStates.Instance.IsMouseButtonDown(MouseButton.Left))
            {
                _state = State.Snap;
            }
            else
            {
                _state = State.SelectPivot;
            }

            if (_state == State.SelectPivot && _selectedParents.Count != 0)
            {
                Camera  camera   = SceneViewCamera.Camera;
                Vector2 mousePos = Event.current.InvMousePos(camera);

                _isPivotAvailable = false;
                float minDistanceSq = float.MaxValue;
                foreach (var parent in _selectedParents)
                {
                    if (parent == null)
                    {
                        continue;
                    }

                    OrientedBox worldOOBB = parent.GetHierarchyWorldOrientedBox();
                    if (worldOOBB.IsValid())
                    {
                        List <Vector3> centerAndCorners = worldOOBB.GetCenterAndCornerPoints();
                        List <Vector2> oobbScreenPts    = Vector2Extensions.GetScreenPoints(centerAndCorners, camera);

                        for (int ptIndex = 0; ptIndex < centerAndCorners.Count; ++ptIndex)
                        {
                            Vector3 worldPt  = centerAndCorners[ptIndex];
                            Vector2 screenPt = oobbScreenPts[ptIndex];
                            float   distSq   = (mousePos - screenPt).sqrMagnitude;
                            if (distSq < minDistanceSq)
                            {
                                minDistanceSq     = distSq;
                                _pivot            = worldPt;
                                _isPivotAvailable = true;
                            }
                        }
                    }
                }
            }
            else
            if (_state == State.Snap && _isPivotAvailable)
            {
                GameObjectExtensions.RecordObjectTransformsForUndo(_selectedParents);
                MouseCursorRayHit cursorHit = MouseCursor.Instance.GetCursorRayHitForGridCell();
                if (cursorHit.WasACellHit)
                {
                    Camera         camera          = SceneViewCamera.Camera;
                    Vector2        mousePos        = Event.current.InvMousePos(camera);
                    GridCellRayHit cellRayHit      = cursorHit.GridCellRayHit;
                    Vector3        snapDestination = Vector3Extensions.GetClosestPointToPoint(cellRayHit.HitCell.Quad.GetCenterAndCornerPoints(), cellRayHit.HitPoint);

                    Vector3 moveVector = snapDestination - _pivot;
                    foreach (var parent in _selectedParents)
                    {
                        if (parent != null)
                        {
                            parent.transform.position += moveVector;
                        }
                    }

                    _pivot = snapDestination;

                    ObjectSelection.Get().ObjectSelectionTransformGizmoSystem.OnObjectSelectionUpdated();
                }
            }
        }
Exemple #25
0
        public void UpdateOnMouseMove()
        {
            if (_isActive)
            {
                MouseCursorRayHit cursorRayHit = GetCursorRayHit();
                if (cursorRayHit == null || !cursorRayHit.WasAnythingHit)
                {
                    return;
                }

                Vector3 hitPoint  = Vector3.zero;
                Vector3 hitNormal = Vector3.zero;

                if (!cursorRayHit.WasAnObjectHit && cursorRayHit.WasACellHit)
                {
                    hitPoint  = cursorRayHit.GridCellRayHit.HitPoint;
                    hitNormal = cursorRayHit.GridCellRayHit.HitNormal;
                }
                else
                if (cursorRayHit.WasAnObjectHit && !cursorRayHit.WasACellHit)
                {
                    hitPoint  = cursorRayHit.ClosestObjectRayHit.HitPoint;
                    hitNormal = cursorRayHit.ClosestObjectRayHit.HitNormal;
                }
                else
                if (cursorRayHit.WasAnObjectHit && cursorRayHit.WasACellHit)
                {
                    if (cursorRayHit.ClosestObjectRayHit.HitEnter < cursorRayHit.GridCellRayHit.HitEnter)
                    {
                        hitPoint  = cursorRayHit.ClosestObjectRayHit.HitPoint;
                        hitNormal = cursorRayHit.ClosestObjectRayHit.HitNormal;
                    }
                    else
                    {
                        hitPoint  = cursorRayHit.GridCellRayHit.HitPoint;
                        hitNormal = cursorRayHit.GridCellRayHit.HitNormal;
                    }
                }

                Plane hitPlane           = new Plane(hitNormal, hitPoint);
                Box   selectionWorldAABB = ObjectSelection.Get().GetWorldBox();
                if (selectionWorldAABB.IsInvalid())
                {
                    return;
                }

                Vector3 oldCenter = selectionWorldAABB.Center;
                selectionWorldAABB.Center = hitPoint;
                selectionWorldAABB.MoveInFrontOfPlane(hitPlane);
                Vector3 moveVector = selectionWorldAABB.Center - oldCenter;

                float snapEps = ObjectSelection.Get().Settings.Object2ObjectSnapSettings.SnapEps;

                GameObjectExtensions.RecordObjectTransformsForUndo(_selectedParents);
                foreach (var parent in _selectedParents)
                {
                    parent.transform.position += moveVector;
                }

                var ignoreObjects = new List <GameObject>(_allSelectedObjects);
                ignoreObjects.AddRange(ObjectSnapping.Get().ObjectSnapMask.ObjectCollectionMask.GetAllMaskedGameObjects());
                Object2ObjectSnap.Snap(_selectedParents, snapEps, ignoreObjects);
            }
        }