Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,30 @@
*/
export const SRGBTransfer = 'srgb';

/**
* No normal map packing.
*

Check failure on line 1285 in src/constants.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

Trailing spaces not allowed
* @type {string}
* @constant
*/
export const NoNormalPacking = '';

/**
* Normal RG packing.
*

Check failure on line 1293 in src/constants.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

Trailing spaces not allowed
* @type {string}
* @constant
*/
export const NormalRGPacking = 'rg';

/**
* Normal GA packing.
*

Check failure on line 1301 in src/constants.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

Trailing spaces not allowed
* @type {string}
* @constant
*/
export const NormalGAPacking = 'ga';

/**
* Sets the stencil buffer value to `0`.
*
Expand Down
6 changes: 6 additions & 0 deletions src/nodes/accessors/MaterialNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ class MaterialNode extends Node {
node = normalMap( this.getTexture( 'normal' ), this.getCache( 'normalScale', 'vec2' ) );
node.normalMapType = material.normalMapType;

if ( material.normalMap.userData?.unpackNormal ) {

node.unpackNormal = material.normalMap.userData.unpackNormal;

}

} else if ( material.bumpMap ) {

node = bumpMap( this.getTexture( 'bump' ).r, this.getFloat( 'bumpScale' ) );
Expand Down
41 changes: 38 additions & 3 deletions src/nodes/display/NormalMapNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import { normalView, transformNormalToView } from '../accessors/Normal.js';
import { TBNViewMatrix } from '../accessors/AccessorsUtils.js';
import { nodeProxy, vec3 } from '../tsl/TSLBase.js';
import { nodeProxy, vec3, float } from '../tsl/TSLBase.js';

Check warning on line 5 in src/nodes/display/NormalMapNode.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'float' is defined but never used
import { dot, sqrt, saturate } from '../math/MathNode.js';

Check warning on line 6 in src/nodes/display/NormalMapNode.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'saturate' is defined but never used

Check warning on line 6 in src/nodes/display/NormalMapNode.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'sqrt' is defined but never used

Check warning on line 6 in src/nodes/display/NormalMapNode.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'dot' is defined but never used

import { TangentSpaceNormalMap, ObjectSpaceNormalMap } from '../../constants.js';
import { TangentSpaceNormalMap, ObjectSpaceNormalMap, NoNormalPacking, NormalRGPacking, NormalGAPacking } from '../../constants.js';
import { directionToFaceDirection } from './FrontFacingNode.js';

/**
Expand Down Expand Up @@ -57,14 +58,48 @@
*/
this.normalMapType = TangentSpaceNormalMap;

/**
* Controls how to unpack the sampled normal map values.
*
* @type {string}
* @default NoNormalPacking
*/
this.unpackNormal = NoNormalPacking;

}

setup( { material } ) {

const { normalMapType, scaleNode } = this;
const { normalMapType, scaleNode, unpackNormal } = this;

let normalMap = this.node.mul( 2.0 ).sub( 1.0 );

if ( normalMapType === TangentSpaceNormalMap ) {

if ( unpackNormal == NormalRGPacking ) {

normalMap = unpackRGNormal( normalMap );

Check warning on line 81 in src/nodes/display/NormalMapNode.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'unpackRGNormal' is not defined

} else if ( unpackNormal == NormalGAPacking ) {

normalMap = unpackGANormal( normalMap );

Check warning on line 85 in src/nodes/display/NormalMapNode.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'unpackGANormal' is not defined

} else if ( unpackNormal != NoNormalPacking ) {

console.error( `THREE.NodeMaterial: Unexpected unpack normal mode: ${ unpackNormal }` );

}

} else {

if ( unpackNormal != NoNormalPacking ) {

console.error( `THREE.NodeMaterial: Normal map type '${ normalMapType }' is not compatible with unpack normal mode '${ unpackNormal }'` );

}

}

if ( scaleNode !== null ) {

let scale = scaleNode;
Expand Down
22 changes: 22 additions & 0 deletions src/nodes/utils/Packing.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@
* @return {Node<vec3>} The direction.
*/
export const colorToDirection = ( node ) => nodeObject( node ).mul( 2.0 ).sub( 1 );

/**
* Unpacks RG-packed normals by reconstructing their Z component by projecting it to the hemisphere.
* The XY coordinates of the input normal are expected to be in the [-1, 1] range.
*
* @tsl
* @function
* @param {Node<vec3>} node - The packed normal to unpack.

Check failure on line 29 in src/nodes/utils/Packing.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

Expected JSDoc for 'normal' but found 'node'
* @return {Node<vec3>} The unpacked normal.
*/
export const unpackRGNormal = ( normal ) => vec3( normal.xy, sqrt( saturate( float( 1.0 ).sub( dot( normal.xy, normal.xy ) ) ) ) );

Check warning on line 32 in src/nodes/utils/Packing.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'float' is not defined

Check warning on line 32 in src/nodes/utils/Packing.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'saturate' is not defined

Check warning on line 32 in src/nodes/utils/Packing.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'sqrt' is not defined

Check warning on line 32 in src/nodes/utils/Packing.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

'vec3' is not defined

/**
* Unpacks GA-packed normals by reconstructing their Z component by projecting it to the hemisphere.

Check failure on line 35 in src/nodes/utils/Packing.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

Trailing spaces not allowed
* The XY coordinates of the input normal are expected to be in the [-1, 1] range.
*
* @tsl
* @function
* @param {Node<vec3>} node - The packed normal to unpack.

Check failure on line 40 in src/nodes/utils/Packing.js

View workflow job for this annotation

GitHub Actions / Lint, Unit, Unit addons, Circular dependencies & Examples testing

Expected JSDoc for 'normal' but found 'node'
* @return {Node<vec3>} The unpacked normal.
*/
export const unpackGANormal = ( normal ) => vec3( normal.yw, sqrt( saturate( float( 1.0 ).sub( dot( normal.yw, normal.yw ) ) ) ) );
Loading