API

Function createTexture2DFromExternalImage

  • Create a fresh, normal Texture2D from an already-decoded external image. Supported sources are the platform types accepted by WebGPU's copyExternalImageToTexture, including ImageBitmap, ImageData, DOM or offscreen canvases, image/video elements, and VideoFrame.

    Every invocation allocates a distinct GPU texture; no source or URL cache is used. The caller keeps ownership of source, and this function never closes it. The returned texture owns one resource-pool reference and must be released with releaseTexture after it is detached from all materials.

    Parameters

    • engine: EngineContext

      Engine context.

    • source: GPUCopyExternalImageSource

      An already-decoded WebGPU external-image source.

    • options: ExternalImageTexture2DOptions = {}

      Sampler, format, upload, and optional downscaling overrides.

    Returns Promise<Texture2D>

    A newly allocated texture. Resize/decode and upload failures reject.

    const bitmap = await createImageBitmap(blob);
    const texture = await createTexture2DFromExternalImage(engine, bitmap, {
    maxDimension: 2048,
    invertY: true,
    srgb: true,
    });
    bitmap.close();
    material.diffuseTexture = texture;
    // After detaching it from every material:
    releaseTexture(texture);