@@ -7,6 +7,8 @@ export const Camera = d.struct({
77 targetPos : d . vec4f ,
88 view : d . mat4x4f ,
99 projection : d . mat4x4f ,
10+ viewInverse : d . mat4x4f ,
11+ projectionInverse : d . mat4x4f ,
1012} ) ;
1113
1214export interface CameraOptions {
@@ -57,11 +59,16 @@ export function setupOrbitCamera(
5759 cameraState . pitch = Math . asin ( cameraVector . y / cameraState . radius ) ;
5860 cameraState . target = tgt ;
5961
62+ const view = calculateView ( newPos , cameraState . target ) ;
63+ const projection = calculateProj ( canvas . clientWidth / canvas . clientHeight ) ;
64+
6065 callback ( Camera ( {
6166 position : newPos ,
6267 targetPos : cameraState . target ,
63- view : calculateView ( newPos , cameraState . target ) ,
64- projection : calculateProj ( canvas . clientWidth / canvas . clientHeight ) ,
68+ view,
69+ projection,
70+ viewInverse : invertMat ( view ) ,
71+ projectionInverse : invertMat ( projection ) ,
6572 } ) ) ;
6673 }
6774
@@ -83,8 +90,11 @@ export function setupOrbitCamera(
8390 cameraState . yaw ,
8491 ) ;
8592
93+ const newView = calculateView ( newCameraPos , cameraState . target ) ;
94+
8695 callback ( {
87- view : calculateView ( newCameraPos , cameraState . target ) ,
96+ view : newView ,
97+ viewInverse : invertMat ( newView ) ,
8898 position : newCameraPos ,
8999 } ) ;
90100 }
@@ -105,13 +115,17 @@ export function setupOrbitCamera(
105115 ) ;
106116 const newView = calculateView ( newPos , cameraState . target ) ;
107117
108- callback ( { view : newView , position : newPos } ) ;
118+ callback ( {
119+ view : newView ,
120+ viewInverse : invertMat ( newView ) ,
121+ position : newPos ,
122+ } ) ;
109123 }
110124
111125 // resize observer
112126 const resizeObserver = new ResizeObserver ( ( ) => {
113127 const projection = calculateProj ( canvas . clientWidth / canvas . clientHeight ) ;
114- callback ( { projection } ) ;
128+ callback ( { projection, projectionInverse : invertMat ( projection ) } ) ;
115129 } ) ;
116130 resizeObserver . observe ( canvas ) ;
117131
@@ -237,3 +251,7 @@ function calculateView(position: d.v4f, target: d.v4f) {
237251function calculateProj ( aspectRatio : number ) {
238252 return m . mat4 . perspective ( Math . PI / 4 , aspectRatio , 0.1 , 1000 , d . mat4x4f ( ) ) ;
239253}
254+
255+ function invertMat ( matrix : d . m4x4f ) {
256+ return m . mat4 . invert ( matrix , d . mat4x4f ( ) ) ;
257+ }
0 commit comments