/
DynamicMaterialProperty.js
125 lines (113 loc) · 4.92 KB
/
DynamicMaterialProperty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*global define*/
define([
'../Core/TimeInterval',
'../Core/TimeIntervalCollection',
'../Core/Iso8601',
'./DynamicColorMaterial',
'./DynamicImageMaterial',
'./DynamicGridMaterial'
], function(
TimeInterval,
TimeIntervalCollection,
Iso8601,
DynamicColorMaterial,
DynamicImageMaterial,
DynamicGridMaterial) {
"use strict";
var potentialMaterials = [DynamicColorMaterial, DynamicImageMaterial, DynamicGridMaterial];
/**
* A dynamic property which stores data for multiple types of materials
* associated with the same property over time. Rather than creating instances
* of this object directly, it's typically created and managed via loading CZML
* data into a DynamicObjectCollection.
*
* @alias DynamicMaterialProperty
* @internalconstructor
*
* @see DynamicObject
* @see DynamicProperty
* @see ReferenceProperty
* @see DynamicPositionProperty
* @see DynamicDirectionsProperty
* @see DynamicVertexPositionsProperty
*/
var DynamicMaterialProperty = function() {
this._intervals = new TimeIntervalCollection();
};
/**
* Processes the provided CZML interval or intervals into this property.
* @memberof DynamicMaterialProperty
*
* @param {Object} czmlIntervals The CZML data to process.
* @param {TimeInterval} [constrainedInterval] Constrains the processing so that any times outside of this interval are ignored.
* @param {String} [sourceUri] The originating url of the CZML being processed.
*/
DynamicMaterialProperty.prototype.processCzmlIntervals = function(czmlIntervals, constrainedInterval, sourceUri) {
if (Array.isArray(czmlIntervals)) {
for ( var i = 0, len = czmlIntervals.length; i < len; i++) {
addCzmlInterval(this, czmlIntervals[i], constrainedInterval, sourceUri);
}
} else {
addCzmlInterval(this, czmlIntervals, constrainedInterval, sourceUri);
}
};
/**
* Returns the value of the property at the specified time.
* @memberof DynamicMaterialProperty
*
* @param {JulianDate} time The time for which to retrieve the value.
* @param {Context} [context] The context in which the material exists.
* @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
* @returns The modified result parameter or a new instance if the result parameter was not supplied.
*/
DynamicMaterialProperty.prototype.getValue = function(time, context, existingMaterial) {
var value = this._intervals.findIntervalContainingDate(time);
var material = typeof value !== 'undefined' ? value.data : undefined;
if (typeof material !== 'undefined') {
return material.getValue(time, context, existingMaterial);
}
return existingMaterial;
};
function addCzmlInterval(dynamicMaterialProperty, czmlInterval, constrainedInterval, sourceUri) {
var iso8601Interval = czmlInterval.interval;
if (typeof iso8601Interval === 'undefined') {
iso8601Interval = Iso8601.MAXIMUM_INTERVAL.clone();
} else {
iso8601Interval = TimeInterval.fromIso8601(iso8601Interval);
}
if (typeof constrainedInterval !== 'undefined') {
iso8601Interval = iso8601Interval.intersect(constrainedInterval);
}
//See if we already have data at that interval.
var thisIntervals = dynamicMaterialProperty._intervals;
var existingInterval = thisIntervals.findInterval(iso8601Interval.start, iso8601Interval.stop);
var foundMaterial = false;
var existingMaterial;
if (typeof existingInterval !== 'undefined') {
//We have an interval, but we need to make sure the
//new data is the same type of material as the old data.
existingMaterial = existingInterval.data;
foundMaterial = existingMaterial.isMaterial(czmlInterval);
} else {
//If not, create it.
existingInterval = iso8601Interval;
thisIntervals.addInterval(existingInterval);
}
//If the new data was a different type, look for a handler for this type.
if (foundMaterial === false) {
for ( var i = 0, len = potentialMaterials.length; i < len; i++) {
var PotentialMaterial = potentialMaterials[i];
foundMaterial = PotentialMaterial.isMaterial(czmlInterval);
if (foundMaterial) {
existingInterval.data = existingMaterial = new PotentialMaterial();
break;
}
}
}
//We could handle the data, prcess it.
if (foundMaterial) {
existingMaterial.processCzmlIntervals(czmlInterval, sourceUri);
}
}
return DynamicMaterialProperty;
});