Texture class
Basic usage
Creation
There are several ways to create textures.
The easiest one is by simply calling its constructor:
You can also create textures by creating a TextureLoader first and then load HTML medias elements with it:
Another way to create a texture is by using the createTexture method of your Plane or ShaderPass class object:
Textures are also automatically created each time one of the plane loading methods is called.
Eventually, if your plane autoloadSources initial parameter is set to true (which is by default), they will be created on initialization for each image, canvas or video children of your plane.
Texture options
Upon creation, you can pass a few options like magnification and minification filters, anisotropy, wrapping or premultiplyAlpha, etc.
See the complete parameters list to have a look at all the options available.
Built-in uniform names
By default the textures sampler and matrix uniform names sent to your shaders will be set upon their media element order inside your plane element, following this rule:
`uSampler{index}` for texture samplers
`uTextureMatrix{index}` for texture matrices
HTML
Javascript
Shaders
Using custom uniform names
You can define custom uniform names by specifying a data-sampler attribute of your media HTML element. The uniform names will then follow these rules:
`{data-sampler}` for texture samplers
`{data-sampler}Matrix` for texture matrices
HTML
Shaders
A newly created texture will just display black pixels until you've set it a source from one of the images, canvases or videos already loaded in your plane.
Using texture matrices
Shaders use texture coordinates (or UVs) to map a texture to the plane.
By default the library sets an arbitrary aTextureCoord attributes that you could use in your shaders:
You may use it to map your texture to your plane, but if the plane and the media aspect ratio are different, you'll end up with a distorted texture.
Fortunately, there's a built-in solution to display your textures without them being distorted: texture matrices. Each time you set a source to a texture (or each time a plane is resized), its texture matrix is automatically computed and sent to your shader as a uniform. With the same example as above, here's how to use it:
The texture matrix uniform name will follow the same rule as the sampler uniform name:
If you did not specify any name, it will be named "uTextureMatrix" + your texture index.
If you did specify a sampler name, it will be named samplerName + "Matrix".
Parameters
Here is the complete list of mandatory and optional parameters for the texture:
- curtains Curtains class object Your Curtains class object.
-
params
object, optional
an object containing the texture parameters:
- sampler string, optional Sampler name to use in your sampler and texture matrix uniforms.
- fromTexture Texture class object, optional Copy the texture passed into this texture
- premultiplyAlpha bool, optional whether this texture should use premultiplied alpha or not. Default to false.
- floatingPoint string, optional whether this texture should use floating points or not (if available, depending on the WebGL context and extensions). Accepted values are "none", "half-float" and "float". Default to "none".
- anisotropy integer, optional texture anisotropy value (see anisotropic filtering explanation). Usually ranging from 1 to 16, depending on the WebGL context and extensions available. Default to 1.
- generateMipmap bool, optional Whether to generate mipmaps for that texture (see mip maps explanation). Default to true for images textures. Note that videos and canvases textures will never generate mip maps as they are frequently uploaded.
- minFilter GLenum, optional the texture minification filter. Use one of the GLenum possible values, depending on the WebGL context and size of the source (some WebGL1 filters need a power of 2 sized source). Default to gl.LINEAR.
- magFilter GLenum, optional the texture magnification filter. Use one of the GLenum possible values, depending on the WebGL context and size of the source (some WebGL1 filters need a power of 2 sized source). Default to gl.LINEAR.
- wrapS GLenum, optional the texture wrapping along the X axis. Use one of the GLenum possible values, depending on the WebGL context and size of the source (some WebGL1 wrapping need a power of 2 sized source). Default to gl.CLAMP_TO_EDGE.
- wrapT GLenum, optional the texture wrapping along the Y axis. Use one of the GLenum possible values, depending on the WebGL context and size of the source (some WebGL1 wrapping need a power of 2 sized source). Default to gl.CLAMP_TO_EDGE.
Properties
-
gl (WebGL context) v7.0read only
Our WebGL context.
-
index (integer): v2.0 read only
The index of your texture in the Plane or ShaderPass textures array.
Since ShaderPass silently creates a texture on init, if you add a texture to it later, this texture will have an index of 1. -
offset (Vec2 class object): v7.3
An object containing the offset applied to your texture on x and y axis. Only applied if you are using the texture matrix to map your texture in your shaders.
-
parameters (object): v7.0read only
Actual texture parameters values used: anisotropy, generateMipmap, minFilter, magFilter, wrapS and wrapT.
-
renderer (Renderer class object): v7.0read only
The renderer created by our curtains object.
-
scale (Vec2 class object): v2.0
An object containing the scale applied to your texture on x and y axis. Only applied if you are using the texture matrix to map your texture in your shaders.
-
shouldUpdate (boolean): v2.0
Depends on your texture sourceType.
By default, a canvas texture will be updated at each requestAnimationFrame call, a video texture will be updated when the next frame is available, and an image texture will never be updated.
If you want to stop updating your canvas or video texture, set this flag to false for performance boost.Examples: used in Text planes using canvas example.
-
source (HTML element): v2.0
The source used by your texture. Could be either an image, canvas or video HTML element. Use it to safely access any property or method of that element, like play() or pause() for videos.
-
sourceType (string): v7.0read only
The type or your texture. Could be either "empty", "image", "canvas", "video" or "fboTexture".
A "fboTexture" is a RenderTarget texture onto which your plane or scene is drawn. -
type (string): v7.0read only
Class object type: "Texture".
-
userData (object): v5.0
An empty object to store any additional data or custom properties into your texture.
-
uuid (string): v6.0read only
The texture's unique identifier.
Methods
-
addParent(parent): v7.0
Adds your texture to a Plane or a ShaderPass so it can display it.
- parent Plane class object or ShaderPass class object The WebGL object that should use that texture.
Examples: used in AJAX navigation with plane removal example.
-
copy(texture): v7.0
Copy an already existing Texture into this texture.
- texture Texture class object The texture to copy.
Examples: used in Ping pong shading flowmap example.
-
hasParent(): v7.0
Whether this texture currently has a parent or not.
returns: true if this texture has a parent, false otherwise.
Examples: used in AJAX navigation with plane removal example.
-
needUpdate(): v4.1
Force your texture to be updated during the next draw call, even if its shouldUpdate property is set to false. Use it if you want to force a canvas texture update just once for example.
Examples: used in Text planes using canvas example.
-
resize(): v4.2
Force the update of your texture matrix. Useful when you manually resize your source (usually a canvas).
Examples: used in Text planes using canvas example.
-
setAnisotropy(anisotropy): v7.0
Set the texture's anisotropy value (see anisotropic filtering explanation).
- anistropy integer anisotropy value to apply (usually between 1 - low and 16 - high).
Examples: used in Plane properties and transformations cheat sheet example.
-
setMagFilter(magFilter): v7.0
Set the texture's magnification filter.
- magFilter GLenum magnification filter to apply. Use one of the GLenum possible values, depending on the WebGL context and size of the source (some WebGL1 filters need a power of 2 sized source).
-
setMinFilter(minFilter): v7.0
Set the texture's minification filter.
- minFilter GLenum minification filter to apply. Use one of the GLenum possible values, depending on the WebGL context and size of the source (some WebGL1 filters need a power of 2 sized source).
Examples: used in Slideshow with a displacement shader example.
-
setOffset(offset): v7.3
Set the texture offset. Only applied if you are using the texture matrix to map your texture in your shaders.
- scale Vec2 class object offset to apply along the X and Y axes (0.5 along the X axis meaning half the width).
Examples: used in GSAP click to fullscreen gallery example.
-
setScale(scale): v2.0updated in v7.0
Set the texture new scale. Only applied if you are using the texture matrix to map your texture in your shaders.
- scale Vec2 class object the scale to set along the X and Y axes.
Examples: used in Multiple planes scroll effect : rotation, scale and parallax, Post processing multiple passes and GSAP click to fullscreen gallery examples.
-
setSource(sourceElement): v2.0
Use this method to set a source to your texture. The source must be already loaded in your plane, otherwise use one of the load method of your Plane or ShaderPass classes.
- sourceElement either an image, canvas or video HTML element a source to apply to your texture. It must be an element already loaded by your plane.
Examples: used in Slideshow with a displacement shader example.
-
setWrapS(wrapS): v7.0
Set the texture's wrapS value.
- wrapS GLenum texture wrapping to apply along the X axis. Use one of the GLenum possible values, depending on the WebGL context and size of the source (some WebGL1 wrapping need a power of 2 sized source).
-
setWrapT(wrapT): v7.0
Set the texture's wrapT value.
- wrapT GLenum texture wrapping to apply along the Y axis. Use one of the GLenum possible values, depending on the WebGL context and size of the source (some WebGL1 wrapping need a power of 2 sized source).
Events
-
onSourceLoaded(callback): v7.0
This function will be called the first time your texture source has been loaded.
- callback function function to execute.
returns: your Texture object, allowing it to be chainable.
-
onSourceUploaded(callback): v7.0
This function will be called the first time your texture source has been uploaded to the GPU.
- callback function function to execute.
returns: your Texture object, allowing it to be chainable.
Examples: used in AJAX navigation with plane removal and GSAP click to fullscreen gallery examples.