public override void OnSceneGUI()
        {
            if (!target)
            {
                return;
            }

            if (Tools.current != Tool.Custom || !ChiselEditGeneratorTool.IsActive())
            {
                OnDefaultSceneTools();
                return;
            }

            var generator = target as T;

            if (GUIUtility.hotControl == 0)
            {
                if (!OnGeneratorValidate(generator))
                {
                    if (validTargets.Contains(generator))
                    {
                        OnGeneratorDeselected(generator);
                        validTargets.Remove(generator);
                    }
                    return;
                }
                if (!validTargets.Contains(generator))
                {
                    OnGeneratorSelected(generator);
                    validTargets.Add(generator);
                }
            }

            var sceneView = SceneView.currentDrawingSceneView;

            var modelMatrix   = ChiselNodeHierarchyManager.FindModelTransformMatrixOfTransform(generator.hierarchyItem.Transform);
            var generatorNode = generator.TopNode;

            if (!generatorNode.Valid)
            {
                return;
            }

            // NOTE: could loop over multiple instances from here, once we support that
            {
                using (new UnityEditor.Handles.DrawingScope(UnityEditor.Handles.yAxisColor, modelMatrix * generatorNode.NodeToTreeSpaceMatrix))
                {
                    EditorGUI.BeginChangeCheck();
                    {
                        OnScene(sceneView, generator);
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        OnTargetModifiedInScene();
                    }
                }
            }
        }
Beispiel #2
0
        public static Bounds CalculateBounds(ChiselGeneratorComponent generator)
        {
            if (!generator.TopTreeNode.Valid)
            {
                return(ChiselHierarchyItem.EmptyBounds);
            }

            var modelMatrix = ChiselNodeHierarchyManager.FindModelTransformMatrixOfTransform(generator.hierarchyItem.Transform);
            var minMax      = new ChiselAABB {
            };
            var boundsCount = 0;

            s_FoundBrushes.Clear();
            ChiselGeneratedComponentManager.GetAllTreeBrushes(generator, s_FoundBrushes);
            foreach (var brush in s_FoundBrushes)
            {
                if (!brush.Valid)
                {
                    continue;
                }

                var transformation = modelMatrix * (Matrix4x4)brush.NodeToTreeSpaceMatrix;
                var childBounds    = brush.Bounds;
                var size           = childBounds.Max - childBounds.Min;
                var magnitude      = math.lengthsq(size);
                if (float.IsInfinity(magnitude) ||
                    float.IsNaN(magnitude))
                {
                    var center   = ((float4)transformation.GetColumn(3)).xyz;
                    var halfSize = size * 0.5f;
                    childBounds = new ChiselAABB {
                        Min = center - halfSize, Max = center + halfSize
                    };
                }
                if (magnitude != 0)
                {
                    if (boundsCount == 0)
                    {
                        minMax = childBounds;
                    }
                    else
                    {
                        minMax.Encapsulate(childBounds);
                    }
                    boundsCount++;
                }
            }
            if (boundsCount == 0)
            {
                return(ChiselHierarchyItem.EmptyBounds);
            }
            var bounds = new Bounds();

            bounds.SetMinMax(minMax.Min, minMax.Max);
            return(bounds);
        }
        public override Bounds CalculateBounds(Matrix4x4 boundsTransformation)
        {
            if (!brushContainerAsset)
            {
                return(ChiselHierarchyItem.EmptyBounds);
            }

            var modelMatrix = ChiselNodeHierarchyManager.FindModelTransformMatrixOfTransform(hierarchyItem.Transform);
            var bounds      = ChiselHierarchyItem.EmptyBounds;

            var foundBrushes = new HashSet <CSGTreeBrush>();

            GetAllTreeBrushes(foundBrushes, false);
            foreach (var brush in foundBrushes)
            {
                if (!brush.Valid)
                {
                    continue;
                }
                var transformation = modelMatrix * brush.NodeToTreeSpaceMatrix * boundsTransformation;
                var childBounds    = brushContainerAsset.CalculateBounds(transformation);
                var magnitude      = childBounds.size.sqrMagnitude;
                if (float.IsInfinity(magnitude) ||
                    float.IsNaN(magnitude))
                {
                    var center = transformation.GetColumn(3);
                    childBounds = new Bounds(center, Vector3.zero);
                }
                if (childBounds.size.sqrMagnitude != 0)
                {
                    if (bounds.size.sqrMagnitude == 0)
                    {
                        bounds = childBounds;
                    }
                    else
                    {
                        bounds.Encapsulate(childBounds);
                    }
                }
            }

            return(bounds);
        }
        public override void OnSceneGUI()
        {
            if (!target)
            {
                return;
            }

            if (Tools.current != Tool.Custom || !ChiselEditGeneratorTool.IsActive())
            {
                OnDefaultSceneTools();
                return;
            }

            // Skip some events, to prevent scalability issues (when selecting thousands of brushes at the same time)
            // Could happen when doing control-A (select all)
            switch (Event.current.type)
            {
            case EventType.MouseEnterWindow:
            case EventType.MouseLeaveWindow:
            case EventType.Ignore:
            case EventType.Used:
                return;

            case EventType.MouseDown:
            case EventType.MouseUp:
            case EventType.MouseDrag:
            case EventType.DragExited:
            case EventType.DragPerform:
            case EventType.DragUpdated:
            {
                // Mouse messages don't make sense when the mouse is not over the current window
                if (SceneView.currentDrawingSceneView != EditorWindow.mouseOverWindow)
                {
                    return;
                }
                break;
            }
            }

            var generator   = target as T;
            var sceneView   = SceneView.currentDrawingSceneView;
            var modelMatrix = ChiselNodeHierarchyManager.FindModelTransformMatrixOfTransform(generator.hierarchyItem.Transform);

            // NOTE: allow invalid nodes to be edited to be able to recover from invalid state

            // NOTE: could loop over multiple instances from here, once we support that
            {
                using (new UnityEditor.Handles.DrawingScope(SceneHandles.handleColor, modelMatrix * generator.LocalTransformationWithPivot))
                {
                    handles.Start(generator, sceneView);
                    {
                        if (GUIUtility.hotControl == 0)
                        {
                            if (!OnGeneratorActive(generator))
                            {
                                if (validTargets.Contains(generator))
                                {
                                    handles.generatorStateLookup.Remove(generator);
                                    OnGeneratorDeselected(generator);
                                    validTargets.Remove(generator);
                                }
                                return;
                            }
                            if (!validTargets.Contains(generator))
                            {
                                handles.generatorStateLookup.Remove(generator);
                                OnGeneratorDeselected(generator);
                                validTargets.Add(generator);
                            }
                        }

                        EditorGUI.BeginChangeCheck();
                        try
                        {
                            OnScene(handles, generator);
                        }
                        finally
                        {
                            if (EditorGUI.EndChangeCheck())
                            {
                                generator.OnValidate();
                                OnTargetModifiedInScene();
                            }
                            handles.End();
                        }
                    }
                }
            }
        }
Beispiel #5
0
        public override void OnSceneGUI()
        {
            if (!target)
            {
                return;
            }

            if (Tools.current != Tool.Custom || !ChiselEditGeneratorTool.IsActive())
            {
                OnDefaultSceneTools();
                return;
            }

            var generator   = target as T;
            var sceneView   = SceneView.currentDrawingSceneView;
            var modelMatrix = ChiselNodeHierarchyManager.FindModelTransformMatrixOfTransform(generator.hierarchyItem.Transform);

            // NOTE: allow invalid nodes to be edited to be able to recover from invalid state

            // NOTE: could loop over multiple instances from here, once we support that
            {
                using (new UnityEditor.Handles.DrawingScope(SceneHandles.handleColor, modelMatrix * generator.LocalTransformationWithPivot))
                {
                    handles.Start(generator, sceneView);
                    {
                        if (GUIUtility.hotControl == 0)
                        {
                            if (!OnGeneratorActive(generator))
                            {
                                if (validTargets.Contains(generator))
                                {
                                    handles.generatorStateLookup.Remove(generator);
                                    OnGeneratorDeselected(generator);
                                    validTargets.Remove(generator);
                                }
                                return;
                            }
                            if (!validTargets.Contains(generator))
                            {
                                handles.generatorStateLookup.Remove(generator);
                                OnGeneratorDeselected(generator);
                                validTargets.Add(generator);
                            }
                        }

                        EditorGUI.BeginChangeCheck();
                        try
                        {
                            OnScene(handles, generator);
                        }
                        finally
                        {
                            if (EditorGUI.EndChangeCheck())
                            {
                                generator.OnValidate();
                                OnTargetModifiedInScene();
                            }
                            handles.End();
                        }
                    }
                }
            }
        }