NormalizationEDLMaterial.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. import {Shaders} from "../../build/shaders/shaders.js";
  3. export class NormalizationEDLMaterial extends THREE.RawShaderMaterial{
  4. constructor(parameters = {}){
  5. super();
  6. let uniforms = {
  7. screenWidth: { type: 'f', value: 0 },
  8. screenHeight: { type: 'f', value: 0 },
  9. edlStrength: { type: 'f', value: 1.0 },
  10. radius: { type: 'f', value: 1.0 },
  11. neighbours: { type: '2fv', value: [] },
  12. uEDLMap: { type: 't', value: null },
  13. uDepthMap: { type: 't', value: null },
  14. uWeightMap: { type: 't', value: null },
  15. };
  16. this.setValues({
  17. uniforms: uniforms,
  18. vertexShader: this.getDefines() + Shaders['normalize.vs'],
  19. fragmentShader: this.getDefines() + Shaders['normalize_and_edl.fs'],
  20. });
  21. this.neighbourCount = 8;
  22. }
  23. getDefines() {
  24. let defines = '';
  25. defines += '#define NEIGHBOUR_COUNT ' + this.neighbourCount + '\n';
  26. return defines;
  27. }
  28. updateShaderSource() {
  29. let vs = this.getDefines() + Shaders['normalize.vs'];
  30. let fs = this.getDefines() + Shaders['normalize_and_edl.fs'];
  31. this.setValues({
  32. vertexShader: vs,
  33. fragmentShader: fs
  34. });
  35. this.uniforms.neighbours.value = this.neighbours;
  36. this.needsUpdate = true;
  37. }
  38. get neighbourCount(){
  39. return this._neighbourCount;
  40. }
  41. set neighbourCount(value){
  42. if (this._neighbourCount !== value) {
  43. this._neighbourCount = value;
  44. this.neighbours = new Float32Array(this._neighbourCount * 2);
  45. for (let c = 0; c < this._neighbourCount; c++) {
  46. this.neighbours[2 * c + 0] = Math.cos(2 * c * Math.PI / this._neighbourCount);
  47. this.neighbours[2 * c + 1] = Math.sin(2 * c * Math.PI / this._neighbourCount);
  48. }
  49. this.updateShaderSource();
  50. }
  51. }
  52. }