public void OnSceneGUI()
        {
            if (!target || !ChiselEditModeManager.EditMode.EnableComponentEditors)
            {
                return;
            }

            using (new UnityEditor.Handles.DrawingScope(UnityEditor.Handles.yAxisColor))
            {
                var generator = target as T;
                if (!generator.isActiveAndEnabled)
                {
                    return;
                }

                var modelMatrix = CSGNodeHierarchyManager.FindModelTransformMatrixOfTransform(generator.hierarchyItem.Transform);
                var brush       = generator.TopNode;
                //foreach (var brush in CSGSyncSelection.GetSelectedVariantsOfBrushOrSelf((CSGTreeBrush)generator.TopNode))
                //foreach (var brush in generator.Node.AllSynchronizedVariants) // <-- this fails when brushes have failed to be created
                {
                    //var directSelect = CSGSyncSelection.IsBrushVariantSelected(brush);
                    //if (!directSelect)
                    //	continue;

                    UnityEditor.Handles.matrix = modelMatrix * brush.NodeToTreeSpaceMatrix;
                    UnityEditor.Handles.color  = UnityEditor.Handles.yAxisColor;

                    OnScene(generator);
                }
            }
        }
Esempio n. 2
0
        protected override void OnScene(ChiselBrush generator)
        {
            var targetBrushContainerAsset = generator.BrushContainerAsset;

            if (!targetBrushContainerAsset)
            {
                return;
            }

            var brushMeshes = targetBrushContainerAsset.BrushMeshes;

            if (brushMeshes == null)
            {
                return;
            }

            for (int m = 0; m < brushMeshes.Length; m++)
            {
                var brushMesh = brushMeshes[m];
                if (brushMesh == null)
                {
                    continue;
                }

                EditorGUI.BeginChangeCheck();

                var modelMatrix = CSGNodeHierarchyManager.FindModelTransformMatrixOfTransform(generator.hierarchyItem.Transform);

                var vertices  = brushMesh.vertices;
                var halfEdges = brushMesh.halfEdges;

                //HashSet<CSGTreeBrush> foundBrushes = new HashSet<CSGTreeBrush>();
                //targetBrush.GetAllTreeBrushes(foundBrushes, false)
                //foreach (var brush in CSGSyncSelection.GetSelectedVariantsOfBrushOrSelf((CSGTreeBrush)generator.TopNode))
                {
                    var brush          = (CSGTreeBrush)generator.TopNode;
                    var transformation = modelMatrix * brush.NodeToTreeSpaceMatrix;
                    for (int e = 0; e < halfEdges.Length; e++)
                    {
                        var vertexIndex1 = halfEdges[e].vertexIndex;
                        var vertexIndex2 = halfEdges[halfEdges[e].twinIndex].vertexIndex;

                        var from = vertices[vertexIndex1];
                        var to   = vertices[vertexIndex2];
                        ChiselOutlineRenderer.DrawLine(transformation, from, to, UnityEditor.Handles.yAxisColor, thickness: 1.0f);
                    }
                }

                //var newBounds = CSGHandles.BoundsHandle(originalBounds, Quaternion.identity, CSGHandles.DotHandleCap);

                if (EditorGUI.EndChangeCheck())
                {
                    //Undo.RecordObject(target, "Changed shape of Brush");
                    //brush.Bounds = newBounds;
                }
            }
        }
        public void OnSceneGUI()
        {
            if (!target || !ChiselEditModeManager.EditMode.EnableComponentEditors)
            {
                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 = CSGNodeHierarchyManager.FindModelTransformMatrixOfTransform(generator.hierarchyItem.Transform);
            var brush       = generator.TopNode;

            // NOTE: could loop over multiple instances from here, once we support that
            {
                using (new UnityEditor.Handles.DrawingScope(UnityEditor.Handles.yAxisColor, modelMatrix * brush.NodeToTreeSpaceMatrix))
                {
                    EditorGUI.BeginChangeCheck();
                    {
                        OnScene(sceneView, generator);
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        OnTargetModifiedInScene();
                    }
                }
            }
        }
        public override Bounds CalculateBounds()
        {
            if (!brushContainerAsset)
            {
                return(CSGHierarchyItem.EmptyBounds);
            }

            var modelMatrix = CSGNodeHierarchyManager.FindModelTransformMatrixOfTransform(hierarchyItem.Transform);
            var bounds      = CSGHierarchyItem.EmptyBounds;

            var foundBrushes = new HashSet <CSGTreeBrush>();

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

            return(bounds);
        }