public CameraParams GetElementViewPosition(ModelUIElement3D model) { Transform3DGroup group = model.Model.Transform as Transform3DGroup; RotateTransform3D rot = group.Children[2] as RotateTransform3D; AxisAngleRotation3D ang = rot.Rotation as AxisAngleRotation3D; TranslateTransform3D trans = group.Children[1] as TranslateTransform3D; return new CameraParams(0.0, trans.OffsetY, 7.0, 0.0, ang.Angle, 0.0); }
private static void ModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ModelUIElement3D owner = ((ModelUIElement3D)d); // if it's not a subproperty change, then we need to change the protected Model property of Visual3D if (!e.IsASubPropertyChange) { owner.Visual3DModel = (Model3D)e.NewValue; } }
/// <summary> /// This method returns a 3D representation of this building walls /// </summary> /// <param name="nodesDict">List of all the nodes on the map</param> /// <param name="map">bounds of the map</param> /// <param name="brush">Color of these walls</param> /// <returns>ModelUIElement3D of these walls</returns> public ModelUIElement3D get3DWalls(Dictionary<long, OsmSharp.Osm.Node> nodesDict, Map map, ImageBrush brush) { // Surrounding tags of the mesh ModelUIElement3D model = new ModelUIElement3D(); GeometryModel3D geometryModel = new GeometryModel3D(); // Mesh and his his properties MeshGeometry3D mesh = new MeshGeometry3D(); DiffuseMaterial material = new DiffuseMaterial((System.Windows.Media.Brush)brush); Point3DCollection positions = new Point3DCollection(); PointCollection texturePoints = new PointCollection(); Int32Collection indices = new Int32Collection(); // Add Points of surface and with add points of surface with height 0 positions = getScaledPositionsWall(nodesDict, map); // Add indices to the collection for (int i = 0; i < positions.Count - 2; i += 2) { indices.Add(i); indices.Add(i + 2); indices.Add(i + 1); indices.Add(i + 3); indices.Add(i + 1); indices.Add(i + 2); // Get the width and height of a wall float widthWall = (float)Math.Sqrt(Math.Pow(positions[i].X - positions[i + 2].X, 2) + Math.Pow(positions[i].Y - positions[i + 2].Y, 2)); int imageWidth = (int)(brush.ImageSource.Width * widthWall); int imageHeight = (int)(brush.ImageSource.Height * height); // Add texture coordinates texturePoints.Add(new System.Windows.Point(0, imageHeight)); texturePoints.Add(new System.Windows.Point(0, 0)); texturePoints.Add(new System.Windows.Point(imageWidth, imageHeight)); texturePoints.Add(new System.Windows.Point(imageWidth, 0)); } // Add these collections to the mesh mesh.Positions = positions; mesh.TriangleIndices = indices; mesh.TextureCoordinates = texturePoints; // Set the color of front and back of the triangle geometryModel.Material = material; geometryModel.BackMaterial = material; // Add the mesh to the model geometryModel.Geometry = mesh; model.Model = geometryModel; return model; }
private ModelUIElement3D CreateMeshModel(Visual visual) { var model3D = ((GeometryModel3D) InternalResources["ElementModel"]).Clone(); var brush = new VisualBrush(visual); RenderOptions.SetCachingHint(brush, CachingHint.Cache); RenderOptions.SetBitmapScalingMode(brush, BitmapScalingMode.LowQuality); ((DiffuseMaterial) model3D.Material).Brush = brush; ((MeshGeometry3D) model3D.Geometry).Positions = CreateMeshPositions(); var model = new ModelUIElement3D {Model = model3D}; return model; }
private ModelUIElement3D CreateMeshModel(Visual visual) { GeometryModel3D model3d = (InternalResources["ElementModel"] as GeometryModel3D).Clone(); VisualBrush brush = new VisualBrush(visual); RenderOptions.SetCachingHint(brush, CachingHint.Cache); (model3d.Material as DiffuseMaterial).Brush = brush; (model3d.Geometry as MeshGeometry3D).Positions = CreateMeshPositions(); ModelUIElement3D model = new ModelUIElement3D(); model.Model = model3d; return model; }
public MainWindow() { InitializeComponent(); var container = new ContainerUIElement3D(); var element = new ModelUIElement3D(); var geometry = new GeometryModel3D(); var meshBuilder = new MeshBuilder(); meshBuilder.AddSphere(new Point3D(0, 0, 0), 2, 100, 50); geometry.Geometry = meshBuilder.ToMesh(); geometry.Material = Materials.Green; element.Model = geometry; element.Transform = new TranslateTransform3D(5, 0, 0); element.MouseDown += this.ContainerElementMouseDown; container.Children.Add(element); view1.Children.Add(container); }
/// <summary> /// This method returns a 3D representation of this area /// </summary> /// <param name="nodesDict">List of all the nodes on the map</param> /// <param name="map">bounds of the map</param> /// <param name="brush">Color of this area</param> /// <returns>ModelUIElement3D of this area</returns> public virtual ModelUIElement3D get3DSurface(Dictionary<long, OsmSharp.Osm.Node> nodesDict, Map map, System.Windows.Media.SolidColorBrush brush) { List<PointF> ptlist = getScaledPointsSurface(nodesDict, map); // Divide the polygons in triangles, this is code (and these two classes) are from: https://polygontriangulation.codeplex.com/ PolygonData poly = new PolygonData(ptlist); List<PointF[]> triangles = Triangulation2D.Triangulate(poly); // Surrounding tags of the mesh ModelUIElement3D model = new ModelUIElement3D(); GeometryModel3D geometryModel = new GeometryModel3D(); // Mesh and his his properties MeshGeometry3D mesh = new MeshGeometry3D(); DiffuseMaterial material = new DiffuseMaterial((System.Windows.Media.Brush)brush); Point3DCollection positions = new Point3DCollection(); Int32Collection indices = new Int32Collection(); // Add points and indices to their collection foreach (PointF[] points in triangles) { foreach (PointF point in points) { positions.Add(new Point3D(point.X, point.Y, height)); } int count = positions.Count; indices.Add(count - 3); indices.Add(count - 2); indices.Add(count - 1); } // Add these collections to the mesh mesh.Positions = positions; mesh.TriangleIndices = indices; // Set the color of front and back of the triangle geometryModel.Material = material; geometryModel.BackMaterial = material; // Add the mesh to the model geometryModel.Geometry = mesh; model.Model = geometryModel; return model; }
void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) { switch (connectionId) { case 1: this.viewport = ((System.Windows.Controls.Viewport3D)(target)); return; case 2: this.camera = ((System.Windows.Media.Media3D.PerspectiveCamera)(target)); return; case 3: this.ringVisual = ((System.Windows.Media.Media3D.ModelUIElement3D)(target)); #line 16 "..\..\HitTesting.xaml" this.ringVisual.MouseDown += new System.Windows.Input.MouseButtonEventHandler(this.ringVisual_MouseDown); #line default #line hidden return; case 4: this.Scene = ((System.Windows.Media.Media3D.Model3DGroup)(target)); return; case 5: this.ringModel = ((System.Windows.Media.Media3D.GeometryModel3D)(target)); return; case 6: this.ringMesh = ((System.Windows.Media.Media3D.MeshGeometry3D)(target)); return; case 7: this.axisRotation = ((System.Windows.Media.Media3D.AxisAngleRotation3D)(target)); return; } this._contentLoaded = true; }
public MainWindow() { InitializeComponent(); var c = new ContainerUIElement3D(); var e = new ModelUIElement3D(); var gm = new GeometryModel3D(); var mb = new MeshBuilder(); mb.AddSphere(new Point3D(0, 0, 0), 2, 100, 50); gm.Geometry = mb.ToMesh(); gm.Material = Materials.Red; e.Model = gm; e.Transform = new TranslateTransform3D(5, 0, 0); e.MouseDown += (sender, args) => gm.Material = Materials.Yellow; c.Children.Add(e); view1.Children.Add(c); //e.Visibility = Visibility.Hidden; //this.Dispatcher.BeginInvoke(DispatcherPriority.Send, new ThreadStart(delegate //{ // e.Visibility = Visibility.Visible; //})); }
public static void drawTriangle( Point3D p0, Point3D p1, Point3D p2, Color color, Viewport3D viewport) { MeshGeometry3D mesh = new MeshGeometry3D(); mesh.Positions.Add(p0); mesh.Positions.Add(p1); mesh.Positions.Add(p2); mesh.TriangleIndices.Add(0); mesh.TriangleIndices.Add(1); mesh.TriangleIndices.Add(2); SolidColorBrush brush = new SolidColorBrush(); brush.Color = color; Material material = new DiffuseMaterial(brush); GeometryModel3D geometry = new GeometryModel3D(mesh, material); ModelUIElement3D model = new ModelUIElement3D(); model.Model = geometry; viewport.Children.Add(model); }
/// <summary> /// The add. /// </summary> /// <param name="element"> /// The element. /// </param> private void Add(ModelUIElement3D element) { // http://social.msdn.microsoft.com/Forums/en/wpf/thread/7ac92a5a-001f-443a-a549-60cd5bb13083 this.Children.Add(element); element.Visibility = Visibility.Hidden; this.Dispatcher.BeginInvoke( DispatcherPriority.Send, new ThreadStart(delegate { element.Visibility = Visibility.Visible; })); }
public CameraParams GetElementViewPosition(ModelUIElement3D model) { Transform3DGroup group = model.Model.Transform as Transform3DGroup; TranslateTransform3D trans = group.Children[2] as TranslateTransform3D; return new CameraParams(0.0, 0.0, trans.OffsetZ + 7, 0.0, 0.0, 0); }
/// <summary> /// Updates the visuals. /// </summary> protected void UpdateVisuals() { this.vertexVisuals = new Dictionary<HalfEdgeMesh.Vertex, ModelUIElement3D>(); this.halfEdgeVisuals = new Dictionary<HalfEdgeMesh.HalfEdge, ModelUIElement3D>(); this.faceVisuals = new Dictionary<HalfEdgeMesh.Face, ModelUIElement3D>(); this.Children.Clear(); if (this.Mesh == null) { return; } if (this.VertexRadius > 0) { // Add the vertices foreach (var vertex in this.Mesh.Vertices) { var gm = new MeshBuilder(false, false); gm.AddSubdivisionSphere(vertex.Position, this.VertexRadius, 4); var vertexElement = new ModelUIElement3D { Model = new GeometryModel3D(gm.ToMesh(), this.VertexMaterial) }; var currentVertex = vertex; vertexElement.MouseLeftButtonDown += (s, e) => this.HighlightVertex(currentVertex); this.vertexVisuals.Add(vertex, vertexElement); this.Add(vertexElement); } } var faceCenter = new Dictionary<HalfEdgeMesh.Face, Point3D>(); foreach (var face in this.Mesh.Faces) { var faceVertices = face.Vertices.Select(v => v.Position).ToList(); // Find the face centroid var center = this.FindCentroid(faceVertices); faceCenter.Add(face, center); if (this.ShrinkFactor < 1) { // Add the faces for (int i = 0; i < faceVertices.Count; i++) { faceVertices[i] += (center - faceVertices[i]) * this.ShrinkFactor; } var gm = new MeshBuilder(false, false); gm.AddTriangleFan(faceVertices); var faceElement = new ModelUIElement3D { Model = new GeometryModel3D(gm.ToMesh(), this.FaceMaterial) { BackMaterial = this.FaceBackMaterial } }; var currentFace = face; faceElement.MouseLeftButtonDown += (s, e) => this.HighlightFace(currentFace); this.faceVisuals.Add(face, faceElement); this.Add(faceElement); } } if (this.EdgeDiameter > 0) { // Add the edges foreach (var edge in this.Mesh.Edges) { var start = edge.StartVertex.Position; var end = edge.EndVertex.Position; var center = faceCenter[edge.Face]; start = start + (center - start) * this.ShrinkFactor; end = end + (center - end) * this.ShrinkFactor; var gm = new MeshBuilder(false, false); gm.AddArrow(start, end, this.EdgeDiameter); var edgeElement = new ModelUIElement3D { Model = new GeometryModel3D(gm.ToMesh(), this.EdgeMaterial) }; var currentEdge = edge; edgeElement.MouseLeftButtonDown += (s, e) => { this.HighlightEdge(currentEdge); }; this.halfEdgeVisuals.Add(edge, edgeElement); this.Add(edgeElement); } } }
/** * starts animation on mouse enter on specific element in the list * * \param model object that sould be animated */ public void SetupAnimationOnMouseEnter(ModelUIElement3D model) { Transform3DGroup group = model.Model.Transform as Transform3DGroup; ScaleTransform3D scale = group.Children[0] as ScaleTransform3D; DoubleAnimation anim = new DoubleAnimation(1.3, new Duration(TimeSpan.FromSeconds(0.25))); scale.BeginAnimation(ScaleTransform3D.ScaleXProperty, anim); scale.BeginAnimation(ScaleTransform3D.ScaleYProperty, anim); scale.BeginAnimation(ScaleTransform3D.ScaleZProperty, anim); RotateTransform3D rotate = group.Children[1] as RotateTransform3D; AxisAngleRotation3D axis = rotate.Rotation as AxisAngleRotation3D; double rot = 0; DoubleAnimation daRotate = new DoubleAnimation(rot, new Duration(TimeSpan.FromSeconds(0.45))); axis.BeginAnimation(AxisAngleRotation3D.AngleProperty, daRotate); }
void model3D_MouseLeave(object sender, MouseEventArgs e) { currentLayout.SetupAnimationOnMouseLeave(sender as ModelUIElement3D); //HelpSystem.GlobalSystem.HideHelp(); if (tempBrush != null) { GeometryModel3D geo = currentlySelected.Model as GeometryModel3D; DiffuseMaterial mat = geo.Material as DiffuseMaterial; mat.Brush = tempBrush; tempBrush = null; } currentlySelected = null; }
/// <summary> /// 駒オブジェクトなどを初期化します。 /// </summary> protected override void InitializeImpl() { base.InitializeImpl(); // 駒はマウスクリックを識別したいので、 // ModelUIElement3Dクラスを使います。 Element = new ModelUIElement3D() { Model = ModelGroup, }; // 表示用の駒オブジェクトを追加します。 var pieceObject = new PieceObject( this.shogi, new BoardPiece(BWType, PieceType)); AddChild(pieceObject); this.numberText = new DecoratedText() { TextFormat = "×{0}", Foreground = Brushes.Black, FontWeight = FontWeights.ExtraBlack, Stroke = Brushes.White, StrokeThickness = 0.8, }; // 駒数の表示用オブジェクトを追加します。 var w = this.shogi.CellSize.Width; var h = this.shogi.CellSize.Height; var numberModel = new GeometryModel3D() { Geometry = CreateDefaultMesh(0.7, 0.4), Material = new DiffuseMaterial(new VisualBrush(this.numberText)), Transform = new Transform3DGroup() .Apply(_ => _.Children.Add(new TranslateTransform3D(0.4, -0.4, 0.0)), _ => _.Children.Add(new ScaleTransform3D(w, h, 1.0))), }; ModelGroup.Children.Add(numberModel); UpdateElement(); }
void model3D_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { ModelUIElement3D model = sender as ModelUIElement3D; // double click? if (e.ClickCount > 1) { viewport.Visibility = Visibility.Collapsed; bigPicture.SetContent(currentList[model]); bigPicture.FadeIn(0.5); return; } { s3dCore.ListLayout.CameraParams dest = currentLayout.GetElementViewPosition(model); camControler.MoveToSelectedElement(dest); currentlySelected = model; } }
public void ExportCurrentModelAsStereoView() { var stereoControl = new Stereo3DViewControl(); foreach ( var elem in Control.ViewPort3D.Children.OfType<ContainerUIElement3D>().SelectMany(child => child.Children)) { if ((elem as ModelUIElement3D).Model.GetType() == typeof (GeometryModel3D)) { var modelUiElement3D = elem as ModelUIElement3D; if (modelUiElement3D != null) { var geometry = (modelUiElement3D.Model as GeometryModel3D).Clone(); var tempModel = new ModelUIElement3D {Model = geometry}; stereoControl.StereoView3D.Children.Add(tempModel); // tempGeom = new GeometryModel3D(); } } } stereoControl.Show(); }
void model3D_MouseEnter(object sender, MouseEventArgs e) { currentLayout.SetupAnimationOnMouseEnter(sender as ModelUIElement3D); HelpSystem.GlobalSystem.ShowHelp("double click to view element in full qulity", 20); if (tempBrush != null) { GeometryModel3D geo = currentlySelected.Model as GeometryModel3D; DiffuseMaterial mat = geo.Material as DiffuseMaterial; mat.Brush = tempBrush; tempBrush = null; } currentlySelected = null; }
/// <summary> /// Create a clone of a Visual3D /// </summary> /// <param name="v"> /// a Visual3D /// </param> /// <returns> /// the clone /// </returns> public static Visual3D CreateClone(Visual3D v) { if (v is ModelUIElement3D) { var m = v as ModelUIElement3D; if (m.Model != null) { /*if (m.Model.CanFreeze) m.Model.Freeze(); if (m.Model.IsFrozen)*/ { var clonedModel = m.Model.Clone(); var clonedElement = new ModelUIElement3D(); clonedElement.Transform = m.Transform; clonedElement.Model = clonedModel; return clonedElement; } } } if (v is ModelVisual3D) { var m = v as ModelVisual3D; var clone = new ModelVisual3D(); clone.Transform = m.Transform; if (m.Content != null && m.Content.CanFreeze) { m.Content.Freeze(); var clonedModel = m.Content.Clone(); clone.Content = clonedModel; } if (m.Children.Count > 0) { foreach (var child in m.Children) { var clonedChild = CreateClone(child); clone.Children.Add(clonedChild); } } return clone; } return null; }
/** * starts animation on mouse leave on specific element in the list * * \param model object that sould be animated */ public void SetupAnimationOnMouseLeave(ModelUIElement3D model) { Transform3DGroup group = model.Model.Transform as Transform3DGroup; ScaleTransform3D scale = group.Children[0] as ScaleTransform3D; DoubleAnimation anim = new DoubleAnimation(1.0, new Duration(TimeSpan.FromSeconds(0.1))); scale.BeginAnimation(ScaleTransform3D.ScaleXProperty, anim); scale.BeginAnimation(ScaleTransform3D.ScaleYProperty, anim); scale.BeginAnimation(ScaleTransform3D.ScaleZProperty, anim); /*TranslateTransform3D translate = group.Children[2] as TranslateTransform3D; DoubleAnimation tranAnimX = new DoubleAnimation(tempPos.X, new Duration(TimeSpan.FromSeconds(0.1))); translate.BeginAnimation(TranslateTransform3D.OffsetXProperty, tranAnimX); DoubleAnimation tranAnimY = new DoubleAnimation(tempPos.Y, new Duration(TimeSpan.FromSeconds(0.1))); translate.BeginAnimation(TranslateTransform3D.OffsetYProperty, tranAnimY);*/ RotateTransform3D rotate = group.Children[1] as RotateTransform3D; AxisAngleRotation3D axis = rotate.Rotation as AxisAngleRotation3D; double rot = 35; DoubleAnimation daRotate = new DoubleAnimation(rot, new Duration(TimeSpan.FromSeconds(0.25))); axis.BeginAnimation(AxisAngleRotation3D.AngleProperty, daRotate); }
private void Create3DItems() { container.Children.Clear(); var rnd = new Random(); var i = 0; foreach (var game in games) { var bmp = new BitmapImage(); bmp.BeginInit(); bmp.CacheOption = BitmapCacheOption.OnLoad; bmp.UriSource = game.GetCardBackUri(); bmp.EndInit(); var halfWidth = game.CardWidth * 1.5 / game.CardHeight; var item = new ModelUIElement3D() { Model = new GeometryModel3D() { Geometry = new MeshGeometry3D() { Positions = new Point3DCollection() { new Point3D(-halfWidth,0,-1), new Point3D(-halfWidth,3,-1), new Point3D(halfWidth,3,-1), new Point3D(halfWidth,0,-1) }, TriangleIndices = new Int32Collection() { 0, 2, 1, 3, 2, 0 }, TextureCoordinates = new PointCollection() { new Point(0, 1), new Point(0, 0), new Point(1, 0), new Point(1, 1) } }, Material = new DiffuseMaterial(new ImageBrush(bmp)), Transform = new Transform3DGroup() { Children = new Transform3DCollection() { new RotateTransform3D() { Rotation = new AxisAngleRotation3D(new Vector3D(0,1,0), i == 0 ? 0 : -40) }, new TranslateTransform3D(i == 0 ? 0 : 0.5+i, 0, i != 0 ? -1 : 0) } } } }; item.MouseDown += Select; container.Children.Add(item); ++i; } if (games.Count > 0) { selectedIdx = 0; nameText.Text = games[0].Name; } }
private void AddCorners() { var a = Size / 2; var sideLength = a / 2; Point3D[] points = { new Point3D(-1,-1,-1 ), new Point3D(1, -1, -1), new Point3D(1, 1, -1), new Point3D(-1, 1, -1), new Point3D(-1,-1,1 ),new Point3D(1,-1,1 ),new Point3D(1,1,1 ),new Point3D(-1,1,1 )}; foreach (var p in points) { var builder = new MeshBuilder(false, true); Point3D center = p.Multiply(a); builder.AddBox(center, sideLength, sideLength, sideLength); var geometry = builder.ToMesh(); geometry.Freeze(); var model = new GeometryModel3D { Geometry = geometry, Material = MaterialHelper.CreateMaterial(Colors.Gold) }; var element = new ModelUIElement3D { Model = model }; faceNormals.Add(element, p.ToVector3D()); faceUpVectors.Add(element, ModelUpDirection); element.MouseLeftButtonDown += FaceMouseLeftButtonDown; element.MouseEnter += CornersMouseEnters; element.MouseLeave += CornersMouseLeave; Children.Add(element); } }
/// <summary> /// Adds a cube face. /// </summary> /// <param name="normal"> /// The normal. /// </param> /// <param name="up"> /// The up vector. /// </param> /// <param name="b"> /// The brush. /// </param> /// <param name="text"> /// The text. /// </param> private void AddCubeFace(Vector3D normal, Vector3D up, Brush b, string text) { var grid = new Grid { Width = 20, Height = 20, Background = b }; grid.Children.Add( new TextBlock { Text = text, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 15, Foreground = Brushes.White }); grid.Arrange(new Rect(new Point(0, 0), new Size(20, 20))); var bmp = new RenderTargetBitmap((int)grid.Width, (int)grid.Height, 96, 96, PixelFormats.Default); bmp.Render(grid); var material = MaterialHelper.CreateMaterial(new ImageBrush(bmp)); double a = this.Size; var builder = new MeshBuilder(false, true); builder.AddCubeFace(this.Center, normal, up, a, a, a); var geometry = builder.ToMesh(); geometry.Freeze(); var model = new GeometryModel3D { Geometry = geometry, Material = material }; var element = new ModelUIElement3D { Model = model }; element.MouseLeftButtonDown += this.FaceMouseLeftButtonDown; this.faceNormals.Add(element, normal); this.faceUpVectors.Add(element, up); this.Children.Add(element); }
/// <summary> /// Returns the grass surface /// </summary> /// <returns>ModelUIElement3D of the grass surface</returns> public ModelUIElement3D getGrassSurface() { // Surrounding tags of the mesh ModelUIElement3D model = new ModelUIElement3D(); GeometryModel3D geometryModel = new GeometryModel3D(); // Mesh and his his properties MeshGeometry3D mesh = new MeshGeometry3D(); DiffuseMaterial material = new DiffuseMaterial((System.Windows.Media.Brush)GRASSBRUSH); Point3DCollection positions = new Point3DCollection(); PointCollection texturePoints = new PointCollection(); Int32Collection indices = new Int32Collection(); // Add Points of surface and with add points of surface with height 0 positions.Add(new Point3D(8000, 8000, -1)); positions.Add(new Point3D(-8000, 8000, -1)); positions.Add(new Point3D(8000, -8000, -1)); positions.Add(new Point3D(-8000, -8000, -1)); // Add indices to the collection indices.Add(0); indices.Add(1); indices.Add(2); indices.Add(1); indices.Add(2); indices.Add(3); texturePoints.Add(new System.Windows.Point(0, 0)); texturePoints.Add(new System.Windows.Point(2000, 0)); texturePoints.Add(new System.Windows.Point(0, 2000)); texturePoints.Add(new System.Windows.Point(2000, 2000)); // Add these collections to the mesh mesh.Positions = positions; mesh.TriangleIndices = indices; mesh.TextureCoordinates = texturePoints; // Set the color of front and back of the triangle geometryModel.Material = material; geometryModel.BackMaterial = material; // Add the mesh to the model geometryModel.Geometry = mesh; model.Model = geometryModel; return model; }
private void AddEdge(Point3D center, double x, double y, double z, Vector3D faceNormal) { var builder = new MeshBuilder(false, true); builder.AddBox(center, x, y, z); var geometry = builder.ToMesh(); geometry.Freeze(); var model = new GeometryModel3D { Geometry = geometry, Material = MaterialHelper.CreateMaterial(Colors.Silver) }; var element = new ModelUIElement3D { Model = model }; faceNormals.Add(element, faceNormal); faceUpVectors.Add(element, ModelUpDirection); element.MouseLeftButtonDown += FaceMouseLeftButtonDown; element.MouseEnter += EdggesMouseEnters; element.MouseLeave += EdgesMouseLeaves; Children.Add(element); }
private void SelectDestinationMaterial() { for (int i = 1; i != _ElementContainer.Children.Count; i++) { _DestinationModel = _ElementContainer.Children[i] as ModelUIElement3D; TranslateTransform3D transform = (_DestinationModel.Model as Model3DGroup).Transform as TranslateTransform3D; Point3D pt = new Point3D(transform.OffsetX, transform.OffsetY, transform.OffsetZ); if (pt == _MovingPoints.Last()) { ChangeMaterial(_DestinationModel, Materials.BoxDestinationMaterial); break; } } }
private void SetPoints() { if (IsAnimationStop && _MovingPoints[_CurrentPointIndex] == _CenterPoint) { _AnimationBoard.SpeedRatio = 1; _AnimationBoard.Stop(); _StopAnimationButton.IsEnabled = false; _CurrentPointIndex = 0; ChangeMaterial(_DestinationModel, Materials.BoxMaterial); _DestinationModel = null; return; } if (_CurrentPointIndex == _MovingPoints.Length - 1) { _CurrentPointIndex = 0; _MovingPoints = _MovingPoints.Reverse().ToArray(); } int nextPointIndex = _CurrentPointIndex + 1; if (_MovingPoints[_CurrentPointIndex].X != _MovingPoints[nextPointIndex].X) { _SphereMovingAnimation.To = _LightMovingAnimation.To = _MovingPoints[nextPointIndex].X; Storyboard.SetTargetProperty(_SphereMovingAnimation, new PropertyPath(TranslateTransform3D.OffsetXProperty)); Storyboard.SetTargetProperty(_LightMovingAnimation, new PropertyPath(TranslateTransform3D.OffsetXProperty)); } else if (_MovingPoints[_CurrentPointIndex].Y != _MovingPoints[nextPointIndex].Y) { _SphereMovingAnimation.To = _LightMovingAnimation.To = _MovingPoints[nextPointIndex].Y; Storyboard.SetTargetProperty(_SphereMovingAnimation, new PropertyPath(TranslateTransform3D.OffsetYProperty)); Storyboard.SetTargetProperty(_LightMovingAnimation, new PropertyPath(TranslateTransform3D.OffsetYProperty)); } else if (_MovingPoints[_CurrentPointIndex].Z != _MovingPoints[nextPointIndex].Z) { _SphereMovingAnimation.To = _LightMovingAnimation.To = _MovingPoints[nextPointIndex].Z; Storyboard.SetTargetProperty(_SphereMovingAnimation, new PropertyPath(TranslateTransform3D.OffsetZProperty)); Storyboard.SetTargetProperty(_LightMovingAnimation, new PropertyPath(TranslateTransform3D.OffsetZProperty)); } ++_CurrentPointIndex; _AnimationBoard.Begin(this, true); }
private void InitializeElements() { _ViewPort.Style = _ViewPort.FindResource(Styles.DefaultStyle.ToString()) as Style; _CenterPoint = new Point3D(); _IsMouseDown = false; IsAnimationStop = true; for (int i = 0; i != 27; i++) { ModelUIElement3D element = new ModelUIElement3D(); Model3DGroup group = new Model3DGroup(); GeometryModel3D geometry = new GeometryModel3D() { Geometry = _ViewPort.FindResource("BoxMesh") as Geometry3D }; group.Transform = _ViewPort.FindResource(string.Format("Box{0}Transform", i + 1)) as TranslateTransform3D; group.Children.Add(geometry); element.Model = group; element.MouseEnter += ElementMouseEnter; element.MouseLeave += ElementMouseLeave; _ElementContainer.Children.Add(element); } InitializeMaterial(); _Timer = new DispatcherTimer() { IsEnabled = false, Interval = new TimeSpan(0, 0, 0, 0, 10) }; _Timer.Tick += SceneTimerRotation; _RotationAxis = new Dictionary<string, Vector3D>() { {"LeftTop", new Vector3D(-1,1,0) }, {"Left" , new Vector3D(0,-1,0)}, {"LeftDown",new Vector3D(1,-1,0)}, {"Down" , new Vector3D(1,0,0)}, {"RightDown", new Vector3D(1,1,0)}, {"Right" , new Vector3D(0,1,0)}, {"RightTop" , new Vector3D(-1,1,0)}, {"Top" , new Vector3D(-1,0,0)} }; _AnimationBoard = _ViewPort.FindResource("StoryBoard") as Storyboard; _AnimationBoard.Completed += new EventHandler((s, e) => SetPoints()); _SphereMovingAnimation = _AnimationBoard.Children.First() as DoubleAnimation; _LightMovingAnimation = _AnimationBoard.Children.Last() as DoubleAnimation; NameScope.SetNameScope(this, new NameScope()); RegisterName("STTransform", _SphereTranslateTransform); RegisterName("LightTransform", _LightTranslateTransform); Storyboard.SetTargetName(_SphereMovingAnimation, "STTransform"); Storyboard.SetTargetName(_LightMovingAnimation, "LightTransform"); }
private void ChangeMaterial(ModelUIElement3D model, Materials material) { GeometryModel3D geometry = (model.Model as Model3DGroup).Children.Last() as GeometryModel3D; geometry.Material = geometry.BackMaterial = _ViewPort.Style.Resources[material.ToString()] as MaterialGroup; }
void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) { switch (connectionId) { case 1: #line 9 "..\..\MainWindow.xaml" ((View.MainWindow)(target)).KeyDown += new System.Windows.Input.KeyEventHandler(this.MainWindow_KeyDown); #line default #line hidden #line 9 "..\..\MainWindow.xaml" ((View.MainWindow)(target)).Closing += new System.ComponentModel.CancelEventHandler(this.MainWindow_Closing); #line default #line hidden return; case 2: this.viewport = ((System.Windows.Controls.Viewport3D)(target)); #line 15 "..\..\MainWindow.xaml" this.viewport.MouseMove += new System.Windows.Input.MouseEventHandler(this.Viewport_MouseMove); #line default #line hidden #line 15 "..\..\MainWindow.xaml" this.viewport.LostFocus += new System.Windows.RoutedEventHandler(this.viewport_LostFocus); #line default #line hidden return; case 3: this.cameraMain = ((System.Windows.Media.Media3D.PerspectiveCamera)(target)); return; case 4: this.lightAmbient = ((System.Windows.Media.Media3D.AmbientLight)(target)); return; case 5: this.lightKey = ((System.Windows.Media.Media3D.DirectionalLight)(target)); return; case 6: this.lightSide = ((System.Windows.Media.Media3D.DirectionalLight)(target)); return; case 7: this.lightPoint = ((System.Windows.Media.Media3D.PointLight)(target)); return; case 8: this.modelBackground = ((System.Windows.Media.Media3D.ModelVisual3D)(target)); return; case 9: this.modelFloor = ((System.Windows.Media.Media3D.ModelVisual3D)(target)); return; case 10: this.modelPlayCursor = ((System.Windows.Media.Media3D.ModelUIElement3D)(target)); return; case 11: this.rotatorPointer = ((System.Windows.Media.Media3D.RotateTransform3D)(target)); return; case 12: this.translatorPointer = ((System.Windows.Media.Media3D.TranslateTransform3D)(target)); return; case 13: this.groupPlayers3D = ((System.Windows.Media.Media3D.ModelVisual3D)(target)); return; case 14: this.groupFood3D = ((System.Windows.Media.Media3D.ModelVisual3D)(target)); return; case 15: #line 152 "..\..\MainWindow.xaml" ((System.Windows.Controls.DockPanel)(target)).Loaded += new System.Windows.RoutedEventHandler(this.MainWindow_Loaded); #line default #line hidden return; case 16: this.lblPlayerName = ((System.Windows.Controls.Label)(target)); return; case 17: this.lblMass = ((System.Windows.Controls.Label)(target)); return; case 18: this.lblFoodCount = ((System.Windows.Controls.Label)(target)); return; case 19: this.lblPlayerCount = ((System.Windows.Controls.Label)(target)); return; case 20: this.lblPlayerPosition = ((System.Windows.Controls.Label)(target)); return; case 21: this.bttnDisconnect = ((System.Windows.Controls.Button)(target)); #line 188 "..\..\MainWindow.xaml" this.bttnDisconnect.Click += new System.Windows.RoutedEventHandler(this.DisconnectButton_Clicked); #line default #line hidden return; case 22: this.txtbxMessages = ((System.Windows.Controls.TextBox)(target)); return; } this._contentLoaded = true; }
ModelUIElement3D CreateModel(Brush brush) { ModelUIElement3D model3D = new ModelUIElement3D { Model = new GeometryModel3D { Geometry = new MeshGeometry3D { TriangleIndices = new Int32Collection( new int[] { 0, 1, 2, 2, 3, 0 }), TextureCoordinates = new PointCollection( new Point[] { new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(0, 0) }), Positions = new Point3DCollection( new Point3D[] { new Point3D(-0.5, -0.5, 0), new Point3D(0.5, -0.5, 0), new Point3D(0.5, 0.5, 0), new Point3D(-0.5, 0.5, 0) }) }, Material = new DiffuseMaterial { Brush = brush }, BackMaterial = new DiffuseMaterial { Brush = Brushes.White }, Transform = currentLayout.GetNextTransformation() } }; model3D.MouseEnter += new MouseEventHandler(model3D_MouseEnter); model3D.MouseLeave += new MouseEventHandler(model3D_MouseLeave); model3D.MouseLeftButtonDown += new MouseButtonEventHandler(model3D_MouseLeftButtonDown); return model3D; }
/// <summary> /// Read FileData /// </summary> /// <param name="path"></param> /// <returns></returns> public ContainerUIElement3D ReadFileData(string path) { var extension = Path.GetExtension(path); // var visModel = new ModelVisual3D(); var container = new ContainerUIElement3D(); switch (extension) { case ".obj": var currentHelixObjReader = new ObjReader(); try { var myModel = currentHelixObjReader.Read(path); foreach (var model in myModel.Children) { if (model is GeometryModel3D) { var geom = model as GeometryModel3D; var element = new ModelUIElement3D {Model = geom}; element.MouseDown += (sender1, e1) => OnElementMouseDown(sender1, e1, this); container.Children.Add(element); } } } catch (Exception) { // ignore } break; case ".stl": var currentHelixStlReader = new StLReader(); try { var myModel = currentHelixStlReader.Read(path); foreach (var model in myModel.Children) { if (model is GeometryModel3D) { var geom = model as GeometryModel3D; var element = new ModelUIElement3D {Model = geom}; element.MouseDown += (sender1, e1) => OnElementMouseDown(sender1, e1, this); container.Children.Add(element); } } } catch (Exception) { // ignore } break; } return container; }