From 1808c82c51db4afe7cc9148bc9d6f8c861d7bc20 Mon Sep 17 00:00:00 2001 From: Josef Date: Wed, 2 Apr 2025 07:44:25 +0200 Subject: [PATCH] Shader Transform Update --- Runtime/Shading/Library/Transform.gdshaderinc | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Runtime/Shading/Library/Transform.gdshaderinc b/Runtime/Shading/Library/Transform.gdshaderinc index 617d56c..b69d55d 100644 --- a/Runtime/Shading/Library/Transform.gdshaderinc +++ b/Runtime/Shading/Library/Transform.gdshaderinc @@ -52,6 +52,12 @@ vec3 worldToLocalDirection( vec3 _VERTEX, mat4 _MODEL_MATRIX ) return ( mw * vec4( _VERTEX, 1.0 ) ).xyz; } + +vec3 worldToView( vec3 world, mat4 _VIEW_MATRIX ) +{ + return ( _VIEW_MATRIX * vec4( world, 1.0 ) ).xyz; +} + vec3 worldToViewDirection( vec3 direction, mat4 _VIEW_MATRIX ) { mat4 mw = _VIEW_MATRIX; @@ -63,6 +69,35 @@ vec3 worldToViewDirection( vec3 direction, mat4 _VIEW_MATRIX ) return ( mw * vec4( direction, 1.0 ) ).xyz; } +vec3 viewToWorld( vec3 view, mat4 _INV_VIEW_MATRIX ) +{ + return ( _INV_VIEW_MATRIX * vec4( view, 1.0 ) ).xyz; +} + +vec3 viewToWorldDirection( vec3 view, mat4 _INV_VIEW_MATRIX ) +{ + mat4 mw = _INV_VIEW_MATRIX; + mw[ 3 ][ 0 ] = 0.0; + mw[ 3 ][ 1 ] = 0.0; + mw[ 3 ][ 2 ] = 0.0; + mw[ 3 ][ 3 ] = 1.0; + + + return ( mw * vec4( view, 1.0 ) ).xyz; +} + +vec3 viewToLocal( vec3 view, mat4 _INV_VIEW_MATRIX, mat4 _MODEL_MATRIX ) +{ + vec3 world = viewToWorld( view, _INV_VIEW_MATRIX ); + return worldToLocal( world, _MODEL_MATRIX ); +} + +vec3 viewToLocalDirection( vec3 view, mat4 _INV_VIEW_MATRIX, mat4 _MODEL_MATRIX ) +{ + vec3 world = viewToWorldDirection( view, _INV_VIEW_MATRIX ); + return worldToLocalDirection( world, _MODEL_MATRIX ); +} + vec3 extractScale( mat3 _MODEL_NORMAL_MATRIX ) { mat3 m = _MODEL_NORMAL_MATRIX;