EyeDomeLightingMaterial.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. import {Shaders} from "../../build/shaders/shaders.js";
  3. //
  4. // Algorithm by Christian Boucheny
  5. // shader code taken and adapted from CloudCompare
  6. //
  7. // see
  8. // https://github.com/cloudcompare/trunk/tree/master/plugins/qEDL/shaders/EDL
  9. // http://www.kitware.com/source/home/post/9
  10. // https://tel.archives-ouvertes.fr/tel-00438464/document p. 115+ (french)
  11. export class EyeDomeLightingMaterial extends THREE.RawShaderMaterial{//base
  12. constructor(parameters = {}){
  13. super();
  14. let uniforms = {
  15. screenWidth: { type: 'f', value: 0 },
  16. screenHeight: { type: 'f', value: 0 },
  17. edlStrength: { type: 'f', value: 1.0 },
  18. uNear: { type: 'f', value: 1.0 },
  19. uFar: { type: 'f', value: 1.0 },
  20. radius: { type: 'f', value: 1.0 },
  21. neighbours: { type: '2fv', value: [] },
  22. depthMap: { type: 't', value: null },
  23. uEDLColor: { type: 't', value: null },
  24. uEDLDepth: { type: 't', value: null },
  25. opacity: { type: 'f', value: 1.0 },
  26. uProj: { type: "Matrix4fv", value: [] },
  27. };
  28. this.setValues({
  29. uniforms: uniforms,
  30. vertexShader: this.getDefines() + Shaders['edl.vs'],
  31. fragmentShader: this.getDefines() + Shaders['edl.fs'],
  32. lights: false
  33. });
  34. this.neighbourCount = 8;
  35. }
  36. getDefines() {
  37. let defines = '';
  38. defines += '#define NEIGHBOUR_COUNT ' + this.neighbourCount + '\n';
  39. return defines;
  40. }
  41. updateShaderSource() {
  42. let vs = this.getDefines() + Shaders['edl.vs'];
  43. let fs = this.getDefines() + Shaders['edl.fs'];
  44. this.setValues({
  45. vertexShader: vs,
  46. fragmentShader: fs
  47. });
  48. this.uniforms.neighbours.value = this.neighbours;
  49. this.needsUpdate = true;
  50. }
  51. get neighbourCount(){
  52. return this._neighbourCount;
  53. }
  54. set neighbourCount(value){
  55. if (this._neighbourCount !== value) {//周围八个格子
  56. this._neighbourCount = value;
  57. this.neighbours = new Float32Array(this._neighbourCount * 2);
  58. for (let c = 0; c < this._neighbourCount; c++) {
  59. this.neighbours[2 * c + 0] = Math.cos(2 * c * Math.PI / this._neighbourCount);
  60. this.neighbours[2 * c + 1] = Math.sin(2 * c * Math.PI / this._neighbourCount);
  61. }
  62. this.updateShaderSource();
  63. }
  64. }
  65. }