Back home 🏡 WGSL Spec Bevy WGSL Functions

maths.wgsl

Import path

Constants

Name Type Value
PI # f32 3.141592653589793
PI_2 # f32 6.283185307179586
HALF_PI # f32 1.57079632679
FRAC_PI_3 # f32 1.0471975512
E # f32 2.718281828459045

Functions

affine2_to_square

#
fn affine2_to_square(affine: mat3x2<f32>) -> mat3x3<f32>
Parameter Type
affine mat3x2<f32>
Returns: mat3x3<f32>

affine3_to_square

#
fn affine3_to_square(affine: mat3x4<f32>) -> mat4x4<f32>
Parameter Type
affine mat3x4<f32>
Returns: mat4x4<f32>

mat2x4_f32_to_mat3x3_unpack

#
fn mat2x4_f32_to_mat3x3_unpack(a: mat2x4<f32>, b: f32) -> mat3x3<f32>
Parameter Type
a mat2x4<f32>
b f32
Returns: mat3x3<f32>

affine3_to_mat3x3

#
Extracts the square portion of an affine matrix: i.e. discards the translation.
fn affine3_to_mat3x3(affine: mat4x3<f32>) -> mat3x3<f32>
Parameter Type
affine mat4x3<f32>
Returns: mat3x3<f32>

inverse_mat3x3

#
Returns the inverse of a 3x3 matrix.
fn inverse_mat3x3(matrix: mat3x3<f32>) -> mat3x3<f32>
Parameter Type
matrix mat3x3<f32>
Returns: mat3x3<f32>

inverse_affine3

#
Returns the inverse of an affine matrix. https://en.wikipedia.org/wiki/Affine_transformation#Groups
fn inverse_affine3(affine: mat4x3<f32>) -> mat4x3<f32>
Parameter Type
affine mat4x3<f32>
Returns: mat4x3<f32>

mat4x4_to_mat3x3

#
Extracts the upper 3x3 portion of a 4x4 matrix.
fn mat4x4_to_mat3x3(m: mat4x4<f32>) -> mat3x3<f32>
Parameter Type
m mat4x4<f32>
Returns: mat3x3<f32>

orthonormalize

#
Creates an orthonormal basis given a Z vector and an up vector (which becomes Y after orthonormalization). The results are equivalent to the Gram-Schmidt process [1]. [1]: https://math.stackexchange.com/a/1849294
fn orthonormalize(z_unnormalized: vec3<f32>, up: vec3<f32>) -> mat3x3<f32>
Parameter Type
z_unnormalized vec3<f32>
up vec3<f32>
Returns: mat3x3<f32>

sphere_intersects_plane_half_space

#
Returns true if any part of a sphere is on the positive side of a plane. sphere_center.w should be 1.0. This is used for frustum culling.
fn sphere_intersects_plane_half_space(plane: vec4<f32>, sphere_center: vec4<f32>, sphere_radius: f32) -> bool
Parameter Type
plane vec4<f32>
sphere_center vec4<f32>
sphere_radius f32
Returns: bool

powsafe

#
pow() but safe for NaNs/negatives
fn powsafe(color: vec3<f32>, power: f32) -> vec3<f32>
Parameter Type
color vec3<f32>
power f32
Returns: vec3<f32>