|
| 1 | +import Node from '../core/Node.js'; |
| 2 | +import { uniform, div, vec2, invert } from '../shadernode/ShaderNodeBaseElements.js'; |
| 3 | +import { Vector2 } from 'three'; |
| 4 | +import { NodeUpdateType } from '../core/constants.js'; |
| 5 | + |
| 6 | +let resolution; |
| 7 | + |
| 8 | +class ViewportNode extends Node { |
| 9 | + |
| 10 | + static COORDINATE = 'coordinate'; |
| 11 | + static RESOLUTION = 'resolution'; |
| 12 | + static TOP_LEFT = 'topLeft'; |
| 13 | + static BOTTOM_LEFT = 'bottomLeft'; |
| 14 | + static TOP_RIGHT = 'topRight'; |
| 15 | + static BOTTOM_RIGHT = 'bottomRight'; |
| 16 | + |
| 17 | + constructor( scope ) { |
| 18 | + |
| 19 | + super(); |
| 20 | + |
| 21 | + this.scope = scope; |
| 22 | + |
| 23 | + this.isScreenNode = true; |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + getNodeType() { |
| 28 | + |
| 29 | + return this.scope === ViewportNode.COORDINATE ? 'vec4' : 'vec2'; |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + getUpdateType() { |
| 34 | + |
| 35 | + let updateType = NodeUpdateType.NONE; |
| 36 | + |
| 37 | + if ( this.scope === ViewportNode.RESOLUTION ) { |
| 38 | + |
| 39 | + updateType = NodeUpdateType.FRAME; |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + this.updateType = updateType; |
| 44 | + |
| 45 | + return updateType; |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + update( { renderer } ) { |
| 50 | + |
| 51 | + renderer.getSize( resolution ); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + construct( builder ) { |
| 56 | + |
| 57 | + const scope = this.scope; |
| 58 | + |
| 59 | + if ( scope === ViewportNode.COORDINATE ) return; |
| 60 | + |
| 61 | + let output = null; |
| 62 | + |
| 63 | + if ( scope === ViewportNode.RESOLUTION ) { |
| 64 | + |
| 65 | + resolution ||= new Vector2(); |
| 66 | + |
| 67 | + output = uniform( resolution ); |
| 68 | + |
| 69 | + } else { |
| 70 | + |
| 71 | + const coordinateNode = vec2( new ViewportNode( ViewportNode.COORDINATE ) ); |
| 72 | + const resolutionNode = new ViewportNode( ViewportNode.RESOLUTION ); |
| 73 | + |
| 74 | + output = div( coordinateNode, resolutionNode ); |
| 75 | + |
| 76 | + let outX = output.x; |
| 77 | + let outY = output.y; |
| 78 | + |
| 79 | + if ( /top/i.test( scope ) && builder.isFlipY() ) outY = invert( outY ); |
| 80 | + else if ( /bottom/i.test( scope ) && builder.isFlipY() === false ) outY = invert( outY ); |
| 81 | + |
| 82 | + if ( /right/i.test( scope ) ) outX = invert( outX ); |
| 83 | + |
| 84 | + output = vec2( outX, outY ); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + return output; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + generate( builder ) { |
| 93 | + |
| 94 | + if ( this.scope === ViewportNode.COORDINATE ) { |
| 95 | + |
| 96 | + return builder.getFragCoord(); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + return super.generate( builder ); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | +export default ViewportNode; |
0 commit comments