TransitionNode.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import TempNode from '../core/TempNode.js';
  2. import { uv } from '../accessors/UVNode.js';
  3. import { addNodeElement, tslFn, nodeObject, float, int, vec4, If } from '../shadernode/ShaderNode.js';
  4. import { clamp, mix } from '../math/MathNode.js';
  5. import { sub } from '../math/OperatorNode.js';
  6. class TransitionNode extends TempNode {
  7. constructor( textureNodeA, textureNodeB, mixTextureNode, mixRatioNode, thresholdNode, useTextureNode ) {
  8. super();
  9. // Input textures
  10. this.textureNodeA = textureNodeA;
  11. this.textureNodeB = textureNodeB;
  12. this.mixTextureNode = mixTextureNode;
  13. // Uniforms
  14. this.mixRatioNode = mixRatioNode;
  15. this.thresholdNode = thresholdNode;
  16. this.useTextureNode = useTextureNode;
  17. }
  18. setup() {
  19. const { textureNodeA, textureNodeB, mixTextureNode, mixRatioNode, thresholdNode, useTextureNode } = this;
  20. const sampleTexture = ( textureNode ) => {
  21. const uvNodeTexture = textureNode.uvNode || uv();
  22. return textureNode.uv( uvNodeTexture );
  23. };
  24. const transition = tslFn( () => {
  25. const texelOne = sampleTexture( textureNodeA );
  26. const texelTwo = sampleTexture( textureNodeB );
  27. const color = vec4().toVar();
  28. If( useTextureNode.equal( int( 1 ) ), () => {
  29. const transitionTexel = sampleTexture( mixTextureNode );
  30. const r = mixRatioNode.mul( thresholdNode.mul( 2.0 ).add( 1.0 ) ).sub( thresholdNode );
  31. const mixf = clamp( sub( transitionTexel.r, r ).mul( float( 1.0 ).div( thresholdNode ) ), 0.0, 1.0 );
  32. color.assign( mix( texelOne, texelTwo, mixf ) );
  33. } ).else( () => {
  34. color.assign( mix( texelTwo, texelOne, mixRatioNode ) );
  35. } );
  36. return color;
  37. } );
  38. const outputNode = transition();
  39. return outputNode;
  40. }
  41. }
  42. export const transition = ( nodeA, nodeB, mixTexture, mixRatio = 0.0, threshold = 0.1, useTexture = 0 ) => nodeObject( new TransitionNode( nodeObject( nodeA ).toTexture(), nodeObject( nodeB ).toTexture(), nodeObject( mixTexture ).toTexture(), nodeObject( mixRatio ), nodeObject( threshold ), nodeObject( useTexture ) ) );
  43. addNodeElement( 'transition', transition );
  44. export default TransitionNode;