LightProbeHelperGPU.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. Mesh,
  3. NodeMaterial,
  4. SphereGeometry
  5. } from 'three';
  6. import { float, Fn, getShIrradianceAt, normalWorld, uniformArray, uniform, vec4 } from 'three/tsl';
  7. /**
  8. * Renders a sphere to visualize a light probe in the scene.
  9. *
  10. * This helper can only be used with {@link WebGPURenderer}.
  11. * When using {@link WebGLRenderer}, import from `LightProbeHelper.js`.
  12. *
  13. * ```js
  14. * const helper = new LightProbeHelper( lightProbe );
  15. * scene.add( helper );
  16. * ```
  17. *
  18. * @private
  19. * @augments Mesh
  20. */
  21. class LightProbeHelper extends Mesh {
  22. /**
  23. * Constructs a new light probe helper.
  24. *
  25. * @param {LightProbe} lightProbe - The light probe to visualize.
  26. * @param {number} [size=1] - The size of the helper.
  27. */
  28. constructor( lightProbe, size = 1 ) {
  29. const sh = uniformArray( lightProbe.sh.coefficients );
  30. const intensity = uniform( lightProbe.intensity );
  31. const RECIPROCAL_PI = float( 1 / Math.PI );
  32. const fragmentNode = Fn( () => {
  33. const irradiance = getShIrradianceAt( normalWorld, sh );
  34. const outgoingLight = RECIPROCAL_PI.mul( irradiance ).mul( intensity );
  35. return vec4( outgoingLight, 1.0 );
  36. } )();
  37. const material = new NodeMaterial();
  38. material.fragmentNode = fragmentNode;
  39. const geometry = new SphereGeometry( 1, 32, 16 );
  40. super( geometry, material );
  41. /**
  42. * The light probe to visualize.
  43. *
  44. * @type {LightProbe}
  45. */
  46. this.lightProbe = lightProbe;
  47. /**
  48. * The size of the helper.
  49. *
  50. * @type {number}
  51. * @default 1
  52. */
  53. this.size = size;
  54. this.type = 'LightProbeHelper';
  55. this._intensity = intensity;
  56. this._sh = sh;
  57. this.onBeforeRender();
  58. }
  59. /**
  60. * Frees the GPU-related resources allocated by this instance. Call this
  61. * method whenever this instance is no longer used in your app.
  62. */
  63. dispose() {
  64. this.geometry.dispose();
  65. this.material.dispose();
  66. }
  67. onBeforeRender() {
  68. this.position.copy( this.lightProbe.position );
  69. this.scale.set( 1, 1, 1 ).multiplyScalar( this.size );
  70. this._intensity.value = this.lightProbe.intensity;
  71. this._sh.array = this.lightProbe.sh.coefficients;
  72. }
  73. }
  74. export { LightProbeHelper };
粤ICP备19079148号