Skip to content

Commit

Permalink
playing with render targets
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhhelgeson committed Jul 6, 2024
1 parent 6f87fc3 commit 0d26ee3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export { default as FilmNode, film } from './display/FilmNode.js';
export { default as Lut3DNode, lut3D } from './display/Lut3DNode.js';
export { default as RenderOutputNode, renderOutput } from './display/RenderOutputNode.js';
export { default as PixelationNode, pixelation } from './display/PixelationNode.js';
export { default as PixelationPassNode, pixelationPass } from './display/PixelationPassNode.js'

export { default as PassNode, pass, texturePass, depthPass, normalPass } from './display/PassNode.js';

Expand Down
10 changes: 5 additions & 5 deletions src/nodes/display/PassNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import MeshNormalNodeMaterial from '../materials/MeshNormalNodeMaterial.js';

const _size = new Vector2();

class PassTextureNode extends TextureNode {
export class PassTextureNode extends TextureNode {

constructor( passNode, texture ) {

Expand Down Expand Up @@ -70,11 +70,11 @@ class PassNode extends TempNode {

if ( this.scope === PassNode.NORMAL ) {

const target = new RenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, { type: HalfFloatType } );
renderTarget.texture.name = 'PostProcessingNormal';
this.normalRenderTarget = target;
const normalTarget = new RenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, { type: HalfFloatType } );
normalTarget.texture.name = 'PostProcessingNormal';
this.normalRenderTarget = normalTarget;

this._normalTextureNode = nodeObject( new PassTextureNode( this, target.texture ) );
this._normalTextureNode = nodeObject( new PassTextureNode( this, normalTarget.texture ) );

}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/display/PixelationNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class PixelationNode extends TempNode {

}

export const pixelation = ( node, depthNode, normalNode, pixelSize, normalEdgeStrength = 0.3, depthEdgeStrength = 0.4 ) => nodeObject( new PixelationNode( nodeObject( node ).toTexture(), nodeObject( depthNode ).toTexture(), nodeObject( normalNode ).toTexture(), nodeObject( pixelSize ), normalEdgeStrength, depthEdgeStrength ) );
export const pixelation = ( node, depthNode, normalNode, pixelSize = 4, normalEdgeStrength = 0.3, depthEdgeStrength = 0.4 ) => nodeObject( new PixelationNode( nodeObject( node ).toTexture(), nodeObject( depthNode ).toTexture(), nodeObject( normalNode ).toTexture(), pixelSize, normalEdgeStrength, depthEdgeStrength ) );

addNodeElement( 'pixelation', pixelation );

Expand Down
90 changes: 90 additions & 0 deletions src/nodes/display/PixelationPassNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { nodeObject } from '../shadernode/ShaderNode.js';
import { addNodeClass } from '../core/Node.js';
import { NodeUpdateType } from '../core/constants.js';
import PassNode from './PassNode.js';
import { RenderTarget } from '../../core/RenderTarget.js';
import { HalfFloatType/*, FloatType*/ } from '../../constants.js';
import { PassTextureNode } from './PassNode.js';
import { Vector2 } from '../../math/Vector2.js';
import { MeshNormalNodeMaterial } from '../Nodes.js';

const _size = new Vector2();

class PixelationPassNode extends PassNode {

constructor( scene, camera, pixelSize, normalEdgeStrength, depthEdgeStrength ) {

super( 'normal', scene, camera );

this.pixelSize = pixelSize;
this.normalEdgeStrength = normalEdgeStrength;
this.depthEdgeStrength = depthEdgeStrength;

this.updateBeforeType = NodeUpdateType.FRAME;

this.isPixelationPassNode = true;

}

updateBefore( frame ) {

const { renderer } = frame;
const { scene, camera } = this;

this._pixelRatio = renderer.getPixelRatio();

const size = renderer.getSize( _size );
const pixelSize = this.pixelSize.value ? this.pixelSize.value : this.pixelSize;

this.setSize( pixelSize >= 1 ? size.width / pixelSize : size.width, pixelSize >= 1 ? size.height / pixelSize : size.height );

const currentRenderTarget = renderer.getRenderTarget();

this._cameraNear.value = camera.near;
this._cameraFar.value = camera.far;

renderer.setRenderTarget( this.renderTarget );
console.log(this.renderTarget)

renderer.render( scene, camera );

renderer.setRenderTarget( this.normalRenderTarget );
const oldMaterial = scene.overrideMaterial;
scene.overrideMaterial = new MeshNormalNodeMaterial();
renderer.render( scene, camera );
scene.overrideMaterial = oldMaterial;

renderer.setRenderTarget( currentRenderTarget );

}

setSize( width, height ) {

super.setSize( width, height );
this.normalRenderTarget.setSize( width * this._pixelRatio, height * this._pixelRatio );

}

setup() {

const pass = super.getTextureDepthNode();
console.log( this.normalEdgeStrength )
return pass.pixelation( this._depthTextureNode, this._normalTextureNode, this.pixelSize, this.normalEdgeStrength, this.depthEdgeStrength );

}

dispose() {

super.dispose();
this.normalRenderTarget.dispose();


}

}

export default PixelationPassNode;

export const pixelationPass = ( scene, camera, pixelSize, normalEdgeStrength, depthEdgeStrength ) => nodeObject( new PixelationPassNode( scene, camera, pixelSize, normalEdgeStrength, depthEdgeStrength ) );

addNodeClass( 'PixelationPassNode', PixelationPassNode );

0 comments on commit 0d26ee3

Please sign in to comment.