Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Texture2D

Wrapper for an WebGL 2D texture providing size accessors and requiring for bind, unbind, resize, validity, and initialization implementations. The texture object is created on initialization and deleted on uninitialization. After being initialized, the texture can be resized, reformated, and data can set directly or via fetch:

const texture = new Texture2D(context, 'Texture');
texture.initialize(1, 1, gl.RGB8, gl.RGB, gl.UNSIGNED_BYTE);
texture.fetch('/img/webgl-operate-logo.png', true);

Hierarchy

  • AbstractObject<WebGLTexture>
    • Texture2D

Implements

  • Bindable

Index

Constructors

constructor

  • Object constructor, requires a context and a valid identifier.

    Parameters

    • context: Context

      Valid context to create the object for.

    • Optional identifier: string

      Meaningful name for identification of this instance.

    Returns Texture2D

Properties

Protected _anisotropy

_anisotropy: undefined | number = ...
see

{@link anisotropy}

Protected _context

_context: Context
see

context

Protected _format

_format: number = 0
see

format

Protected _height

_height: number = 0
see

height

Protected _identifier

_identifier: string

Protected _internalFormat

_internalFormat: number = 0

Protected _mipmap

_mipmap: boolean = false

Whether or not to generate mip maps (based on minification settings). If true, gl.generateMipmap will be called whenever the image data changes.

Protected _object

_object: undefined | WebGLTexture
see

object

Protected _referenceCount

_referenceCount: number = 0

Number of references to this object. If at least a single reference was counted, this object can neither be initialized (and thus created) nor uninitialized (and thus deleted). The reference count is controlled via ref() and unref() functions.

Protected _type

_type: number = 0
see

type

Protected _valid

_valid: boolean = false
see

valid

Protected _width

_width: number = 0
see

width

Protected assertInitialized

assertInitialized: () => void = ...

Asserts the objects initialization status to be true. Note that the implementation is cached and forwarded to either an empty function when initialized and to an acutal assert(false) otherwise.

Type declaration

    • (): void
    • Returns void

Protected assertUninitialized

assertUninitialized: () => void = ...

Asserts the objects initialization status to be false. Note that the implementation is cached and forwarded to either an empty function when uninitialized and to an acutal assert(false) otherwise.

Type declaration

    • (): void
    • Returns void

Static Readonly DEFAULT_TEXTURE

DEFAULT_TEXTURE: undefined = ...

Default texture, e.g., used for unbind.

Static MAX_ANISOTROPY

MAX_ANISOTROPY: undefined | number = ...

Accessors

bytes

  • get bytes(): number
  • Returns the number of bytes this object approximately allocates on the GPU. The size will be zero when no image data was passed to the texture object.

    Returns number

context

  • Read-only access to the objects context, used to get context information and WebGL API access.

    Returns Context

format

  • get format(): number
  • Cached format of the data provided to the texture object for efficient resize. This is set on initialization and might change on data transfers.

    Returns number

height

  • get height(): number

identifier

  • get identifier(): string
  • Every GPU asset that allocates memory should provide a human readable identifier for GPU allocation tracking and debugging purposes. Please note that the identifier might changed on initialization due to the generation and assignment of a unique identifier.

    Returns string

    • This assets identifier used for gpu allocation tracking and debugging.

initialized

  • get initialized(): boolean
  • Property getter for readonly access to the initialization status of an initializable instance.

    Returns boolean

internalFormat

  • get internalFormat(): number
  • Cached internal format of the texture for efficient resize. This can only be changed by re-initialization.

    Returns number

object

  • get object(): T

size

type

  • get type(): number
  • Cached type of the data provided to the texture used for efficient resize. This is set on initialization and might change on data transfers.

    Returns number

valid

  • get valid(): boolean
  • Cached object status used to derive validity when initialized.

    Returns boolean

    • True if the object status is complete, false otherwise.

width

  • get width(): number

Methods

bind

  • bind(unit?: number): void
  • Bind the texture object to a texture unit.

    Parameters

    • Optional unit: number

    Returns void

Protected create

  • create(width: number, height: number, internalFormat: number, format: number, type: number): undefined | WebGLTexture
  • Create a texture object on the GPU.

    Parameters

    • width: number

      Initial width of the texture in px.

    • height: number

      Initial height of the texture in px.

    • internalFormat: number

      Internal format of the texture object.

    • format: number

      Format of the texture data even though no data is passed.

    • type: number

      Data type of the texel data.

    Returns undefined | WebGLTexture

data

  • data(data: TexImage2DData, bind?: boolean, unbind?: boolean): void
  • Pass image data to the texture object.

    Parameters

    • data: TexImage2DData

      Texel data that will be copied into the objects data store.

    • bind: boolean = true

      Allows to skip binding the texture (e.g., when binding is handled outside).

    • unbind: boolean = true

      Allows to skip unbinding the texture (e.g., when binding is handled outside).

    Returns void

Protected delete

  • delete(): void
  • Delete the texture object on the GPU. This should have the reverse effect of create.

    Returns void

fetch

  • fetch(url: string, crossOrigin?: boolean, flipY?: boolean): Promise<void>
  • Asynchronous load of an image via URL or data URI.

    Parameters

    • url: string

      Uniform resource locator string referencing the image that should be loaded (data URI supported).

    • crossOrigin: boolean = false

      Enable cross origin data loading.

    • flipY: boolean = false

    Returns Promise<void>

    • Promise for handling image load status.

filter

  • filter(mag: number, min: number, bind?: boolean, unbind?: boolean): void
  • Sets the texture object's magnification and minification filter. If a mipmap mode is set for minification, will be generated automatically whenever the image data changes. The MipMap can be generated manually as well by calling generateMipMap (@see generateMipMap).

    Parameters

    • mag: number

      Value for the TEXTURE_MAG_FILTER parameter.

    • min: number

      Value for the TEXTURE_MIN_FILTER parameter.

    • bind: boolean = true

      Allows to skip binding the texture (e.g., when binding is handled outside).

    • unbind: boolean = true

      Allows to skip unbinding the texture (e.g., when binding is handled outside).

    Returns void

generateMipMap

  • generateMipMap(bind?: boolean, unbind?: boolean): void
  • Generate MipMap for the texture. This is only required when minification filter is set to use the MipMap. If the mipmap is generated, it will be automatically regenerated whenever the image data changes via one of this classes' methods. Should be called manually when updated image data from outside though.

    Parameters

    • bind: boolean = true

      Allows to skip binding the texture (e.g., when binding is handled outside).

    • unbind: boolean = true

      Allows to skip unbinding the texture (e.g., when binding is handled outside).

    Returns void

initialize

  • initialize(...args: any[]): boolean
  • override

    Ensure that an object handle is created at the point of initialization. When overriding this function super.initialize() has to be invoked immediately/first. Please note that initialization of invalid object raises an assertion in order to prevent further actions without a valid WebGL object. After object creation the valid property is expected to be set accordingly.

    Parameters

    • Rest ...args: any[]

    Returns boolean

maxAnisotropy

  • maxAnisotropy(max: undefined | number, bind?: boolean, unbind?: boolean): undefined | number
  • Sets this textures anisotropy value. If anisotropy is not supported a debug message is logged. If anisotropy is supported, the given value is clamped to the maximum supported anisotropy value and setup. Note that using as well as changing a texture's anisotropy value requires (re)generating a MipMap and takes only effect when minification filtering is setup to use MipMapping.

    Parameters

    • max: undefined | number

      Targeted maximum anisotropy value (will be clamped to [0.0, MAX_ANISOTROPY]).

    • bind: boolean = true

      Allows to skip binding the texture (e.g., when binding is handled outside).

    • unbind: boolean = true

      Allows to skip unbinding the texture (e.g., when binding is handled outside).

    Returns undefined | number

    • The anisotropy value that was actually set (undefined if anisotropy is not supported).

Protected reallocate

  • reallocate(): void

ref

  • ref(): void
  • Increment the reference count of this object.

    Returns void

reformat

  • reformat(internalFormat: number, format?: number, type?: number, bind?: boolean, unbind?: boolean): void
  • This can be used to reformat the texture image without creating a new texture object. Please note that this resets the texture's image data to undefined. @see data for setting new image data.

    Parameters

    • internalFormat: number

      Internal format of the texture object.

    • Optional format: number

      Format of the texture data even though no data is passed.

    • Optional type: number

      Data type of the texel data.

    • bind: boolean = true

      Allows to skip binding the texture (e.g., when binding is handled outside).

    • unbind: boolean = true

      Allows to skip unbinding the texture (e.g., when binding is handled outside).

    Returns void

resize

  • resize(width: number, height: number, bind?: boolean, unbind?: boolean): void
  • This should be used to implement efficient resize the texture.

    Parameters

    • width: number

      Targeted/new width of the texture in px.

    • height: number

      Targeted/new height of the texture in px.

    • bind: boolean = true

      Allows to skip binding the texture (e.g., when binding is handled outside).

    • unbind: boolean = true

      Allows to skip unbinding the texture (e.g., when binding is handled outside).

    Returns void

unbind

  • unbind(unit?: number): void
  • Unbind the texture object from a texture unit.

    Parameters

    • Optional unit: number

    Returns void

uninitialize

  • uninitialize(): void
  • override

    Ensure that an object handle is deleted, invalidated, and its allocated GPU resources are set to zero. When overriding this function super.uninitialize() has to be invoked last/at the end. Note that an object cannot be uninitialized if it is referenced (reference count > 0).

    Returns void

unref

  • unref(): void
  • Decrement the reference count of this object.

    Returns void

wrap

  • wrap(wrap_s: number, wrap_t: number, bind?: boolean, unbind?: boolean): void
  • Sets the texture object's wrapping function for s and t coordinates.

    Parameters

    • wrap_s: number

      Value for the TEXTURE_WRAP_S parameter, defaulted to CLAMP_TO_EDGE.

    • wrap_t: number

      Value for the TEXTURE_WRAP_T parameter, defaulted to CLAMP_TO_EDGE.

    • bind: boolean = true

      Allows to skip binding the texture (e.g., when binding is handled outside).

    • unbind: boolean = true

      Allows to skip unbinding the texture (e.g., when binding is handled outside).

    Returns void

Static Protected Readonly assertInitializedFalse

Static Protected Readonly assertUninitializedFalse

Static assert_initialized

  • assert_initialized(): MethodDecorator
  • Method decorator for asserting the initialization status of an initializable to be true.

    see

    assertInitialized

    Returns MethodDecorator

Static assert_uninitialized

  • assert_uninitialized(): MethodDecorator

Static discard

  • discard(): MethodDecorator
  • Method decorator for discarding of Initializable inheritors. This decorator asserts the initialization status of the instance that is to be discarded, invokes its uninitialization, and falsifies the initialization status. In order to encourage the use of assertInitialized and assertUninitialized they are dynamically bound to a static, always-failing assert and an empty/undefined function respectively.

    Returns MethodDecorator

Static initialize

  • initialize(): MethodDecorator
  • Method decorator for initialization of Initializable inheritors. This decorator asserts the initialization status of the instance that is to be initialized, invokes its initialization with arbitrary number of parameters, and sets the initialization status to the initialization success (either false or true). In order to encourage the use of assertInitialized and assertUninitialized they are dynamically bound to either a static, always-failing assert or an empty/undefined function.

    Returns MethodDecorator

Static uninitialize

  • uninitialize(): MethodDecorator
  • Method decorator for uninitialization of Initializable inheritors. This decorator asserts the initialization status of the instance that is to be uninitialized, invokes its uninitialization, and falsifies the initialization status. In order to encourage the use of assertInitialized and assertUninitialized they are dynamically bound to a static, always-failing assert and an empty/undefined function respectively.

    Returns MethodDecorator