Skip to content

Commit

Permalink
Merge pull request #10703 from CesiumGS/points-back-face-culling
Browse files Browse the repository at this point in the history
Add support for point cloud back face culling
  • Loading branch information
j9liu authored Aug 19, 2022
2 parents 318bfae + 60694dd commit 4b333bc
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 148 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
##### Additions :tada:

- `Model` can now classify other assets with a given `classificationType`. [#10623](https://github.com/CesiumGS/cesium/pull/10623)
- `Model` now supports back face culling for point clouds. [#10703](https://github.com/CesiumGS/cesium/pull/10703)

##### Fixes :wrench:

Expand Down
2 changes: 1 addition & 1 deletion Source/Scene/Model/MaterialPipelineStage.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ MaterialPipelineStage.process = function (
shaderBuilder.addDefine(
"HAS_DOUBLE_SIDED_MATERIAL",
undefined,
ShaderDestination.FRAGMENT
ShaderDestination.BOTH
);
}
};
Expand Down
16 changes: 12 additions & 4 deletions Source/Scene/Model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ function Model(options) {
const pointCloudShading = new PointCloudShading(options.pointCloudShading);
this._pointCloudShading = pointCloudShading;
this._attenuation = pointCloudShading.attenuation;
this._pointCloudBackFaceCulling = pointCloudShading.backFaceCulling;

// If the given clipping planes don't have an owner, make this model its owner.
// Otherwise, the clipping planes are passed down from a tileset.
Expand Down Expand Up @@ -1674,7 +1675,7 @@ Model.prototype.update = function (frameState) {
return;
}

updatePointCloudAttenuation(this);
updatePointCloudShading(this);
updateSilhouette(this, frameState);
updateSkipLevelOfDetail(this, frameState);
updateClippingPlanes(this, frameState);
Expand Down Expand Up @@ -1733,12 +1734,19 @@ function updateImageBasedLighting(model, frameState) {
}
}

function updatePointCloudAttenuation(model) {
function updatePointCloudShading(model) {
const pointCloudShading = model.pointCloudShading;

// Check if the shader needs to be updated for point cloud attenuation
// settings.
if (model.pointCloudShading.attenuation !== model._attenuation) {
if (pointCloudShading.attenuation !== model._attenuation) {
model.resetDrawCommands();
model._attenuation = pointCloudShading.attenuation;
}

if (pointCloudShading.backFaceCulling !== model._pointCloudBackFaceCulling) {
model.resetDrawCommands();
model._attenuation = model.pointCloudShading.attenuation;
model._pointCloudBackFaceCulling = pointCloudShading.backFaceCulling;
}
}

Expand Down
4 changes: 3 additions & 1 deletion Source/Scene/Model/ModelRuntimePrimitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ ModelRuntimePrimitive.prototype.configurePipeline = function (frameState) {
const pointCloudShading = model.pointCloudShading;
const hasAttenuation =
defined(pointCloudShading) && pointCloudShading.attenuation;
const hasPointCloudBackFaceCulling =
defined(pointCloudShading) && pointCloudShading.backFaceCulling;
const hasPointCloudStyle =
primitive.primitiveType === PrimitiveType.POINTS &&
(defined(style) || hasAttenuation);
(defined(style) || hasAttenuation || hasPointCloudBackFaceCulling);

const hasOutlines =
model._enableShowOutline && defined(primitive.outlineCoordinates);
Expand Down
10 changes: 9 additions & 1 deletion Source/Scene/Model/PointCloudStylingPipelineStage.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,22 @@ PointCloudStylingPipelineStage.process = function (
}

const pointCloudShading = model.pointCloudShading;
if (model._attenuation) {
if (pointCloudShading.attenuation) {
shaderBuilder.addDefine(
"HAS_POINT_CLOUD_ATTENUATION",
undefined,
ShaderDestination.VERTEX
);
}

if (pointCloudShading.backFaceCulling) {
shaderBuilder.addDefine(
"HAS_POINT_CLOUD_BACK_FACE_CULLING",
undefined,
ShaderDestination.VERTEX
);
}

let content;
let is3DTiles;
let usesAddRefinement;
Expand Down
2 changes: 1 addition & 1 deletion Source/Shaders/Model/GeometryStageVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ vec4 geometryStage(inout ProcessedAttributes attributes, mat4 modelView, mat3 no
#endif

#ifdef HAS_NORMALS
v_normalEC = normal * attributes.normalMC;
v_normalEC = normalize(normal * attributes.normalMC);
#endif

#ifdef HAS_TANGENTS
Expand Down
1 change: 0 additions & 1 deletion Source/Shaders/Model/MaterialStageFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ vec3 computeNormal(ProcessedAttributes attributes)

void materialStage(inout czm_modelMaterial material, ProcessedAttributes attributes, SelectedFeature feature)
{

#ifdef HAS_NORMALS
material.normalEC = computeNormal(attributes);
#endif
Expand Down
4 changes: 4 additions & 0 deletions Source/Shaders/Model/ModelVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ void main()
float show = 1.0;
#endif

#ifdef HAS_POINT_CLOUD_BACK_FACE_CULLING
show *= pointCloudBackFaceCullingStage();
#endif

#ifdef HAS_POINT_CLOUD_COLOR_STYLE
v_pointCloudColor = pointCloudColorStylingStage(attributes, metadata);
#endif
Expand Down
11 changes: 11 additions & 0 deletions Source/Shaders/Model/PointCloudStylingStageVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ float pointCloudPointSizeStylingStage(in ProcessedAttributes attributes, in Meta
float pointCloudPointSizeStylingStage(in ProcessedAttributes attributes, in Metadata metadata) {
return getPointSizeFromAttenuation(v_positionEC);
}
#endif

#ifdef HAS_POINT_CLOUD_BACK_FACE_CULLING
float pointCloudBackFaceCullingStage() {
#if defined(HAS_NORMALS) && !defined(HAS_DOUBLE_SIDED_MATERIAL)
// This needs to be computed in eye coordinates so we can't use attributes.normalMC
return step(-v_normalEC.z, 0.0);
#else
return 1.0;
#endif
}
#endif
Loading

0 comments on commit 4b333bc

Please sign in to comment.