Skip to content

Instantly share code, notes, and snippets.

@samhains
Last active December 3, 2022 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhains/2a2816f289e45d5673e23c489aacb3a5 to your computer and use it in GitHub Desktop.
Save samhains/2a2816f289e45d5673e23c489aacb3a5 to your computer and use it in GitHub Desktop.
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