Skip to content

Image Ktx2

Updated

A GPU-compressed image, fast to load and light on memory.

Type
ent-image-ktx2
Script class
KTX2Entity
Family
Media

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 .ktx2 file, as a URL or an asset from your project.
  • width and height: the plane’s size in metres; with sizing: 'auto' they become bounds and the picture keeps its own proportions.
  • opacity, color, grayscale and zoom: fade, tint, desaturate or zoom the picture.
  • projection and side: equirectangular with side: 'back' for a 360 panorama seen from inside.
  1. Prepare the picture as a .ktx2 file.
  2. Add an Image KTX2 entity to the scene and name it, such as Floor Map.
  3. Set its src to the file, and width and height to the size you want in metres.
  4. Optionally add a Signal Emitter to hear KTX2::ready and KTX2::error in scripts.

Show a light picture first and replace it with a larger one when the player asks.

DetailSwap.jx
#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 src loads the new file; the current picture stays visible until it is ready.

Log the picture’s size when it loads, and hide the entity if it fails.

KtxStatus.jx
#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 map as { entityId, eventType, data }.
  • KTX2::ready carries the texture’s size in pixels; KTX2::error a message.
  • setEnabled(false) switches the entity off so an empty plane is never left in the scene.
  • Only src reloads the file. opacity, color, grayscale and zoom change the live surface and are cheap to animate.
  • A .ktx2 file must be prepared beforehand; renaming a PNG to .ktx2 does not work and ends in KTX2::error.

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-transform 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.001 ≤ x ≤ 1000
grayscale 0 0 ≤ x ≤ 1
opacity 1 0 ≤ x ≤ 1
projection "planar"
side "double"

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • KTX2::ready
  • KTX2::error

Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

KTX2Entity.jx
#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 });
}