AnamorphicNode.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import TempNode from '../core/TempNode.js';
  2. import { nodeObject, addNodeElement, tslFn, float, vec2, vec3 } from '../shadernode/ShaderNode.js';
  3. import { loop } from '../utils/LoopNode.js';
  4. import { uniform } from '../core/UniformNode.js';
  5. import { NodeUpdateType } from '../core/constants.js';
  6. import { threshold } from './ColorAdjustmentNode.js';
  7. import { uv } from '../accessors/UVNode.js';
  8. import { passTexture } from './PassNode.js';
  9. import QuadMesh from '../../renderers/common/QuadMesh.js';
  10. import { Vector2 } from '../../math/Vector2.js';
  11. import { RenderTarget } from '../../core/RenderTarget.js';
  12. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  13. class AnamorphicNode extends TempNode {
  14. constructor( textureNode, tresholdNode, scaleNode, samples ) {
  15. super( 'vec4' );
  16. this.textureNode = textureNode;
  17. this.tresholdNode = tresholdNode;
  18. this.scaleNode = scaleNode;
  19. this.colorNode = vec3( 0.1, 0.0, 1.0 );
  20. this.samples = samples;
  21. this.resolution = new Vector2( 1, 1 );
  22. this._renderTarget = new RenderTarget();
  23. this._renderTarget.texture.name = 'anamorphic';
  24. this._invSize = uniform( new Vector2() );
  25. this._textureNode = passTexture( this, this._renderTarget.texture );
  26. this.updateBeforeType = NodeUpdateType.RENDER;
  27. }
  28. getTextureNode() {
  29. return this._textureNode;
  30. }
  31. setSize( width, height ) {
  32. this._invSize.value.set( 1 / width, 1 / height );
  33. width = Math.max( Math.round( width * this.resolution.x ), 1 );
  34. height = Math.max( Math.round( height * this.resolution.y ), 1 );
  35. this._renderTarget.setSize( width, height );
  36. }
  37. updateBefore( frame ) {
  38. const { renderer } = frame;
  39. const textureNode = this.textureNode;
  40. const map = textureNode.value;
  41. this._renderTarget.texture.type = map.type;
  42. const currentRenderTarget = renderer.getRenderTarget();
  43. const currentTexture = textureNode.value;
  44. _quadMesh.material = this._material;
  45. this.setSize( map.image.width, map.image.height );
  46. // render
  47. renderer.setRenderTarget( this._renderTarget );
  48. _quadMesh.render( renderer );
  49. // restore
  50. renderer.setRenderTarget( currentRenderTarget );
  51. textureNode.value = currentTexture;
  52. }
  53. setup( builder ) {
  54. const textureNode = this.textureNode;
  55. const uvNode = textureNode.uvNode || uv();
  56. const sampleTexture = ( uv ) => textureNode.uv( uv );
  57. const anamorph = tslFn( () => {
  58. const samples = this.samples;
  59. const halfSamples = Math.floor( samples / 2 );
  60. const total = vec3( 0 ).toVar();
  61. loop( { start: - halfSamples, end: halfSamples }, ( { i } ) => {
  62. const softness = float( i ).abs().div( halfSamples ).oneMinus();
  63. const uv = vec2( uvNode.x.add( this._invSize.x.mul( i ).mul( this.scaleNode ) ), uvNode.y );
  64. const color = sampleTexture( uv );
  65. const pass = threshold( color, this.tresholdNode ).mul( softness );
  66. total.addAssign( pass );
  67. } );
  68. return total.mul( this.colorNode );
  69. } );
  70. //
  71. const material = this._material || ( this._material = builder.createNodeMaterial() );
  72. material.fragmentNode = anamorph();
  73. //
  74. const properties = builder.getNodeProperties( this );
  75. properties.textureNode = textureNode;
  76. //
  77. return this._textureNode;
  78. }
  79. dispose() {
  80. this._renderTarget.dispose();
  81. }
  82. }
  83. export const anamorphic = ( node, threshold = .9, scale = 3, samples = 32 ) => nodeObject( new AnamorphicNode( nodeObject( node ).toTexture(), nodeObject( threshold ), nodeObject( scale ), samples ) );
  84. addNodeElement( 'anamorphic', anamorphic );
  85. export default AnamorphicNode;
粤ICP备19079148号