I can paint Model3DGroup in the color i want by using
Material material = new DiffuseMaterial(new SolidColorBrush(Colors.White));
GeometryModel3D model = new GeometryModel3D(mesh, material);
(following this tutorial)
to set the color of GeometryModel3D i include in the Model3DGroup.
I have an application where I have to place a map on a terrain in 3D (terrain is completely flat), the map is not an image, and I have detailed data of the points of the shapes I want to draw, and I also have DrawingVisual objects of all the shapes that I want to project on the 3D surface. In 2D mode, I draw them on a custom canvas (derived from Canvas) where I add them using
myCanvas.addVisualChild(item);
myCanvas.addLogicvalChild(item);
My question is how to “paint” or draw shapes and lines etc on 3D items? These shapes do not cover the terrain fully. I have tried using Viewport2DVisual3D class and tried placing a canvas on a 3D surface (a simple canvas with a button) using the following code:
Canvas testCanvas = new Canvas();
testCanvas.Background = Brushes.Green;
Button b1 = new Button();
b1.Content = "Hello there";
Canvas.SetTop(b1, 50);
Canvas.SetLeft(b1, 50);
Canvas.SetZIndex(b1, 2);
testCanvas.Children.Add(b1);
testVisual3d.Visual = testCanvas; // testVisual3d is a Viewport2DVisual3D declared in xaml
But the problem is that I am not able to figure out how the Canvas or any Visual “fills” the Viewport2DVisual3D class because:
- The button filled the canvas completely.
- Empty areas of the canvas (Canvas.SetTop(b1, 50)) is not visible.
- I have no idea idea about the size of the canvas related to the size of Viewport2DVisual3D object because the button always fills the object completely.
Also i cannot use Viewport2DVisual3D everywhere, as I also have to create models of buildings etc. when I am projecting the map to a 3D terrain, so I’ll have to paint areas of a building model (which will be a Model3DGroup) differently to give a realistic effects, but if i manage to project the map on a Viewport2DVisual3D, it’ll solve lots of problems as i’ll be able to directly project all the shapes including grid on the Viewport2DVisual3D object or terrain.
I am using .NET 4.0 and C#.
Please help.
Update
Using this code solves the initial problem of canvas size and space:
Canvas testCanvas = new Canvas();
testCanvas.Background = Brushes.Green;
Button b1 = new Button();
b1.Width = 120;
b1.Height = 25;
testCanvas.Width = 200;
testCanvas.Height = 200;
b1.Content = "Hello there";
Canvas.SetTop(b1, 50);
Canvas.SetLeft(b1, 50);
Canvas.SetZIndex(b1, 2);
testCanvas.Children.Add(b1);
testVisual3d.Visual = testCanvas;
The size of Viewport2DVisual3D is
Positions="0,0,0 0,0,30 30,0,30 30,0,0"
And the canvas resizes to fit the boundaries of the Viewport2DVisual3D, but will it work with a class derived from Canvas in which shapes have directly been added using Canvas.AddVisualChild and Canvas.AddLogicalChild, i am yet to try that.
And also the original question of painting on the Model3DGroup remains, how to do it?
After a bit of experimenting, i have the answer, simple way is to set the color of each triangle while defining the mesh. In WPF, what is needed is to set the Material color when combining the triangle (MeshGeometry3D) and material while defining a new GeometryModel3D.
(from this tutorial)
I am a beginner of 3D and I have not yet explored the concept of materials.
If someone knows another way, please post here.