new CSG()
源码参见https://github.com/jscad/csg.js
Constructive Solid Geometry (CSG) is a modeling technique that uses Boolean
operations like union and intersection to combine 3D solids. This library
implements CSG operations on meshes elegantly and concisely using BSP trees,
and is meant to serve as an easily understandable implementation of the
algorithm. All edge cases involving overlapping coplanar polygons in both
solids are correctly handled.
Constructive Solid Geometry (CSG) is a modeling technique that uses Boolean
operations like union and intersection to combine 3D solids. This library
implements CSG operations on meshes elegantly and concisely using BSP trees,
and is meant to serve as an easily understandable implementation of the
algorithm. All edge cases involving overlapping coplanar polygons in both
solids are correctly handled.
- Source:
Example
MeshVisualizer = Cesium.MeshVisualizer;
Mesh = Cesium.Mesh;
MeshMaterial = Cesium.MeshMaterial;
CSG = Cesium.CSG;
GeometryUtils = Cesium.GeometryUtils;
//示例1:
var cube = CSG.cube();
var sphere = CSG.sphere({ radius: 1.3 });
var polygons = cube.subtract(sphere).toPolygons();
//示例2:
var center = Cesium.Cartesian3.fromDegrees(homePosition[0], homePosition[1], 50000);
var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(center);
var meshVisualizer = new MeshVisualizer({
modelMatrix: modelMatrix,
up: { z: 1 }
});
viewer.scene.primitives.add(meshVisualizer);
var material = new MeshMaterial({
defaultColor: "rgba(0,0,255,1.0)",
wireframe: true,
side: MeshMaterial.Sides.DOUBLE
});
//创建盒子
var dimensions = new Cesium.Cartesian3(100000, 50000, 50000);
var boxGeometry = Cesium.BoxGeometry.createGeometry(Cesium.BoxGeometry.fromDimensions({
dimensions: dimensions,
vertexFormat: Cesium.VertexFormat.POSITION_ONLY
}));
var box = GeometryUtils.toCSG(boxGeometry);
var boxMesh = new Mesh(box, material);
meshVisualizer.add(boxMesh);
//创建球体
var sphere = new Cesium.SphereGeometry({
radius: 50000.0,
vertexFormat: Cesium.VertexFormat.POSITION_ONLY
});
sphere = Cesium.SphereGeometry.createGeometry(sphere);
sphere = CSG.toCSG(sphere);
var sphereMesh = new Mesh(sphere, material);
sphereMesh.position = new Cesium.Cartesian3(100000, 0, 0)
meshVisualizer.add(sphereMesh);
//并
var unionResult = box.union(sphere);
var unionResultMesh = new Mesh(unionResult, material);
unionResultMesh.position = new Cesium.Cartesian3(300000, 0, 0)
meshVisualizer.add(unionResultMesh);
//交
var intersectResult = box.intersect(sphere);
var intersectResultMesh = new Mesh(intersectResult, material);
intersectResultMesh.position = new Cesium.Cartesian3(500000, 0, 0)
meshVisualizer.add(intersectResultMesh);
//球体减盒子
var subResult = sphere.subtract(box);
var subResultMesh = new Mesh(subResult, material);
subResultMesh.position = new Cesium.Cartesian3(700000, 0, 0)
meshVisualizer.add(subResultMesh);
//盒子减球体
var subResult2 = box.subtract(sphere);
var subResultMesh2 = new Mesh(subResult2, material);
subResultMesh2.position = new Cesium.Cartesian3(900000, 0, 0)
meshVisualizer.add(subResultMesh2);
//渲染CSG创建的几何体
var cube = CSG.cube({
center: [0, 0, 0],
radius: 20000
});
var cubeMtl = new MeshMaterial({
defaultColor: "rgba(255,0,0,1)"
});
meshVisualizer.add(new Mesh({
geometry: cube,
material: cubeMtl,
position: new Cesium.Cartesian3(-100000, 0, 0)
}));
Classes
Members
(static) Plane.EPSILON
`CSG.Plane.EPSILON` is the tolerance used by `splitPolygon()` to decide if a
point is on the plane.
- Source:
Methods
(static) CSG.cube(options) → {Cesium.CSG}
Construct an axis-aligned solid cuboid. Optional parameters are `center` and
`radius`, which default to `[0, 0, 0]` and `[1, 1, 1]`. The radius can be
specified using a single number or a list of three numbers, one for each axis.
`radius`, which default to `[0, 0, 0]` and `[1, 1, 1]`. The radius can be
specified using a single number or a list of three numbers, one for each axis.
Parameters:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
Properties
|
- Source:
Returns:
- Type
- Cesium.CSG
Example
var cube = CSG.cube({
center: [0, 0, 0],
radius: 1
});
(static) CSG.cylinder(options) → {Cesium.CSG}
Construct a solid cylinder. Optional parameters are `start`, `end`,
`radius`, and `slices`, which default to `[0, -1, 0]`, `[0, 1, 0]`, `1`, and
`16`. The `slices` parameter controls the tessellation.
`radius`, and `slices`, which default to `[0, -1, 0]`, `[0, 1, 0]`, `1`, and
`16`. The `slices` parameter controls the tessellation.
Parameters:
Name | Type | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
Properties
|
- Source:
Returns:
- Type
- Cesium.CSG
Example
var cylinder = CSG.cylinder({
start: [0, -1, 0],
end: [0, 1, 0],
radius: 1,
slices: 16
});
(static) CSG.sphere(options) → {Cesium.CSG}
Construct a solid sphere. Optional parameters are `center`, `radius`,
`slices`, and `stacks`, which default to `[0, 0, 0]`, `1`, `16`, and `8`.
The `slices` and `stacks` parameters control the tessellation along the
longitude and latitude directions.
`slices`, and `stacks`, which default to `[0, 0, 0]`, `1`, `16`, and `8`.
The `slices` and `stacks` parameters control the tessellation along the
longitude and latitude directions.
Parameters:
Name | Type | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
Properties
|
- Source:
Returns:
- Type
- Cesium.CSG
Example
var sphere = CSG.sphere({
center: [0, 0, 0],
radius: 1,
slices: 16,
stacks: 8
});
(static) fromCSG(csg_model) → {Cesium.Geometry}
Parameters:
Name | Type | Description |
---|---|---|
csg_model |
CSG |
- Source:
Returns:
- Type
- Cesium.Geometry
(static) fromPolygons(polygons)
Construct a CSG solid from a list of `CSG.Polygon` instances.
Parameters:
Name | Type | Description |
---|---|---|
polygons |
Array.<Cesium.CSG.Polygon> | |
|
Array.<Cesium.CSG> |
- Source:
(static) Plane.fromPoints(a, b, c)
Parameters:
Name | Type | Description |
---|---|---|
a |
Cesium.CSG.Vector | |
b |
Cesium.CSG.Vector | |
c |
Cesium.CSG.Vector |
- Source:
(static) toCSG(geometry, offsetopt) → {CSG}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
geometry |
Cesium.Geometry | ||
offset |
Cesium.Cartesian3 |
<optional> |
- Source:
Returns:
- Type
- CSG
clone() → {Cesium.CSG}
- Source:
Returns:
- Type
- Cesium.CSG
intersect(csg) → {Cesium.CSG}
Return a new CSG solid representing space both this solid and in the
solid `csg`. Neither this solid nor the solid `csg` are modified.
A.intersect(B)
solid `csg`. Neither this solid nor the solid `csg` are modified.
A.intersect(B)
+-------+
| |
| A |
| +--+----+ = +--+
+----+--+ | +--+
| B |
| |
+-------+
Parameters:
Name | Type | Description |
---|---|---|
csg |
Cesium.CSG |
- Source:
Returns:
- Type
- Cesium.CSG
inverse() → {Cesium.CSG}
Return a new CSG solid with solid and empty space switched. This solid is
not modified.
- Source:
Returns:
- Type
- Cesium.CSG
subtract(csg) → {Cesium.CSG}
Return a new CSG solid representing space in this solid but not in the
solid `csg`. Neither this solid nor the solid `csg` are modified.
A.subtract(B)
solid `csg`. Neither this solid nor the solid `csg` are modified.
A.subtract(B)
+-------+ +-------+
| | | |
| A | | |
| +--+----+ = | +--+
+----+--+ | +----+
| B |
| |
+-------+
Parameters:
Name | Type | Description |
---|---|---|
csg |
Cesium.CSG |
- Source:
Returns:
- Type
- Cesium.CSG
toPolygons() → {Array.<Cesium.CSG.Polygon>}
- Source:
Returns:
- Type
- Array.<Cesium.CSG.Polygon>
union(csg) → {Cesium.CSG}
Return a new CSG solid representing space in either this solid or in the
solid `csg`. Neither this solid nor the solid `csg` are modified.
A.union(B)
solid `csg`. Neither this solid nor the solid `csg` are modified.
A.union(B)
+-------+ +-------+
| | | |
| A | | |
| +--+----+ = | +----+
+----+--+ | +----+ |
| B | | |
| | | |
+-------+ +-------+
Parameters:
Name | Type | Description |
---|---|---|
csg |
Cesium.CSG |
- Source:
Returns:
- Type
- Cesium.CSG