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 @@ -10,6 +10,7 @@ API between minor releases.

- `ioviewer` reads Navisworks `.nwc` caches through `NwcIO.Scene` 0.13.0.
- Standalone HTML opens with the section box or cutting planes saved in `ViewerDocument`.
- Change or reset the viewport background color from the display menu.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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), grid lines and a global opacity slider;
- point-to-point distance measurement, 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
46 changes: 23 additions & 23 deletions src/IOViewer/Viewer/dist/viewer.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/IOViewer/Viewer/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const dictionaries = {
'display.textures': 'Текстуры',
'display.white': 'Белый',
'display.wireframe': 'Каркас',
'display.background': 'Цвет фона',
'display.backgroundReset': 'Фон по умолчанию',
'opacity.label': 'Непрозрачность',
'opacity.value': '{value} %',
'views.tour': 'Тур по видам',
Expand Down Expand Up @@ -198,6 +200,8 @@ const dictionaries = {
'display.textures': 'Textures',
'display.white': 'White',
'display.wireframe': 'Wireframe',
'display.background': 'Background color',
'display.backgroundReset': 'Reset background',
'opacity.label': 'Opacity',
'opacity.value': '{value}%',
'views.tour': 'Play tour',
Expand Down
13 changes: 11 additions & 2 deletions src/IOViewer/Viewer/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ function start(data, context) {
renderer.localClippingEnabled = true;

const world = new THREE.Scene();
world.background = new THREE.Color('#1f2228');
const initialBackground = '#1f2228';
world.background = new THREE.Color(initialBackground);
const sky = new THREE.HemisphereLight('#ffffff', '#8a8f99', 1.6);
sky.position.set(0, 0, 1);
world.add(sky);
Expand Down Expand Up @@ -157,7 +158,15 @@ function start(data, context) {
world.add(clashes.group); // overlay, like the measure
const timeliner = mountTimeliner({ root: uiRoot, ...ui, timeliner: meta.timeliner, elements: meta.elements, scene });
mountLinks({ ...ui, files: meta.linkedFiles ?? [], elements: meta.elements, scene });
for (const popover of mountDisplay({ root: uiRoot, toolbar: ui.toolbar, scene, meta: data.meta, grids })) ui.onEscape(popover.close);
const setBackground = (color) => {
const { r, g, b } = world.background.set(color);
// The equality point of the white and black contrast ratios against a linear sRGB background.
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
const text = luminance > Math.sqrt(0.0525) - 0.05 ? '#000' : '#fff';
uiRoot.style.setProperty('--viewport-text', color === initialBackground ? '' : text);
requestRender();
};
for (const popover of mountDisplay({ root: uiRoot, toolbar: ui.toolbar, scene, meta, grids, initialBackground, setBackground })) ui.onEscape(popover.close);
ui.toolbar.setHandler('distance', () => {
measure.setActive(!measure.active);
ui.toolbar.setActive('distance', measure.active);
Expand Down
42 changes: 36 additions & 6 deletions src/IOViewer/Viewer/src/tools/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ export function initialDisplayMode(options) {
}

/**
* Display tools: `displayMode` opens a menu of the modes (textures only when META has textures; the current one is
* checked), `grids` shows or hides the grid axes and their labels (off when there are none), `opacity` opens a
* Display tools: `displayMode` opens the mode choices and the viewport background setting, `grids` shows or hides
* the grid axes and their labels (off when there are none), `opacity` opens a
* 10–100 % slider for the whole model. Returns the popovers, for Esc.
*/
export function mountDisplay({ root, toolbar, scene, meta, grids }) {
export function mountDisplay({ root, toolbar, scene, meta, grids, initialBackground, setBackground }) {
const anchor = (id) => toolbar.element.querySelector(`[data-tool="${id}"]`);

const modes = createPopover(root, anchor('displayMode'), t('tool.displayMode'), 'menu');
const displayButton = anchor('displayMode');
const modes = createPopover(root, displayButton, t('group.display'));
modes.element.classList.add('ioviewer-display-modes');
const menu = el('div');
menu.setAttribute('role', 'menu');
menu.setAttribute('aria-label', t('tool.displayMode'));
modes.element.append(menu);
const hasTextures = (meta.textures ?? []).length > 0;
const items = DISPLAY_MODES.map((mode) => {
const item = el('button', 'ioviewer-menu-item', t(`display.${mode}`));
Expand All @@ -28,16 +33,41 @@ export function mountDisplay({ root, toolbar, scene, meta, grids }) {
item.addEventListener('click', () => {
setMode(mode);
modes.close();
displayButton.focus();
});
modes.element.append(item);
menu.append(item);
return item;
});
const setMode = (mode) => {
scene.setDisplayMode(mode);
for (const item of items) item.setAttribute('aria-checked', String(item.dataset.mode === mode));
};
setMode(initialDisplayMode(meta.options));
toolbar.setHandler('displayMode', modes.toggle);
toolbar.setHandler('displayMode', () => {
modes.toggle();
if (modes.isOpen) menu.querySelector('[aria-checked="true"]')?.focus();
});

const background = el('div', 'ioviewer-background-setting');
const label = el('label', '', t('display.background'));
const color = el('input');
Object.assign(color, { type: 'color', value: initialBackground });
label.append(color);
const reset = el('button', 'ioviewer-text-button', t('display.backgroundReset'));
reset.type = 'button';
reset.disabled = true;
const applyBackground = () => {
setBackground(color.value);
reset.disabled = color.value === initialBackground;
};
color.addEventListener('input', applyBackground);
reset.addEventListener('click', () => {
color.value = initialBackground;
color.focus();
applyBackground();
});
background.append(label, reset);
modes.element.append(background);

toolbar.setActive('grids', grids.visible);
if (grids.count > 0) {
Expand Down
32 changes: 30 additions & 2 deletions src/IOViewer/Viewer/styles/viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ input:focus-visible {
align-items: center;
justify-content: center;
font-size: 18px;
color: #94a3b8;
color: var(--viewport-text, #94a3b8);
}

#ioviewer-status {
position: absolute;
right: 14px;
bottom: 10px;
font-size: 11px;
color: #94a3b8;
color: var(--viewport-text, #94a3b8);
}

/* Rail of panel icons and the floating panels it opens */
Expand Down Expand Up @@ -677,6 +677,34 @@ input:focus-visible {
content: "\2713";
}

.ioviewer-background-setting {
display: grid;
gap: 6px;
padding: 6px 10px 4px;
border-top: 1px solid var(--border);
}

.ioviewer-background-setting label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}

.ioviewer-background-setting input {
width: 40px;
height: 28px;
padding: 3px;
border: 0;
border-radius: 4px;
background: var(--button);
cursor: pointer;
}

.ioviewer-background-setting input:hover {
background: var(--button-hover);
}

.ioviewer-opacity {
width: 200px;
padding: 8px 12px;
Expand Down
20 changes: 15 additions & 5 deletions src/IOViewer/Viewer/test/e2e/sections.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ async function rgb(page, [x, y]) {
}, png.toString('base64'));
}
const distance = (a, b) => a.reduce((sum, c, i) => sum + Math.abs(c - b[i]), 0);

async function visibleColors(page, points) {
let colors;
// The viewer's ready promise marks the render call, which can precede the browser's composited screenshot.
await expect.poll(async () => {
colors = [];
for (const point of points) colors.push(await rgb(page, point));
return Math.min(...colors.map((color) => distance(color, BACKGROUND)));
}).toBeGreaterThan(60);
return colors;
}

// The pixel now shows (nearly) the background where it showed the model: at most a quarter of the old difference
// is left, which allows for the translucent plane rectangle lying in front of it.
const cleared = (now, before) => distance(now, BACKGROUND) < 0.25 * distance(before, BACKGROUND);
Expand Down Expand Up @@ -120,12 +132,9 @@ test('a Z plane through the middle cuts the roof away; flipping it brings the ro
// floors above it (nothing below it).
const roofAt = await screenAt(page, roof, [0.5, 0.95, 1]);
const groundAt = await screenAt(page, ground, [0.985, 0.5, 1]);
const roofBefore = await rgb(page, roofAt);
const groundBefore = await rgb(page, groundAt);
const [roofBefore, groundBefore] = await visibleColors(page, [roofAt, groundAt]);
expect(await pickAt(page, roofAt)).toBe(roof);
expect(await pickAt(page, groundAt)).toBe(ground);
expect(distance(roofBefore, BACKGROUND)).toBeGreaterThan(60);
expect(distance(groundBefore, BACKGROUND)).toBeGreaterThan(60);

await tool(page, 'plane').click();
await expect(panel(page)).toBeVisible();
Expand Down Expand Up @@ -168,7 +177,8 @@ test('box around the selected column: keepInside shows only the column, cutInsid
const d2At = await screenAt(page, d2);
const d1At = await screenAt(page, d1);
const roofAt = await screenAt(page, roof, [0.5, 0.95, 1]);
const before = { d2: await rgb(page, d2At), d1: await rgb(page, d1At), roof: await rgb(page, roofAt) };
const [d2Before, d1Before, roofBefore] = await visibleColors(page, [d2At, d1At, roofAt]);
const before = { d2: d2Before, d1: d1Before, roof: roofBefore };

await page.mouse.click(...d2At);
expect(await selected(page)).toEqual([d2]);
Expand Down
48 changes: 33 additions & 15 deletions src/IOViewer/Viewer/test/e2e/smoke.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@ const nextFrame = (page) =>
page.evaluate(() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))));

const region = (page, x, y) => page.screenshot({ clip: { x, y, width: 40, height: 40 } });
const centerRegion = (page) => {
const { width, height } = page.viewportSize();
return region(page, width / 2 - 20, height / 2 - 20);
};

// A screenshot can still show the background after the first render call has returned.
async function renderedCenter(page) {
const background = await region(page, 80, 0); // right of the panel rail
let center;
await expect.poll(async () => {
center = await centerRegion(page);
return center.equals(background);
}).toBe(false);
return center;
}

test('building renders the model and counts elements', async ({ page }) => {
const { errors, requests } = await open(page, 'building');

const { width, height } = page.viewportSize();
const center = await region(page, width / 2 - 20, height / 2 - 20);
const background = await region(page, 80, 0); // right of the panel rail
expect(center.equals(background)).toBe(false);
await renderedCenter(page);
await expect(page.locator('#ioviewer-status')).toHaveText(`Выбрано: 0 · Элементов: ${building.Elements.length}`);
expect(errors).toEqual([]);
expect(requests).toEqual([]);
Expand Down Expand Up @@ -106,28 +118,28 @@ test('a mirrored instance is shaded like its unmirrored twin', async ({ page })
return [((point.x + 1) / 2) * innerWidth, ((1 - point.y) / 2) * innerHeight];
});
}, twins);
const plain = await brightness(page, ...points[0]);
const mirrored = await brightness(page, ...points[1]);
const background = await brightness(page, 100, 20);

expect(Math.abs(plain - background)).toBeGreaterThan(10); // the sample hits the column, not the background
let plain, mirrored;
await expect.poll(async () => {
plain = await brightness(page, ...points[0]);
mirrored = await brightness(page, ...points[1]);
return Math.min(Math.abs(plain - background), Math.abs(mirrored - background));
}).toBeGreaterThan(10); // Both columns have reached the composited frame.
expect(Math.abs(mirrored - plain)).toBeLessThanOrEqual(3);
expect(errors).toEqual([]);
});

test('highlight, visibility and display modes change the picture without errors', async ({ page }) => {
const { errors } = await open(page, 'building');
const { width, height } = page.viewportSize();
const shot = () => region(page, width / 2 - 20, height / 2 - 20);
const before = await shot();
const before = await renderedCenter(page);

await page.evaluate((n) => window.__ioviewer.scene.setHighlighted([...Array(n).keys()]), building.Elements.length);
await nextFrame(page);
expect((await shot()).equals(before)).toBe(false);
expect((await centerRegion(page)).equals(before)).toBe(false);

await page.evaluate(() => window.__ioviewer.scene.setHighlighted([]));
await nextFrame(page);
expect((await shot()).equals(before)).toBe(true);
expect((await centerRegion(page)).equals(before)).toBe(true);

for (const mode of ['white', 'wireframe', 'textures', 'materials']) {
await page.evaluate((m) => window.__ioviewer.scene.setDisplayMode(m), mode);
Expand All @@ -137,7 +149,7 @@ test('highlight, visibility and display modes change the picture without errors'
await nextFrame(page);
await page.evaluate(() => window.__ioviewer.scene.setOpacity(1));
await nextFrame(page);
expect((await shot()).equals(before)).toBe(true);
expect((await centerRegion(page)).equals(before)).toBe(true);
expect(errors).toEqual([]);
});

Expand Down Expand Up @@ -173,7 +185,13 @@ async function cylinderJumps(page) {
const [b, y] = screen(centre.clone().addScaledVector(right, 1.6));
return [Math.round(a), Math.round(b), Math.round(y)];
});
const row = await greys(page, { x: x0, y, width: x1 - x0, height: 1 });
const clip = { x: x0, y, width: x1 - x0, height: 1 };
const background = await brightness(page, 100, 20);
let row;
await expect.poll(async () => {
row = await greys(page, clip);
return row.some((grey) => Math.abs(grey - background) > 10);
}).toBe(true);
return row.slice(1).filter((g, i) => Math.abs(g - row[i]) > 1.5).length;
}

Expand Down
35 changes: 34 additions & 1 deletion src/IOViewer/Viewer/test/e2e/tools.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ test('distance: two clicks on the slab top 6 m apart measure 6.00 m; the selecti

test('display mode menu: materials checked, textures disabled without textures, each mode changes the picture', async ({ page }) => {
const errors = await open(page);
const pictures = { materials: await shot(page) };
await tool(page, 'displayMode').click();
await expect(page.locator('.ioviewer-display-modes')).toBeVisible();
await expect(page.locator('.ioviewer-display-modes .ioviewer-menu-item')).toHaveText(['Материалы', 'Текстуры', 'Белый', 'Каркас']);
await expect(modeItem(page, 'materials')).toHaveAttribute('aria-checked', 'true');
await expect(modeItem(page, 'textures')).toBeDisabled();

const pictures = { materials: await shot(page) };
for (const mode of ['white', 'wireframe']) {
if (!(await page.locator('.ioviewer-display-modes').isVisible())) await tool(page, 'displayMode').click();
await modeItem(page, mode).click();
Expand All @@ -124,6 +124,39 @@ test('display mode menu: materials checked, textures disabled without textures,
expect(errors).toEqual([]);
});

test('viewport background changes from the display menu and can be reset', async ({ page }) => {
const errors = await open(page);
const background = async () => {
await frame(page);
return page.screenshot({ clip: { x: 110, y: 40, width: 2, height: 2 } });
};
const original = await background();
const display = tool(page, 'displayMode');
const panel = page.getByRole('dialog', { name: 'Отображение' });
const color = panel.getByLabel('Цвет фона');
const reset = panel.getByRole('button', { name: 'Фон по умолчанию' });

await display.click();
await expect(color).toHaveValue('#1f2228');
await expect(reset).toBeDisabled();
await color.fill('#ffffff');
await expect(page.locator('#ioviewer-status')).toHaveCSS('color', 'rgb(0, 0, 0)');
const white = await background();
expect(white.equals(original)).toBe(false);
await color.press('Escape');
await expect(panel).toBeHidden();
await expect(display).toBeFocused();
expect((await background()).equals(white)).toBe(true);

await display.click();
await expect(color).toHaveValue('#ffffff');
await reset.click();
await expect(color).toHaveValue('#1f2228');
await expect(reset).toBeDisabled();
expect((await background()).equals(original)).toBe(true);
expect(errors).toEqual([]);
});

test('initialWhiteMode from the export options opens in white', async ({ page }) => {
const errors = await open(page, 'building-white');
const white = await shot(page);
Expand Down
Loading