Compiler2 min read

Spaces Module

Coordinate-space transforms, projection helpers, depth reconstruction, and tangent bases.

Reading Time
2 min
Word Count
256
Sections
8
Try It Live

Turn the guide into code

Take the key idea from this page into the playground and validate it in a real shader instead of leaving it as theory.

Open Playground

Coordinate-space transforms, projection helpers, depth reconstruction, and tangent bases.

Import

bwsl
import Spaces

This module imports: Math.

Constants

None.

Structs

Ray

bwsl
struct Ray {
float3 origin;
float3 direction;
};

Basis

bwsl
struct Basis {
float3 tangent;
float3 bitangent;
float3 normal;
};

Enums

No enums found.

Functions

transform_point

Reference: https://en.wikipedia.org/wiki/Homogeneous_coordinates

bwsl
transform_point :: (mat4 m, float3 p) -> float3

transform_vector

Transforms a direction or offset by a 4x4 matrix without translation.

bwsl
transform_vector :: (mat4 m, float3 v) -> float3

transform_direction

Transforms and normalizes a direction or offset by a 4x4 matrix.

bwsl
transform_direction :: (mat4 m, float3 v) -> float3

transform_normal

Reference: https://en.wikipedia.org/wiki/Normal_(geometry)

bwsl
transform_normal :: (mat4 objectToWorld, float3 n) -> float3

project_point

Projects a world-space position to clip space with a view-projection matrix.

bwsl
project_point :: (mat4 viewProjection, float3 worldPos) -> float4

clip_to_ndc

Reference: https://en.wikipedia.org/wiki/Viewing_frustum

bwsl
clip_to_ndc :: (float4 clipPos) -> float3

ndc_to_uv

Converts normalized device coordinates in [-1, 1] to texture UV coordinates in [0, 1].

bwsl
ndc_to_uv :: (float2 ndc) -> float2

uv_to_ndc

Converts texture UV coordinates in [0, 1] to normalized device coordinates in [-1, 1].

bwsl
uv_to_ndc :: (float2 uv) -> float2

screen_to_uv

Converts pixel coordinates to normalized UV coordinates using viewport size.

bwsl
screen_to_uv :: (float2 pixel, float2 viewportSize) -> float2

uv_to_screen

Converts normalized UV coordinates to pixel coordinates using viewport size.

bwsl
uv_to_screen :: (float2 uv, float2 viewportSize) -> float2

linearize_depth_01

Reference: https://developer.nvidia.com/content/depth-precision-visualized

bwsl
linearize_depth_01 :: (float depth, float nearPlane, float farPlane) -> float

linearize_depth_ndc

Linearizes an NDC depth value in [-1, 1] to view-space depth units.

bwsl
linearize_depth_ndc :: (float ndcDepth, float nearPlane, float farPlane) -> float

reconstruct_view_position

Reference: https://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/

bwsl
reconstruct_view_position :: (float2 uv, float depth, mat4 inverseProjection) -> float3

reconstruct_world_position

Reference: https://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/

bwsl
reconstruct_world_position :: (float2 uv, float depth, mat4 inverseViewProjection) -> float3

make_basis

Reference: https://en.wikipedia.org/wiki/Orthonormal_basis

bwsl
make_basis :: (float3 normal) -> Basis

tangent_to_world

Converts a tangent-space vector to world space using a Basis.

bwsl
tangent_to_world :: (float3 tangentSpaceVector, Basis basis) -> float3

tangent_to_world

Converts a tangent-space vector to world space from explicit tangent, bitangent, and normal.

bwsl
tangent_to_world :: (float3 tangentSpaceVector, float3 tangent, float3 bitangent, float3 normal) -> float3

world_to_tangent

Converts a world-space vector into tangent space using a Basis.

bwsl
world_to_tangent :: (float3 worldVector, Basis basis) -> float3

view_ray_from_uv

Builds a view-space ray through a screen UV from an inverse projection matrix.

bwsl
view_ray_from_uv :: (float2 uv, mat4 inverseProjection) -> Ray

Source

Generated from compiler-reference/modules/Spaces.bwsl.