diff --git a/bindings/bf/bgfx.bf b/bindings/bf/bgfx.bf index f3ff2415f..164ee4d00 100644 --- a/bindings/bf/bgfx.bf +++ b/bindings/bf/bgfx.bf @@ -3398,6 +3398,7 @@ public static class bgfx /// - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices. /// - `u_modelView mat4` - concatenated model view matrix, only first /// model matrix from array is used. + /// - `u_invModelView mat4` - inverted concatenated model view matrix. /// - `u_modelViewProj mat4` - concatenated model view projection matrix. /// - `u_alphaRef float` - alpha reference value for alpha test. /// diff --git a/bindings/cs/bgfx.cs b/bindings/cs/bgfx.cs index 499439e6e..65fda2c8c 100644 --- a/bindings/cs/bgfx.cs +++ b/bindings/cs/bgfx.cs @@ -3353,6 +3353,7 @@ public static partial class bgfx /// - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices. /// - `u_modelView mat4` - concatenated model view matrix, only first /// model matrix from array is used. + /// - `u_invModelView mat4` - inverted concatenated model view matrix. /// - `u_modelViewProj mat4` - concatenated model view projection matrix. /// - `u_alphaRef float` - alpha reference value for alpha test. /// diff --git a/bindings/d/package.d b/bindings/d/package.d index d696b590a..b26728dd2 100644 --- a/bindings/d/package.d +++ b/bindings/d/package.d @@ -2807,6 +2807,7 @@ mixin(joinFnBinds((){ * - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices. * - `u_modelView mat4` - concatenated model view matrix, only first * model matrix from array is used. + * - `u_invModelView mat4` - inverted concatenated model view matrix. * - `u_modelViewProj mat4` - concatenated model view projection matrix. * - `u_alphaRef float` - alpha reference value for alpha test. Params: diff --git a/bindings/zig/bgfx.zig b/bindings/zig/bgfx.zig index 8f721f160..6c70fcfc5 100644 --- a/bindings/zig/bgfx.zig +++ b/bindings/zig/bgfx.zig @@ -2731,6 +2731,7 @@ extern fn bgfx_destroy_frame_buffer(_handle: FrameBufferHandle) void; /// - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices. /// - `u_modelView mat4` - concatenated model view matrix, only first /// model matrix from array is used. +/// - `u_invModelView mat4` - inverted concatenated model view matrix. /// - `u_modelViewProj mat4` - concatenated model view projection matrix. /// - `u_alphaRef float` - alpha reference value for alpha test. /// Uniform name in shader. diff --git a/docs/tools.rst b/docs/tools.rst index ea2e0fced..4f66fb90c 100644 --- a/docs/tools.rst +++ b/docs/tools.rst @@ -128,6 +128,7 @@ Predefined Uniforms mat4 u_invViewProj Transform clip-to-world space. mat4[N] u_model Transform local-to-world space array. mat4 u_modelView Transform local-to-view space. + mat4 u_invModelView Transform view-to-local space. mat4 u_modelViewProj Transform local-to-clip space. float u_alphaRef | The reference value to which incoming alpha | values are compared. diff --git a/include/bgfx/bgfx.h b/include/bgfx/bgfx.h index d35a5921b..01e1b54ed 100644 --- a/include/bgfx/bgfx.h +++ b/include/bgfx/bgfx.h @@ -3244,6 +3244,7 @@ namespace bgfx /// - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices. /// - `u_modelView mat4` - concatenated model view matrix, only first /// model matrix from array is used. + // - `u_invModelView mat4` - inverted model view matrix. /// - `u_modelViewProj mat4` - concatenated model view projection matrix. /// - `u_alphaRef float` - alpha reference value for alpha test. /// diff --git a/include/bgfx/c99/bgfx.h b/include/bgfx/c99/bgfx.h index 08d627579..d0c598cce 100644 --- a/include/bgfx/c99/bgfx.h +++ b/include/bgfx/c99/bgfx.h @@ -2167,6 +2167,7 @@ BGFX_C_API void bgfx_destroy_frame_buffer(bgfx_frame_buffer_handle_t _handle); * - `u_modelView mat4` - concatenated model view matrix, only first * model matrix from array is used. * - `u_modelViewProj mat4` - concatenated model view projection matrix. + * - `u_invModelView mat4` - inverted concatenated model view matrix. * - `u_alphaRef float` - alpha reference value for alpha test. * * @param[in] _name Uniform name in shader. diff --git a/scripts/bgfx.idl b/scripts/bgfx.idl index e87b841bd..9c74e034a 100644 --- a/scripts/bgfx.idl +++ b/scripts/bgfx.idl @@ -1954,6 +1954,7 @@ func.destroy { cname = "destroy_frame_buffer" } --- - `u_model mat4[BGFX_CONFIG_MAX_BONES]` - array of model matrices. --- - `u_modelView mat4` - concatenated model view matrix, only first --- model matrix from array is used. +--- - `u_invModelView mat4` - inverted concatenated model view matrix. --- - `u_modelViewProj mat4` - concatenated model view projection matrix. --- - `u_alphaRef float` - alpha reference value for alpha test. --- diff --git a/src/bgfx.cpp b/src/bgfx.cpp index f34c186a3..23af75f45 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -982,6 +982,7 @@ namespace bgfx "u_invViewProj", "u_model", "u_modelView", + "u_invModelView", "u_modelViewProj", "u_alphaRef4", }; diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 651d585fa..e67c020bb 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -843,6 +843,7 @@ namespace bgfx InvViewProj, Model, ModelView, + InvModelView, ModelViewProj, AlphaRef, Count diff --git a/src/bgfx_shader.sh b/src/bgfx_shader.sh index 052867d29..209b0951e 100644 --- a/src/bgfx_shader.sh +++ b/src/bgfx_shader.sh @@ -721,6 +721,7 @@ uniform mat4 u_viewProj; uniform mat4 u_invViewProj; uniform mat4 u_model[BGFX_CONFIG_MAX_BONES]; uniform mat4 u_modelView; +uniform mat4 u_invModelView; uniform mat4 u_modelViewProj; uniform vec4 u_alphaRef4; #define u_alphaRef u_alphaRef4.x diff --git a/src/renderer.h b/src/renderer.h index dd5f96948..c8971fa82 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -244,6 +244,26 @@ namespace bgfx } break; + case PredefinedUniform::InvModelView: + { + Matrix4 modelView; + Matrix4 invModelView; + const Matrix4& model = frameCache.m_matrixCache.m_cache[_draw.m_startMatrix]; + bx::model4x4_mul(&modelView.un.f4x4 + , &model.un.f4x4 + , &m_view[_view].un.f4x4 + ); + bx::float4x4_inverse(&invModelView.un.f4x4 + , &modelView.un.f4x4 + ); + _renderer->setShaderUniform4x4f(flags + , predefined.m_loc + , invModelView.un.val + , bx::uint32_min(mtxRegs, predefined.m_count) + ); + } + break; + case PredefinedUniform::ModelViewProj: { Matrix4 modelViewProj;