How can I load an image from a URL, and then convert it to a sprite to change the UIImage sprite?
Hello Tomislav Å tefan. You can load an image dynamically by using Three.jsās TextureLoader to fetch the texture from a URL and then assign it to your UI componentās image property. In Needle Engine the UI Image component (defined in the engineās Image.ts īciteīturn0file0ī) automatically converts the assigned texture into a sprite. For example, if you have a UI Image component set up in your scene, you can write a Behaviour like this:
import { Behaviour, serializable, Image } from "@needle-tools/engine";
import { TextureLoader } from "three";
export class DynamicImageLoader extends Behaviour {
// Reference your UI Image component from the scene.
@serializable(Image)
uiImage?: Image;
start() {
const url = "https://example.com/your-image.png"; // Replace with your image URL
new TextureLoader().load(
url,
(texture) => {
// Ensure the texture updates correctly
texture.needsUpdate = true;
// Assign the loaded texture to the UI Image component.
if (this.uiImage) {
this.uiImage.image = texture;
} else {
console.warn("UI Image reference is not set.");
}
},
undefined, // Optionally track the loading progress.
(error) => {
console.error("Failed to load the texture:", error);
}
);
}
}
In this snippet, the TextureLoader fetches the image from the given URL. Once loaded, the texture is automatically converted into the proper sprite for the UI Image (via the setter in the Image component) as shown in the engineās internal code īciteīturn0file0ī.
This approach lets you dynamically update your UI images at runtime. Let me know if you need further details!
4 replies left
thread_hsHu8g84wpuHhe7dB92p4akg
Reply to this post directly if you need further help (bottom right button)
Loading works but my images are upside down
Hello Tomislav Å tefan,
When images appear upside down itās usually due to the default vertical flipping that Three.js applies to textures. By default textures have flipY set to true, which can sometimes cause them to render upside down in your scene. To fix this, ensure that after loading your texture you set its flipY property to false.
For example, you might modify your Needle Engine component as follows:
import { Behaviour } from "@needle-tools/engine"
import { TextureLoader, MeshStandardMaterial, Object3D } from "three"
export class FixTextureOrientation extends Behaviour {
start() {
const loader = new TextureLoader();
loader.load("path/to/your/image.png", (texture) => {
// Disable vertical flipping of the texture
texture.flipY = false;
// Find the material on your game object and apply the texture
const mesh = this.gameObject as Object3D;
// Assumes your mesh has a material property from a Renderer component
if (mesh && (mesh as any).material) {
const material = (mesh as any).material as MeshStandardMaterial;
material.map = texture;
material.needsUpdate = true;
}
});
}
}
This snippet sets texture.flipY to false so the image is displayed with the correct orientation. If you are loading a glTF or another asset type, double-check whether their import settings or shaders require any additional adjustments regarding image orientation.
For more details on texture settings in Three.js, you can refer to the Three.js Texture docs. Let me know if you need further assistance with your Needle Engine project.
4 replies left
thread_9yelXGHdvs5Z5sFLsXe5GTo9
Reply to this post directly if you need further help (bottom right button)
How can I get the size of the texture in pixels?
Hi Tomislav Å tefan,
You can access a textureās pixel size by reading the width and height properties on its image. When a texture is loaded (using Three.jsās TextureLoader or via a glTF import), the texture object will have an image property that is typically an HTMLImageElement, canvas, or ImageBitmap. You can then use texture.image.width and texture.image.height to get the dimensions in pixels.
Below is an example Needle Engine component that loads a texture and logs its dimensions:
import { Behaviour } from "@needle-tools/engine";
import { TextureLoader } from "three";
export class TextureSizeLogger extends Behaviour {
start() {
const loader = new TextureLoader();
loader.load("path/to/your/texture.png", (texture) => {
// Access the textureās underlying image dimensions
const width = texture.image.width;
const height = texture.image.height;
console.log(`Texture size: ${width}px x ${height}px`);
});
}
}
With this approach, once the texture is loaded the image property will be available and you can easily access its pixel width and height. Let me know if you need further clarification!
4 replies left
thread_9yelXGHdvs5Z5sFLsXe5GTo9
Reply to this post directly if you need further help (bottom right button)
This topic was automatically closed 13 days after the last reply. New replies are no longer allowed.