Last active
December 3, 2022 00:29
-
-
Save samhains/2a2816f289e45d5673e23c489aacb3a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pass } from "three/examples/jsm/postprocessing/Pass"; | |
import { Color, WebGLRenderTarget } from "three"; | |
class RenderPass2 extends Pass { | |
constructor(scene, camera, overrideMaterials, overrideMesh) { | |
super(); | |
this.scene = scene; | |
this.camera = camera; | |
this.clearAlpha = 0; | |
this.renderToScreen = false; | |
this.clear = false; | |
this.overrideMesh = overrideMesh; | |
this.clearDepth = false; | |
this.needsSwap = false; | |
this.defaultMaterials = []; | |
this.setOvverideMaterials(overrideMaterials); | |
this.hasRendered = false; | |
} | |
setDefaultMaterials() { | |
this.defaultMaterials = this.overrideMesh.map((mesh) => { | |
return mesh.material; | |
}); | |
} | |
setOvverideMaterials(overrideMaterials) { | |
this.overrideMaterials = this.overrideMesh.map((mesh, i) => { | |
const overrideIndex = i % overrideMaterials.length; | |
return overrideMaterials[overrideIndex]; | |
}); | |
} | |
overrideSceneMaterials() { | |
this.overrideMesh.forEach((mesh, i) => { | |
mesh.material = this.overrideMaterials[i]; | |
}); | |
} | |
restoreDefaultMaterials() { | |
this.overrideMesh.forEach((mesh, i) => { | |
mesh.material = this.defaultMaterials[i]; | |
}); | |
} | |
render(renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */) { | |
if (!this.hasRendered) { | |
this.setDefaultMaterials(); | |
this.hasRendered = true; | |
} | |
this.overrideSceneMaterials(); | |
renderer.autoClear = true; | |
// const readBuffer = new WebGLRenderTarget(1000, 1000); | |
renderer.setRenderTarget(this.renderToScreen ? null : readBuffer); | |
renderer.render(this.scene, this.camera); | |
renderer.autoClear = false; | |
renderer.clearDepth(); | |
renderer.setRenderTarget(null); | |
this.restoreDefaultMaterials(); | |
} | |
} | |
export { RenderPass2 }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment