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

CheckerboardMaterialProperty #2385

Merged
merged 1 commit into from
Jan 7, 2015
Merged
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Change Log
* Improved performance of asynchronous geometry creation (as much as 20% faster in some use cases). [#2342](https://github.com/AnalyticalGraphicsInc/cesium/issues/2342)
* Added `PolylineVolumeGraphics` and `Entity.polylineVolume`
* Added `BillboardGraphics.imageSubRegion`, to enable custom texture atlas use for `Entity` instances.
* Added `CheckerboardMaterialProperty` to enable use of the checkerboard material with the entity API.

### 1.5 - 2015-01-05

Expand Down
136 changes: 136 additions & 0 deletions Source/DataSources/CheckerboardMaterialProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*global define*/
define([
'../Core/Cartesian2',
'../Core/Color',
'../Core/defined',
'../Core/defineProperties',
'../Core/Event',
'./createPropertyDescriptor',
'./Property'
], function(
Cartesian2,
Color,
defined,
defineProperties,
Event,
createPropertyDescriptor,
Property) {
"use strict";

var defaultEvenColor = Color.WHITE;
var defaultOddColor = Color.BLACK;
var defaultRepeat = new Cartesian2(2.0, 2.0);

/**
* A {@link MaterialProperty} that maps to checkerboard {@link Material} uniforms.
*
* @alias CheckerboardMaterialProperty
* @constructor
*/
var CheckerboardMaterialProperty = function() {
this._definitionChanged = new Event();

this._evenColor = undefined;
this._evenColorSubscription = undefined;

this._oddColor = undefined;
this._oddColorSubscription = undefined;

this._repeat = undefined;
this._repeatSubscription = undefined;
};

defineProperties(CheckerboardMaterialProperty.prototype, {
/**
* Gets a value indicating if this property is constant. A property is considered
* constant if getValue always returns the same result for the current definition.
* @memberof CheckerboardMaterialProperty.prototype
*
* @type {Boolean}
* @readonly
*/
isConstant : {
get : function() {
return Property.isConstant(this._evenColor) && //
Property.isConstant(this._oddColor) && //
Property.isConstant(this._repeat);
}
},
/**
* Gets the event that is raised whenever the definition of this property changes.
* The definition is considered to have changed if a call to getValue would return
* a different result for the same time.
* @memberof CheckerboardMaterialProperty.prototype
*
* @type {Event}
* @readonly
*/
definitionChanged : {
get : function() {
return this._definitionChanged;
}
},
/**
* Gets or sets the {@link Color} property which determines the first color.
* @memberof CheckerboardMaterialProperty.prototype
* @type {Property}
*/
evenColor : createPropertyDescriptor('evenColor'),
/**
* Gets or sets the {@link Color} property which determines the second color.
* @memberof CheckerboardMaterialProperty.prototype
* @type {Property}
*/
oddColor : createPropertyDescriptor('oddColor'),
/**
* A {@link Cartesian2} property which determines how many times the checkerboard tiles repeat in each direction.
* @memberof CheckerboardMaterialProperty.prototype
* @type {Property}
*/
repeat : createPropertyDescriptor('repeat')
});

/**
* Gets the {@link Material} type at the provided time.
*
* @param {JulianDate} time The time for which to retrieve the type.
* @returns {String} The type of material.
*/
CheckerboardMaterialProperty.prototype.getType = function(time) {
return 'Checkerboard';
};

/**
* Gets the value of the property at the provided time.
*
* @param {JulianDate} time The time for which to retrieve the value.
* @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
* @returns {Object} The modified result parameter or a new instance if the result parameter was not supplied.
*/
CheckerboardMaterialProperty.prototype.getValue = function(time, result) {
if (!defined(result)) {
result = {};
}
result.lightColor = Property.getValueOrClonedDefault(this._evenColor, time, defaultEvenColor, result.lightColor);
result.darkColor = Property.getValueOrClonedDefault(this._oddColor, time, defaultOddColor, result.darkColor);
result.repeat = Property.getValueOrDefault(this._repeat, time, defaultRepeat);
return result;
};

/**
* Compares this property to the provided property and returns
* <code>true</code> if they are equal, <code>false</code> otherwise.
*
* @param {Property} [other] The other property.
* @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
*/
CheckerboardMaterialProperty.prototype.equals = function(other) {
return this === other || //
(other instanceof CheckerboardMaterialProperty && //
Property.equals(this._evenColor, other._evenColor) && //
Property.equals(this._oddColor, other._oddColor) && //
Property.equals(this._repeat, other._repeat));
};

return CheckerboardMaterialProperty;
});
133 changes: 133 additions & 0 deletions Specs/DataSources/CheckerboardMaterialPropertySpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*global defineSuite*/
defineSuite([
'DataSources/CheckerboardMaterialProperty',
'Core/Cartesian2',
'Core/Color',
'Core/JulianDate',
'Core/TimeInterval',
'DataSources/ConstantProperty',
'DataSources/TimeIntervalCollectionProperty',
'Specs/testDefinitionChanged'
], function(
CheckerboardMaterialProperty,
Cartesian2,
Color,
JulianDate,
TimeInterval,
ConstantProperty,
TimeIntervalCollectionProperty,
testDefinitionChanged) {
"use strict";
/*global jasmine,describe,xdescribe,it,xit,expect,beforeEach,afterEach,beforeAll,afterAll,spyOn,runs,waits,waitsFor*/

it('constructor provides the expected defaults', function() {
var property = new CheckerboardMaterialProperty();
expect(property.getType()).toEqual('Checkerboard');
expect(property.isConstant).toBe(true);
expect(property.evenColor).toBeUndefined();
expect(property.oddColor).toBeUndefined();
expect(property.repeat).toBeUndefined();

var result = property.getValue();
expect(result.lightColor).toEqual(Color.WHITE);
expect(result.darkColor).toEqual(Color.BLACK);
expect(result.repeat).toEqual(new Cartesian2(2.0, 2.0));
});

it('works with constant values', function() {
var property = new CheckerboardMaterialProperty();
property.evenColor = new ConstantProperty(Color.RED);
property.oddColor = new ConstantProperty(Color.BLUE);
property.repeat = new ConstantProperty(new Cartesian2(5, 5));

var result = property.getValue(JulianDate.now());
expect(result.lightColor).toEqual(Color.RED);
expect(result.darkColor).toEqual(Color.BLUE);
expect(result.repeat).toEqual(new Cartesian2(5, 5));
});

it('works with dynamic values', function() {
var property = new CheckerboardMaterialProperty();
property.evenColor = new TimeIntervalCollectionProperty();
property.oddColor = new TimeIntervalCollectionProperty();
property.repeat = new TimeIntervalCollectionProperty();

var start = new JulianDate(1, 0);
var stop = new JulianDate(2, 0);
property.evenColor.intervals.addInterval(new TimeInterval({
start : start,
stop : stop,
data : Color.RED
}));
property.oddColor.intervals.addInterval(new TimeInterval({
start : start,
stop : stop,
data : Color.BLUE
}));
property.repeat.intervals.addInterval(new TimeInterval({
start : start,
stop : stop,
data : new Cartesian2(5, 5)
}));

expect(property.isConstant).toBe(false);

var result = property.getValue(start);
expect(result.lightColor).toEqual(Color.RED);
expect(result.darkColor).toEqual(Color.BLUE);
expect(result.repeat).toEqual(new Cartesian2(5, 5));
});

it('works with a result parameter', function() {
var property = new CheckerboardMaterialProperty();
property.evenColor = new ConstantProperty(Color.RED);
property.oddColor = new ConstantProperty(Color.BLUE);
property.repeat = new ConstantProperty(new Cartesian2(5, 5));

var result = {
lightColor : Color.YELLOW.clone(),
darkColor : Color.YELLOW.clone(),
repeat : new Cartesian2(1, 1)
};
var returnedResult = property.getValue(JulianDate.now(), result);
expect(returnedResult).toBe(result);
expect(result.lightColor).toEqual(Color.RED);
expect(result.darkColor).toEqual(Color.BLUE);
expect(result.repeat).toEqual(new Cartesian2(5, 5));
});

it('equals works', function() {
var left = new CheckerboardMaterialProperty();
left.evenColor = new ConstantProperty(Color.RED);
left.oddColor = new ConstantProperty(Color.BLUE);
left.repeat = new ConstantProperty(new Cartesian2(5, 5));

var right = new CheckerboardMaterialProperty();
right.evenColor = new ConstantProperty(Color.RED);
right.oddColor = new ConstantProperty(Color.BLUE);
right.repeat = new ConstantProperty(new Cartesian2(5, 5));

expect(left.equals(right)).toEqual(true);

right.evenColor = new ConstantProperty(Color.BLACK);
expect(left.equals(right)).toEqual(false);

right.evenColor = new ConstantProperty(Color.RED);
right.oddColor = new ConstantProperty(Color.BLACK);
expect(left.equals(right)).toEqual(false);

right.oddColor = new ConstantProperty(Color.BLUE);
right.repeat = new ConstantProperty(new Cartesian2(5, 6));
expect(left.equals(right)).toEqual(false);

right.repeat = new ConstantProperty(new Cartesian2(5, 5));
expect(left.equals(right)).toEqual(true);
});

it('raises definitionChanged when a property is assigned or modified', function() {
var property = new CheckerboardMaterialProperty();
testDefinitionChanged(property, 'evenColor', Color.RED, Color.BLUE);
testDefinitionChanged(property, 'oddColor', Color.RED, Color.BLUE);
testDefinitionChanged(property, 'repeat', new Cartesian2(5, 5), new Cartesian2(7, 7));
});
});