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

Adds normalShading option to the repo #5433

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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: 22 additions & 5 deletions Source/Scene/PointCloud3DTileContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ define([
this.backFaceCulling = false;
this._backFaceCulling = false;

// Whether to enable normal shading
this.normalShading = true;
this._normalShading = true;

this._opaqueRenderState = undefined;
this._translucentRenderState = undefined;

Expand Down Expand Up @@ -786,6 +790,7 @@ define([
var hasNormals = content._hasNormals;
var hasBatchIds = content._hasBatchIds;
var backFaceCulling = content._backFaceCulling;
var normalShading = content._normalShading;
var vertexArray = content._drawCommand.vertexArray;

var colorStyleFunction;
Expand Down Expand Up @@ -860,13 +865,20 @@ define([
colorVertexAttribute.enabled = usesColors;
}

var usesNormals = hasNormals && (normalShading || backFaceCulling || usesNormalSemantic);
if (hasNormals) {
// Disable the normal vertex attribute if normals are not used
var normalVertexAttribute = getVertexAttribute(vertexArray, normalLocation);
normalVertexAttribute.enabled = usesNormals;
}

var attributeLocations = {
a_position : positionLocation
};
if (usesColors) {
attributeLocations.a_color = colorLocation;
}
if (hasNormals) {
if (usesNormals) {
attributeLocations.a_normal = normalLocation;
}
if (hasBatchIds) {
Expand Down Expand Up @@ -921,7 +933,7 @@ define([
vs += 'attribute vec3 a_color; \n';
}
}
if (hasNormals) {
if (usesNormals) {
if (isOctEncoded16P) {
vs += 'attribute vec2 a_normal; \n';
} else {
Expand Down Expand Up @@ -980,7 +992,7 @@ define([
}
vs += ' vec3 position_absolute = vec3(czm_model * vec4(position, 1.0)); \n';

if (hasNormals) {
if (usesNormals) {
if (isOctEncoded16P) {
vs += ' vec3 normal = czm_octDecode(a_normal); \n';
} else {
Expand All @@ -1006,7 +1018,7 @@ define([

vs += ' color = color * u_highlightColor; \n';

if (hasNormals) {
if (usesNormals && normalShading) {
vs += ' normal = czm_normal * normal; \n' +
' float diffuseStrength = czm_getLambertDiffuse(czm_sunDirectionEC, normal); \n' +
' diffuseStrength = max(diffuseStrength, 0.4); \n' + // Apply some ambient lighting
Expand All @@ -1016,7 +1028,7 @@ define([
vs += ' v_color = color; \n' +
' gl_Position = czm_modelViewProjection * vec4(position, 1.0); \n';

if (hasNormals && backFaceCulling) {
if (usesNormals && backFaceCulling) {
vs += ' float visible = step(-normal.z, 0.0); \n' +
' gl_Position *= visible; \n' +
' gl_PointSize *= visible; \n';
Expand Down Expand Up @@ -1219,6 +1231,11 @@ define([
createShaders(this, frameState, tileset.style);
}

if (this.normalShading !== this._normalShading) {
this._normalShading = this.normalShading;
createShaders(this, frameState, tileset.style);
}

// Update the render state
var isTranslucent = (this._highlightColor.alpha < 1.0) || (this._constantColor.alpha < 1.0) || this._styleTranslucent;
this._drawCommand.renderState = isTranslucent ? this._translucentRenderState : this._opaqueRenderState;
Expand Down
11 changes: 11 additions & 0 deletions Specs/Scene/PointCloud3DTileContentSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,17 @@ defineSuite([
});
});

it('rebuilds shader when normalShading changes', function() {
return Cesium3DTilesTester.loadTileset(scene, pointCloudQuantizedOctEncodedUrl).then(function(tileset) {
var content = tileset._root.content;
content.normalShading = true;
expect(scene).toRenderAndCall(function(rgba) {
content.normalShading = false;
expect(scene).notToRender(rgba);
});
});
});

it('applies shader style to point cloud with normals', function() {
return Cesium3DTilesTester.loadTileset(scene, pointCloudQuantizedOctEncodedUrl).then(function(tileset) {
tileset.style = new Cesium3DTileStyle({
Expand Down