Skip to content

Commit 279fe72

Browse files
committed
Merge pull request #3915 from AnalyticalGraphicsInc/bounding-sphere-culling-volume
Create a culling volume from a bounding sphere
2 parents 8d8e234 + dfad5fa commit 279fe72

File tree

2 files changed

+265
-66
lines changed

2 files changed

+265
-66
lines changed

Source/Scene/CullingVolume.js

+74-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/*global define*/
22
define([
33
'../Core/Cartesian3',
4+
'../Core/Cartesian4',
45
'../Core/defaultValue',
56
'../Core/defined',
67
'../Core/DeveloperError',
78
'../Core/Intersect',
89
'../Core/Plane'
910
], function(
1011
Cartesian3,
12+
Cartesian4,
1113
defaultValue,
1214
defined,
1315
DeveloperError,
@@ -21,7 +23,7 @@ define([
2123
* @alias CullingVolume
2224
* @constructor
2325
*
24-
* @param {Cartesian4[]} planes An array of clipping planes.
26+
* @param {Cartesian4[]} [planes] An array of clipping planes.
2527
*/
2628
function CullingVolume(planes) {
2729
/**
@@ -34,7 +36,78 @@ define([
3436
this.planes = defaultValue(planes, []);
3537
}
3638

39+
var faces = [new Cartesian3(), new Cartesian3(), new Cartesian3()];
40+
Cartesian3.clone(Cartesian3.UNIT_X, faces[0]);
41+
Cartesian3.clone(Cartesian3.UNIT_Y, faces[1]);
42+
Cartesian3.clone(Cartesian3.UNIT_Z, faces[2]);
43+
44+
var scratchPlaneCenter = new Cartesian3();
45+
var scratchPlaneNormal = new Cartesian3();
3746
var scratchPlane = new Plane(new Cartesian3(), 0.0);
47+
48+
/**
49+
* Constructs a culling volume from a bounding sphere. Creates six planes that create a box containing the sphere.
50+
* The planes are aligned to the x, y, and z axes in world coordinates.
51+
*
52+
* @param {BoundingSphere} boundingSphere The bounding sphere used to create the culling volume.
53+
* @param {CullingVolume} [result] The object onto which to store the result.
54+
* @returns {CullingVolume} The culling volume created from the bounding sphere.
55+
*/
56+
CullingVolume.fromBoundingSphere = function(boundingSphere, result) {
57+
//>>includeStart('debug', pragmas.debug);
58+
if (!defined(boundingSphere)) {
59+
throw new DeveloperError('boundingSphere is required.');
60+
}
61+
//>>includeEnd('debug');
62+
63+
if (!defined(result)) {
64+
result = new CullingVolume();
65+
}
66+
67+
var length = faces.length;
68+
var planes = result.planes;
69+
planes.length = 2 * length;
70+
71+
var center = boundingSphere.center;
72+
var radius = boundingSphere.radius;
73+
74+
var planeIndex = 0;
75+
76+
for (var i = 0; i < length; ++i) {
77+
var faceNormal = faces[i];
78+
79+
var plane0 = planes[planeIndex];
80+
var plane1 = planes[planeIndex + 1];
81+
82+
if (!defined(plane0)) {
83+
plane0 = planes[planeIndex] = new Cartesian4();
84+
}
85+
if (!defined(plane1)) {
86+
plane1 = planes[planeIndex + 1] = new Cartesian4();
87+
}
88+
89+
Cartesian3.multiplyByScalar(faceNormal, -radius, scratchPlaneCenter);
90+
Cartesian3.add(center, scratchPlaneCenter, scratchPlaneCenter);
91+
92+
plane0.x = faceNormal.x;
93+
plane0.y = faceNormal.y;
94+
plane0.z = faceNormal.z;
95+
plane0.w = -Cartesian3.dot(faceNormal, scratchPlaneCenter);
96+
97+
Cartesian3.multiplyByScalar(faceNormal, radius, scratchPlaneCenter);
98+
Cartesian3.add(center, scratchPlaneCenter, scratchPlaneCenter);
99+
100+
plane1.x = -faceNormal.x;
101+
plane1.y = -faceNormal.y;
102+
plane1.z = -faceNormal.z;
103+
plane1.w = -Cartesian3.dot(Cartesian3.negate(faceNormal, scratchPlaneNormal), scratchPlaneCenter);
104+
105+
planeIndex += 2;
106+
}
107+
108+
return result;
109+
};
110+
38111
/**
39112
* Determines whether a bounding volume intersects the culling volume.
40113
*

0 commit comments

Comments
 (0)