-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(PNTSLoader): support NORMAL and NORMAL_OCT16P semantics (#594)
- Loading branch information
Showing
3 changed files
with
107 additions
and
2 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,38 @@ | ||
import { Vector2, MathUtils, Vector3 } from 'three'; | ||
|
||
const f = new Vector2(); | ||
|
||
// https://stackoverflow.com/a/74745666/2704779 | ||
export function decodeOctNormal( x, y, target = new Vector3() ) { | ||
|
||
f.set( x, y ).divideScalar( 256 ).multiplyScalar( 2 ).subScalar( 1 ); | ||
|
||
target.set( f.x, f.y, 1 - Math.abs( f.x ) - Math.abs( f.y ) ); | ||
|
||
const t = MathUtils.clamp( - target.z, 0, 1 ); | ||
|
||
if ( target.x >= 0 ) { | ||
|
||
target.setX( target.x - t ); | ||
|
||
} else { | ||
|
||
target.setX( target.x + t ); | ||
|
||
} | ||
|
||
if ( target.y >= 0 ) { | ||
|
||
target.setY( target.y - t ); | ||
|
||
} else { | ||
|
||
target.setY( target.y + t ); | ||
|
||
} | ||
|
||
target.normalize(); | ||
|
||
return target; | ||
|
||
} |
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,39 @@ | ||
import { decodeOctNormal } from '../src/utilities/decodeOctNormal'; | ||
|
||
const PRECISION = 0.005; | ||
|
||
describe( 'decodeOctNormal', () => { | ||
|
||
it.each( [ | ||
// 4 corners of the projected octahedron | ||
[ 0, 0, 0, 0, - 1 ], | ||
[ 255, 255, 0, 0, - 1 ], | ||
[ 0, 255, 0, 0, - 1 ], | ||
[ 255, 0, 0, 0, - 1 ], | ||
|
||
// Center of the projected octahedron | ||
[ 127, 127, 0, 0, 1 ], | ||
|
||
// Midpoint of left edge of the projected octahedron | ||
[ 0, 127, - 1, 0, 0 ], | ||
|
||
// Midpoint of right edge of the projected octahedron | ||
[ 255, 127, 1, 0, 0 ], | ||
|
||
// Midpoint of top edge of the projected octahedron | ||
[ 127, 255, 0, 1, 0 ], | ||
|
||
// Midpoint of bottom edge of the projected octahedron | ||
[ 127, 0, 0, - 1, 0 ], | ||
] )( 'should decode (%s, %s) into [%s, %s, %s]', ( encX, encY, x, y, z ) => { | ||
|
||
const result = decodeOctNormal( encX, encY ); | ||
|
||
expect( result.length() ).toBeCloseTo( 1, PRECISION ); | ||
expect( result.x ).toBeCloseTo( x, PRECISION ); | ||
expect( result.y ).toBeCloseTo( y, PRECISION ); | ||
expect( result.z ).toBeCloseTo( z, PRECISION ); | ||
|
||
} ); | ||
|
||
} ); |