Documents
Updated
A PDF entity (ent-pdf) shows the pages of a PDF document on a plane in the world: a menu on a stand, a handout on a desk, a slide deck a presenter pages through. Its PDF component (script class PDFComponent) shows one page at a time and can draw a small previous/next pill under the page. This page explains its props, how it sizes itself, the three ways a page changes, and how scripts build slide remotes, page indicators and document switchers on top of it.
Before you start
Section titled “Before you start”- Props and
setProps. Scripts change the page by setting thepageprop. See Components. - Signals.
PDF::ready,PDF::errorandPDF::pagereach scripts through a Signal Emitter on the PDF entity. - Intrinsic sizing. The
sizing: 'auto'mode is explained on Images. - Keyboard and UI input. Examples use
Input.keyboardand UI buttons. See Input and interaction and UI.
Properties
Section titled “Properties”| Prop | Type | Default | Description |
|---|---|---|---|
src |
string | none | The document: a URL or an asset from the project. |
width |
number | 1.2 |
Plane width in metres. The height follows the page’s aspect ratio. |
sizing |
'manual' | 'auto' |
none | auto fits each page inside width × height instead. |
height |
number | none | The height bound for sizing: 'auto'; absent, it equals width. Ignored otherwise. |
page |
number | 1 |
The page shown, counted from 1. Kept within the document’s pages. |
controls |
boolean | true |
A previous/next pill under the page, when the document has more than one page. |
Sizing
Section titled “Sizing”Without sizing: 'auto' the plane is width wide and as tall as the page’s shape makes it. A portrait A4 page at width 1.2 is about 1.7 tall, and a landscape slide at the same width is much shorter.
With sizing: 'auto' each page fits inside width × height (a square of width when height is absent). Landscape and portrait pages then take the same box, which is useful when a document mixes both.
The plane is square until the first page has rendered, then takes the page’s shape. A Frame on the entity follows every resize.
The page shown changes in three ways:
- The pill. Previous and next step one page and wrap around at the ends. A step writes the new number into the
pageprop, so it is saved, and shared with other players when the entity is networked. - The
pageprop. A script sets it withsetProps({ page }). Once the document has loaded, a number past the end is stored as the last page, one below 1 as the first. - Loading. A new
srcloads the document and showspage.
Each page change draws the new page into a fresh texture. That takes a moment for a heavy page, so change pages in response to a person, not every frame.
Events
Section titled “Events”Sent through the Signal Emitters on the entity.
| Event | EventType |
Data | When |
|---|---|---|---|
PDF::ready |
Event |
{ pages }, the page count |
The document and its first page are shown. |
PDF::error |
Event |
{ message } |
The document cannot load. |
PDF::page |
Event |
{ page, pages } |
Another page is shown, from the pill or a page change. |
The first page is announced by ready, not by page. Read the page count from ready: it is the only place a script learns it.
Examples
Section titled “Examples”1. Turn pages with the arrow keys
Section titled “1. Turn pages with the arrow keys”A slide deck the presenter drives with the left and right arrow keys. The scene has a PDF entity named Deck with a Signal Emitter whose signal is deck. In the live example below, Deck shows a sample PDF served with these docs at /example-assets/pdf-sample.pdf.
#pragma lifecycle(startup, update)
let deck;let pages = 1;
function startup() { deck = findEntity('Deck').getComponent(PDFComponent); deck.setProps({ controls: false, page: 1 });
on('deck', (event) => { if (event.eventType === EventType.PDF.Ready) pages = event.data.pages; });}
function update() { const page = deck.props.page; if (Input.keyboard.isKeyReleased(Key.ArrowRight) && page < pages) { deck.setProps({ page: page + 1 }); } if (Input.keyboard.isKeyReleased(Key.ArrowLeft) && page > 1) { deck.setProps({ page: page - 1 }); }}A presenter's screen shows a PDF slide deck; the left and right arrow keys turn its pages, stopping at the first and last, with the page count read from the PDF::ready event.
Click the view, then press the right and left arrow keys to turn the pages. The arrow keys do not move the camera here; drag to orbit, scroll to zoom.
controls: falsehides the pill, because the keys replace it.pagesstarts at 1 and is updated fromPDF::ready, the only event that carries the page count.deck.props.pageis the page shown now, whether the keys, the pill or another player changed it last.- The checks against
1andpagesstop at the first and last slides instead of wrapping.
2. A remote with a page indicator
Section titled “2. A remote with a page indicator”UI buttons that turn pages, and a label that shows “Page 3 of 12”. The scene has:
- A PDF entity named
Handoutwith a Signal Emitter whose signal ishandout. - Two UI Buttons, each with a Signal Emitter: signal
handout/nexton one,handout/prevon the other. - A Text entity named
Page Label.
#pragma lifecycle(startup)
let handout;let label;let pages = 1;
function showPage(page) { label.setProps({ text: `Page ${page} of ${pages}` });}
function startup() { handout = findEntity('Handout').getComponent(PDFComponent); label = findEntity('Page Label').getComponent(TextComponent); label.setProps({ text: 'Loading…' });
on('handout', (event) => { if (event.eventType === EventType.PDF.Ready) { pages = event.data.pages; showPage(handout.props.page); } if (event.eventType === EventType.PDF.Page) { pages = event.data.pages; showPage(event.data.page); } if (event.eventType === EventType.PDF.Error) { label.setProps({ text: 'Document unavailable' }); } });
on('handout/next', (event) => { if (event.eventType !== EventType.UIButton.Press) return; const page = handout.props.page; handout.setProps({ page: page < pages ? page + 1 : 1 }); });
on('handout/prev', (event) => { if (event.eventType !== EventType.UIButton.Press) return; const page = handout.props.page; handout.setProps({ page: page > 1 ? page - 1 : pages }); });}Two UI buttons turn the pages of a PDF handout and a label shows "Page 2 of 3", kept right by the document's own ready and page events: a PDF, a Text and a Behavior script.
Press Next and Previous, or use the pill under the document. Drag to orbit the camera, scroll to zoom.
- The label updates from
PDF::page, so it is also right when someone uses the pill. PDF::readycovers the first page, whichPDF::pagedoes not announce.- The buttons wrap around at both ends, like the built-in pill.
- Each button event is checked against
EventType.UIButton.Press, so other events from the same emitter are ignored.
3. Switch between documents
Section titled “3. Switch between documents”A menu stand that shows the lunch or dinner menu depending on which sign is clicked. The scene has a PDF entity named Menu with a Signal Emitter whose signal is menu, and two Primitive Meshes with Selectables and Signal Emitters: signal menu/lunch and menu/dinner.
#pragma lifecycle(startup)
const MENUS = { lunch: '/example-assets/handout.pdf', dinner: '/example-assets/pdf-sample.pdf',};
let menu;
function show(name) { menu.setProps({ src: MENUS[name], page: 1 }); log(`Showing the ${name} menu`);}
function startup() { menu = findEntity('Menu').getComponent(PDFComponent); menu.setProps({ sizing: 'auto', width: 0.8, height: 1.1 });
for (const name of Object.keys(MENUS)) { on(`menu/${name}`, (event) => { if (event.eventType === EventType.Selectable.Select) { findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again show(name); } }); }
on('menu', (event) => { if (event.eventType === EventType.PDF.Error) { log('Menu failed to load:', event.data.message); show('lunch'); } });}Clicking the Lunch or Dinner sign loads that document into the PDF entity's src, back on page 1; a load error falls back to lunch.
Click the Lunch or Dinner sign to show that document. Drag elsewhere to orbit the camera, scroll to zoom.
setProps({ src, page: 1 })changes the document and resets the page in one call. Withoutpage: 1, a new document opens at the old page number.sizing: 'auto'with a 0.8 × 1.1 box keeps both menus the same size on the stand, whatever their page shape.- A loop registers one listener per menu, so adding a third menu needs only a new line in
MENUS. - On
PDF::errorthe script falls back to the lunch menu.
4. An unattended kiosk
Section titled “4. An unattended kiosk”A noticeboard that turns to the next page every eight seconds, and pauses for half a minute when a visitor uses the pill. The Behavior entity is a child of a PDF entity with a Signal Emitter whose signal is kiosk.
#pragma lifecycle(startup)
const PAGE_MS = 8000;const IDLE_MS = 30000;
let board;let pages = 1;let expected = 1;let cancelNext = () => {};
function schedule(ms) { cancelNext(); cancelNext = delay(() => { expected = board.props.page < pages ? board.props.page + 1 : 1; board.setProps({ page: expected }); schedule(PAGE_MS); }, ms);}
function startup() { board = self.parent.getComponent(PDFComponent);
on('kiosk', (event) => { if (event.eventType === EventType.PDF.Ready) { pages = event.data.pages; if (pages > 1) schedule(PAGE_MS); } if (event.eventType === EventType.PDF.Page && event.data.page !== expected) { // Someone used the pill: leave them time to read. expected = event.data.page; schedule(IDLE_MS); } });}A PDF noticeboard turns to the next page every eight seconds, and waits thirty seconds after a visitor turns a page with its pill.
Watch the pages turn every eight seconds, or use the page pill to read at your own pace. Drag elsewhere to orbit the camera, scroll to zoom.
delayreturns a function that cancels the call.schedulecancels the pending turn before setting a new one, so only one is ever waiting.expectedremembers which page the script asked for. APDF::pagefor any other page must have come from the pill, so the script waits longer.- A one-page document never schedules anything.
Performance tips
Section titled “Performance tips”- Turn pages in response to people, not every frame. Each change draws a page, and opening a large document or a heavy page can take a few frames.
- Use
sizing: 'auto'when documents of different shapes share one place, instead of resizing the entity from a script. - The page is drawn unlit and double-sided, so it costs no lighting work.
Common mistakes
Section titled “Common mistakes”- Counting pages from
PDF::pageonly. The first page is announced byPDF::ready, which is also where the page count arrives. - Forgetting
page: 1when changingsrc. The new document opens at the current page number. - Adding a Selectable to the PDF entity. It does not accept one; take clicks on a separate entity, a UI button or a key.
- Setting
heightwithoutsizing: 'auto'.heightis only a bound for automatic sizing; otherwise the page’s shape decides the height. - Assuming pages start at 0. Pages are counted from 1.