private Vector4 GenerateRotation()
        {
            float      xAngle = m_randomXAngle ? Random.Range(0f, 1f) * 360f : 0;
            float      yAngle = m_randomYAngle ? Random.Range(0f, 1f) * 360f : 0;
            float      zAngle = m_randomZAngle ? Random.Range(0f, 1f) * 360f : 0;
            Quaternion temp   = Quaternion.Euler(xAngle, yAngle, zAngle);

            return(InstancingUtilities.VectorFromQuaternion(temp));
        }
Example #2
0
        private static void DrawHandle()
        {
            Tools.hidden = false;
            if (m_selectedInstancedAssets.Count > 0)
            {
                Tools.hidden = true;

                Vector2 lastSelectedInstance            = m_lastSelectedInstancedObject;
                IndirectInstancedAsset asset            = m_registeredInstancedAssets[(int)lastSelectedInstance.x];
                Vector3    lastSelectedInstancePosition = asset.m_instanceData.m_positions[(int)lastSelectedInstance.y];
                Vector4    vec4Rotation = asset.m_instanceData.m_rotations[(int)lastSelectedInstance.y];
                Quaternion lastSelectedInstanceRotation = InstancingUtilities.QuaternionFromVector(vec4Rotation);

                if (Tools.pivotMode == PivotMode.Center)
                {
                    Bounds selectedObjectsBounds = new Bounds(lastSelectedInstancePosition, Vector3.zero);
                    for (int i = 0; i < m_selectedInstancedAssets.Count; i++)
                    {
                        lastSelectedInstance = m_selectedInstancedAssets[i];
                        asset = m_registeredInstancedAssets[(int)lastSelectedInstance.x];
                        lastSelectedInstancePosition = asset.m_instanceData.m_positions[(int)lastSelectedInstance.y];
                        selectedObjectsBounds.Encapsulate(lastSelectedInstancePosition);
                    }
                    for (int i = 0; i < Selection.transforms.Length; i++)
                    {
                        selectedObjectsBounds.Encapsulate(Selection.transforms[i].position);
                    }
                    lastSelectedInstancePosition = selectedObjectsBounds.center;
                }

                float handleSize = HandleUtility.GetHandleSize(lastSelectedInstancePosition);

                Quaternion previousRotation;
                if (Tools.pivotRotation == PivotRotation.Global)
                {
                    previousRotation = Tools.handleRotation;
                }
                else
                {
                    previousRotation = lastSelectedInstanceRotation;
                }

                EditorGUI.BeginChangeCheck();
                Transform[] selectedGameObjects = Selection.transforms;
                switch (Tools.current)
                {
                case Tool.Move:

                    if (m_currentEvent.type == EventType.MouseDown)
                    {
                        Debug.Log("Rebuilding initial lists.");
                        //build a list of initial sizes of selected instances
                        m_selectedInitialInstances.Clear();
                        for (int i = 0; i < m_selectedInstancedAssets.Count; i++)
                        {
                            Debug.Log("adding");
                            Vector2 currentInstance = m_selectedInstancedAssets[i];
                            Vector3 position        = m_registeredInstancedAssets[(int)currentInstance.x].m_instanceData.m_positions[(int)currentInstance.y];
                            m_selectedInitialInstances.Add(position);
                            Debug.Log("Count is: " + m_selectedInitialInstances.Count);
                        }

                        //build a list of initial sizes of selected game objects
                        m_selectedInitialGameObjects.Clear();
                        for (int i = 0; i < selectedGameObjects.Length; i++)
                        {
                            Vector3 position = selectedGameObjects[i].localPosition;
                            m_selectedInitialGameObjects.Add(position);
                        }
                    }

                    Vector3 newPosition;
                    if (Tools.pivotRotation == PivotRotation.Global)
                    {
                        newPosition = Handles.PositionHandle(lastSelectedInstancePosition, Quaternion.identity);
                    }
                    else
                    {
                        newPosition = Handles.PositionHandle(lastSelectedInstancePosition, lastSelectedInstanceRotation);
                    }

                    float xAxisMovement = newPosition.x - lastSelectedInstancePosition.x;
                    float yAxisMovement = newPosition.y - lastSelectedInstancePosition.y;
                    float zAxisMovement = newPosition.z - lastSelectedInstancePosition.z;

                    if (EditorGUI.EndChangeCheck())
                    {
                        UndoStateInstance.m_instance.m_lastUsedTool = Tool.Move;
                        //set undo state here, else last used tool will change on mouse down and cause incorrect undo history based on tool swapping
                        UndoStateInstance.m_instance.SetUndoState(m_selectedInitialInstances, m_selectedInstancedAssets);

                        for (int i = 0; i < m_selectedInstancedAssets.Count; i++)
                        {
                            Vector2 currentInstance             = m_selectedInstancedAssets[i];
                            IndirectInstancedAsset currentAsset = m_registeredInstancedAssets[(int)currentInstance.x];

                            Undo.RegisterCompleteObjectUndo(UndoStateInstance.m_instance, "Undo instanced move");
                            UndoStateInstance.m_instance.IncrementModifiedCounter();

                            if (Mathf.Abs(xAxisMovement) > 0.01f)
                            {
                                currentAsset.m_instanceData.m_positions[(int)currentInstance.y].x += xAxisMovement;
                            }
                            if (Mathf.Abs(yAxisMovement) > 0.01f)
                            {
                                currentAsset.m_instanceData.m_positions[(int)currentInstance.y].y += yAxisMovement;
                            }
                            if (Mathf.Abs(zAxisMovement) > 0.01f)
                            {
                                currentAsset.m_instanceData.m_positions[(int)currentInstance.y].z += zAxisMovement;
                            }

                            if (Mathf.Abs(xAxisMovement) > 0.01f || Mathf.Abs(yAxisMovement) > 0.01f || Mathf.Abs(zAxisMovement) > 0.01f)
                            {
                                currentAsset.AddDirtyPosition((int)currentInstance.y);
                            }
                        }

                        Undo.RecordObjects(selectedGameObjects, "Undo game object move");
                        for (int i = 0; i < selectedGameObjects.Length; i++)
                        {
                            Vector3 displacedPosition = selectedGameObjects[i].position;
                            if (Mathf.Abs(xAxisMovement) > 0.01f)
                            {
                                displacedPosition.x += xAxisMovement;
                            }
                            if (Mathf.Abs(yAxisMovement) > 0.01f)
                            {
                                displacedPosition.y += yAxisMovement;
                            }
                            if (Mathf.Abs(zAxisMovement) > 0.01f)
                            {
                                displacedPosition.z += zAxisMovement;
                            }

                            selectedGameObjects[i].position = displacedPosition;
                        }
                    }
                    break;

                case Tool.Rotate:

                    //allows rotation back to (0,0,0) with shift + n
                    if (m_currentEvent.keyCode == KeyCode.N && m_currentEvent.shift)
                    {
                        for (int i = 0; i < m_selectedInstancedAssets.Count; i++)
                        {
                            Vector2 currentInstance             = m_selectedInstancedAssets[i];
                            IndirectInstancedAsset currentAsset = m_registeredInstancedAssets[(int)currentInstance.x];
                            currentAsset.m_instanceData.m_rotations[(int)currentInstance.y] = new Vector4(0, 0, 0, 1);     //The identity quaternion
                            currentAsset.AddDirtyRotation((int)currentInstance.y);
                        }
                        //also reset the gizmo, this isnt really needed as the next frame will already be correct and 1 frame is not noticeable, but added for consistency
                        Tools.handleRotation = Quaternion.identity;
                        previousRotation     = Quaternion.identity;
                    }

                    if (m_currentEvent.type == EventType.MouseDown)
                    {
                        Debug.Log("Rebuilding initial lists.");
                        //build a list of initial sizes of selected instances
                        m_selectedInitialInstances.Clear();
                        for (int i = 0; i < m_selectedInstancedAssets.Count; i++)
                        {
                            Vector2 currentInstance = m_selectedInstancedAssets[i];
                            Vector4 rotation        = m_registeredInstancedAssets[(int)currentInstance.x].m_instanceData.m_rotations[(int)currentInstance.y];
                            m_selectedInitialInstances.Add(rotation);
                        }

                        //build a list of initial sizes of selected game objects
                        m_selectedInitialGameObjects.Clear();
                        for (int i = 0; i < selectedGameObjects.Length; i++)
                        {
                            Vector4 rotation = InstancingUtilities.VectorFromQuaternion(selectedGameObjects[i].rotation);
                            m_selectedInitialGameObjects.Add(rotation);
                        }
                    }

                    Quaternion newRotation = Handles.RotationHandle(previousRotation, lastSelectedInstancePosition);     //The new rotation value modified by the user's interaction with the handle.
                    if (EditorGUI.EndChangeCheck())
                    {
                        UndoStateInstance.m_instance.m_lastUsedTool = Tool.Rotate;
                        UndoStateInstance.m_instance.SetUndoState(m_selectedInitialInstances, m_selectedInstancedAssets);

                        Quaternion delta = Quaternion.Inverse(previousRotation) * newRotation;
                        for (int i = 0; i < m_selectedInstancedAssets.Count; i++)
                        {
                            Vector2 currentInstance                        = m_selectedInstancedAssets[i];
                            IndirectInstancedAsset currentAsset            = m_registeredInstancedAssets[(int)currentInstance.x];
                            Quaternion             instancedObjectRotation = InstancingUtilities.QuaternionFromVector(currentAsset.m_instanceData.m_rotations[(int)currentInstance.y]);

                            Undo.RegisterCompleteObjectUndo(UndoStateInstance.m_instance, "Undo instanced rotation");
                            UndoStateInstance.m_instance.IncrementModifiedCounter();

                            //lhs happens first, so if we want a local space rotation, we do a delta rotation in the objects frame of reference by object quaternion on lhs
                            //if we want a world space rotation, then lhs is the delta, and the objects rotation happens after, in the world space frame of reference
                            if (Tools.pivotRotation == PivotRotation.Local)
                            {
                                instancedObjectRotation = instancedObjectRotation * delta;
                            }
                            else
                            {
                                instancedObjectRotation = delta * instancedObjectRotation;
                            }

                            currentAsset.m_instanceData.m_rotations[(int)currentInstance.y] = InstancingUtilities.VectorFromQuaternion(instancedObjectRotation);
                            currentAsset.AddDirtyRotation((int)currentInstance.y);
                        }

                        Undo.RecordObjects(selectedGameObjects, "Undo game object rotation");
                        for (int i = 0; i < selectedGameObjects.Length; i++)
                        {
                            Quaternion gameObjectRotation = selectedGameObjects[i].rotation;
                            if (Tools.pivotRotation == PivotRotation.Local)
                            {
                                gameObjectRotation = gameObjectRotation * delta;
                            }
                            else
                            {
                                gameObjectRotation = delta * gameObjectRotation;
                            }
                            selectedGameObjects[i].rotation = gameObjectRotation;
                        }

                        //https://math.stackexchange.com/questions/40164/how-do-you-rotate-a-vector-by-a-unit-quaternion
                        Tools.handleRotation = newRotation;
                    }
                    break;

                case Tool.Scale:

                    //allows scaling back to (1,1,1) with shift + n
                    if (m_currentEvent.keyCode == KeyCode.N && m_currentEvent.shift)
                    {
                        for (int i = 0; i < m_selectedInstancedAssets.Count; i++)
                        {
                            Vector2 currentInstance             = m_selectedInstancedAssets[i];
                            IndirectInstancedAsset currentAsset = m_registeredInstancedAssets[(int)currentInstance.x];
                            currentAsset.m_instanceData.m_positions[(int)currentInstance.y].w = InstancingUtilities.PackScaleVectorToFloat(Vector3.one);
                            currentAsset.AddDirtyScale((int)currentInstance.y);
                        }
                    }

                    if (m_currentEvent.type == EventType.MouseDown)
                    {
                        m_currentScale = Vector3.one;

                        Debug.Log("Rebuilding initial lists.");
                        //build a list of initial sizes of selected instances
                        m_selectedInitialInstances.Clear();
                        for (int i = 0; i < m_selectedInstancedAssets.Count; i++)
                        {
                            Vector2 currentInstance = m_selectedInstancedAssets[i];
                            float   scale           = m_registeredInstancedAssets[(int)currentInstance.x].m_instanceData.m_positions[(int)currentInstance.y].w;
                            m_selectedInitialInstances.Add(new Vector4(scale, 0, 0, 0));
                        }

                        //build a list of initial sizes of selected game objects
                        m_selectedInitialGameObjects.Clear();
                        for (int i = 0; i < selectedGameObjects.Length; i++)
                        {
                            Vector3 scale = selectedGameObjects[i].localScale;
                            m_selectedInitialGameObjects.Add(scale);
                        }
                    }

                    Vector3 newScale = Handles.ScaleHandle(m_currentScale, lastSelectedInstancePosition, lastSelectedInstanceRotation, handleSize);
                    if (EditorGUI.EndChangeCheck())
                    {
                        UndoStateInstance.m_instance.m_lastUsedTool = Tool.Scale;
                        UndoStateInstance.m_instance.SetUndoState(m_selectedInitialInstances, m_selectedInstancedAssets);

                        m_currentScale = newScale;
                        for (int i = 0; i < m_selectedInstancedAssets.Count; i++)
                        {
                            Vector2 currentInstance             = m_selectedInstancedAssets[i];
                            IndirectInstancedAsset currentAsset = m_registeredInstancedAssets[(int)currentInstance.x];

                            Undo.RegisterCompleteObjectUndo(UndoStateInstance.m_instance, "Undo instanced scaling");
                            UndoStateInstance.m_instance.IncrementModifiedCounter();

                            currentAsset.m_instanceData.ModifyScale((int)currentInstance.y, m_currentScale, m_selectedInitialInstances[i].x);

                            currentAsset.AddDirtyScale((int)currentInstance.y);
                        }

                        Undo.RecordObjects(selectedGameObjects, "Undo game object scaling");
                        for (int i = 0; i < selectedGameObjects.Length; i++)
                        {
                            Vector3 objectScale = InstancingUtilities.MultiplyVector3(m_selectedInitialGameObjects[i], m_currentScale);
                            selectedGameObjects[i].localScale = objectScale;
                        }
                    }

                    break;
                }
            }
        }