PMREMNode.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import TempNode from '../core/TempNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import { texture } from '../accessors/TextureNode.js';
  4. import { textureCubeUV } from './PMREMUtils.js';
  5. import { uniform } from '../core/UniformNode.js';
  6. import { NodeUpdateType } from '../core/constants.js';
  7. import { nodeProxy } from '../shadernode/ShaderNode.js';
  8. let _generator = null;
  9. const _cache = new WeakMap();
  10. function _generateCubeUVSize( imageHeight ) {
  11. const maxMip = Math.log2( imageHeight ) - 2;
  12. const texelHeight = 1.0 / imageHeight;
  13. const texelWidth = 1.0 / ( 3 * Math.max( Math.pow( 2, maxMip ), 7 * 16 ) );
  14. return { texelWidth, texelHeight, maxMip };
  15. }
  16. function _getPMREMFromTexture( texture ) {
  17. let cacheTexture = _cache.get( texture );
  18. if ( cacheTexture === undefined ) {
  19. if ( texture.isCubeTexture ) {
  20. if ( texture.source.data.some( ( texture ) => texture === undefined ) ) {
  21. throw new Error( 'PMREMNode: Undefined texture in CubeTexture. Use onLoad callback or async loader' );
  22. }
  23. cacheTexture = _generator.fromCubemap( texture );
  24. } else {
  25. if ( texture.image === undefined ) {
  26. throw new Error( 'PMREMNode: Undefined image in Texture. Use onLoad callback or async loader' );
  27. }
  28. cacheTexture = _generator.fromEquirectangular( texture );
  29. }
  30. _cache.set( texture, cacheTexture );
  31. }
  32. return cacheTexture.texture;
  33. }
  34. class PMREMNode extends TempNode {
  35. constructor( value, uvNode = null, levelNode = null ) {
  36. super( 'vec3' );
  37. this._value = value;
  38. this._pmrem = null;
  39. this.uvNode = uvNode;
  40. this.levelNode = levelNode;
  41. this._generator = null;
  42. this._texture = texture( null );
  43. this._width = uniform( 0 );
  44. this._height = uniform( 0 );
  45. this._maxMip = uniform( 0 );
  46. this.updateBeforeType = NodeUpdateType.RENDER;
  47. }
  48. set value( value ) {
  49. this._value = value;
  50. this._pmrem = null;
  51. }
  52. get value() {
  53. return this._value;
  54. }
  55. updateFromTexture( texture ) {
  56. const cubeUVSize = _generateCubeUVSize( texture.image.height );
  57. this._texture.value = texture;
  58. this._width.value = cubeUVSize.texelWidth;
  59. this._height.value = cubeUVSize.texelHeight;
  60. this._maxMip.value = cubeUVSize.maxMip;
  61. }
  62. updateBefore( frame ) {
  63. let pmrem = this._pmrem;
  64. if ( pmrem === null ) {
  65. const texture = this._value;
  66. if ( texture.isPMREMTexture === true ) {
  67. pmrem = texture;
  68. } else {
  69. pmrem = _getPMREMFromTexture( texture );
  70. }
  71. this._pmrem = pmrem;
  72. this.updateFromTexture( pmrem );
  73. }
  74. }
  75. setup( builder ) {
  76. if ( _generator === null ) {
  77. _generator = builder.createPMREMGenerator();
  78. }
  79. //
  80. this.updateBefore( builder );
  81. //
  82. let uvNode = this.uvNode;
  83. if ( uvNode === null && builder.context.getUV ) {
  84. uvNode = builder.context.getUV( this );
  85. }
  86. //
  87. let levelNode = this.levelNode;
  88. if ( levelNode === null && builder.context.getTextureLevel ) {
  89. levelNode = builder.context.getTextureLevel( this );
  90. }
  91. //
  92. return textureCubeUV( this._texture, uvNode, levelNode, this._width, this._height, this._maxMip );
  93. }
  94. }
  95. export const pmremTexture = nodeProxy( PMREMNode );
  96. addNodeClass( 'PMREMNode', PMREMNode );
  97. export default PMREMNode;