Skip to content

Commit fe4849e

Browse files
committed
integrate ApproximateTerrainHeights into GroundPolylineGeometry, permit asynchronous createGeometry worker functions [ci skip]
1 parent 612152b commit fe4849e

6 files changed

+190
-81
lines changed

Source/Core/ApproximateTerrainHeights.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ define([
4949
* Initializes the minimum and maximum terrain heights
5050
* @return {Promise}
5151
*/
52-
ApproximateTerrainHeights.initialize = function() {
52+
ApproximateTerrainHeights.initialize = function(url) {
5353
var initPromise = ApproximateTerrainHeights._initPromise;
5454
if (defined(initPromise)) {
5555
return initPromise;
5656
}
5757

58-
ApproximateTerrainHeights._initPromise = Resource.fetchJson(buildModuleUrl('Assets/approximateTerrainHeights.json')).then(function(json) {
58+
url = defaultValue(url, 'Assets/approximateTerrainHeights.json');
59+
ApproximateTerrainHeights._initPromise = Resource.fetchJson(buildModuleUrl(url)).then(function(json) {
5960
ApproximateTerrainHeights._terrainHeights = json;
6061
});
6162

Source/Core/GroundPolylineGeometry.js

+69-29
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ define([
399399
cartographicsArray.push(startCartographic.longitude);
400400
}
401401

402-
return generateGeometryAttributes(projection, bottomPositionsArray, topPositionsArray, normalsArray, cartographicsArray, loop);
402+
return generateGeometryAttributes(groundPolylineGeometry, projection, bottomPositionsArray, topPositionsArray, normalsArray, cartographicsArray);
403403
};
404404

405405
var positionCartographicScratch = new Cartographic();
@@ -417,14 +417,40 @@ define([
417417
return result;
418418
}
419419

420-
var cartographicScratch = new Cartographic();
420+
var adjustHeightNormalScratch = new Cartesian3();
421+
var adjustHeightOffsetScratch = new Cartesian3();
422+
function adjustHeights(groundPolylineGeometry, bottom, top, minHeight, maxHeight, adjustHeightBottom, adjustHeightTop) {
423+
// bottom and top should be at groundPolylineGeometry.minimumTerrainHeight and groundPolylineGeometry.maximumTerrainHeight, respectively
424+
var adjustHeightNormal = Cartesian3.subtract(top, bottom, adjustHeightNormalScratch);
425+
Cartesian3.normalize(adjustHeightNormal, adjustHeightNormal);
426+
427+
var distanceForBottom = minHeight - groundPolylineGeometry.minimumTerrainHeight;
428+
var adjustHeightOffset = Cartesian3.multiplyByScalar(adjustHeightNormal, distanceForBottom, adjustHeightOffsetScratch);
429+
Cartesian3.add(bottom, adjustHeightOffset, adjustHeightBottom);
430+
431+
var distanceForTop = maxHeight - groundPolylineGeometry.maximumTerrainHeight;
432+
adjustHeightOffset = Cartesian3.multiplyByScalar(adjustHeightNormal, distanceForTop, adjustHeightOffsetScratch);
433+
Cartesian3.add(top, adjustHeightOffset, adjustHeightTop);
434+
}
435+
436+
var startCartographicScratch = new Cartographic();
437+
var endCartographicScratch = new Cartographic();
438+
421439
var segmentStartTopScratch = new Cartesian3();
422440
var segmentEndTopScratch = new Cartesian3();
423441
var segmentStartBottomScratch = new Cartesian3();
424442
var segmentEndBottomScratch = new Cartesian3();
425443
var segmentStartNormalScratch = new Cartesian3();
426444
var segmentEndNormalScratch = new Cartesian3();
427445

446+
var getHeightCartographics = [startCartographicScratch, endCartographicScratch];
447+
var getHeightRectangleScratch = new Rectangle();
448+
449+
var adjustHeightStartTopScratch = new Cartesian3();
450+
var adjustHeightEndTopScratch = new Cartesian3();
451+
var adjustHeightStartBottomScratch = new Cartesian3();
452+
var adjustHeightEndBottomScratch = new Cartesian3();
453+
428454
var segmentStart2DScratch = new Cartesian3();
429455
var segmentEnd2DScratch = new Cartesian3();
430456
var segmentStartNormal2DScratch = new Cartesian3();
@@ -452,9 +478,11 @@ define([
452478
3, 6, 2, 3, 7, 6 // top
453479
];
454480
var REFERENCE_INDICES_LENGTH = REFERENCE_INDICES.length;
455-
function generateGeometryAttributes(projection, bottomPositionsArray, topPositionsArray, normalsArray, cartographicsArray, loop) {
481+
function generateGeometryAttributes(groundPolylineGeometry, projection, bottomPositionsArray, topPositionsArray, normalsArray, cartographicsArray) {
456482
var i;
457483
var index;
484+
var loop = groundPolylineGeometry.loop;
485+
var ellipsoid = groundPolylineGeometry.ellipsoid;
458486

459487
// Each segment will have 8 vertices
460488
var segmentCount = (bottomPositionsArray.length / 3) - 1;
@@ -482,7 +510,7 @@ define([
482510
var length2D = 0.0;
483511
var length3D = 0.0;
484512

485-
var cartographic = cartographicScratch;
513+
var cartographic = startCartographicScratch;
486514
cartographic.latitude = cartographicsArray[0];
487515
cartographic.longitude = cartographicsArray[1];
488516
cartographic.height = 0.0;
@@ -533,9 +561,11 @@ define([
533561
endGeometryNormal = Cartesian3.multiplyByScalar(endGeometryNormal, -1.0, endGeometryNormal);
534562
}
535563
}
536-
cartographic.latitude = cartographicsArray[0];
537-
cartographic.longitude = cartographicsArray[1];
538-
var end2D = projection.project(cartographic, segmentEnd2DScratch);
564+
var endCartographic = endCartographicScratch;
565+
var startCartographic = startCartographicScratch;
566+
endCartographic.latitude = cartographicsArray[0];
567+
endCartographic.longitude = cartographicsArray[1];
568+
var end2D = projection.project(endCartographic, segmentEnd2DScratch);
539569
var endGeometryNormal2D = projectNormal(projection, endBottom, endGeometryNormal, end2D, segmentEndNormal2DScratch);
540570

541571
var lengthSoFar3D = 0.0;
@@ -549,10 +579,11 @@ define([
549579

550580
var start2D = Cartesian3.clone(end2D, segmentStart2DScratch);
551581
var startGeometryNormal2D = Cartesian3.clone(endGeometryNormal2D, segmentStartNormal2DScratch);
582+
startCartographic = Cartographic.clone(endCartographic, startCartographic);
552583

553584
if (miterBroken) {
554585
// If miter was broken for the previous segment's end vertex, flip for this segment's start vertex
555-
// These "normals" are "right facing."
586+
// These normals are "right facing."
556587
startGeometryNormal = Cartesian3.multiplyByScalar(startGeometryNormal, -1.0, startGeometryNormal);
557588
startGeometryNormal2D = Cartesian3.multiplyByScalar(startGeometryNormal2D, -1.0, startGeometryNormal2D);
558589
}
@@ -566,9 +597,9 @@ define([
566597

567598
miterBroken = breakMiter(endGeometryNormal, startBottom, endBottom, endTop);
568599

569-
cartographic.latitude = cartographicsArray[cartographicsIndex];
570-
cartographic.longitude = cartographicsArray[cartographicsIndex + 1];
571-
end2D = projection.project(cartographic, segmentEnd2DScratch);
600+
endCartographic.latitude = cartographicsArray[cartographicsIndex];
601+
endCartographic.longitude = cartographicsArray[cartographicsIndex + 1];
602+
end2D = projection.project(endCartographic, segmentEnd2DScratch);
572603
endGeometryNormal2D = projectNormal(projection, endBottom, endGeometryNormal, end2D, segmentEndNormal2DScratch);
573604

574605
/****************************************
@@ -645,7 +676,6 @@ define([
645676
rightNormal_andTextureCoordinateNormalizationY[wIndex] = texcoordNormalization3DY;
646677

647678
// 2D
648-
649679
startHiLo2D[vec4Index] = encodedStart2D.high.x;
650680
startHiLo2D[vec4Index + 1] = encodedStart2D.high.y;
651681
startHiLo2D[vec4Index + 2] = encodedStart2D.low.x;
@@ -671,39 +701,49 @@ define([
671701
* Encode which side of the line segment each position is on by
672702
* pushing it "away" by 1 meter along the geometry normal.
673703
*
674-
* This helps when pushing the vertices out by varying amounts to
704+
* Needed when pushing the vertices out by varying amounts to
675705
* help simulate constant screen-space line width.
676706
****************************************************************/
707+
// Adjust heights of positions in 3D
708+
var adjustHeightStartBottom = adjustHeightStartBottomScratch;
709+
var adjustHeightEndBottom = adjustHeightEndBottomScratch;
710+
var adjustHeightStartTop = adjustHeightStartTopScratch;
711+
var adjustHeightEndTop = adjustHeightEndTopScratch;
712+
713+
var getHeightsRectangle = Rectangle.fromCartographicArray(getHeightCartographics, getHeightRectangleScratch);
714+
var minMaxHeights = ApproximateTerrainHeights.getApproximateTerrainHeights(getHeightsRectangle, ellipsoid);
715+
var minHeight = minMaxHeights.minimumTerrainHeight;
716+
var maxHeight = minMaxHeights.maximumTerrainHeight;
717+
718+
adjustHeights(groundPolylineGeometry, startBottom, startTop, minHeight, maxHeight, adjustHeightStartBottom, adjustHeightStartTop);
719+
adjustHeights(groundPolylineGeometry, endBottom, endTop, minHeight, maxHeight, adjustHeightEndBottom, adjustHeightEndTop);
720+
677721
// Push out by 1.0 in the "right" direction
678-
var pushedStartBottom = Cartesian3.add(startBottom, startGeometryNormal, startBottom);
679-
var pushedEndBottom = Cartesian3.add(endBottom, endGeometryNormal, endBottom);
680-
var pushedEndTop = Cartesian3.add(endTop, endGeometryNormal, endTop);
681-
var pushedStartTop = Cartesian3.add(startTop, startGeometryNormal, startTop);
722+
var pushedStartBottom = Cartesian3.add(adjustHeightStartBottom, startGeometryNormal, adjustHeightStartBottom);
723+
var pushedEndBottom = Cartesian3.add(adjustHeightEndBottom, endGeometryNormal, adjustHeightEndBottom);
724+
var pushedEndTop = Cartesian3.add(adjustHeightEndTop, endGeometryNormal, adjustHeightEndTop);
725+
var pushedStartTop = Cartesian3.add(adjustHeightStartTop, startGeometryNormal, adjustHeightStartTop);
682726
Cartesian3.pack(pushedStartBottom, positionsArray, vec3sWriteIndex);
683727
Cartesian3.pack(pushedEndBottom, positionsArray, vec3sWriteIndex + 3);
684728
Cartesian3.pack(pushedEndTop, positionsArray, vec3sWriteIndex + 6);
685729
Cartesian3.pack(pushedStartTop, positionsArray, vec3sWriteIndex + 9);
686730

687731
// Return to center
688-
pushedStartBottom = Cartesian3.subtract(startBottom, startGeometryNormal, startBottom);
689-
pushedEndBottom = Cartesian3.subtract(endBottom, endGeometryNormal, endBottom);
690-
pushedEndTop = Cartesian3.subtract(endTop, endGeometryNormal, endTop);
691-
pushedStartTop = Cartesian3.subtract(startTop, startGeometryNormal, startTop);
732+
pushedStartBottom = Cartesian3.subtract(adjustHeightStartBottom, startGeometryNormal, adjustHeightStartBottom);
733+
pushedEndBottom = Cartesian3.subtract(adjustHeightEndBottom, endGeometryNormal, adjustHeightEndBottom);
734+
pushedEndTop = Cartesian3.subtract(adjustHeightEndTop, endGeometryNormal, adjustHeightEndTop);
735+
pushedStartTop = Cartesian3.subtract(adjustHeightStartTop, startGeometryNormal, adjustHeightStartTop);
692736

693737
// Push out by 1.0 in the "left" direction
694-
pushedStartBottom = Cartesian3.subtract(startBottom, startGeometryNormal, startBottom);
695-
pushedEndBottom = Cartesian3.subtract(endBottom, endGeometryNormal, endBottom);
696-
pushedEndTop = Cartesian3.subtract(endTop, endGeometryNormal, endTop);
697-
pushedStartTop = Cartesian3.subtract(startTop, startGeometryNormal, startTop);
738+
pushedStartBottom = Cartesian3.subtract(adjustHeightStartBottom, startGeometryNormal, adjustHeightStartBottom);
739+
pushedEndBottom = Cartesian3.subtract(adjustHeightEndBottom, endGeometryNormal, adjustHeightEndBottom);
740+
pushedEndTop = Cartesian3.subtract(adjustHeightEndTop, endGeometryNormal, adjustHeightEndTop);
741+
pushedStartTop = Cartesian3.subtract(adjustHeightStartTop, startGeometryNormal, adjustHeightStartTop);
698742
Cartesian3.pack(pushedStartBottom, positionsArray, vec3sWriteIndex + 12);
699743
Cartesian3.pack(pushedEndBottom, positionsArray, vec3sWriteIndex + 15);
700744
Cartesian3.pack(pushedEndTop, positionsArray, vec3sWriteIndex + 18);
701745
Cartesian3.pack(pushedStartTop, positionsArray, vec3sWriteIndex + 21);
702746

703-
// Return next segment's start to center
704-
pushedEndBottom = Cartesian3.add(endBottom, endGeometryNormal, endBottom);
705-
pushedEndTop = Cartesian3.add(endTop, endGeometryNormal, endTop);
706-
707747
cartographicsIndex += 2;
708748
index += 3;
709749

Source/Scene/GroundPolylinePrimitive.js

+44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
define([
2+
'../Core/ApproximateTerrainHeights',
23
'../Core/Check',
34
'../Core/ComponentDatatype',
45
'../Core/defaultValue',
@@ -25,6 +26,7 @@ define([
2526
'./Primitive',
2627
'./SceneMode'
2728
], function(
29+
ApproximateTerrainHeights,
2830
Check,
2931
ComponentDatatype,
3032
defaultValue,
@@ -140,6 +142,37 @@ define([
140142
});
141143
}
142144

145+
GroundPolylinePrimitive._initialized = false;
146+
GroundPolylinePrimitive._initPromise = undefined;
147+
148+
GroundPolylinePrimitive.initializeTerrainHeights = function() {
149+
var initPromise = GroundPolylinePrimitive._initPromise;
150+
if (defined(initPromise)) {
151+
return initPromise;
152+
}
153+
154+
GroundPolylinePrimitive._initPromise = ApproximateTerrainHeights.initialize()
155+
.then(function() {
156+
GroundPolylinePrimitive._initialized = true;
157+
});
158+
159+
return GroundPolylinePrimitive._initPromise;
160+
};
161+
162+
GroundPolylinePrimitive._initializeTerrainHeightsWorker = function() {
163+
var initPromise = GroundPolylinePrimitive._initPromise;
164+
if (defined(initPromise)) {
165+
return initPromise;
166+
}
167+
168+
GroundPolylinePrimitive._initPromise = ApproximateTerrainHeights.initialize('../Assets/approximateTerrainHeights.json')
169+
.then(function() {
170+
GroundPolylinePrimitive._initialized = true;
171+
});
172+
173+
return GroundPolylinePrimitive._initPromise;
174+
};
175+
143176
// TODO: remove
144177
function validateShaderMatching(shaderProgram, attributeLocations) {
145178
// For a VAO and shader program to be compatible, the VAO must have
@@ -400,6 +433,17 @@ define([
400433
return;
401434
}
402435

436+
if (!GroundPolylinePrimitive._initialized) {
437+
//>>includeStart('debug', pragmas.debug);
438+
if (!this.asynchronous) {
439+
throw new DeveloperError('For synchronous GroundPolylinePrimitives, you must call GroundPolylinePrimitives.initializeTerrainHeights() and wait for the returned promise to resolve.');
440+
}
441+
//>>includeEnd('debug');
442+
443+
GroundPolylinePrimitive.initializeTerrainHeights();
444+
return;
445+
}
446+
403447
var i;
404448

405449
var that = this;

Source/Workers/createGeometry.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
define([
22
'../Core/defined',
33
'../Scene/PrimitivePipeline',
4+
'../ThirdParty/when',
45
'./createTaskProcessorWorker',
56
'require'
67
], function(
78
defined,
89
PrimitivePipeline,
10+
when,
911
createTaskProcessorWorker,
1012
require) {
1113
'use strict';
@@ -33,7 +35,7 @@ define([
3335
function createGeometry(parameters, transferableObjects) {
3436
var subTasks = parameters.subTasks;
3537
var length = subTasks.length;
36-
var results = new Array(length);
38+
var resultsOrPromises = new Array(length);
3739

3840
for (var i = 0; i < length; i++) {
3941
var task = subTasks[i];
@@ -42,14 +44,16 @@ define([
4244

4345
if (defined(moduleName)) {
4446
var createFunction = getModule(moduleName);
45-
results[i] = createFunction(geometry, task.offset);
47+
resultsOrPromises[i] = createFunction(geometry, task.offset);
4648
} else {
4749
//Already created geometry
48-
results[i] = geometry;
50+
resultsOrPromises[i] = geometry;
4951
}
5052
}
5153

52-
return PrimitivePipeline.packCreateGeometryResults(results, transferableObjects);
54+
return when.all(resultsOrPromises, function(results) {
55+
return PrimitivePipeline.packCreateGeometryResults(results, transferableObjects);
56+
});
5357
}
5458

5559
return createTaskProcessorWorker(createGeometry);

Source/Workers/createGroundPolylineGeometry.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
define([
22
'../Core/defined',
3-
'../Core/GroundPolylineGeometry'
3+
'../Core/GroundPolylineGeometry',
4+
'../Scene/GroundPolylinePrimitive'
45
], function(
56
defined,
6-
GroundPolylineGeometry) {
7+
GroundPolylineGeometry,
8+
GroundPolylinePrimitive) {
79
'use strict';
810

911
function createGroundPolylineGeometry(groundPolylineGeometry, offset) {
10-
if (defined(offset)) {
11-
groundPolylineGeometry = GroundPolylineGeometry.unpack(groundPolylineGeometry, offset);
12-
}
13-
return GroundPolylineGeometry.createGeometry(groundPolylineGeometry);
12+
return GroundPolylinePrimitive._initializeTerrainHeightsWorker()
13+
.then(function() {
14+
if (defined(offset)) {
15+
groundPolylineGeometry = GroundPolylineGeometry.unpack(groundPolylineGeometry, offset);
16+
}
17+
return GroundPolylineGeometry.createGeometry(groundPolylineGeometry);
18+
});
1419
}
1520

1621
return createGroundPolylineGeometry;

0 commit comments

Comments
 (0)