Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#define_import_path bevy_render::maths
3const PI: f32 = 3.141592653589793; // π
4const PI_2: f32 = 6.283185307179586; // 2π
5const HALF_PI: f32 = 1.57079632679; // π/2
6const FRAC_PI_3: f32 = 1.0471975512; // π/3
7const E: f32 = 2.718281828459045; // exp(1)
9fn affine2_to_square(affine: mat3x2<f32>) -> mat3x3<f32> {
10 return mat3x3<f32>(
11 vec3<f32>(affine[0].xy, 0.0),
12 vec3<f32>(affine[1].xy, 0.0),
13 vec3<f32>(affine[2].xy, 1.0),
14 );
17fn affine3_to_square(affine: mat3x4<f32>) -> mat4x4<f32> {
18 return transpose(mat4x4<f32>(
19 affine[0],
20 affine[1],
21 affine[2],
22 vec4<f32>(0.0, 0.0, 0.0, 1.0),
23 ));
26fn mat2x4_f32_to_mat3x3_unpack(
27 a: mat2x4<f32>,
28 b: f32,
29) -> mat3x3<f32> {
30 return mat3x3<f32>(
31 a[0].xyz,
32 vec3<f32>(a[0].w, a[1].xy),
33 vec3<f32>(a[1].zw, b),
34 );
37// Extracts the square portion of an affine matrix: i.e. discards the
38// translation.
39fn affine3_to_mat3x3(affine: mat4x3<f32>) -> mat3x3<f32> {
40 return mat3x3<f32>(affine[0].xyz, affine[1].xyz, affine[2].xyz);
43// Returns the inverse of a 3x3 matrix.
44fn inverse_mat3x3(matrix: mat3x3<f32>) -> mat3x3<f32> {
45 let tmp0 = cross(matrix[1], matrix[2]);
46 let tmp1 = cross(matrix[2], matrix[0]);
47 let tmp2 = cross(matrix[0], matrix[1]);
48 let inv_det = 1.0 / dot(matrix[2], tmp2);
49 return transpose(mat3x3<f32>(tmp0 * inv_det, tmp1 * inv_det, tmp2 * inv_det));
52// Returns the inverse of an affine matrix.
53//
54// https://en.wikipedia.org/wiki/Affine_transformation#Groups
55fn inverse_affine3(affine: mat4x3<f32>) -> mat4x3<f32> {
56 let matrix3 = affine3_to_mat3x3(affine);
57 let inv_matrix3 = inverse_mat3x3(matrix3);
58 return mat4x3<f32>(inv_matrix3[0], inv_matrix3[1], inv_matrix3[2], -(inv_matrix3 * affine[3]));
61// Extracts the upper 3x3 portion of a 4x4 matrix.
62fn mat4x4_to_mat3x3(m: mat4x4<f32>) -> mat3x3<f32> {
63 return mat3x3<f32>(m[0].xyz, m[1].xyz, m[2].xyz);
66// Creates an orthonormal basis given a Z vector and an up vector (which becomes
67// Y after orthonormalization).
68//
69// The results are equivalent to the Gram-Schmidt process [1].
70//
71// [1]: https://math.stackexchange.com/a/1849294
72fn orthonormalize(z_unnormalized: vec3<f32>, up: vec3<f32>) -> mat3x3<f32> {
73 let z_basis = normalize(z_unnormalized);
74 let x_basis = normalize(cross(z_basis, up));
75 let y_basis = cross(z_basis, x_basis);
76 return mat3x3(x_basis, y_basis, z_basis);
79// Returns true if any part of a sphere is on the positive side of a plane.
80//
81// `sphere_center.w` should be 1.0.
82//
83// This is used for frustum culling.
84fn sphere_intersects_plane_half_space(
85 plane: vec4<f32>,
86 sphere_center: vec4<f32>,
87 sphere_radius: f32
88) -> bool {
89 return dot(plane, sphere_center) + sphere_radius > 0.0;
92// pow() but safe for NaNs/negatives
93fn powsafe(color: vec3<f32>, power: f32) -> vec3<f32> {
94 return pow(abs(color), vec3(power)) * sign(color);
Contents