
我创建了一个剪裁平面的最小可复制模型。来自DevDept 的体积渲染示例。我注意到的两件事是,您需要确保在调用 Edit(null) 之后重绘视口(viewport.Invalidate(); )。同样在示例中,Plane.Origin.Z 被弄乱了。除非clippingPlane1的整个平面被改变,否则这不会被重新渲染:
viewportLayout1.ClippingPlane1.Plane = new Plane(new Point3D(7, 7, 7), new Vector3D(0, 0, 1));
ClippingPlane1 的代码:
private void addClippingPlane()
{
// Remove Clipping Plane if Active
if (viewportLayout1.ClippingPlane1.Active) { viewportLayout1.ClippingPlane1.Cancel(); }
//sets the Z coordinate of the origin of the clippingPlane
viewportLayout1.ClippingPlane1.Plane = new Plane(new Point3D(7, 7, 7), new Vector3D(0, 0, 1));
// enables a clippingPlane
viewportLayout1.ClippingPlane1.Edit(null);
// refresh the viewport
viewportLayout1.Invalidate();
}
另一种剪裁网格的方法是CutBy。此方法允许通过平面单独切割网格。不幸的是,平面是几何子副和实体子,因此它不能作为平面添加到视口中。为了缓解这种情况,可以使用Quad实体绘制它,并为绘制的边指定长度。
剪切示例:
private void addCutByPlane()
{
// Remove Clipping Plane if Active
if (viewportLayout1.ClippingPlane1.Active) { viewportLayout1.ClippingPlane1.Cancel(); }
// Create Meshes
Plane cutPlane1 = new Plane(new Point3D(7, 7, 7), new Vector3D(0, 0, 1));
Mesh square = Mesh.CreateBox(15, 15, 15);
// Cut Mesh by Plane
square.CutBy(cutPlane1);
// Length of Drawn part of plane
double drawPlaneLength = 16;
double hL = drawPlaneLength / 2;
// Draw Plane region as a Quad Entity
Point3D pO = cutPlane1.Origin;
Point3D q1 = (pO + (cutPlane1.AxisX + cutPlane1.AxisY) * hL);
Point3D q2 = (pO - (cutPlane1.AxisX - cutPlane1.AxisY) * hL);
Point3D q3 = (pO - (cutPlane1.AxisX + cutPlane1.AxisY) * hL);
Point3D q4 = (pO + (cutPlane1.AxisX - cutPlane1.AxisY) * hL);
Quad drawCutPlane1 = new Quad(q1, q2, q3, q4);
viewportLayout1.Entities.Clear();
viewportLayout1.Entities.Add(drawCutPlane1, Color.Red);
viewportLayout1.Entities.Add(square, Color.Blue);
viewportLayout1.Invalidate();
}
完整示例:
using System;
using System.Drawing;
using System.Windows.Forms;
using devDept.Eyeshot.Entities;
using devDept.Geometry;
namespace EyeshotTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
makeSquare();
}
private void makeSquare()
{
Mesh square = Mesh.CreateBox(15, 15, 15);
viewportLayout1.Entities.Add(square, Color.Green);
viewportLayout1.Invalidate();
}
private void addClippingPlane()
{
// Remove Clipping Plane if Active
if (viewportLayout1.ClippingPlane1.Active) { viewportLayout1.ClippingPlane1.Cancel(); }
//sets the Z coordinate of the origin of the clippingPlane
viewportLayout1.ClippingPlane1.Plane = new Plane(new Point3D(7, 7, 7), new Vector3D(0, 0, 1));
// enables a clippingPlane
viewportLayout1.ClippingPlane1.Edit(null);
// refresh the viewport
//viewportLayout1.Entities.Regen();
// ^ unneeded, but when in doubt always Regen & Invalidate
// Then remove them where ever possible for speed improvments
viewportLayout1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
addClippingPlane();
}
private void addCutByPlane()
{
// Remove Clipping Plane if Active
if (viewportLayout1.ClippingPlane1.Active) { viewportLayout1.ClippingPlane1.Cancel(); }
// Create Meshes
Plane cutPlane1 = new Plane(new Point3D(7, 7, 7), new Vector3D(0, 0, 1));
Mesh square = Mesh.CreateBox(15, 15, 15);
// Cut Mesh by Plane
square.CutBy(cutPlane1);
// Length of Drawn part of plane
double drawPlaneLength = 16;
double hL = drawPlaneLength / 2;
// Draw Plane region as a Quad Entity
Point3D pO = cutPlane1.Origin;
Point3D q1 = (pO + (cutPlane1.AxisX + cutPlane1.AxisY) * hL);
Point3D q2 = (pO - (cutPlane1.AxisX - cutPlane1.AxisY) * hL);
Point3D q3 = (pO - (cutPlane1.AxisX + cutPlane1.AxisY) * hL);
Point3D q4 = (pO + (cutPlane1.AxisX - cutPlane1.AxisY) * hL);
Quad drawCutPlane1 = new Quad(q1, q2, q3, q4);
viewportLayout1.Entities.Clear();
viewportLayout1.Entities.Add(drawCutPlane1, Color.Red);
viewportLayout1.Entities.Add(square, Color.Blue);
viewportLayout1.Invalidate();
}
private void button2_Click(object sender, EventArgs e)
{
addCutByPlane();
}
}
}