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 (if cubemaps then pass an array[6] with the data for every face)
- 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
check for more info about formats: https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texImage2D
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
- blend
- blit
- copyTo
- delete
- drawTo
- drawToColorAndDepth
- drawToColorAndDepth
- fill
- generateMetadata
- getPixels
- isDepthSupported
- renderQuad
- setPixels
- 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.getTemporary
- Texture.getWhiteTexture
- Texture.releaseTemporary
- Texture.setUploadOptions
- toBase64
- toBinary
- toBlob
- toCanvas
- toViewport
- unbind
- uploadData
- uploadImage
Methods
applyBlur
-
offsetx -
offsety -
intensity -
output_texture -
temp_texture
Applies a blur filter of 5x5 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)
-
output_textureTexture[optional] if not passed the output is the own texture
-
temp_textureTextureblur needs a temp texture, if not supplied it will use the temporary textures pool
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
blend
-
a -
b -
out
blends texture A and B and stores the result in OUT
Parameters:
-
aTexture -
bTexture -
outTexture[optional]
Returns:
blit
-
target_texture -
[shader=null] -
[uniforms=null]
Similar to CopyTo, but more specific, only for color texture_2D. It doesnt change the blend flag
Parameters:
-
target_textureGL.Texture -
[shader=null]GL.Shader optionaloptional shader to apply while copying
-
[uniforms=null]Object optionaloptional uniforms for the shader
copyTo
-
target_texture -
[shader=null] -
[uniforms=null]
Copy content of one texture into another TODO: check using copyTexImage2D
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
Similar to drawTo but it also stores the depth in a depth texture
Parameters:
-
color_textureTexture -
depth_textureTexture -
callbackFunction
drawToColorAndDepth
-
color_texture -
depth_texture -
callback
Static version of drawTo meant to be used with several buffers
Parameters:
-
color_textureTexture -
depth_textureTexture -
callbackFunction
fill
-
color -
skip_mipmaps
Fills the texture with a constant color (uses gl.clear)
Parameters:
-
colorVec4rgba
-
skip_mipmapsBooleanif true the mipmaps wont be updated
generateMetadata
()
Object
generates some basic metadata about the image
Returns:
getPixels
-
cubemap_face -
mipmap
returns an ArrayBuffer with the pixels in the texture, they are fliped in Y
Parameters:
-
cubemap_faceNumber[optional] the index of the cubemap face to read (ignore if texture_2D)
-
mipmapNumberlevel [optional, default is 0]
Returns:
the data ( Uint8Array, Uint16Array 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
setPixels
-
data -
no_flip -
skip_mipmaps -
cubemap_face
uploads some pixels to the texture (see uploadData method for more options)
Parameters:
-
dataArrayBuffergl.UNSIGNED_BYTE or gl.FLOAT data
-
no_flipBooleando not flip in Y
-
skip_mipmapsBooleando not update mipmaps when possible
-
cubemap_faceNumberif the texture is a cubemap, which face
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
-
old_texture -
options
Create a clone of a texture
Parameters:
-
old_textureTexture -
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.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.getTemporary
-
width -
height -
options -
gl
Returns a texture from the texture pool, if none matches the specifications it creates one
Parameters:
-
widthNumberthe texture width
-
heightNumberthe texture height
-
optionsObject | Textureto specifiy texture_type,type,format, it can be an object or another texture
-
glWebGLContext[optional]
Returns:
the textures that matches this settings
Texture.getWhiteTexture
()
Texture
returns a white texture of 1x1 pixel
Returns:
the white texture
Texture.releaseTemporary
-
tex -
gl
Given a texture it adds it to the texture pool so it can be reused in the future
Parameters:
-
texGL.Texture -
glWebGLContext[optional]
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
toBinary
-
flip_y
returns the texture file in binary format
Parameters:
-
flip_yBoolean
Returns:
the arraybuffer of the file containing the image
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, cubemap_face, mipmap_level)
uploadImage
-
img -
options
Given an Image/Canvas/Video it uploads it to the GPU
Parameters:
-
imgImage -
optionsObject[optional] upload options (premultiply_alpha, no_flip)
