Skip to content

Commit

Permalink
Merge pull request #4775 from leesafini/unaryfns
Browse files Browse the repository at this point in the history
More unary fns for 3d-tiles
  • Loading branch information
lilleyse authored Jan 13, 2017
2 parents 70e841b + 792d246 commit 69fcd93
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 11 deletions.
20 changes: 20 additions & 0 deletions Apps/Sandcastle/gallery/3D Tiles Point Cloud Styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@
pointSize : "5"
});

addStyle('Sign', {
color : "rgb(sign(${POSITION}[0]) * 255, sign(${POSITION}[1]) * 255, sign(${POSITION}[2]) * 255)",
pointSize : "5"
});

addStyle('Rounding Functions', {
color : "rgb(floor(${POSITION}[0]) * 255, ceil(${POSITION}[1]) * 255, round(${POSITION}[2]) * 255)",
pointSize : "5"
});

addStyle('Exp and Log Functions', {
color : "rgb(log(${POSITION}[0]) * 255, log2(${POSITION}[1]) * 255 + exp2(${POSITION}[1]) * 255, exp(${POSITION}[2]) * 255)",
pointSize : "5"
});

addStyle('Fractional Part', {
color : "rgb(fract(${POSITION}[0]) * 255, fract(${POSITION}[1]) * 255, fract(${POSITION}[2]) * 255)",
pointSize : "5"
});

addStyle('Pow', {
color : "color() * pow(${temperature}, 3)",
pointSize : "5"
Expand Down
1 change: 0 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
* [Safe Software](https://www.safe.com)
* [Joel Depooter](https://github.com/JDepooter)


## [Individual CLA](http://www.agi.com/licenses/individual-cla-agi-v1.0.txt)
* [Victor Berchet](https://github.com/vicb)
* [Caleb Morse](https://github.com/cmorse)
Expand Down
33 changes: 26 additions & 7 deletions Source/Scene/Expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,35 @@ define([
asin : Math.asin,
atan : Math.atan,
radians : CesiumMath.toRadians,
degrees : CesiumMath.toDegrees
degrees : CesiumMath.toDegrees,
sign : CesiumMath.sign,
floor : Math.floor,
ceil : Math.ceil,
round : Math.round,
exp : Math.exp,
exp2 : exp2,
log : Math.log,
log2 : log2,
fract : fract
};

var ternaryFunctions = {
clamp : CesiumMath.clamp,
mix : CesiumMath.lerp
};

function fract(number) {
return number - Math.floor(number);
}

function exp2(exponent) {
return Math.pow(2.0,exponent);
}

function log2(number) {
return CesiumMath.logBase(number, 2.0);
}

/**
* Evaluates an expression defined using the
* {@link https://github.com/AnalyticalGraphicsInc/3d-tiles/tree/master/Styling|3D Tiles Styling language}.
Expand Down Expand Up @@ -1418,12 +1439,10 @@ define([
return 'bool(' + left + ')';
} else if (value === 'Number') {
return 'float(' + left + ')';
} else if (value === 'abs') {
return 'abs(' + left + ')';
} else if (value === 'cos') {
return 'cos(' + left + ')';
} else if (value === 'sqrt') {
return 'sqrt(' + left + ')';
} else if (value === 'round') {
return 'floor(' + left + ' + 0.5)';
} else if (defined(unaryFunctions[value])) {
return value + '(' + left + ')';
} else if ((value === 'isNaN') || (value === 'isFinite') || (value === 'String') || (value === 'isExactClass') || (value === 'isClass') || (value === 'getExactClassName')) {
//>>includeStart('debug', pragmas.debug);
throw new DeveloperError('Error generating style shader: "' + value + '" is not supported.');
Expand Down
6 changes: 3 additions & 3 deletions Source/Scene/modelMaterialsCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ define([
vertexShader += 'attribute vec4 a_joint;\n';
vertexShader += 'attribute vec4 a_weight;\n';
}

if (options.addBatchIdToGeneratedShaders) {
techniqueAttributes.a_batchId = 'batchId';
techniqueParameters.batchId = {
Expand Down Expand Up @@ -696,7 +696,7 @@ define([
if (!defined(gltf)) {
return undefined;
}

options = defaultValue(options, defaultValue.EMPTY_OBJECT);

var hasExtension = false;
Expand Down Expand Up @@ -724,7 +724,7 @@ define([
}

var lightParameters = generateLightParameters(gltf);

var hasCesiumRTCExtension = defined(gltf.extensions) && defined(gltf.extensions.CESIUM_RTC);
var addBatchIdToGeneratedShaders = defaultValue(options.addBatchIdToGeneratedShaders, false);

Expand Down
246 changes: 246 additions & 0 deletions Specs/Scene/ExpressionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,189 @@ defineSuite([
}).toThrowDeveloperError();
});

it('evaluates sign function', function() {
var expression = new Expression('sign(5.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(1.0);

expression = new Expression('sign(0.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.0);

expression = new Expression('sign(-5.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(-1.0);
});

it('throws if sign function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('sign()');
}).toThrowDeveloperError();

expect(function() {
return new Expression('sign(1, 2)');
}).toThrowDeveloperError();
});

it('evaluates floor function', function() {
var expression = new Expression('floor(5.5)');
expect(expression.evaluate(frameState, undefined)).toEqual(5.0);

expression = new Expression('floor(0.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.0);

expression = new Expression('floor(-1.2)');
expect(expression.evaluate(frameState, undefined)).toEqual(-2.0);
});

it('throws if floor function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('floor()');
}).toThrowDeveloperError();

expect(function() {
return new Expression('floor(1, 2)');
}).toThrowDeveloperError();
});

it('evaluates ceil function', function() {
var expression = new Expression('ceil(5.5)');
expect(expression.evaluate(frameState, undefined)).toEqual(6.0);

expression = new Expression('ceil(0.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.0);

expression = new Expression('ceil(-1.2)');
expect(expression.evaluate(frameState, undefined)).toEqual(-1.0);
});

it('throws if ceil function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('ceil()');
}).toThrowDeveloperError();

expect(function() {
return new Expression('ceil(1, 2)');
}).toThrowDeveloperError();
});

it('evaluates round function', function() {
var expression = new Expression('round(5.5)');
expect(expression.evaluate(frameState, undefined)).toEqual(6);

expression = new Expression('round(0.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(0);

expression = new Expression('round(1.2)');
expect(expression.evaluate(frameState, undefined)).toEqual(1);
});

it('throws if round function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('round()');
}).toThrowDeveloperError();

expect(function() {
return new Expression('round(1, 2)');
}).toThrowDeveloperError();
});

it('evaluates exp function', function() {
var expression = new Expression('exp(1.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(Math.E);

expression = new Expression('exp(0.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(1.0);
});

it('throws if exp function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('exp()');
}).toThrowDeveloperError();

expect(function() {
return new Expression('exp(1, 2)');
}).toThrowDeveloperError();
});

it('evaluates exp2 function', function() {
var expression = new Expression('exp2(1.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(2.0);

expression = new Expression('exp2(0.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(1.0);

expression = new Expression('exp2(2.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(4.0);
});

it('throws if exp2 function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('exp2()');
}).toThrowDeveloperError();

expect(function() {
return new Expression('exp2(1, 2)');
}).toThrowDeveloperError();
});

it('evaluates log function', function() {
var expression = new Expression('log(1.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.0);

expression = new Expression('log(10.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(2.302585092994046);
});

it('throws if log function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('log()');
}).toThrowDeveloperError();

expect(function() {
return new Expression('log(1, 2)');
}).toThrowDeveloperError();
});

it('evaluates log2 function', function() {
var expression = new Expression('log2(1.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.0);

expression = new Expression('log2(2.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(1.0);

expression = new Expression('log2(4.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(2.0);
});

it('throws if log2 function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('log2()');
}).toThrowDeveloperError();

expect(function() {
return new Expression('log2(1, 2)');
}).toThrowDeveloperError();
});

it('evaluates fract function', function() {
var expression = new Expression('fract(1.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.0);

expression = new Expression('fract(2.25)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.25);

expression = new Expression('fract(-2.25)');
expect(expression.evaluate(frameState, undefined)).toEqual(0.75);
});

it('throws if fract function takes an invalid number of arguments', function() {
expect(function() {
return new Expression('log2()');
}).toThrowDeveloperError();

expect(function() {
return new Expression('log2(1, 2)');
}).toThrowDeveloperError();
});

it('evaluates clamp function', function() {
var expression = new Expression('clamp(50.0, 0.0, 100.0)');
expect(expression.evaluate(frameState, undefined)).toEqual(50.0);
Expand Down Expand Up @@ -2458,6 +2641,69 @@ defineSuite([
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for sign', function() {
var expression = new Expression('sign(1.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'sign(1.0)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for floor', function() {
var expression = new Expression('floor(1.5)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'floor(1.5)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for ceil', function() {
var expression = new Expression('ceil(1.2)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'ceil(1.2)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for round', function() {
var expression = new Expression('round(1.2)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'floor(1.2 + 0.5)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for exp', function() {
var expression = new Expression('exp(1.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'exp(1.0)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for exp2', function() {
var expression = new Expression('exp2(1.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'exp2(1.0)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for log', function() {
var expression = new Expression('log(1.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'log(1.0)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for log2', function() {
var expression = new Expression('log2(1.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'log2(1.0)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for fract', function() {
var expression = new Expression('fract(1.0)');
var shaderExpression = expression.getShaderExpression('', {});
var expected = 'fract(1.0)';
expect(shaderExpression).toEqual(expected);
});

it('gets shader expression for clamp', function() {
var expression = new Expression('clamp(50.0, 0.0, 100.0)');
var shaderExpression = expression.getShaderExpression('', {});
Expand Down

0 comments on commit 69fcd93

Please sign in to comment.