addTexture

Adds a new texture/image to the model, and returns its id.

addTexture(
	image: TextureData | Blob
): Promise<string>

Parameters
Description
Type

image

Object containing the ArrayBuffer, width and height of the texture to be set.

Usage:

// Working with Canvas and ImageData
const img = new Image();
img.onload = async () => {
	canvas.width = img.width;
	canvas.height = img.height;
	ctx.drawImage(img, 0, 0, img.width, img.height);
	const imgData = ctx.getImageData(0, 0, img.width, img.height);

	newTextureId = await modelApi.addTexture({
		image: imgData.data,
		width: imgData.width,
		height: imgData.height
	});
};
img.src = "{your_image_url}"
// Working with <input type="file">
uploadButton.addEventListener('change', async (e) => {
		const blob = uploadButton.files[0];
		newTextureId = await modelApi.addTexture(blob);
	}
});
// Working with URL directly
fetch("{your_image_url}")
	.then(response => response.blob())
	.then(blob => {
		newTextureId = await modelApi.addTexture(blob);
	});

Return value:

"230fceca-e2cc-4011-9bf4-56c6b091ec00" // uuidv4 format

Last updated