Thanks, but i must do it codebehind. I am really wondering how they actually do it in server side, the image I give in question is produced on runtime. I found a library and trying to create a cube with it. And I will try to use my uploaded image as image pattern here is my code to generate a cube:
public static void CreateWPFImage(string fileName)
{
Thread t = new Thread(() => CreateImage(fileName));
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
}
private static void CreateImage(string fileName)
{
var viewport = CreateViewport();
viewport.Children.Add(GetModel());
var size = new Size();
size.Width = 600;
size.Height = 400;
viewport.Measure(size);
var rect = new Rect();
rect.Width = 600;
rect.Height = 400;
viewport.Arrange(rect);
SaveViewport(viewport, fileName);
}
private static Viewport3D CreateViewport()
{
Viewport3D viewport = new Viewport3D();
viewport.ClipToBounds = true;
var camera = new PerspectiveCamera(new Point3D(500, 200, 500),
new Vector3D(-0.01, -0.01, -0.1),
new Vector3D(-0.1,1,-0.3), 500);
viewport.Camera = camera;
var light1 = new DirectionalLight(Colors.White, new Vector3D(-3, 1, -1));
var model1 = new ModelVisual3D();
model1.Content = light1;
viewport.Children.Add(model1);
var light2 = new DirectionalLight(Colors.White,
new Vector3D(-3, -5, -1));
var model2 = new ModelVisual3D();
model2.Content = light2;
viewport.Children.Add(model2);
var light3 = new PointLight(Colors.White,
new Point3D(-5, 2.5, 150));
var model3 = new ModelVisual3D();
model3.Content = light3;
viewport.Children.Add(model3);
return viewport;
}
private static void SaveViewport(Viewport3D viewport3D, string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
RenderTargetBitmap rtb = new RenderTargetBitmap(600, 400, 96d, 96d, PixelFormats.Default);
rtb.Render(viewport3D);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(fs);
fs.Close();
}
public static ModelVisual3D GetModel()
{
Model3DGroup cube = new Model3DGroup();
Point3D p0 = new Point3D(-250, -250, 0);
Point3D p1 = new Point3D(400, -400, 0);
Point3D p2 = new Point3D(400, -400, 100);
Point3D p3 = new Point3D(-250, -250, 100);
Point3D p4 = new Point3D(-250, 250, 0);
Point3D p5 = new Point3D(400, 400, 0);
Point3D p6 = new Point3D(400, 400, 100);
Point3D p7 = new Point3D(-250, 250, 100);
cube.Children.Add(CreateBrushModel(p3, p2, p6, Colors.Gold));
cube.Children.Add(CreateTriangleModel(p3, p6, p7, Colors.Gold));
cube.Children.Add(CreateTriangleModel(p2, p1, p5, Colors.Green));
cube.Children.Add(CreateTriangleModel(p2, p5, p6, Colors.Green));
cube.Children.Add(CreateTriangleModel(p1, p0, p4, Colors.Green));
cube.Children.Add(CreateTriangleModel(p1, p4, p5, Colors.Green));
cube.Children.Add(CreateTriangleModel(p0, p3, p7, Colors.Green));
cube.Children.Add(CreateTriangleModel(p0, p7, p4, Colors.Green));
cube.Children.Add(CreateTriangleModel(p7, p6, p5, Colors.Green));
cube.Children.Add(CreateTriangleModel(p7, p5, p4, Colors.Green));
cube.Children.Add(CreateTriangleModel(p2, p3, p0, Colors.Green));
cube.Children.Add(CreateTriangleModel(p2, p0, p1, Colors.Green));
ModelVisual3D model = new ModelVisual3D();
model.Content = cube;
return model;
}
private static Model3DGroup CreateTriangleModel(Point3D p0, Point3D p1, Point3D p2, Color color)
{
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);
Vector3D normal = CalculateNormal(p0, p1, p2);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
Material material = new DiffuseMaterial(new SolidColorBrush(color));
GeometryModel3D model = new GeometryModel3D(mesh, material);
Model3DGroup group = new Model3DGroup();
group.Children.Add(model);
return group;
}
private static Model3DGroup CreateBrushModel(Point3D p0, Point3D p1, Point3D p2, Color color)
{
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);
Vector3D normal = CalculateNormal(p0, p1, p2);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
ImageBrush imgBrush = new ImageBrush(new BitmapImage(new Uri(@"http://www.kanertuncel.com/Test.png")));
GeometryModel3D model = new GeometryModel3D(mesh, new DiffuseMaterial(imgBrush));
Model3DGroup group = new Model3DGroup();
group.Children.Add(model);
return group;
}
private static Vector3D CalculateNormal(Point3D p0, Point3D p1, Point3D p2)
{
Vector3D v0 = new Vector3D(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
Vector3D v1 = new Vector3D(p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z);
return Vector3D.CrossProduct(v0, v1);
}
|