PostProcessingComponent.re.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import * as RE from 'rogue-engine';
  2. import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
  3. import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
  4. import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
  5. import { RGBShiftShader } from 'three/examples/jsm/shaders/RGBShiftShader.js';
  6. import { FocusShader } from 'three/examples/jsm/shaders/FocusShader.js'
  7. import { FilmShader } from 'three/examples/jsm/shaders/FilmShader.js'
  8. export default class PostProcessingComponent extends RE.Component {
  9. composer: EffectComposer
  10. shaders: ShaderPass[] = []
  11. elapsed: number = 0
  12. awake() {
  13. this.elapsed = 0
  14. this.composer = new EffectComposer(RE.Runtime.renderer);
  15. this.composer.addPass(new RenderPass(RE.Runtime.scene, RE.Runtime.camera));
  16. this.addShader(RGBShiftShader, {
  17. 'angle': 0,
  18. 'amount': 0.002,
  19. })
  20. this.addShader(FocusShader, {
  21. 'sampleDistance': 0.95,
  22. 'waveFactor': 0.003,
  23. })
  24. this.addShader(FilmShader, {
  25. 'time': 0.0,
  26. 'nIntensity': 1.0,
  27. 'sIntensity': 0.8,
  28. 'sCount': 1024,
  29. 'grayscale': false,
  30. })
  31. // const effect3 = new OutputPass();
  32. // this.composer.addPass( effect3 );
  33. RE.Runtime.renderFunc = () => {
  34. this.composer.render(RE.Runtime.deltaTime);
  35. }
  36. RE.Runtime.onStop(() => {
  37. RE.Runtime.renderFunc = () => RE.Runtime.defaultRenderFunc();
  38. });
  39. }
  40. addShader(shaderClass, uniforms = {}) {
  41. const effect1 = new ShaderPass(shaderClass);
  42. for (let key in uniforms) {
  43. effect1.uniforms[key].value = uniforms[key]
  44. }
  45. this.shaders.push(effect1)
  46. this.composer.addPass(effect1);
  47. }
  48. start() {
  49. }
  50. update() {
  51. this.elapsed += RE.Runtime.deltaTime
  52. this.shaders.forEach((shaderPass) => {
  53. if (shaderPass.uniforms['time']) {
  54. shaderPass.uniforms['time'].value = this.elapsed
  55. }
  56. if (shaderPass.uniforms['uTime']) {
  57. shaderPass.uniforms['uTime'].value = this.elapsed
  58. }
  59. if (shaderPass.uniforms['u_time']) {
  60. shaderPass.uniforms['u_time'].value = this.elapsed
  61. }
  62. })
  63. }
  64. }
  65. RE.registerComponent(PostProcessingComponent);