Skip to content

Commit b80f2e8

Browse files
author
Omar Shehata
authored
Merge pull request #8621 from CesiumGS/glsl-null-undefined
Add isNaN, isFinite, null, and undefined support to GLSL styling backend
2 parents 47f7c61 + 54f71be commit b80f2e8

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Change Log
99
* Added `tileset.uri`, `tileset.show`, and `tileset.maximumScreenSpaceError` properties to CZML processing for loading 3D Tiles.
1010
* Added `Color.lerp` for linearly interpolating between two RGB colors. [#8607](https://github.com/CesiumGS/cesium/pull/8607)
1111
* `CesiumTerrainProvider` now supports terrain tiles using a `WebMercatorTilingScheme` by specifying `"projection": "EPSG:3857"` in `layer.json`. It also now supports numbering tiles from the North instead of the South by specifying `"scheme": "slippyMap"` in `layer.json`. [#8563](https://github.com/CesiumGS/cesium/pull/8563)
12+
* Added basic support for `isNaN`, `isFinite`, `null`, and `undefined` in the 3D Tiles styling GLSL backend for point clouds. [#8621](https://github.com/CesiumGS/cesium/pull/8621)
1213

1314
##### Fixes :wrench:
1415

Source/Scene/Expression.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,8 @@ import ExpressionNodeType from './ExpressionNodeType.js';
15591559
return expressions;
15601560
}
15611561

1562+
var nullSentinel = 'czm_infinity'; // null just needs to be some sentinel value that will cause "[expression] === null" to be false in nearly all cases. GLSL doesn't have a NaN constant so use czm_infinity.
1563+
15621564
Node.prototype.getShaderExpression = function(attributePrefix, shaderState, parent) {
15631565
var color;
15641566
var left;
@@ -1603,7 +1605,13 @@ import ExpressionNodeType from './ExpressionNodeType.js';
16031605
return 'floor(' + left + ' + 0.5)';
16041606
} else if (defined(unaryFunctions[value])) {
16051607
return value + '(' + left + ')';
1606-
} else if ((value === 'isNaN') || (value === 'isFinite') || (value === 'String') || (value === 'isExactClass') || (value === 'isClass') || (value === 'getExactClassName')) {
1608+
} else if (value === 'isNaN') {
1609+
// In GLSL 2.0 use isnan instead
1610+
return '(' + left + ' != ' + left + ')';
1611+
} else if (value === 'isFinite') {
1612+
// In GLSL 2.0 use isinf instead. GLSL doesn't have an infinity constant so use czm_infinity which is an arbitrarily big enough number.
1613+
return '(abs(' + left + ') < czm_infinity)';
1614+
} else if ((value === 'String') || (value === 'isExactClass') || (value === 'isClass') || (value === 'getExactClassName')) {
16071615
throw new RuntimeError('Error generating style shader: "' + value + '" is not supported.');
16081616
} else if (defined(unaryFunctions[value])) {
16091617
return value + '(' + left + ')';
@@ -1659,7 +1667,7 @@ import ExpressionNodeType from './ExpressionNodeType.js';
16591667
case ExpressionNodeType.VARIABLE_IN_STRING:
16601668
throw new RuntimeError('Error generating style shader: Converting a variable to a string is not supported.');
16611669
case ExpressionNodeType.LITERAL_NULL:
1662-
throw new RuntimeError('Error generating style shader: null is not supported.');
1670+
return nullSentinel;
16631671
case ExpressionNodeType.LITERAL_BOOLEAN:
16641672
return value ? 'true' : 'false';
16651673
case ExpressionNodeType.LITERAL_NUMBER:
@@ -1745,7 +1753,7 @@ import ExpressionNodeType from './ExpressionNodeType.js';
17451753
case ExpressionNodeType.LITERAL_REGEX:
17461754
throw new RuntimeError('Error generating style shader: Regular expressions are not supported.');
17471755
case ExpressionNodeType.LITERAL_UNDEFINED:
1748-
throw new RuntimeError('Error generating style shader: undefined is not supported.');
1756+
return nullSentinel;
17491757
case ExpressionNodeType.BUILTIN_VARIABLE:
17501758
if (value === 'tiles3d_tileset_time') {
17511759
return 'u_time';

Specs/Scene/ExpressionSpec.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -3546,6 +3546,34 @@ describe('Scene/Expression', function() {
35463546
expect(shaderExpression).toEqual(expected);
35473547
});
35483548

3549+
it('gets shader expression for isNaN', function() {
3550+
var expression = new Expression('isNaN(1.0)');
3551+
var shaderExpression = expression.getShaderExpression('', {});
3552+
var expected = '(1.0 != 1.0)';
3553+
expect(shaderExpression).toEqual(expected);
3554+
});
3555+
3556+
it('gets shader expression for isFinite', function() {
3557+
var expression = new Expression('isFinite(1.0)');
3558+
var shaderExpression = expression.getShaderExpression('', {});
3559+
var expected = '(abs(1.0) < czm_infinity)';
3560+
expect(shaderExpression).toEqual(expected);
3561+
});
3562+
3563+
it('gets shader expression for null', function() {
3564+
var expression = new Expression('null');
3565+
var shaderExpression = expression.getShaderExpression('', {});
3566+
var expected = 'czm_infinity';
3567+
expect(shaderExpression).toEqual(expected);
3568+
});
3569+
3570+
it('gets shader expression for undefined', function() {
3571+
var expression = new Expression('undefined');
3572+
var shaderExpression = expression.getShaderExpression('', {});
3573+
var expected = 'czm_infinity';
3574+
expect(shaderExpression).toEqual(expected);
3575+
});
3576+
35493577
it('throws when getting shader expression for regex', function() {
35503578
var expression = new Expression('regExp("a").test("abc")');
35513579
expect(function() {
@@ -3610,34 +3638,6 @@ describe('Scene/Expression', function() {
36103638
}).toThrowRuntimeError();
36113639
});
36123640

3613-
it('throws when getting shader expression for literal undefined', function() {
3614-
var expression = new Expression('undefined');
3615-
expect(function() {
3616-
return expression.getShaderExpression('', {});
3617-
}).toThrowRuntimeError();
3618-
});
3619-
3620-
it('throws when getting shader expression for literal null', function() {
3621-
var expression = new Expression('null');
3622-
expect(function() {
3623-
return expression.getShaderExpression('', {});
3624-
}).toThrowRuntimeError();
3625-
});
3626-
3627-
it('throws when getting shader expression for isNaN', function() {
3628-
var expression = new Expression('isNaN(1.0)');
3629-
expect(function() {
3630-
return expression.getShaderExpression('', {});
3631-
}).toThrowRuntimeError();
3632-
});
3633-
3634-
it('throws when getting shader expression for isFinite', function() {
3635-
var expression = new Expression('isFinite(1.0)');
3636-
expect(function() {
3637-
return expression.getShaderExpression('', {});
3638-
}).toThrowRuntimeError();
3639-
});
3640-
36413641
it('throws when getting shader expression for isExactClass', function() {
36423642
var expression = new Expression('isExactClass("door")');
36433643
expect(function() {

0 commit comments

Comments
 (0)