2
0
Эх сурвалжийг харах

ShaderNode: posterize() and reciprocal() (#24767)

* ShaderNode: Add posterize()

* Add reciprocal() and added PosterizeNode

* Missing PosterizeNode in Nodes.js

* optimization: posterize steps
sunag 2 жил өмнө
parent
commit
c63283a703

+ 3 - 0
examples/jsm/nodes/Nodes.js

@@ -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
@@ -183,6 +184,7 @@ const nodeLib = {
 	ColorSpaceNode,
 	FrontFacingNode,
 	NormalMapNode,
+	PosterizeNode,
 	ToneMappingNode,
 
 	// math
@@ -300,6 +302,7 @@ export {
 	ColorSpaceNode,
 	FrontFacingNode,
 	NormalMapNode,
+	PosterizeNode,
 	ToneMappingNode,
 
 	// math

+ 25 - 0
examples/jsm/nodes/display/PosterizeNode.js

@@ -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 - 0
examples/jsm/nodes/math/MathNode.js

@@ -33,6 +33,7 @@ class MathNode extends TempNode {
 	static DFDX = 'dFdx';
 	static DFDY = 'dFdy';
 	static ROUND = 'round';
+	static RECIPROCAL = 'reciprocal';
 
 	// 2 inputs
 
@@ -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 = [];

+ 1 - 0
examples/jsm/nodes/shadernode/ShaderNodeBaseElements.js

@@ -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 );

+ 3 - 0
examples/jsm/nodes/shadernode/ShaderNodeElements.js

@@ -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
@@ -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