BaseBatch Class
A batcher mixin composed of quads (two tris, indexed).
This is used internally; users should look at SpriteBatch instead, which inherits from this class.
The batcher itself is not managed by WebGLContext; however, it makes use of Mesh and Texture which will be managed. For this reason, the batcher does not hold a direct reference to the GL state.
Subclasses must implement the following:
_createShader
_createVertexAttributes
getVertexSize
Constructor
BaseBatch
-
context
-
size
Parameters:
-
context
WebGLContextthe context this batcher belongs to
-
size
Numberthe optional size of this batch, i.e. max number of quads
Item Index
Methods
Methods
_createMesh
-
size
Called from the constructor to create a new Mesh based on the expected batch size. Should set up verts & indices properly.
Users should not call this directly; instead, it should only be implemented by subclasses.
Parameters:
-
size
Numberthe size passed through the constructor
_createShader
()
Number
Returns a shader for this batch. If you plan to support multiple instances of your batch, it may or may not be wise to use a shared shader to save resources.
This method initially throws an error; so it must be overridden by subclasses of BaseBatch.
Returns:
the size of a vertex, in # of floats
_createVertexAttributes
()
Array
Returns an array of vertex attributes for this mesh; subclasses should implement this with the attributes expected for their batch.
This method initially throws an error; so it must be overridden by subclasses of BaseBatch.
Returns:
an array of Mesh.VertexAttrib objects
_preRender
()
Called before rendering to bind new textures. This method does nothing by default.
begin
()
Begins the sprite batch. This will bind the shader and mesh. Subclasses may want to disable depth or set up blending.
destroy
()
Destroys the batch, deleting its buffers and removing it from the WebGLContext management. Trying to use this batch after destroying it can lead to unpredictable behaviour.
If ownsShader
is true, this will also delete the defaultShader
object.
draw
-
texture
-
x
-
y
-
width
-
height
-
u1
-
v1
-
u2
-
v2
Adds a sprite to this batch. The specifics depend on the sprite batch implementation.
Parameters:
-
texture
Texturethe texture for this sprite
-
x
Numberthe x position, defaults to zero
-
y
Numberthe y position, defaults to zero
-
width
Numberthe width, defaults to the texture width
-
height
Numberthe height, defaults to the texture height
-
u1
Numberthe first U coordinate, default zero
-
v1
Numberthe first V coordinate, default zero
-
u2
Numberthe second U coordinate, default one
-
v2
Numberthe second V coordinate, default one
drawVertices
-
texture
-
verts
-
off
Adds a single quad mesh to this sprite batch from the given array of vertices. The specifics depend on the sprite batch implementation.
Parameters:
-
texture
Texturethe texture we are drawing for this sprite
-
verts
Float32Arrayan array of vertices
-
off
Numberthe offset into the vertices array to read from
end
()
Ends the sprite batch. This will flush any remaining data and set GL state back to normal.
flush
()
Flushes the batch by pushing the current data to GL.
getVertexSize
()
Number
Returns the number of floats per vertex for this batcher.
This method initially throws an error; so it must be overridden by subclasses of BaseBatch.
Returns:
the size of a vertex, in # of floats
setBlendFunction
-
blendSrc
-
blendDst
Sets the blend source and destination parameters. This is a convenience function for the blendSrc and blendDst setters. If we are currently drawing, this will flush the batch.
Setting either to null
or a falsy value tells the SpriteBatch
to ignore gl.blendFunc. This is useful if you wish to use your
own blendFunc or blendFuncSeparate.
Parameters:
-
blendSrc
GLenumthe source blend parameter
-
blendDst
GLenumthe destination blend parameter
setColor
-
r
-
g
-
b
-
a
Sets the color of this sprite batcher, which is used in subsequent draw calls. This does not flush the batch.
If r, g, b, are all numbers, this method assumes that RGB or RGBA float values (0.0 to 1.0) are being passed. Alpha defaults to one if undefined.
If the first three arguments are not numbers, we only consider the first argument and assign it to all four components -- this is useful for setting transparency in a premultiplied alpha stage.
If the first argument is invalid or not a number, the color defaults to (1, 1, 1, 1).
Parameters:
-
r
Numberthe red component, normalized
-
g
Numberthe green component, normalized
-
b
Numberthe blue component, normalized
-
a
Numberthe alpha component, normalized
Properties
blendDst
GLenum
Sets the blend source parameters. If we are currently drawing, this will flush the batch.
Setting either src or dst to null
or a falsy value tells the SpriteBatch
to ignore gl.blendFunc. This is useful if you wish to use your
own blendFunc or blendFuncSeparate.
blendingEnabled
Boolean
A property to enable or disable blending for this sprite batch. If we are currently drawing, this will first flush the batch, and then update GL_BLEND state (enabled or disabled) with our new value.
blendSrc
GLenum
Sets the blend destination parameters. If we are currently drawing, this will flush the batch.
Setting either src or dst to null
or a falsy value tells the SpriteBatch
to ignore gl.blendFunc. This is useful if you wish to use your
own blendFunc or blendFuncSeparate.
color
Number
The ABGR packed color, as a single float. The default value is the color white (255, 255, 255, 255).
drawing
Boolean
Whether we are currently drawing to the batch. Do not modify.
ownsShader
Boolean
By default, a SpriteBatch is created with its own ShaderProgram,
stored in defaultShader
. If this flag is true, on deleting the SpriteBatch, its
defaultShader
will also be deleted. If this flag is false, no shaders
will be deleted on destroy.
Note that if you re-assign defaultShader
, you will need to dispose the previous
default shader yoursel.
premultiplied
Boolean
Whether to premultiply alpha on calls to setColor. This is true by default, so that we can conveniently write:
batch.setColor(1, 0, 0, 0.25); //tints red with 25% opacity
If false, you must premultiply the colors yourself to achieve the same tint, like so:
batch.setColor(0.25, 0, 0, 0.25);
Default: true
shader
ShaderProgram
This is a setter/getter for this batch's current ShaderProgram. If this is set when the batch is drawing, the state will be flushed to the GPU and the new shader will then be bound.
If null
or a falsy value is specified, the batch's defaultShader
will be used.
Note that shaders are bound on batch.begin().