Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ API between minor releases.
- Filter the model tree by source model, level and category, and optionally isolate the matches in 3D.
- Export explicitly placed source models together in one standalone HTML viewer with independent visibility.
- Import BCF 2.1 topics and open their cameras, clipping and matched components in the offline viewer.
- Preview distance measurement points and snap to visible vertices, edge midpoints and edges near the pointer.

### Fixed

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Built clean-room under the [IOExtend](https://github.com/IOExtend) organization.
- context menu: properties, hide, isolate, show all, zoom to object, section box around the object, copy Id;
- view cube that follows the camera exactly, with face clicks, 90° arrows and a home button;
- section planes (up to six, moved and rotated with an in-scene gizmo) or a section box (keep inside or cut inside, face handles, move by the centre, box around the selection);
- point-to-point distance measurement, display modes (materials, textures, white, wireframe), viewport background color, grid lines and a global opacity slider;
- point-to-point distance measurement previews the point under the cursor and snaps to visible vertices, edge midpoints and edges within eight screen pixels;
- display modes (materials, textures, white, wireframe), viewport background color, grid lines and a global opacity slider;
- saved viewpoints with a viewpoint tour, clash results, a 4D schedule player and linked-file visibility, each shown only when the document has that data.
- Imports binary glTF 2.0 (`.glb`), Wavefront OBJ (`.obj`), and JSON: viewer JSON, IOScene scene files and IOViewer 0.1/0.2 snapshots.

Expand Down
17 changes: 16 additions & 1 deletion docs/viewer-performance-baseline.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ for ($trial = 1; $trial -le 3; $trial++) {

Each `ladder-<label>.json` contains both workloads, with separate `perf-<label>-<model>.json` files for each page.
Use `readyMs` for navigation to first frame submission, `orbit.p95Ms` and `orbitFull.p95Ms` for the p95 interval
between animation frames, and `pickMs` for the median of ten picks near the home view. The adaptive orbit reports
between animation frames, and `pickMs` for the median pick at the fitted scale. The adaptive orbit reports
the median share of in-view instances drawn in `orbit.drawnMedian`. `memory.gpuBytes` counts the scene's GPU buffers
and slot textures, and `heapMB` samples Chromium's JavaScript heap just after load. Divide byte counts by 1,048,576
for MiB. The harness's `htmlMB` and `heapMB` fields already use MiB.
Expand Down Expand Up @@ -97,3 +97,18 @@ are medians across the three runs.
When evaluating instancing and picking, compare full-detail p95 with the same renderer and workload, and read adaptive
p95 alongside its drawn share. The RTX full-detail range on the synthetic scene is too wide to establish a regression
threshold from these runs. [Instancing and picking issue](https://github.com/IOExtend/IOViewer/issues/12)

## Distance measurement hover

On the same machine and Synthetic-400k workload, the harness records 60 paired pick and measurement samples per GPU.
Each pair uses the same pixel after the orbit returns to the fitted scale, and consecutive pairs use different
pixels. The harness warms the picking shaders and geometry cache first. The timings cover the synchronous GPU
readback and target computation. Every measured hover snapped to a vertex.

| GPU | Pick p50 / p95, ms | Measurement hover p50 / p95, ms |
| --- | ---: | ---: |
| Radeon | 10.8 / 11.4 | 12.3 / 12.9 |
| RTX 4080 | 1.7 / 2.0 | 2.0 / 2.4 |

Set `PERF_MODELS` to the Synthetic-400k HTML exported with the current bundle and run the same harness. Read
`pickMs` and `pickP95Ms` alongside `measureSnap.medianMs`, `measureSnap.p95Ms` and `measureSnap.kinds`.
374 changes: 187 additions & 187 deletions src/IOViewer/Viewer/dist/viewer.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/IOViewer/Viewer/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export default defineConfig({
{
name: 'firefox',
testMatch: ['**/smoke.spec.js', '**/selection.spec.js', '**/sections.spec.js', '**/accessibility.spec.js',
'**/tree-selection.spec.js', '**/tree-filters.spec.js', '**/federation.spec.js', '**/bcf.spec.js'],
'**/tree-selection.spec.js', '**/tree-filters.spec.js', '**/federation.spec.js', '**/bcf.spec.js',
'**/measure-snap.spec.js'],
use: {
browserName: 'firefox',
// Under Linux, Xvfb supplies the display Firefox uses to create a WebGL context in CI.
Expand Down
70 changes: 58 additions & 12 deletions src/IOViewer/Viewer/src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,48 @@ function decodeBase64(element) {
for (const node of element.childNodes) total += node.length;
const bytes = new Uint8Array(Math.ceil(total / 4) * 3); // at most; padding and whitespace make it less
let offset = 0;
let canonical = true;
let carry = ''; // characters past the last whole group of 4
for (const node of element.childNodes) {
for (let start = 0; start < node.length; start += CHUNK_CHARS) {
const text = carry + node.substringData(start, CHUNK_CHARS).trim();
const raw = node.substringData(start, CHUNK_CHARS);
const trimmed = raw.trim();
const text = carry + trimmed;
const whole = text.length - (text.length % 4);
offset = decodeInto(bytes, offset, text.slice(0, whole));
const aligned = text.slice(0, whole);
const next = decodeInto(bytes, offset, aligned);
const padding = aligned.endsWith('==') ? 2 : aligned.endsWith('=') ? 1 : 0;
canonical &&= raw.length === trimmed.length && next - offset === whole * 3 / 4 - padding;
offset = next;
carry = text.slice(whole);
}
}
return bytes.subarray(0, decodeInto(bytes, offset, carry)); // an unpadded tail, as atob accepts it
const end = decodeInto(bytes, offset, carry); // an unpadded tail, as atob accepts it
canonical &&= end - offset === Math.floor(carry.length * 3 / 4);
return { bytes: bytes.subarray(0, end), canonical };
}

// Decode just the aligned base64 groups spanning a view without retaining a decoded copy of the model.
function readRange(element, offset, length) {
const group = Math.floor(offset / 3);
let start = group * 4;
const skip = offset - group * 3;
let remaining = Math.ceil((skip + length) / 3) * 4;
const encoded = [];
for (const node of element.childNodes) {
if (start >= node.length) {
start -= node.length;
continue;
}
const take = Math.min(remaining, node.length - start);
encoded.push(node.substringData(start, take));
remaining -= take;
start = 0;
if (!remaining) break;
}
const bytes = new Uint8Array(Math.ceil((skip + length) / 3) * 3);
if (decodeInto(bytes, 0, encoded.join('')) < skip + length) throw new ModelDataError('The binary data is truncated.');
return bytes.slice(skip, skip + length).buffer;
}

// Writes the bytes of base64 `text` into `bytes` at `offset`; returns the offset after them. Uses the native decoder
Expand Down Expand Up @@ -133,32 +165,46 @@ export function readModelMeta(doc = document, startup) {
}

export function loadModelData(doc = document, startup, meta = readModelMeta(doc, startup)) {
let bin;
let bin, binElement, canonical, legacyBin;
try {
startup?.('binaryStart');
const binElement = doc.getElementById('ioviewer-bin');
binElement = doc.getElementById('ioviewer-bin');
if (!binElement) throw new Error("Data block 'ioviewer-bin' is missing.");
bin = decodeBase64(binElement);
({ bytes: bin, canonical } = decodeBase64(binElement));
startup?.('binaryEnd');
} catch (error) {
throw new ModelDataError(error.message, { cause: error });
}
const binaryLength = bin.length;
function viewType(v) {
const Type = VIEW_TYPES[v?.t];
if (!Type) throw new ModelDataError(`Unknown view type '${v?.t}'.`);
const size = Type.BYTES_PER_ELEMENT;
if (!(Number.isInteger(v.o) && Number.isInteger(v.n) && v.o >= 0 && v.n >= 0 && v.o % size === 0 && v.o + v.n * size <= binaryLength)) {
throw new ModelDataError(`View ${JSON.stringify(v)} is outside the ${binaryLength} bytes of binary data.`);
}
return Type;
}
let props = null;
return {
meta,
view(v) {
if (!bin) throw new Error('The binary data has been released.');
const Type = VIEW_TYPES[v?.t];
if (!Type) throw new ModelDataError(`Unknown view type '${v?.t}'.`);
const size = Type.BYTES_PER_ELEMENT;
if (!(Number.isInteger(v.o) && Number.isInteger(v.n) && v.o >= 0 && v.n >= 0 && v.o % size === 0 && v.o + v.n * size <= bin.length)) {
throw new ModelDataError(`View ${JSON.stringify(v)} is outside the ${bin.length} bytes of binary data.`);
}
const Type = viewType(v);
return new Type(bin.buffer, v.o, v.n);
},
readView(v) {
const Type = viewType(v);
if (!v.n) return new Type(0);
if (!bin && !canonical) legacyBin ??= decodeBase64(binElement).bytes;
const bytes = bin ?? legacyBin;
return bytes ? new Type(bytes.buffer, v.o, v.n)
: new Type(readRange(binElement, v.o, v.n * Type.BYTES_PER_ELEMENT));
},
// Frees the decoded BIN once the scene holds its own copies; view() throws afterwards.
releaseBinary() {
bin = null;
legacyBin = null;
},
props(elementId) {
props ??= JSON.parse(block(doc, 'ioviewer-props'));
Expand Down
4 changes: 4 additions & 0 deletions src/IOViewer/Viewer/src/engine/modelScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ export class ModelScene {
return this._elementOf[this._slotInstance[slot]];
}

slotInstance(slot) {
return this._slotInstance[slot];
}

// Runs fn with `material` on every chunk's triangles (and triangles shown in wireframe mode), e.g. for picking.
withMaterial(material, fn) {
this._picking = true;
Expand Down
119 changes: 105 additions & 14 deletions src/IOViewer/Viewer/src/engine/picking.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ export class Picker {
// The last pick and what it depends on: the pixel, the canvas size, the camera and what is shown and cut away
// (picks come on every wheel notch and pointer press, and each draws the model; colours do not matter).
this._last = null;
this._lastTile = null;
this._key = new Float64Array(36);
modelScene.root.addEventListener('change', (e) => e.detail.kind !== 'appearance' && (this._last = null));
modelScene.root.addEventListener('change', (e) => {
if (e.detail.kind !== 'appearance') this._last = this._lastTile = null;
});
const uniforms = modelScene.slotUniforms;
this._float = options.pickFloat !== false && renderer.extensions.has('EXT_color_buffer_float');
if (this._float) {
Expand All @@ -112,55 +115,143 @@ export class Picker {
const x = Math.floor(clientX - rect.left);
const y = Math.floor(clientY - rect.top);
if (this._lost || !(x >= 0 && y >= 0 && x < rect.width && y < rect.height)) return null;
const key = this._sampleKey(x, y, rect);
const camera = this.camera;
camera.updateMatrixWorld();
const key = this._key;
key.set([x, y, rect.width, rect.height]);
key.set(camera.matrixWorld.elements, 4);
key.set(camera.projectionMatrix.elements, 20);
const last = this._last;
if (last && last.camera === camera && last.key.every((v, k) => v === key[k])) return copy(last.hit);
const hit = this._pick(x, y, rect);
this._last = { camera, key: key.slice(), hit };
return copy(hit);
}

_sampleKey(x, y, rect) {
this.camera.updateMatrixWorld();
const key = this._key;
key.set([x, y, rect.width, rect.height]);
key.set(this.camera.matrixWorld.elements, 4);
key.set(this.camera.projectionMatrix.elements, 20);
return key;
}

// One small region replaces per-candidate GPU passes. The returned samples are used synchronously, before
// another region is read. Sample coordinates are CSS pixels relative to the canvas.
measureTile(clientX, clientY, radius) {
const rect = this.renderer.domElement.getBoundingClientRect();
const x = Math.floor(clientX - rect.left);
const y = Math.floor(clientY - rect.top);
if (this._lost || !(x >= 0 && y >= 0 && x < rect.width && y < rect.height)) return null;
const key = this._sampleKey(x, y, rect);
const camera = this.camera;
const last = this._lastTile;
if (last && last.camera === camera && last.radius === radius && last.key.every((v, k) => v === key[k]))
return last.tile;
const side = radius * 2 + 1;
if (!this._measure || this._measure.target.width !== side) {
this._measure?.target.dispose();
this._measure = {
target: new THREE.WebGLRenderTarget(side, side, this._float ? { type: THREE.FloatType } : {}),
ids: this._float ? new Float32Array(side * side * 4) : new Uint8Array(side * side * 4),
depths: this._float ? null : new Uint8Array(side * side * 4),
};
}
const { target, ids, depths } = this._measure;
const left = x - radius;
const top = y - radius;
this._render(this._material, left, top, rect, target, ids);
const index = (px, py) => {
const col = Math.floor(px) - left;
const row = Math.floor(py) - top;
return col >= 0 && col < side && row >= 0 && row < side ? (side - 1 - row) * side + col : -1;
};
const encoded = this._float ? null : new DataView(ids.buffer);
const slotAtIndex = (i) => (this._float ? Math.round(ids[i * 4 + 3]) : encoded.getUint32(i * 4, true)) - 1;
const slot = slotAtIndex(index(x, y));
let tile = null;
if (slot >= 0) {
if (depths) this._render(this._depthMaterial, left, top, rect, target, depths);
const encodedDepth = depths && new DataView(depths.buffer);
const m = camera.matrixWorldInverse.elements.slice();
const depth = (i) => {
if (encodedDepth) return encodedDepth.getFloat32(i * 4, true);
const start = i * 4;
return -(m[2] * ids[start] + m[6] * ids[start + 1] + m[10] * ids[start + 2] + m[14]);
};
const i = index(x, y);
const point = this._float ? new THREE.Vector3().fromArray(ids, i * 4) : this._depthPoint(x, y, rect, depth(i));
const slots = new Set([slot]);
const element = this.scene.slotElement(slot);
if (element >= 0) {
for (let j = 0; j < side * side; j++) {
const nearby = slotAtIndex(j);
if (nearby >= 0 && !slots.has(nearby) && this.scene.slotElement(nearby) === element) slots.add(nearby);
}
}
const perspective = camera.isPerspectiveCamera;
tile = { rect, point, element, slots, depthAt(px, py, candidateSlot) {
const j = index(px, py);
const front = j < 0 ? -1 : slotAtIndex(j);
if (front < 0) return Infinity;
if (front !== candidateSlot) return depth(j);
// Depth (reciprocal depth in perspective) is linear on a face. Correct for the distance
// between the exact projected target and the centres of the GPU pixels on its slot.
const linear = (k) => perspective ? 1 / depth(k) : depth(k);
const center = linear(j);
function correction(dx, dy, fraction) {
const direction = Math.sign(fraction);
const next = index(px + direction * dx, py + direction * dy);
if (next >= 0 && slotAtIndex(next) === front) return (linear(next) - center) * Math.abs(fraction);
const previous = index(px - direction * dx, py - direction * dy);
return previous >= 0 && slotAtIndex(previous) === front ? (center - linear(previous)) * Math.abs(fraction) : 0;
}
const sample = center + correction(1, 0, px - Math.floor(px) - 0.5)
+ correction(0, 1, py - Math.floor(py) - 0.5);
return sample > 0 ? (perspective ? 1 / sample : sample) : depth(j);
} };
}
this._lastTile = { camera, key: key.slice(), radius, tile };
this._last = { camera, key: this._lastTile.key, hit: tile };
return tile;
}

_pick(x, y, rect) {
const pixel = this._render(this._material, x, y, rect);
const slot = (this._float ? Math.round(pixel[3]) : new Uint32Array(pixel.buffer)[0]) - 1;
if (slot < 0) return null;
const element = this.scene.slotElement(slot);
if (this._float) return { element, point: new THREE.Vector3(pixel[0], pixel[1], pixel[2]) };
const bytes = this._render(this._depthMaterial, x, y, rect);
const depth = new DataView(bytes.buffer).getFloat32(0, true);
return { element, point: this._depthPoint(x, y, rect, new DataView(bytes.buffer).getFloat32(0, true)) };
}

_depthPoint(x, y, rect, depth) {
// Recover the world point from view-space depth, independent of the depth buffer mapping.
const camera = this.camera;
const point = new THREE.Vector3(((x + 0.5) / rect.width) * 2 - 1, 1 - ((y + 0.5) / rect.height) * 2, -1)
.applyMatrix4(camera.projectionMatrixInverse);
if (camera.isPerspectiveCamera) point.multiplyScalar(-depth / point.z);
else point.z = -depth;
return { element, point: point.applyMatrix4(camera.matrixWorld) };
return point.applyMatrix4(camera.matrixWorld);
}

// Draws the model with `material` into the 1x1 target at pixel (x, y) and reads it back.
_render(material, x, y, rect) {
// Draws the model with `material` into the requested tile at pixel (x, y) and reads it back.
_render(material, x, y, rect, target = this._target, pixels = this._pixel) {
const { renderer, camera } = this;
const previous = { target: renderer.getRenderTarget(), color: renderer.getClearColor(new THREE.Color()), alpha: renderer.getClearAlpha() };
const source = this.scene._buckets[0]?.material; // setClipping gives every bucket the same planes
material.clippingPlanes = source?.clippingPlanes ?? null;
material.clipIntersection = source?.clipIntersection ?? false;
camera.updateMatrixWorld();
camera.setViewOffset(rect.width, rect.height, x, y, 1, 1);
camera.setViewOffset(rect.width, rect.height, x, y, target.width, target.height);
try {
renderer.setRenderTarget(this._target);
renderer.setRenderTarget(target);
renderer.setClearColor(0x000000, 0);
this.scene.withMaterial(material, () => renderer.render(this.scene.root, camera));
renderer.readRenderTargetPixels(this._target, 0, 0, 1, 1, this._pixel);
renderer.readRenderTargetPixels(target, 0, 0, target.width, target.height, pixels);
} finally {
renderer.setRenderTarget(previous.target);
renderer.setClearColor(previous.color, previous.alpha);
camera.clearViewOffset();
}
return this._pixel;
return pixels;
}
}
Loading
Loading