BlendModeNode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import TempNode from '../core/TempNode.js';
  2. import { EPSILON } from '../math/MathNode.js';
  3. import { addNodeClass } from '../core/Node.js';
  4. import { addNodeElement, tslFn, nodeProxy, vec3 } from '../shadernode/ShaderNode.js';
  5. export const BurnNode = tslFn( ( { base, blend } ) => {
  6. const fn = ( c ) => blend[ c ].lessThan( EPSILON ).cond( blend[ c ], base[ c ].oneMinus().div( blend[ c ] ).oneMinus().max( 0 ) );
  7. return vec3( fn( 'x' ), fn( 'y' ), fn( 'z' ) );
  8. } ).setLayout( {
  9. name: 'burnColor',
  10. type: 'vec3',
  11. inputs: [
  12. { name: 'base', type: 'vec3' },
  13. { name: 'blend', type: 'vec3' }
  14. ]
  15. } );
  16. export const DodgeNode = tslFn( ( { base, blend } ) => {
  17. const fn = ( c ) => blend[ c ].equal( 1.0 ).cond( blend[ c ], base[ c ].div( blend[ c ].oneMinus() ).max( 0 ) );
  18. return vec3( fn( 'x' ), fn( 'y' ), fn( 'z' ) );
  19. } ).setLayout( {
  20. name: 'dodgeColor',
  21. type: 'vec3',
  22. inputs: [
  23. { name: 'base', type: 'vec3' },
  24. { name: 'blend', type: 'vec3' }
  25. ]
  26. } );
  27. export const ScreenNode = tslFn( ( { base, blend } ) => {
  28. const fn = ( c ) => base[ c ].oneMinus().mul( blend[ c ].oneMinus() ).oneMinus();
  29. return vec3( fn( 'x' ), fn( 'y' ), fn( 'z' ) );
  30. } ).setLayout( {
  31. name: 'screenColor',
  32. type: 'vec3',
  33. inputs: [
  34. { name: 'base', type: 'vec3' },
  35. { name: 'blend', type: 'vec3' }
  36. ]
  37. } );
  38. export const OverlayNode = tslFn( ( { base, blend } ) => {
  39. const fn = ( c ) => base[ c ].lessThan( 0.5 ).cond( base[ c ].mul( blend[ c ], 2.0 ), base[ c ].oneMinus().mul( blend[ c ].oneMinus() ).oneMinus() );
  40. return vec3( fn( 'x' ), fn( 'y' ), fn( 'z' ) );
  41. } ).setLayout( {
  42. name: 'overlayColor',
  43. type: 'vec3',
  44. inputs: [
  45. { name: 'base', type: 'vec3' },
  46. { name: 'blend', type: 'vec3' }
  47. ]
  48. } );
  49. class BlendModeNode extends TempNode {
  50. constructor( blendMode, baseNode, blendNode ) {
  51. super();
  52. this.blendMode = blendMode;
  53. this.baseNode = baseNode;
  54. this.blendNode = blendNode;
  55. }
  56. setup() {
  57. const { blendMode, baseNode, blendNode } = this;
  58. const params = { base: baseNode, blend: blendNode };
  59. let outputNode = null;
  60. if ( blendMode === BlendModeNode.BURN ) {
  61. outputNode = BurnNode( params );
  62. } else if ( blendMode === BlendModeNode.DODGE ) {
  63. outputNode = DodgeNode( params );
  64. } else if ( blendMode === BlendModeNode.SCREEN ) {
  65. outputNode = ScreenNode( params );
  66. } else if ( blendMode === BlendModeNode.OVERLAY ) {
  67. outputNode = OverlayNode( params );
  68. }
  69. return outputNode;
  70. }
  71. }
  72. BlendModeNode.BURN = 'burn';
  73. BlendModeNode.DODGE = 'dodge';
  74. BlendModeNode.SCREEN = 'screen';
  75. BlendModeNode.OVERLAY = 'overlay';
  76. export default BlendModeNode;
  77. export const burn = nodeProxy( BlendModeNode, BlendModeNode.BURN );
  78. export const dodge = nodeProxy( BlendModeNode, BlendModeNode.DODGE );
  79. export const overlay = nodeProxy( BlendModeNode, BlendModeNode.OVERLAY );
  80. export const screen = nodeProxy( BlendModeNode, BlendModeNode.SCREEN );
  81. addNodeElement( 'burn', burn );
  82. addNodeElement( 'dodge', dodge );
  83. addNodeElement( 'overlay', overlay );
  84. addNodeElement( 'screen', screen );
  85. addNodeClass( 'BlendModeNode', BlendModeNode );