# graphics3d/jointblock (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/graphics3d/jointblock/

Joint matrix uniform block. (original graphics3d/jointblock.js)
- 96 joint matrices are 384 vec4 slots, which exceeds the 256 vertex uniform slots (MAX_VERTEX_UNIFORM_VECTORS) that WebGL2 guarantees at minimum.
  Desktop GPUs have 4096 slots so it still runs there, but Android Chrome (Galaxy Z Fold3) fails to link. Large arrays go through a uniform block.
  (On mobile, Unity passes skinning matrices as a constant buffer and Unreal reads a bone buffer, the same approach)
- The block is std140, so a mat4 is four column-major vec4 and 16 f32 values fit as they are. The shader body uses jointMatrices[index] unchanged.
- ShaderProgram binds the binding point to JOINT_BLOCK_BINDING after linking.
- When giving SkinnedModelRenderer a replacement vertex shader, insert JOINT_BLOCK_GLSL instead of `uniform mat4 jointMatrices[96];`.

Example: Insert the joint matrix block into a replacement skinning vertex shader

```rust
use vanilla::{JOINT_BLOCK_GLSL, JOINT_BLOCK_MAXIMUM};
let vertex_shader_source = format!("#version 300 es\nlayout(location = 0) in vec3 vertexPosition;\nlayout(location = 3) in uvec4 vertexJoints;\nlayout(location = 4) in vec4 vertexWeights;\n{}\nvoid main() {{\n\tmat4 skinMatrix = vertexWeights.x * jointMatrices[vertexJoints.x] + vertexWeights.y * jointMatrices[vertexJoints.y];\n\tgl_Position = skinMatrix * vec4(vertexPosition, 1.0);\n}}", JOINT_BLOCK_GLSL);
assert!(vertex_shader_source.contains("uniform JointBlock"));
assert_eq!(JOINT_BLOCK_MAXIMUM, 96);
```

## JointBlockBuffer (struct)

Joint matrix block buffer. (original JointBlockBuffer; each renderer owns one per context)

Example

```rust
use vanilla::JOINT_BLOCK_MAXIMUM;
// The buffer needs a GL context, so only the size is checked here. (96 joints x 64 bytes per mat4 = 6144 bytes, within the 16384 minimum of MAX_UNIFORM_BLOCK_SIZE)
assert!(JOINT_BLOCK_MAXIMUM * 64 <= 16384);
```

- `pub fn new(gl: &GlContext) -> Result<JointBlockBuffer, String>` (constructor, since 0.4.0): Creation. (Err carrying the error text if the buffer cannot be created)
  - `gl` &GlContext: GL context.
  - returns Result<JointBlockBuffer, String>: A uniform buffer that holds 6144 bytes of joint matrices. Err(error text) if the buffer cannot be created.
- `pub fn upload(&self, gl: &GlContext, values: &[f32])` (method, since 0.4.0): Upload joint matrices and bind to the binding point. (anything past JOINT_BLOCK_MAXIMUM is dropped; if the values are unchanged it only binds without uploading)
  - `gl` &GlContext: The GL context that created the buffer.
  - `values` &[f32]: Number of joint matrices. (a multiple of 16)
- `pub fn destroy(&self, gl: &GlContext)` (method, since 0.4.0): Disposes.
  - `gl` &GlContext: The GL context that created the buffer.

## Functions and constants

- `pub const JOINT_BLOCK_MAXIMUM: usize = 96` (field, since 0.4.0): Maximum joint count of the joint matrix block. (the same for the toon renderer's TOON_MAXIMUM_JOINTS and the skinning renderer)
  - returns usize
- `pub const MORPH_WEIGHT_VECTOR_COUNT: usize = 24` (field, since 0.4.0): Number of vec4 slots in the morph weight array. (MORPH_TARGET_MAXIMUM 96 weights, four per slot)
  - returns usize
- `pub const JOINT_BLOCK_GLSL: &str = joint_block_glsl_source!()` (field, since 0.4.0): Joint matrix block declaration. (original JOINT_BLOCK_GLSL)
  - returns &str
