VertexLayout.Builder


class VertexLayout.Builder


Builder for VertexLayout.

This builder is capable of building a layout where vertex attributes are spread across multiple vertex buffers. Attributes added with addAttribute belong to the current buffer. To start adding attributes to a new buffer, call startNextBuffer.

An IllegalArgumentException will be thrown by addAttribute if duplicate attributes are added, and by build if the layout does not contain a VertexAttribute.POSITION attribute, or if only one of VertexAttribute.BONE_INDICES or VertexAttribute.BONE_WEIGHTS is present.

Basic example of creating a layout with a single vertex buffer:

val layout = VertexLayout.Builder()
.addAttribute(VertexAttribute.POSITION, VertexAttributeType.FLOAT3)
.addAttribute(VertexAttribute.NORMAL, VertexAttributeType.FLOAT3)
.addAttribute(VertexAttribute.UV0, VertexAttributeType.FLOAT2)
.build()

Example of creating a layout with multiple vertex buffers, explicit offsets, and strides:

val layout = VertexLayout.Builder()
// First buffer layout (e.g. position and normal interleaved)
.addAttribute(VertexAttribute.POSITION, VertexAttributeType.FLOAT3, offset = 0)
.addAttribute(VertexAttribute.NORMAL, VertexAttributeType.FLOAT3, offset = 12)
.setStride(24)
.startNextBuffer()
// Second buffer layout (e.g. UV and color interleaved)
.addAttribute(VertexAttribute.UV0, VertexAttributeType.FLOAT2, offset = 0)
.addAttribute(VertexAttribute.COLOR, VertexAttributeType.UBYTE4_NORM, offset = 8)
.setStride(12)
.build()

Summary

Public constructors

Public functions

VertexLayout.Builder

Adds a vertex attribute to the current buffer.

VertexLayout.Builder
addAttribute(
    attribute: VertexAttribute,
    type: VertexAttributeType,
    offset: @IntRange(from = -1, to = 32767) Int
)

Adds a vertex attribute to the current buffer.

VertexLayout

Builds the VertexLayout.

VertexLayout.Builder
setStride(stride: @IntRange(from = -1, to = 32767) Int)

Sets the stride for the current buffer.

VertexLayout.Builder

Commits the current buffer and prepares for the next buffer.

Public constructors

Builder

Added in 1.0.0-alpha16
Builder()

Public functions

addAttribute

Added in 1.0.0-alpha16
fun addAttribute(descriptor: VertexAttributeDescriptor): VertexLayout.Builder

Adds a vertex attribute to the current buffer.

Parameters
descriptor: VertexAttributeDescriptor

The VertexAttributeDescriptor defining the attribute to add.

Throws
IllegalArgumentException

if the attribute has already been added to the layout.

addAttribute

Added in 1.0.0-alpha16
fun addAttribute(
    attribute: VertexAttribute,
    type: VertexAttributeType,
    offset: @IntRange(from = -1, to = 32767) Int = VertexAttributeDescriptor.AUTO_OFFSET
): VertexLayout.Builder

Adds a vertex attribute to the current buffer.

Parameters
attribute: VertexAttribute

The VertexAttribute semantic of the attribute.

type: VertexAttributeType

The VertexAttributeType data type of the attribute.

offset: @IntRange(from = -1, to = 32767) Int = VertexAttributeDescriptor.AUTO_OFFSET

The byte offset of the attribute from the start of the vertex data. If set to VertexAttributeDescriptor.AUTO_OFFSET, the attribute will be placed immediately after the previous attribute in the same buffer, or at the beginning of the vertex data if this is the first attribute.

Throws
IllegalArgumentException

if the attribute has already been added to the layout.

build

Added in 1.0.0-alpha16
fun build(): VertexLayout

Builds the VertexLayout.

Throws
IllegalArgumentException

if no attributes were added, if the layout does not contain a VertexAttribute.POSITION attribute, if only one of VertexAttribute.BONE_INDICES or VertexAttribute.BONE_WEIGHTS is present, if any attributes in the current buffer overlap, or if an explicitly provided stride is smaller than the minimum byte stride required to encompass all attributes.

setStride

Added in 1.0.0-alpha16
fun setStride(stride: @IntRange(from = -1, to = 32767) Int): VertexLayout.Builder

Sets the stride for the current buffer.

Parameters
stride: @IntRange(from = -1, to = 32767) Int

The byte stride of the buffer. If set to VertexBufferLayout.AUTO_STRIDE, the stride will be computed automatically to be the minimum byte stride required to encompass all attributes in the buffer. Otherwise, it must be a positive value up to VertexBufferLayout.MAX_STRIDE.

startNextBuffer

Added in 1.0.0-alpha16
fun startNextBuffer(): VertexLayout.Builder

Commits the current buffer and prepares for the next buffer.

This method will add the previously set attributes and stride to the layout as a new VertexBufferLayout, and then reset the buffer layout state. The stride will be reset to VertexBufferLayout.AUTO_STRIDE. Subsequent calls to addAttribute and setStride will apply to a new buffer.

Throws
IllegalStateException

if no attributes were added to the current buffer.

IllegalArgumentException

if any attributes in the current buffer overlap, or if an explicitly provided stride is smaller than the minimum byte stride required to encompass all attributes in the buffer.