環境
Windows 11 Pro (64bit)
dev.bat
@echo off
path %path%;C:\Windows\Microsoft.NET\Framework64\v4.0.30319
prompt $e[33m$p$g$e[m
cmd
wpf.rsp
# csc @wpf.rsp foo.cs
/r:System.Xaml.dll
/r:WPF\PresentationCore.dll
/r:WPF\PresentationFramework.dll
/r:WPF\WindowsBase.dll
三角形
wpf3d1.cs
// csc /t:winexe @wpf.rsp wpf3d1.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Media3D;
class MainWnd : Window {
public MainWnd() {
var pos = new Point3DCollection();
pos.Add(new Point3D(1, 0, 0));
pos.Add(new Point3D(0, 1, 0));
pos.Add(new Point3D(0, 0, 0));
var model = new GeometryModel3D();
model.Geometry = new MeshGeometry3D() { Positions = pos };
model.Material = new DiffuseMaterial(Brushes.LightGreen);
var light = new DirectionalLight();
var cam = new OrthographicCamera();
cam.Position = new Point3D(0, 0, 5);
var vp3d = new Viewport3D();
vp3d.Camera = cam;
vp3d.Children.Add(new ModelVisual3D() { Content = model });
vp3d.Children.Add(new ModelVisual3D() { Content = light });
Width = 300;
Height = 300;
Content = vp3d;
}
}
class Program {
[STAThread]
static void Main() {
var wnd = new MainWnd();
wnd.ShowDialog();
}
}
球
wpf3d2.cs
// csc /t:winexe @wpf.rsp wpf3d2.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Media3D;
class MainWnd : Window {
public MainWnd() {
var stacks = 12;
var slices = 16;
var pos = new Point3DCollection();
var idx = new Int32Collection();
for (var st = 0; st <= stacks; st++) {
var a = Math.PI * st / stacks;
var y = Math.Cos(a);
var r = Math.Sin(a);
for (var sl = 0; sl <= slices; sl++) {
var b = 2 * Math.PI * sl / slices + Math.PI;
var z = Math.Cos(b);
var x = Math.Sin(b);
pos.Add(new Point3D(x * r, y, z * r));
}
}
for (var st = 0; st < stacks; st++) {
var h0 = (slices + 1) * st;
var h1 = (slices + 1) * (st + 1);
for (var sl = 0; sl < slices; sl++) {
var i0 = h0 + sl;
var i1 = h1 + sl;
var i2 = h0 + sl + 1;
var i3 = h1 + sl + 1;
idx.Add(i0);
idx.Add(i1);
idx.Add(i2);
idx.Add(i3);
idx.Add(i2);
idx.Add(i1);
}
}
var model = new GeometryModel3D();
model.Geometry = new MeshGeometry3D() {
Positions = pos,
TriangleIndices = idx,
};
model.Material = new DiffuseMaterial(Brushes.LightGreen);
var light = new DirectionalLight();
light.Direction = new Vector3D(-1, -2, -3);
var cam = new OrthographicCamera();
cam.Position = new Point3D(0, 0, 5);
var vp3d = new Viewport3D();
vp3d.Camera = cam;
vp3d.Children.Add(new ModelVisual3D() { Content = model });
vp3d.Children.Add(new ModelVisual3D() { Content = light });
Width = 300;
Height = 300;
Content = vp3d;
}
}
class Program {
[STAThread]
static void Main() {
var wnd = new MainWnd();
wnd.ShowDialog();
}
}
参考