Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clipping planes for models, tilesets, point clouds #5913

Merged
merged 20 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ define([
* @param {Color} [options.silhouetteColor=Color.RED] The silhouette color. If more than 256 models have silhouettes enabled, there is a small chance that overlapping models will have minor artifacts.
* @param {Number} [options.silhouetteSize=0.0] The size of the silhouette in pixels.
* @param {Plane[]} [options.clippingPlanes=[]] An array of {@link Plane} used to clip the model.
* @param {}
*
* @exception {DeveloperError} bgltf is not a valid Binary glTF file.
* @exception {DeveloperError} Only glTF Binary version 1 is supported.
Expand Down Expand Up @@ -699,6 +698,8 @@ define([
this._rtcCenterEye = undefined; // in eye coordinates
this._rtcCenter3D = undefined; // in world coordinates
this._rtcCenter2D = undefined; // in projected world coordinates

this._packedClippingPlanes = [];
}

defineProperties(Model.prototype, {
Expand Down Expand Up @@ -3312,7 +3313,6 @@ define([
}

var scratchCartesian = new Cartesian3();
var scratchPlane = new Cartesian4();
var scratchMatrix = new Matrix4();

function createClippingPlanesFunction(model, context) {
Expand All @@ -3323,15 +3323,15 @@ define([

var planes = model.clippingPlanes;
var length = planes.length;
var packedPlanes = new Array(length);
var packedPlanes = model._packedClippingPlanes;
for (var i = 0; i < length; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be limited to 6. Check what happens if you have more than 6 clipping planes, there may be an error in setting the uniform.

var plane = planes[i];
Matrix4.multiplyByPointAsVector(scratchMatrix, plane.normal, scratchPlane);
Cartesian3.normalize(scratchPlane, scratchPlane);
var packedPlane = packedPlanes[i];
Matrix4.multiplyByPointAsVector(scratchMatrix, plane.normal, packedPlane);
Cartesian3.normalize(packedPlane, packedPlane);
Cartesian3.multiplyByScalar(plane.normal, plane.distance, scratchCartesian);
Matrix4.multiplyByPoint(scratchMatrix, scratchCartesian, scratchCartesian);
scratchPlane.w = Cartesian3.dot(scratchPlane, scratchCartesian);
packedPlanes[i] = scratchPlane.clone();
packedPlane.w = Cartesian3.dot(packedPlane, scratchCartesian);
}
return packedPlanes;
};
Expand Down Expand Up @@ -4263,6 +4263,18 @@ define([
}
}

function updateClippingPlanes(model) {
var length = model.clippingPlanes.length;

if (model._packedClippingPlanes.length !== length) {
model._packedClippingPlanes= new Array(length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight spacing tweak:
model._packedClippingPlanes = new Array(length);


for (var i = 0; i < length; ++i) {
model._packedClippingPlanes[i] = new Cartesian4();
}
}
}

var scratchBoundingSphere = new BoundingSphere();

function scaleInPixels(positionWC, radius, frameState) {
Expand Down Expand Up @@ -4727,6 +4739,7 @@ define([
updateShadows(this);
updateColor(this, frameState);
updateSilhouette(this, frameState);
updateClippingPlanes(this);
}

if (justLoaded) {
Expand Down
22 changes: 17 additions & 5 deletions Source/Scene/PointCloud3DTileContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ define([

this._features = undefined;

this._packedClippingPlanes = [];

/**
* @inheritdoc Cesium3DTileContent#featurePropertiesDirty
*/
Expand Down Expand Up @@ -513,7 +515,6 @@ define([
}

var scratchCartesian = new Cartesian3();
var scratchPlane = new Cartesian4();

var uniformMap = {
u_pointSizeAndTilesetTime : function() {
Expand All @@ -533,14 +534,15 @@ define([
u_clippingPlanes : function() {
var planes = content._tileset.clippingPlanes;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment.

var length = planes.length;
var packedPlanes = new Array(length);
var packedPlanes = content._packedClippingPlanes;
for (var i = 0; i < length; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limit to 6 here as well.

var plane = planes[i];
Matrix3.multiplyByVector(context.uniformState.normal, plane.normal, scratchPlane);
var packedPlane = packedPlanes[i];

Matrix3.multiplyByVector(context.uniformState.normal, plane.normal, packedPlane);
Cartesian3.multiplyByScalar(plane.normal, plane.distance, scratchCartesian);
Matrix4.multiplyByPoint(context.uniformState.modelView3D, scratchCartesian, scratchCartesian);
scratchPlane.w = Cartesian3.dot(scratchPlane, scratchCartesian);
packedPlanes[i] = scratchPlane.clone();
packedPlane.w = Cartesian3.dot(packedPlane, scratchCartesian);
}
return packedPlanes;
}
Expand Down Expand Up @@ -1262,6 +1264,16 @@ define([
if (passes.pick) {
commandList.push(this._pickCommand);
}

// update clipping planes
var length = this._tileset.clippingPlanes.length;
if (this._packedClippingPlanes.length !== length) {
this._packedClippingPlanes = new Array(length);

for (var i = 0; i < length; ++i) {
this._packedClippingPlanes[i] = new Cartesian4();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place this above the command list code. In general commands go last.

};

/**
Expand Down