Skip to content

Commit

Permalink
Fixed an issue with PointBatch auto fill draw ranges, where invalid r…
Browse files Browse the repository at this point in the history
…anges were set. Implemented a draw range flattener for the PointBatch which avoids redundant draw calls, which in some cases could get into the tens of thousands killing performance entirely. Fixed an issue where gradient ramp index was not neing set correctly when it was 0 (#1288)
  • Loading branch information
AlexandruPopovici committed Jan 4, 2023
1 parent bf5a749 commit 4181217
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/viewer-sandbox/src/Sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,8 @@ export default class Sandbox {
Area: 'parameters.HOST_AREA_COMPUTED.value',
SpeckleType: 'speckle_type',
DisplayName: 'DisplayName',
EmbodiedCarbon: 'EmbodiedCarbon'
EmbodiedCarbon: 'EmbodiedCarbon',
Floor: 'Floor'
}
})

Expand Down
2 changes: 1 addition & 1 deletion packages/viewer/src/modules/batching/MeshBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default class MeshBatch implements Batch {
let maxGradientIndex = 0
for (let k = 0; k < sortedRanges.length; k++) {
if (sortedRanges[k].materialOptions) {
if (sortedRanges[k].materialOptions.rampIndex) {
if (sortedRanges[k].materialOptions.rampIndex !== undefined) {
const start = sortedRanges[k].offset
const len = sortedRanges[k].offset + sortedRanges[k].count
/** The ramp indices specify the *begining* of each ramp color. When sampling with Nearest filter (since we don't want filtering)
Expand Down
61 changes: 60 additions & 1 deletion packages/viewer/src/modules/batching/PointBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,16 @@ export default class PointBatch implements Batch {
if (k === 0) {
if (sortedRanges[k].start > 0) {
this.geometry.addGroup(0, sortedRanges[k].start, 0)
continue
}
if (
sortedRanges.length === 1 &&
sortedRanges[k].start + sortedRanges[k].count < this.getCount()
) {
this.geometry.addGroup(
sortedRanges[k].start + sortedRanges[k].count,
this.getCount() - sortedRanges[k].start + sortedRanges[k].count,
0
)
}
} else if (k === sortedRanges.length - 1) {
if (sortedRanges[k].start + sortedRanges[k].count < this.getCount()) {
Expand Down Expand Up @@ -192,6 +201,56 @@ export default class PointBatch implements Batch {
console.error(`DrawRange autocomplete failed! ${count}vs${this.getCount()}`)
}

/** We're flattening sequential groups to avoid redundant draw calls.
* ! Not thoroughly tested !
*/
const materialOrder = []
this.geometry.groups.reduce((previousValue, currentValue) => {
if (previousValue.indexOf(currentValue.materialIndex) === -1) {
previousValue.push(currentValue.materialIndex)
}
return previousValue
}, materialOrder)
const grouped = []
for (let k = 0; k < materialOrder.length; k++) {
grouped.push(
this.geometry.groups.filter((val) => {
return val.materialIndex === materialOrder[k]
})
)
}
this.geometry.groups = []
for (let matIndex = 0; matIndex < grouped.length; matIndex++) {
const matGroup = grouped[matIndex]
for (let k = 0; k < matGroup.length; ) {
let offset = matGroup[k].start
let count = matGroup[k].count
let runningCount = matGroup[k].count
let n = k + 1
for (; n < matGroup.length; n++) {
if (offset + count === matGroup[n].start) {
offset = matGroup[n].start
count = matGroup[n].count
runningCount += matGroup[n].count
} else {
this.geometry.addGroup(
matGroup[k].start,
runningCount,
matGroup[k].materialIndex
)
break
}
}
if (n === matGroup.length) {
this.geometry.addGroup(
matGroup[k].start,
runningCount,
matGroup[k].materialIndex
)
}
k = n
}
}
// console.warn(
// `Batch ID ${this.id} Group count ${this.geometry.groups.length} AUTOCOMPLETE`
// )
Expand Down

0 comments on commit 4181217

Please sign in to comment.