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

add param computeLength and append v_distanceCamera when needed #12343

Closed
wants to merge 5 commits into from
Closed
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
38 changes: 38 additions & 0 deletions packages/engine/Source/Core/GeometryPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -2928,6 +2928,41 @@ function updateAdjacencyAfterSplit(geometry) {
}
}

function updateLineLength(geometry) {
const attributes = geometry.attributes;
const sts = attributes.st.values;

if(!sts || !attributes.length) {
return;
}
const lineLengths = attributes.length.values;
const positions = attributes.position.values;
let prevS = -1;
let prevPosition = null;
let polylineLength = 0;

const length = positions.length;
for (let j = 0; (j*3) < length; j++) {
const s = sts[j*2];
const t = sts[j*2+1];
if(s <= prevS || t !== 0) {
lineLengths.push(polylineLength);
continue;
}
prevS = s;
const position = Cartesian3.unpack(positions, j*3, cartesian3Scratch0);
if(prevPosition === null) {
// first point
prevPosition = position.clone();
lineLengths.push(0);
continue;
}
polylineLength+= Cartesian3.distance(prevPosition, position);
lineLengths.push(polylineLength);
prevPosition = position.clone();
}
}

const offsetScalar = 5.0 * CesiumMath.EPSILON9;
const coplanarOffset = CesiumMath.EPSILON6;

Expand Down Expand Up @@ -3229,6 +3264,9 @@ function splitLongitudePolyline(instance) {
updateAdjacencyAfterSplit(eastGeometry);
}

updateLineLength(westGeometry);
updateLineLength(eastGeometry);

updateInstanceAfterSplit(instance, westGeometry, eastGeometry);
}

Expand Down
37 changes: 33 additions & 4 deletions packages/engine/Source/Core/PolylineGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function interpolateColors(p0, p1, color0, color1, numPoints) {
* @param {number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude if options.arcType is not ArcType.NONE. Determines the number of positions in the buffer.
* @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
* @param {Ellipsoid} [options.ellipsoid=Ellipsoid.default] The ellipsoid to be used as a reference.
* @param {boolean} [options.computeLength=false] compute the length to send the parameter to the custom shader, usefull for dash polyline
*
* @exception {DeveloperError} At least two positions are required.
* @exception {DeveloperError} width must be greater than or equal to one.
Expand All @@ -93,7 +94,7 @@ function interpolateColors(p0, p1, color0, color1, numPoints) {
* 5.0, 0.0,
* 5.0, 5.0
* ]),
* width : 10.0
* width : 10.0,
* });
* const geometry = Cesium.PolylineGeometry.createGeometry(polyline);
*/
Expand All @@ -102,6 +103,7 @@ function PolylineGeometry(options) {
const positions = options.positions;
const colors = options.colors;
const width = defaultValue(options.width, 1.0);
const computeLength = defaultValue(options.computeLength, false);
const colorsPerVertex = defaultValue(options.colorsPerVertex, false);

//>>includeStart('debug', pragmas.debug);
Expand All @@ -124,6 +126,7 @@ function PolylineGeometry(options) {
this._colors = colors;
this._width = width;
this._colorsPerVertex = colorsPerVertex;
this._computeLength = computeLength;
this._vertexFormat = VertexFormat.clone(
defaultValue(options.vertexFormat, VertexFormat.DEFAULT),
);
Expand All @@ -146,7 +149,7 @@ function PolylineGeometry(options) {
* @type {number}
*/
this.packedLength =
numComponents + Ellipsoid.packedLength + VertexFormat.packedLength + 4;
numComponents + Ellipsoid.packedLength + VertexFormat.packedLength + 4 + 1;
}

/**
Expand Down Expand Up @@ -197,7 +200,8 @@ PolylineGeometry.pack = function (value, array, startingIndex) {
array[startingIndex++] = value._width;
array[startingIndex++] = value._colorsPerVertex ? 1.0 : 0.0;
array[startingIndex++] = value._arcType;
array[startingIndex] = value._granularity;
array[startingIndex++] = value._granularity;
array[startingIndex] = value._computeLength;

return array;
};
Expand All @@ -213,6 +217,7 @@ const scratchOptions = {
colorsPerVertex: undefined,
arcType: undefined,
granularity: undefined,
computeLength: false,
};

/**
Expand Down Expand Up @@ -261,7 +266,8 @@ PolylineGeometry.unpack = function (array, startingIndex, result) {
const width = array[startingIndex++];
const colorsPerVertex = array[startingIndex++] === 1.0;
const arcType = array[startingIndex++];
const granularity = array[startingIndex];
const granularity = array[startingIndex++];
const computeLength = array[startingIndex++];

if (!defined(result)) {
scratchOptions.positions = positions;
Expand All @@ -270,6 +276,7 @@ PolylineGeometry.unpack = function (array, startingIndex, result) {
scratchOptions.colorsPerVertex = colorsPerVertex;
scratchOptions.arcType = arcType;
scratchOptions.granularity = granularity;
scratchOptions.computeLength = computeLength;
return new PolylineGeometry(scratchOptions);
}

Expand All @@ -281,6 +288,7 @@ PolylineGeometry.unpack = function (array, startingIndex, result) {
result._colorsPerVertex = colorsPerVertex;
result._arcType = arcType;
result._granularity = granularity;
result._computeLength = computeLength;

return result;
};
Expand All @@ -304,6 +312,7 @@ PolylineGeometry.createGeometry = function (polylineGeometry) {
const arcType = polylineGeometry._arcType;
const granularity = polylineGeometry._granularity;
const ellipsoid = polylineGeometry._ellipsoid;
const computeLength = polylineGeometry._computeLength;

let i;
let j;
Expand Down Expand Up @@ -433,19 +442,25 @@ PolylineGeometry.createGeometry = function (polylineGeometry) {
const expandAndWidth = new Float32Array(size * 2);
const st = vertexFormat.st ? new Float32Array(size * 2) : undefined;
const finalColors = defined(colors) ? new Uint8Array(size * 4) : undefined;
const lineLengths = computeLength ? new Float32Array(size) : undefined;

let positionIndex = 0;
let expandAndWidthIndex = 0;
let stIndex = 0;
let colorIndex = 0;
let position;
let polylineLength = 0;
let polylineLengthIndex = 0;

for (j = 0; j < positionsLength; ++j) {
if (j === 0) {
position = scratchCartesian3;
Cartesian3.subtract(positions[0], positions[1], position);
Cartesian3.add(positions[0], position, position);
} else {
if(computeLength){
polylineLength+= Cartesian3.distance(positions[j-1], positions[j]);
}
position = positions[j - 1];
}

Expand Down Expand Up @@ -487,6 +502,11 @@ PolylineGeometry.createGeometry = function (polylineGeometry) {
Cartesian3.pack(scratchPrevPosition, prevPositions, positionIndex);
Cartesian3.pack(scratchNextPosition, nextPositions, positionIndex);
positionIndex += 3;

if(computeLength) {
lineLengths[polylineLengthIndex++] = polylineLength;
}


const direction = k - 2 < 0 ? -1.0 : 1.0;
expandAndWidth[expandAndWidthIndex++] = 2 * (k % 2) - 1; // expand direction
Expand Down Expand Up @@ -551,6 +571,15 @@ PolylineGeometry.createGeometry = function (polylineGeometry) {
});
}

if (computeLength) {
attributes.length = new GeometryAttribute({
componentDatatype: ComponentDatatype.FLOAT,
componentsPerAttribute: 1,
values: lineLengths,
normalize: false,
});
}

const indices = IndexDatatype.createTypedArray(size, positionsLength * 6 - 6);
let index = 0;
let indicesIndex = 0;
Expand Down
3 changes: 2 additions & 1 deletion packages/engine/Source/Scene/Primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ Primitive._appendDistanceDisplayConditionToShader = function (
"czm_non_distanceDisplayCondition_main",
);
let distanceDisplayConditionMain =
"out float v_distanceCamera; \n" +
"void main() \n" +
"{ \n" +
" czm_non_distanceDisplayCondition_main(); \n" +
Expand Down Expand Up @@ -1001,7 +1002,7 @@ Primitive._appendDistanceDisplayConditionToShader = function (
" { \n" +
" distanceSq = dot(centerRTE.xyz, centerRTE.xyz) - radiusSq; \n" +
" } \n" +
" distanceSq = max(distanceSq, 0.0); \n" +
" v_distanceCamera = distanceSq = max(distanceSq, 0.0); \n" +
" float nearSq = distanceDisplayCondition.x * distanceDisplayCondition.x; \n" +
" float farSq = distanceDisplayCondition.y * distanceDisplayCondition.y; \n" +
" float show = (distanceSq >= nearSq && distanceSq <= farSq) ? 1.0 : 0.0; \n" +
Expand Down