Beispiel #1
0
        public void Insert()
        {
            SelectionManager selectionManager = RenderStack.Services.BaseServices.Get <SelectionManager>();
            BrushManager     brushManager     = RenderStack.Services.BaseServices.Get <BrushManager>();

            if (
                (selectionManager == null) ||  
                    (brushManager == null) ||
                (selectionManager.HoverModel == null) ||
                (selectionManager.HoverPolygon == null)
                )
            {
                return;
            }

            GeometryMesh mesh = selectionManager.HoverModel.Batch.MeshSource as GeometryMesh;

            if (mesh == null)
            {
                return;
            }

            int   cornerCount = selectionManager.HoverPolygon.Corners.Count;
            Brush brush       = brushManager.CurrentBrush;

            if (
                (brush == null) ||
                (brush.PolygonDictionary.ContainsKey(cornerCount) == false)
                )
            {
                Dictionary <int, Brush> dictionary = brushManager.Dictionary(userInterfaceManager.CurrentPalette);
                if (dictionary == null)
                {
                    return;
                }

                if (dictionary.ContainsKey(cornerCount))
                {
                    brush = dictionary[cornerCount];
                }
                else
                {
                    return;
                }
            }

            GeometryMesh brushPolyMesh = brush.Model.Batch.MeshSource as GeometryMesh;

            Polygon        brushPolygon = brush.PolygonDictionary[cornerCount];
            ReferenceFrame hoverFrame   = Operations.MakePolygonReference(mesh.Geometry, selectionManager.HoverPolygon);
            ReferenceFrame brushFrame   = Operations.MakePolygonReference(brushPolyMesh.Geometry, brushPolygon);

            float scale = hoverFrame.Scale / brushFrame.Scale;

            if (scale != 1.0f)
            {
                //Geometry newGeometry = new CloneGeometryOperation(brushPolyMesh.Geometry, null).Destination;
                Matrix4 scaleTransform;
                Matrix4.CreateScale(scale, out scaleTransform);
                //newGeometry.Transform(scaleTransform);
                //newGeometry.ComputePolygonCentroids();
                //newGeometry.ComputePolygonNormals();
                ////newGeometry.ComputeCornerNormals(0.0f);
                //newGeometry.SmoothNormalize("corner_normals", "polygon_normals", (0.0f * (float)Math.PI));
                //newGeometry.BuildEdges();

                brushFrame.Transform(scaleTransform);

                brushPolyMesh = brush.ScaledMesh(scale); //new GeometryMesh(newGeometry, NormalStyle.PolygonNormals);
            }

            //  Flip target normal..
            hoverFrame.Normal *= -1.0f;

            Matrix4 hoverTransform = hoverFrame.GetTransform();
            Matrix4 brushTransform = brushFrame.GetTransform();
            Matrix4 inverseBrush   = Matrix4.Invert(brushTransform);
            Matrix4 align          = hoverTransform * inverseBrush;

            Material material = materialManager[userInterfaceManager.CurrentMaterial];

            Model newModel = new Model(
                brush.Model.Name,
                brushPolyMesh,
                material,
                align
                );

            if (Configuration.physics)
            {
                newModel.PhysicsShape = brush.ScaledShape(scale);
            }

            newModel.Frame.Updated = false;
            newModel.Frame.Parent  = selectionManager.HoverModel.Frame;
            newModel.Frame.UpdateHierarchicalNoCache();
            sceneManager.UnparentModel(newModel);
            //newModel.Frame.Updated = true;

            //  Added objects are not static in physics sence
            newModel.Static = !userInterfaceManager.AddWithPhysics;

            Insert op = new Insert(newModel);

            operationStack.Do(op);

#if false
            newModel.Frame.Debug(0);

            newModel.Selected = true;
            selectionManager.Models.Add(newModel);
            selectionManager.HoverModel.Selected = true;
            selectionManager.Models.Add(selectionManager.HoverModel);
#endif
        }