GL.Texture Class
Texture class to upload images to the GPU, default is gl.TEXTURE_2D, gl.RGBA of gl.UNSIGNED_BYTE with filters set to gl.LINEAR and wrap to gl.CLAMP_TO_EDGE
There is a list of options
==========================
- texture_type: gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP, default gl.TEXTURE_2D
- format: gl.RGB, gl.RGBA, gl.DEPTH_COMPONENT, default gl.RGBA
- type: gl.UNSIGNED_BYTE, gl.UNSIGNED_SHORT, gl.HALF_FLOAT_OES, gl.FLOAT, default gl.UNSIGNED_BYTE
- filter: filtering for mag and min: gl.NEAREST or gl.LINEAR, default gl.NEAREST
- magFilter: magnifying filter: gl.NEAREST, gl.LINEAR, default gl.NEAREST
- minFilter: minifying filter: gl.NEAREST, gl.LINEAR, gl.LINEAR_MIPMAP_LINEAR, default gl.NEAREST
- wrap: texture wrapping: gl.CLAMP_TO_EDGE, gl.REPEAT, gl.MIRROR, default gl.CLAMP_TO_EDGE (also accepts wrapT and wrapS for separate settings)
- pixel_data: ArrayBufferView with the pixel data to upload to the texture, otherwise the texture will be black
- premultiply_alpha : multiply the color by the alpha value when uploading, default FALSE
- no_flip : do not flip in Y, default TRUE
- anisotropic : number of anisotropic fetches, default 0
Constructor
GL.Texture
-
width -
height -
options
Parameters:
-
widthNumbertexture width (any supported but Power of Two allows to have mipmaps), 0 means no memory reserved till its filled
-
heightNumbertexture height (any supported but Power of Two allows to have mipmaps), 0 means no memory reserved till its filled
-
optionsObjectCheck the list in the description
Item Index
Methods
- applyBlur
- bind
- copyTo
- delete
- drawTo
- drawToColorAndDepth
- drawToColorAndDepth
- fill
- generateMetadata
- getPixels
- isDepthSupported
- renderQuad
- Texture.cubemapFromImage
- Texture.cubemapFromImages
- Texture.cubemapFromURL
- Texture.fromDDSInMemory
- Texture.fromImage
- Texture.fromShader
- Texture.fromTexture
- Texture.fromTexture
- Texture.fromURL
- Texture.fromVideo
- Texture.generateCubemapCrossFaces
- Texture.getBlackTexture
- Texture.getWhiteTexture
- Texture.setUploadOptions
- toBase64
- toBlob
- toCanvas
- toViewport
- unbind
- uploadData
- uploadImage
Methods
applyBlur
-
offsetx -
offsety -
intensity -
temp_texture -
output_texture
Applies a blur filter of four pixels to the texture (be careful using it, it is slow)
Parameters:
-
offsetxNumberscalar that multiplies the offset when fetching pixels horizontally (default 1)
-
offsetyNumberscalar that multiplies the offset when fetching pixels vertically (default 1)
-
intensityNumberscalar that multiplies the result (default 1)
-
temp_textureTextureblur needs a temp texture, if not supplied it will create a new one each time!
-
output_textureTexture[optional] if not passed the output is the own texture
Returns:
returns the temp_texture in case you want to reuse it
bind
-
unit
Binds the texture to one texture unit
Parameters:
-
unitNumbertexture unit
Returns:
returns the texture unit
copyTo
-
target_texture -
[shader=null] -
[uniforms=null]
Copy content of one texture into another
Parameters:
-
target_textureGL.Texture -
[shader=null]GL.Shader optionaloptional shader to apply while copying
-
[uniforms=null]Object optionaloptional uniforms for the shader
delete
()
Free the texture memory from the GPU, sets the texture handler to null
drawTo
-
callback
Render to texture using FBO, just pass the callback to a rendering function and the content of the texture will be updated If the texture is a cubemap, the callback will be called six times, once per face, the number of the face is passed as a second parameter for further info about how to set up the propper cubemap camera, check the GL.Texture.cubemap_camera_parameters with the direction and up vector for every face.
Keep in mind that it tries to reuse the last renderbuffer for the depth, and if it cannot (different size) it creates a new one (throwing the old)
Parameters:
-
callbackFunctionfunction that does all the rendering inside this texture
drawToColorAndDepth
-
color_texture -
depth_texture -
callback
Static version of drawTo meant to be used with several buffers
Parameters:
-
color_textureTexture -
depth_textureTexture -
callbackFunction
drawToColorAndDepth
-
color_texture -
depth_texture -
callback
Similar to drawTo but it also stores the depth in a depth texture
Parameters:
-
color_textureTexture -
depth_textureTexture -
callbackFunction
fill
-
color
Fills the texture with a constant color (uses gl.clear)
Parameters:
-
colorVec4rgba
generateMetadata
()
Object
generates some basic metadata about the image
Returns:
getPixels
-
type -
force_rgba
returns an ArrayBuffer with the pixels in the texture, they are fliped in Y
Parameters:
-
typeEnumgl.UNSIGNED_BYTE or gl.FLOAT, if omited then the one in the texture is read
-
force_rgbaBoolif yo want to force the output to have 4 components per pixel (useful to transfer to canvas)
Returns:
the data ( Uint8Array or Float32Array )
isDepthSupported
()
Boolean
Returns if depth texture is supported by the GPU
Returns:
true if supported
renderQuad
-
x -
y -
width -
height
Render texture in a quad of specified area
Parameters:
-
xNumber -
yNumber -
widthNumber -
heightNumber
Texture.cubemapFromImage
-
image -
options
Create a cubemap texture from a single image that contains all six images If it is a cross, it must be horizontally aligned, and options.is_cross must be equal to the column where the top and bottom are located (usually 1 or 2) otherwise it assumes the 6 images are arranged vertically, in the order of OpenGL: +X, -X, +Y, -Y, +Z, -Z
Parameters:
-
imageImage -
optionsObject
Returns:
the texture
Texture.cubemapFromImages
-
images -
options
Create a cubemap texture from a set of 6 images
Parameters:
-
imagesArray -
optionsObject
Returns:
the texture
Texture.cubemapFromURL
-
image -
options -
on_complete
Create a cubemap texture from a single image url that contains the six images if it is a cross, it must be horizontally aligned, and options.is_cross must be equal to the column where the top and bottom are located (usually 1 or 2) otherwise it assumes the 6 images are arranged vertically.
Parameters:
-
imageImage -
optionsObject -
on_completeFunctioncallback
Returns:
the texture
Texture.fromDDSInMemory
-
DDS -
options
Create a texture from an ArrayBuffer containing the pixels
Parameters:
-
DDSArrayBufferdata
-
optionsObject
Returns:
the texture
Texture.fromImage
-
image -
options
Create a texture from an Image
Parameters:
-
imageImage -
optionsObject
Returns:
the texture
Texture.fromShader
-
width -
height -
shader -
options
Create a generative texture from a shader ( must GL.Shader.getScreenShader as reference for the shader )
Parameters:
-
widthNumber -
heightNumber -
shaderShader -
optionsObject
Returns:
the texture
Texture.fromTexture
-
width -
height -
pixels -
options
Create a texture from an ArrayBuffer containing the pixels
Parameters:
-
widthNumber -
heightNumber -
pixelsArrayBuffer -
optionsObject
Returns:
the texture
Texture.fromTexture
-
old_texture -
options
Create a clone of a texture
Parameters:
-
old_textureTexture -
optionsObject
Returns:
the texture
Texture.fromURL
-
url -
options -
on_complete
Loads and uploads a texture from a url
Parameters:
-
urlString -
optionsObject -
on_completeFunction
Returns:
the texture
Texture.fromVideo
-
video -
options
Create a texture from a Video
Parameters:
-
videoVideo -
optionsObject
Returns:
the texture
Texture.generateCubemapCrossFaces
-
width -
column
Given the width and the height of an image, and in which column is the top and bottom sides of the cubemap, it gets the info to pass to Texture.cubemapFromImage in options.faces
Parameters:
-
widthNumberof the CROSS image (not the side image)
-
columnNumberthe column where the top and the bottom is located
Returns:
object to pass to Texture.cubemapFromImage in options.faces
Texture.getBlackTexture
()
Texture
returns a black texture of 1x1 pixel
Returns:
the black texture
Texture.getWhiteTexture
()
Texture
returns a white texture of 1x1 pixel
Returns:
the white texture
Texture.setUploadOptions
-
options
Unbinds the texture
Parameters:
-
optionsObjecta list of options to upload the texture
- premultiply_alpha : multiply the color by the alpha value, default FALSE
- no_flip : do not flip in Y, default TRUE
toBase64
-
flip_y
returns a base64 String containing all the data from the texture
Parameters:
-
flip_yBooleanif you want to flip vertically the image, WebGL saves the images upside down
Returns:
the data in base64 format
toBlob
()
Blob
returns a Blob containing all the data from the texture
Returns:
the blob containing the data
toCanvas
-
canvas -
flip_y -
max_size
Copy texture content to a canvas
Parameters:
-
canvasCanvasmust have the same size, if different the canvas will be resized
-
flip_yBooleanoptional, flip vertically
-
max_sizeNumberoptional, if it is supplied the canvas wont be bigger of max_size (the image will be scaled down)
toViewport
-
shader -
uniforms
Render texture in a quad to full viewport size
Parameters:
-
shaderShaderto apply, otherwise a default textured shader is applied [optional]
-
uniformsObjectfor the shader if needed [optional]
unbind
-
unit
Unbinds the texture
Parameters:
-
unitNumbertexture unit
Returns:
returns the texture unit
uploadData
-
data -
options
Uploads data to the GPU (data must have the appropiate size)
Parameters:
-
dataArrayBuffer -
optionsObject[optional] upload options (premultiply_alpha, no_flip)
uploadImage
-
img -
options
Given an Image/Canvas/Video it uploads it to the GPU
Parameters:
-
imgImage -
optionsObject[optional] upload options (premultiply_alpha, no_flip)
