Image Ktx2
Updated
A GPU-compressed image, fast to load and light on memory.
- Type
ent-image-ktx2- Script class
KTX2Entity- Family
- Media
Overview
Section titled “Overview”An Image KTX2 entity shows a picture stored as a .ktx2 file: a texture already compressed for the graphics card. A normal image is decompressed into full-size pixels in GPU memory; a KTX2 image stays compressed there, so it loads faster and uses much less memory. That matters most on phones and standalone headsets, and in scenes with many or large pictures.
It behaves exactly like an Image: an unlit, cover-fitted picture on a plane, or on a sphere for a 360 panorama, the same in the editor and in play. When several surfaces show the same file, they share one copy of the texture.
Use it when you can prepare .ktx2 files ahead of time. For a picture that arrives as a plain PNG or JPEG, such as one chosen at runtime, use an Image instead.
The props that matter most, on its KTX2 component (KTX2Component in scripts):
src: the.ktx2file, as a URL or an asset from your project.widthandheight: the plane’s size in metres; withsizing: 'auto'they become bounds and the picture keeps its own proportions.opacity,color,grayscaleandzoom: fade, tint, desaturate or zoom the picture.projectionandside:equirectangularwithside: 'back'for a 360 panorama seen from inside.
Set it up
Section titled “Set it up”- Prepare the picture as a
.ktx2file. - Add an Image KTX2 entity to the scene and name it, such as
Floor Map. - Set its
srcto the file, andwidthandheightto the size you want in metres. - Optionally add a Signal Emitter to hear
KTX2::readyandKTX2::errorin scripts.
Examples
Section titled “Examples”Switch to a high-detail version
Section titled “Switch to a high-detail version”Show a light picture first and replace it with a larger one when the player asks.
#pragma lifecycle(startup, update)
// Scene: an Image KTX2 entity "Map".let map;let detailed = false;
function startup() { map = findEntity('Map').getComponent(KTX2Component);}
function update() { if (!detailed && Input.keyboard.isKeyReleased(Key.Space)) { detailed = true; map.setProps({ src: '/assets/map-4k.ktx2' }); }}How it works:
getComponent(KTX2Component)reaches the picture’s settings.Input.keyboard.isKeyReleased(Key.Space)is true once per key press.- Setting
srcloads the new file; the current picture stays visible until it is ready.
Report a missing file
Section titled “Report a missing file”Log the picture’s size when it loads, and hide the entity if it fails.
#pragma lifecycle(startup)
// Scene: an Image KTX2 entity "Map" with a Signal Emitter on the signal 'map'.function startup() { on('map', (event) => { const entity = findEntity(event.entityId); if (event.eventType === EventType.KTX2.Ready) { log(entity.name, 'loaded:', event.data.width, 'x', event.data.height, 'px'); } else if (event.eventType === EventType.KTX2.Error) { log(entity.name, 'failed:', event.data.message); entity.setEnabled(false); } });}How it works:
- The Signal Emitter puts the component’s events on
mapas{ entityId, eventType, data }. KTX2::readycarries the texture’s size in pixels;KTX2::erroramessage.setEnabled(false)switches the entity off so an empty plane is never left in the scene.
- Only
srcreloads the file.opacity,color,grayscaleandzoomchange the live surface and are cheap to animate. - A
.ktx2file must be prepared beforehand; renaming a PNG to.ktx2does not work and ends inKTX2::error.
Components
Section titled “Components”What a new Image Ktx2 is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Ktx2 | comp-ktx2 |
Required | One | A GPU-compressed image, fast to load and light on memory. |
You can also add 19 optional components, such as Animation Graph, Animation Timeline, Attribution, Billboard, Cache Mesh, Enable On Signal.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
width |
1 |
0 ≤ x ≤ 10000 |
height |
1 |
0 ≤ x ≤ 10000 |
color |
"#ffffff" |
|
zoom |
1 |
0. |
grayscale |
0 |
0 ≤ x ≤ 1 |
opacity |
1 |
0 ≤ x ≤ 1 |
projection |
"planar" |
|
side |
"double" |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
KTX2::readyKTX2::error
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
A framed picture on an easel shows a .ktx2 texture, which stays compressed in GPU memory. The script logs its pixel size when it is ready.
The picture on the easel is a KTX2 texture; the log gives its size. Drag to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Image Ktx2.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Image Ktx2'); if (!(entity instanceof KTX2Entity)) return;
// Its Ktx2 component holds the settings listed under Props. const component = entity.getComponent(KTX2Component); log('width is', component.props.width); component.setProps({ width: 1.5 });}