1
0

LightProbeHelperGPU.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {
  2. Mesh,
  3. NodeMaterial,
  4. SphereGeometry
  5. } from 'three';
  6. import { float, Fn, getShIrradianceAt, normalWorld, uniformArray, uniform, vec4 } from 'three/tsl';
  7. class LightProbeHelper extends Mesh {
  8. constructor( lightProbe, size = 1 ) {
  9. const sh = uniformArray( lightProbe.sh.coefficients );
  10. const intensity = uniform( lightProbe.intensity );
  11. const RECIPROCAL_PI = float( 1 / Math.PI );
  12. const fragmentNode = Fn( () => {
  13. const irradiance = getShIrradianceAt( normalWorld, sh );
  14. const outgoingLight = RECIPROCAL_PI.mul( irradiance ).mul( intensity );
  15. return vec4( outgoingLight, 1.0 );
  16. } )();
  17. const material = new NodeMaterial();
  18. material.fragmentNode = fragmentNode;
  19. const geometry = new SphereGeometry( 1, 32, 16 );
  20. super( geometry, material );
  21. this.lightProbe = lightProbe;
  22. this.size = size;
  23. this.type = 'LightProbeHelper';
  24. this._intensity = intensity;
  25. this._sh = sh;
  26. this.onBeforeRender();
  27. }
  28. dispose() {
  29. this.geometry.dispose();
  30. this.material.dispose();
  31. }
  32. onBeforeRender() {
  33. this.position.copy( this.lightProbe.position );
  34. this.scale.set( 1, 1, 1 ).multiplyScalar( this.size );
  35. this._intensity.value = this.lightProbe.intensity;
  36. this._sh.array = this.lightProbe.sh.coefficients;
  37. }
  38. }
  39. export { LightProbeHelper };
粤ICP备19079148号