-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import '../../../tests/matchers/toBeCloseToArray'; | ||
import { vec3OrthoNormalize } from '../vec3OrthoNormalize'; | ||
import type { RawVector3 } from '../RawVector3'; | ||
|
||
describe( 'vec3OrthoNormalize', () => { | ||
it( 'returns a orthogonal tangent', () => { | ||
const normal: RawVector3 = [ 0.0, 0.0, 5.0 ]; | ||
const tangent: RawVector3 = [ 1.0, 1.0, 1.0 ]; | ||
const subject = vec3OrthoNormalize( normal, tangent ); | ||
|
||
expect( subject.normal ).toBeCloseToArray( [ 0.0, 0.0, 1.0 ] ); | ||
expect( subject.tangent ).toBeCloseToArray( [ 0.707, 0.707, 0.0 ] ); | ||
expect( subject.binormal ).toBeCloseToArray( [ -0.707, 0.707, 0.0 ] ); | ||
} ); | ||
|
||
it( 'returns a orthogonal tangent and binormal', () => { | ||
const normal: RawVector3 = [ 0.0, 0.0, 5.0 ]; | ||
const tangent: RawVector3 = [ 1.0, 1.0, 1.0 ]; | ||
const binormal: RawVector3 = [ 0.0, -1.0, 0.0 ]; | ||
const subject = vec3OrthoNormalize( normal, tangent, binormal ); | ||
|
||
expect( subject.normal ).toBeCloseToArray( [ 0.0, 0.0, 1.0 ] ); | ||
expect( subject.tangent ).toBeCloseToArray( [ 0.707, 0.707, 0.0 ] ); | ||
expect( subject.binormal ).toBeCloseToArray( [ 0.707, -0.707, 0.0 ] ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { vec3Cross } from '.'; | ||
import { vecDot, vecNeg, vecNormalize, vecScale, vecSub } from '..'; | ||
import type { RawVector3 } from './RawVector3'; | ||
|
||
/** | ||
* Return a tangent which is orthogonal to normal. | ||
* If binormal is specified, it is also returned and it's orthogonal to both normal and tangent. | ||
*/ | ||
export function vec3OrthoNormalize( | ||
normal: RawVector3, | ||
tangent: RawVector3, | ||
binormal?: RawVector3, | ||
): { | ||
normal: RawVector3, | ||
tangent: RawVector3, | ||
binormal: RawVector3, | ||
} { | ||
const n = vecNormalize( normal ); | ||
|
||
const dotNT = vecDot( n, tangent ); | ||
const t = vecNormalize( vecSub( tangent, vecScale( n, dotNT ) ) ); | ||
|
||
let b = vec3Cross( n, t ); | ||
if ( binormal && vecDot( b, binormal ) < 0.0 ) { | ||
b = vecNeg( b ); | ||
} | ||
|
||
return { | ||
normal: n, | ||
tangent: t, | ||
binormal: b, | ||
}; | ||
} |