|
| 1 | +import { nodeObject } from '../shadernode/ShaderNode.js'; |
| 2 | +import { addNodeClass } from '../core/Node.js'; |
| 3 | +import { NodeUpdateType } from '../core/constants.js'; |
| 4 | +import PassNode from './PassNode.js'; |
| 5 | +import { RenderTarget } from '../../core/RenderTarget.js'; |
| 6 | +import { HalfFloatType/*, FloatType*/ } from '../../constants.js'; |
| 7 | +import { PassTextureNode } from './PassNode.js'; |
| 8 | +import { Vector2 } from '../../math/Vector2.js'; |
| 9 | +import { MeshNormalNodeMaterial } from '../Nodes.js'; |
| 10 | + |
| 11 | +const _size = new Vector2(); |
| 12 | + |
| 13 | +class PixelationPassNode extends PassNode { |
| 14 | + |
| 15 | + constructor( scene, camera, pixelSize, normalEdgeStrength, depthEdgeStrength ) { |
| 16 | + |
| 17 | + super( 'normal', scene, camera ); |
| 18 | + |
| 19 | + this.pixelSize = pixelSize; |
| 20 | + this.normalEdgeStrength = normalEdgeStrength; |
| 21 | + this.depthEdgeStrength = depthEdgeStrength; |
| 22 | + |
| 23 | + this.updateBeforeType = NodeUpdateType.FRAME; |
| 24 | + |
| 25 | + this.isPixelationPassNode = true; |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + updateBefore( frame ) { |
| 30 | + |
| 31 | + const { renderer } = frame; |
| 32 | + const { scene, camera } = this; |
| 33 | + |
| 34 | + this._pixelRatio = renderer.getPixelRatio(); |
| 35 | + |
| 36 | + const size = renderer.getSize( _size ); |
| 37 | + const pixelSize = this.pixelSize.value ? this.pixelSize.value : this.pixelSize; |
| 38 | + |
| 39 | + this.setSize( pixelSize >= 1 ? size.width / pixelSize : size.width, pixelSize >= 1 ? size.height / pixelSize : size.height ); |
| 40 | + |
| 41 | + const currentRenderTarget = renderer.getRenderTarget(); |
| 42 | + |
| 43 | + this._cameraNear.value = camera.near; |
| 44 | + this._cameraFar.value = camera.far; |
| 45 | + |
| 46 | + renderer.setRenderTarget( this.renderTarget ); |
| 47 | + console.log(this.renderTarget) |
| 48 | + |
| 49 | + renderer.render( scene, camera ); |
| 50 | + |
| 51 | + renderer.setRenderTarget( this.normalRenderTarget ); |
| 52 | + const oldMaterial = scene.overrideMaterial; |
| 53 | + scene.overrideMaterial = new MeshNormalNodeMaterial(); |
| 54 | + renderer.render( scene, camera ); |
| 55 | + scene.overrideMaterial = oldMaterial; |
| 56 | + |
| 57 | + renderer.setRenderTarget( currentRenderTarget ); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + setSize( width, height ) { |
| 62 | + |
| 63 | + super.setSize( width, height ); |
| 64 | + this.normalRenderTarget.setSize( width * this._pixelRatio, height * this._pixelRatio ); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + setup() { |
| 69 | + |
| 70 | + const pass = super.getTextureDepthNode(); |
| 71 | + console.log( this.normalEdgeStrength ) |
| 72 | + return pass.pixelation( this._depthTextureNode, this._normalTextureNode, this.pixelSize, this.normalEdgeStrength, this.depthEdgeStrength ); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + dispose() { |
| 77 | + |
| 78 | + super.dispose(); |
| 79 | + this.normalRenderTarget.dispose(); |
| 80 | + |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +export default PixelationPassNode; |
| 87 | + |
| 88 | +export const pixelationPass = ( scene, camera, pixelSize, normalEdgeStrength, depthEdgeStrength ) => nodeObject( new PixelationPassNode( scene, camera, pixelSize, normalEdgeStrength, depthEdgeStrength ) ); |
| 89 | + |
| 90 | +addNodeClass( 'PixelationPassNode', PixelationPassNode ); |
0 commit comments