Skip to content

Commit

Permalink
ShaderNode: posterize() and reciprocal() (#24767)
Browse files Browse the repository at this point in the history
* ShaderNode: Add posterize()

* Add reciprocal() and added PosterizeNode

* Missing PosterizeNode in Nodes.js

* optimization: posterize steps
  • Loading branch information
sunag authored Oct 11, 2022
1 parent 3f235f8 commit c63283a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import ColorAdjustmentNode from './display/ColorAdjustmentNode.js';
import ColorSpaceNode from './display/ColorSpaceNode.js';
import FrontFacingNode from './display/FrontFacingNode.js';
import NormalMapNode from './display/NormalMapNode.js';
import PosterizeNode from './display/PosterizeNode.js';
import ToneMappingNode from './display/ToneMappingNode.js';

// math
Expand Down Expand Up @@ -183,6 +184,7 @@ const nodeLib = {
ColorSpaceNode,
FrontFacingNode,
NormalMapNode,
PosterizeNode,
ToneMappingNode,

// math
Expand Down Expand Up @@ -300,6 +302,7 @@ export {
ColorSpaceNode,
FrontFacingNode,
NormalMapNode,
PosterizeNode,
ToneMappingNode,

// math
Expand Down
25 changes: 25 additions & 0 deletions examples/jsm/nodes/display/PosterizeNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import TempNode from '../core/Node.js';
import { mul, floor, reciprocal } from '../shadernode/ShaderNodeBaseElements.js';

class PosterizeNode extends TempNode {

constructor( sourceNode, stepsNode ) {

super();

this.sourceNode = sourceNode;
this.stepsNode = stepsNode;

}

construct() {

const { sourceNode, stepsNode } = this;

return mul( floor( mul( sourceNode, stepsNode ) ), reciprocal( stepsNode ) );

}

}

export default PosterizeNode;
5 changes: 5 additions & 0 deletions examples/jsm/nodes/math/MathNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MathNode extends TempNode {
static DFDX = 'dFdx';
static DFDY = 'dFdy';
static ROUND = 'round';
static RECIPROCAL = 'reciprocal';

// 2 inputs

Expand Down Expand Up @@ -159,6 +160,10 @@ class MathNode extends TempNode {

return builder.format( '( 1.0 - ' + a.build( builder, inputType ) + ' )', type, output );

} else if ( method === MathNode.RECIPROCAL ) {

return builder.format( '( 1.0 / ' + a.build( builder, inputType ) + ' )', type, output );

} else {

const params = [];
Expand Down
1 change: 1 addition & 0 deletions examples/jsm/nodes/shadernode/ShaderNodeBaseElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export const invert = nodeProxy( MathNode, MathNode.INVERT );
export const dFdx = nodeProxy( MathNode, MathNode.DFDX );
export const dFdy = nodeProxy( MathNode, MathNode.DFDY );
export const round = nodeProxy( MathNode, MathNode.ROUND );
export const reciprocal = nodeProxy( MathNode, MathNode.RECIPROCAL );

export const atan2 = nodeProxy( MathNode, MathNode.ATAN2 );
export const min = nodeProxy( MathNode, MathNode.MIN );
Expand Down
3 changes: 3 additions & 0 deletions examples/jsm/nodes/shadernode/ShaderNodeElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import BlendModeNode from '../display/BlendModeNode.js';
import ColorAdjustmentNode from '../display/ColorAdjustmentNode.js';
import ColorSpaceNode from '../display/ColorSpaceNode.js';
import NormalMapNode from '../display/NormalMapNode.js';
import PosterizeNode from '../display/PosterizeNode.js';
import ToneMappingNode from '../display/ToneMappingNode.js';

// lighting
Expand Down Expand Up @@ -88,6 +89,8 @@ export const colorSpace = ( node, encoding ) => nodeObject( new ColorSpaceNode(
export const normalMap = nodeProxy( NormalMapNode );
export const toneMapping = ( mapping, exposure, color ) => nodeObject( new ToneMappingNode( mapping, nodeObject( exposure ), nodeObject( color ) ) );

export const posterize = nodeProxy( PosterizeNode );

// lighting

//export const lighting = nodeProxy( LightingNode ); // abstract
Expand Down

0 comments on commit c63283a

Please sign in to comment.