Skip to content

Commit

Permalink
Upsample quantized terrain from mesh. WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
bagnell committed Dec 15, 2015
1 parent 5041481 commit 5093874
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 27 deletions.
18 changes: 14 additions & 4 deletions Source/Core/QuantizedMeshTerrainData.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,15 @@ define([
var occlusionPoint = defaultValue(result.occludeePointInScaledSpace, that._horizonOcclusionPoint);
var stride = result.vertexStride;
var terrainEncoding = TerrainEncoding.clone(result.encoding);
var skirtIndex = result.skirtIndex;
var vertexCountWithoutSkirts = that._quantizedVertices.length / 3;

return new TerrainMesh(
rtc,
vertices,
vertexCountWithoutSkirts,
indicesTypedArray,
skirtIndex,
minimumHeight,
maximumHeight,
boundingSphere,
Expand Down Expand Up @@ -333,7 +337,7 @@ define([
* or undefined if too many asynchronous upsample operations are in progress and the request has been
* deferred.
*/
QuantizedMeshTerrainData.prototype.upsample = function(tilingScheme, thisX, thisY, thisLevel, descendantX, descendantY, descendantLevel) {
QuantizedMeshTerrainData.prototype.upsample = function(mesh, tilingScheme, thisX, thisY, thisLevel, descendantX, descendantY, descendantLevel) {
//>>includeStart('debug', pragmas.debug);
if (!defined(tilingScheme)) {
throw new DeveloperError('tilingScheme is required.');
Expand Down Expand Up @@ -369,9 +373,15 @@ define([
var childRectangle = tilingScheme.tileXYToRectangle(descendantX, descendantY, descendantLevel);

var upsamplePromise = upsampleTaskProcessor.scheduleTask({
vertices : this._quantizedVertices,
indices : this._indices,
encodedNormals : this._encodedNormals,
//vertices : this._quantizedVertices,
//indices : this._indices,
//encodedNormals : this._encodedNormals,
quantizedVertices : this._quantizedVertices,
vertices : mesh.vertices,
vertexCountWithoutSkirts : mesh.vertexCountWithoutSkirts,
indices : mesh.indices,
skirtIndex : mesh.skirtIndex,
encoding : mesh.encoding,
minimumHeight : this._minimumHeight,
maximumHeight : this._maximumHeight,
isEastChild : isEastChild,
Expand Down
36 changes: 36 additions & 0 deletions Source/Core/TerrainEncoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,42 @@ define([
return Cartesian3.add(result, this.center, result);
};

TerrainEncoding.prototype.decodeTextureCoordinates = function(buffer, index, result) {
if (!defined(result)) {
result = new Cartesian3();
}

index *= this.getStride();

if (this.quantization === TerrainQuantization.BITS12) {
return AttributeCompression.decompressTextureCoordinates(buffer[index + 2], result);
}

return Cartesian2.fromElements(buffer[index + 4], buffer[index + 5], result);
};

TerrainEncoding.prototype.decodeHeight = function(buffer, index) {
index *= this.getStride();

if (this.quantization === TerrainQuantization.BITS12) {
var zh = AttributeCompression.decompressTextureCoordinates(buffer[index + 1], cartesian2Scratch);
return zh.y * (this.maximumHeight - this.minimumHeight) + this.minimumHeight;
}

return buffer[index + 3];
};

TerrainEncoding.prototype.getOctEncodedNormal = function(buffer, index, result) {
var stride = this.getStride();
index = (index + 1) * stride - 1;

var temp = buffer[index] / 256.0;
var x = Math.floor(temp);
var y = (temp - x) * 256.0;

return Cartesian2.fromElements(x, y, result);
}

TerrainEncoding.prototype.getStride = function() {
var vertexStride;

Expand Down
5 changes: 4 additions & 1 deletion Source/Core/TerrainMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ define([
*
* @private
*/
var TerrainMesh = function TerrainMesh(center, vertices, indices, minimumHeight, maximumHeight, boundingSphere3D, occludeePointInScaledSpace, vertexStride, orientedBoundingBox, encoding) {
var TerrainMesh = function TerrainMesh(center, vertices, vertexCountWithoutSkirts, indices, skirtIndex, minimumHeight, maximumHeight, boundingSphere3D, occludeePointInScaledSpace, vertexStride, orientedBoundingBox, encoding) {
/**
* The center of the tile. Vertex positions are specified relative to this center.
* @type {Cartesian3}
Expand Down Expand Up @@ -98,6 +98,9 @@ define([
* @type {TerrainEncoding}
*/
this.encoding = encoding;

this.skirtIndex = skirtIndex;
this.vertexCountWithoutSkirts = vertexCountWithoutSkirts;
};

return TerrainMesh;
Expand Down
6 changes: 5 additions & 1 deletion Source/Scene/GlobeSurfaceTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ define([
function getUpsampleTileDetails(tile) {
// Find the nearest ancestor with loaded terrain.
var sourceTile = tile.parent;
while (defined(sourceTile) && defined(sourceTile.data) && !defined(sourceTile.data.terrainData)) {
//while (defined(sourceTile) && defined(sourceTile.data) && !defined(sourceTile.data.terrainData)) {
while (defined(sourceTile) && defined(sourceTile.data) && !defined(sourceTile.data.pickTerrain)) {
sourceTile = sourceTile.parent;
}

Expand All @@ -522,6 +523,7 @@ define([

return {
data : sourceTile.data.terrainData,
mesh : sourceTile.data.pickTerrain.mesh,
x : sourceTile.x,
y : sourceTile.y,
level : sourceTile.level
Expand Down Expand Up @@ -556,6 +558,7 @@ define([
}
childSurfaceTile.upsampledTerrain = new TileTerrain({
data : surfaceTile.terrainData,
mesh : surfaceTile.pickTerrain.mesh,
x : tile.x,
y : tile.y,
level : tile.level
Expand Down Expand Up @@ -592,6 +595,7 @@ define([
}
childSurfaceTile.upsampledTerrain = new TileTerrain({
data : surfaceTile.terrainData,
mesh : surfaceTile.pickTerrain.mesh,
x : tile.x,
y : tile.y,
level : tile.level
Expand Down
2 changes: 1 addition & 1 deletion Source/Scene/TileTerrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ define([
var sourceY = upsampleDetails.y;
var sourceLevel = upsampleDetails.level;

this.data = sourceData.upsample(terrainProvider.tilingScheme, sourceX, sourceY, sourceLevel, x, y, level);
this.data = sourceData.upsample(upsampleDetails.mesh, terrainProvider.tilingScheme, sourceX, sourceY, sourceLevel, x, y, level);
if (!defined(this.data)) {
// The upsample request has been deferred - try again later.
return;
Expand Down
3 changes: 2 additions & 1 deletion Source/Workers/createVerticesFromQuantizedTerrainMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ define([
boundingSphere : boundingSphere,
orientedBoundingBox : orientedBoundingBox,
occludeePointInScaledSpace : occludeePointInScaledSpace,
encoding : encoding
encoding : encoding,
skirtIndex : parameters.indices.length
};
}

Expand Down
70 changes: 51 additions & 19 deletions Source/Workers/upsampleQuantizedTerrainMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define([
'../Core/Intersections2D',
'../Core/Math',
'../Core/OrientedBoundingBox',
'../Core/TerrainEncoding',
'./createTaskProcessorWorker'
], function(
AttributeCompression,
Expand All @@ -26,6 +27,7 @@ define([
Intersections2D,
CesiumMath,
OrientedBoundingBox,
TerrainEncoding,
createTaskProcessorWorker) {
"use strict";

Expand All @@ -45,6 +47,8 @@ define([
var horizonOcclusionPointScratch = new Cartesian3();
var boundingSphereScratch = new BoundingSphere();
var orientedBoundingBoxScratch = new OrientedBoundingBox();
var decodeTexCoordsScratch = new Cartesian2();
var octEncodedNormalScratch = new Cartesian3();

function upsampleQuantizedTerrainMesh(parameters, transferableObjects) {
var isEastChild = parameters.isEastChild;
Expand All @@ -71,18 +75,58 @@ define([
var vertexMap = {};

var parentVertices = parameters.vertices;
var parentNormalBuffer = parameters.encodedNormals;
var parentIndices = parameters.indices;
parentIndices = parentIndices.subarray(0, parameters.skirtIndex);

var quantizedVertexCount = parentVertices.length / 3;
var parentUBuffer = parentVertices.subarray(0, quantizedVertexCount);
var parentVBuffer = parentVertices.subarray(quantizedVertexCount, 2 * quantizedVertexCount);
var parentHeightBuffer = parentVertices.subarray(quantizedVertexCount * 2, 3 * quantizedVertexCount);
var encoding = TerrainEncoding.clone(parameters.encoding);
var hasVertexNormals = encoding.hasVertexNormals;

var vertexCount = 0;
var hasVertexNormals = defined(parentNormalBuffer);
var quantizedVertexCount = parameters.vertexCountWithoutSkirts;

var i, n, u, v;
var parentMinimumHeight = parameters.minimumHeight;
var parentMaximumHeight = parameters.maximumHeight;

var parentUBuffer = new Array(quantizedVertexCount);
var parentVBuffer = new Array(quantizedVertexCount);
var parentHeightBuffer = new Array(quantizedVertexCount);
var parentNormalBuffer = hasVertexNormals ? new Array(quantizedVertexCount * 2) : undefined;

var threshold = 20;

var i, n;
for (i = 0, n = 0; i < quantizedVertexCount; ++i, n += 2) {
var texCoords = encoding.decodeTextureCoordinates(parentVertices, i, decodeTexCoordsScratch);
var height = encoding.decodeHeight(parentVertices, i);

parentUBuffer[i] = CesiumMath.clamp((texCoords.x * maxShort) | 0, 0, maxShort);
parentVBuffer[i] = CesiumMath.clamp((texCoords.y * maxShort) | 0, 0, maxShort);
parentHeightBuffer[i] = CesiumMath.clamp((((height - parentMinimumHeight) / (parentMaximumHeight - parentMinimumHeight)) * maxShort) | 0, 0, maxShort);

if (parentUBuffer[i] < threshold) {
parentUBuffer[i] = 0;
}

if (parentVBuffer[i] < threshold) {
parentVBuffer[i] = 0;
}

if (maxShort - parentUBuffer[i] < threshold) {
parentUBuffer[i] = maxShort;
}

if (maxShort - parentVBuffer[i] < threshold) {
parentVBuffer[i] = maxShort;
}

if (hasVertexNormals) {
var encodedNormal = encoding.getOctEncodedNormal(parentVertices, i, octEncodedNormalScratch);
parentNormalBuffer[n] = encodedNormal.x;
parentNormalBuffer[n + 1] = encodedNormal.y;
}
}

var u, v;
for (i = 0, n = 0; i < quantizedVertexCount; ++i, n += 2) {
u = parentUBuffer[i];
v = parentVBuffer[i];
Expand Down Expand Up @@ -167,9 +211,6 @@ define([
var uOffset = isEastChild ? -maxShort : 0;
var vOffset = isNorthChild ? -maxShort : 0;

var parentMinimumHeight = parameters.minimumHeight;
var parentMaximumHeight = parameters.maximumHeight;

var westIndices = [];
var southIndices = [];
var eastIndices = [];
Expand Down Expand Up @@ -330,15 +371,6 @@ define([
this.ratio = undefined;
};

Vertex.prototype.initializeInterpolated = function(first, second, ratio) {
this.vertexBuffer = undefined;
this.index = undefined;
this.newIndex = undefined;
this.first = first;
this.second = second;
this.ratio = ratio;
};

Vertex.prototype.initializeFromClipResult = function(clipResult, index, vertices) {
var nextIndex = index + 1;

Expand Down

0 comments on commit 5093874

Please sign in to comment.