API Docs for: 0.5.1
Show:

BaseBatch Class

Defined in: lib/BaseBatch.js:13
Module: kami

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
)

Defined in lib/BaseBatch.js:13

Parameters:

  • context WebGLContext

    the context this batcher belongs to

  • size Number

    the optional size of this batch, i.e. max number of quads

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 Number

    the 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:

Number:

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:

Array:

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 Texture

    the texture for this sprite

  • x Number

    the x position, defaults to zero

  • y Number

    the y position, defaults to zero

  • width Number

    the width, defaults to the texture width

  • height Number

    the height, defaults to the texture height

  • u1 Number

    the first U coordinate, default zero

  • v1 Number

    the first V coordinate, default zero

  • u2 Number

    the second U coordinate, default one

  • v2 Number

    the 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 Texture

    the texture we are drawing for this sprite

  • verts Float32Array

    an array of vertices

  • off Number

    the 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:

Number:

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 GLenum

    the source blend parameter

  • blendDst GLenum

    the 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 Number

    the red component, normalized

  • g Number

    the green component, normalized

  • b Number

    the blue component, normalized

  • a Number

    the 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

Defined in lib/BaseBatch.js:90

The ABGR packed color, as a single float. The default value is the color white (255, 255, 255, 255).

drawing

Boolean

Defined in lib/BaseBatch.js:80

Whether we are currently drawing to the batch. Do not modify.

ownsShader

Boolean

Defined in lib/BaseBatch.js:64

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

Defined in lib/BaseBatch.js:99

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

Defined in lib/BaseBatch.js:56

This shader will be used whenever "null" is passed as the batch's shader.

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().