AnamorphicNode.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import TempNode from '../core/TempNode.js';
  2. import { nodeObject, addNodeElement, tslFn, float, vec2, vec3, vec4 } 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 { texturePass } from './PassNode.js';
  9. import { Vector2, RenderTarget } from 'three';
  10. import QuadMesh from '../../objects/QuadMesh.js';
  11. const quadMesh = new QuadMesh();
  12. class AnamorphicNode extends TempNode {
  13. constructor( textureNode, tresholdNode, scaleNode, samples ) {
  14. super( 'vec4' );
  15. this.textureNode = textureNode;
  16. this.tresholdNode = tresholdNode;
  17. this.scaleNode = scaleNode;
  18. this.colorNode = vec3( 0.1, 0.0, 1.0 );
  19. this.samples = samples;
  20. this.resolution = new Vector2( 1, 1 );
  21. this._renderTarget = new RenderTarget();
  22. this._renderTarget.texture.name = 'anamorphic';
  23. this._invSize = uniform( new Vector2() );
  24. this._textureNode = texturePass( this, this._renderTarget.texture );
  25. this.updateBeforeType = NodeUpdateType.RENDER;
  26. }
  27. getTextureNode() {
  28. return this._textureNode;
  29. }
  30. setSize( width, height ) {
  31. this._invSize.value.set( 1 / width, 1 / height );
  32. width = Math.max( Math.round( width * this.resolution.x ), 1 );
  33. height = Math.max( Math.round( height * this.resolution.y ), 1 );
  34. this._renderTarget.setSize( width, height );
  35. }
  36. updateBefore( frame ) {
  37. const { renderer } = frame;
  38. const textureNode = this.textureNode;
  39. const map = textureNode.value;
  40. this._renderTarget.texture.type = map.type;
  41. const currentRenderTarget = renderer.getRenderTarget();
  42. const currentTexture = textureNode.value;
  43. quadMesh.material = this._material;
  44. this.setSize( map.image.width, map.image.height );
  45. // render
  46. renderer.setRenderTarget( this._renderTarget );
  47. quadMesh.render( renderer );
  48. // restore
  49. renderer.setRenderTarget( currentRenderTarget );
  50. textureNode.value = currentTexture;
  51. }
  52. setup( builder ) {
  53. const textureNode = this.textureNode;
  54. if ( textureNode.isTextureNode !== true ) {
  55. console.error( 'AnamorphNode requires a TextureNode.' );
  56. return vec4();
  57. }
  58. //
  59. const uvNode = textureNode.uvNode || uv();
  60. const sampleTexture = ( uv ) => textureNode.cache().context( { getUV: () => uv, forceUVContext: true } );
  61. const anamorph = tslFn( () => {
  62. const samples = this.samples;
  63. const halfSamples = Math.floor( samples / 2 );
  64. const total = vec3( 0 ).toVar();
  65. loop( { start: - halfSamples, end: halfSamples }, ( { i } ) => {
  66. const softness = float( i ).abs().div( halfSamples ).oneMinus();
  67. const uv = vec2( uvNode.x.add( this._invSize.x.mul( i ).mul( this.scaleNode ) ), uvNode.y );
  68. const color = sampleTexture( uv );
  69. const pass = threshold( color, this.tresholdNode ).mul( softness );
  70. total.addAssign( pass );
  71. } );
  72. return total.mul( this.colorNode );
  73. } );
  74. //
  75. const material = this._material || ( this._material = builder.createNodeMaterial() );
  76. material.fragmentNode = anamorph();
  77. //
  78. const properties = builder.getNodeProperties( this );
  79. properties.textureNode = textureNode;
  80. //
  81. return this._textureNode;
  82. }
  83. }
  84. export const anamorphic = ( node, threshold = .9, scale = 3, samples = 32 ) => nodeObject( new AnamorphicNode( nodeObject( node ), nodeObject( threshold ), nodeObject( scale ), samples ) );
  85. addNodeElement( 'anamorphic', anamorphic );
  86. export default AnamorphicNode;