PMREMGenerator.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /**
  2. * @author Emmett Lalish / elalish
  3. *
  4. * This class generates a Prefiltered, Mipmapped Radiance Environment Map
  5. * (PMREM) from a cubeMap environment texture. This allows different levels of
  6. * blur to be quickly accessed based on material roughness. It is packed into a
  7. * special CubeUV format that allows us to perform custom interpolation so that
  8. * we can support nonlinear formats such as RGBE. Unlike a traditional mipmap
  9. * chain, it only goes down to the LOD_MIN level (above), and then creates extra
  10. * even more filtered 'mips' at the same LOD_MIN resolution, associated with
  11. * higher roughness levels. In this way we maintain resolution to smoothly
  12. * interpolate diffuse lighting while limiting sampling computation.
  13. */
  14. import {
  15. BufferAttribute,
  16. BufferGeometry,
  17. CubeUVReflectionMapping,
  18. GammaEncoding,
  19. LinearEncoding,
  20. LinearToneMapping,
  21. Mesh,
  22. NearestFilter,
  23. NoBlending,
  24. OrthographicCamera,
  25. PerspectiveCamera,
  26. RGBDEncoding,
  27. RGBEEncoding,
  28. RGBEFormat,
  29. RGBM16Encoding,
  30. RGBM7Encoding,
  31. RawShaderMaterial,
  32. Scene,
  33. UnsignedByteType,
  34. Vector2,
  35. Vector3,
  36. WebGLRenderTarget,
  37. sRGBEncoding
  38. } from "../../../build/three.module.js";
  39. var PMREMGenerator = ( function () {
  40. var LOD_MIN = 4;
  41. var LOD_MAX = 8;
  42. var SIZE_MAX = Math.pow( 2, LOD_MAX );
  43. // The standard deviations (radians) associated with the extra mips. These are
  44. // chosen to approximate a Trowbridge-Reitz distribution function times the
  45. // geometric shadowing function. These sigma values squared must match the
  46. // variance #defines in cube_uv_reflection_fragment.glsl.js.
  47. var EXTRA_LOD_SIGMA = [ 0.125, 0.215, 0.35, 0.446, 0.526, 0.582 ];
  48. var TOTAL_LODS = LOD_MAX - LOD_MIN + 1 + EXTRA_LOD_SIGMA.length;
  49. // The maximum length of the blur for loop. Smaller sigmas will use fewer
  50. // samples and exit early, but not recompile the shader.
  51. var MAX_SAMPLES = 20;
  52. var ENCODINGS = {
  53. [ LinearEncoding ]: 0,
  54. [ sRGBEncoding ]: 1,
  55. [ RGBEEncoding ]: 2,
  56. [ RGBM7Encoding ]: 3,
  57. [ RGBM16Encoding ]: 4,
  58. [ RGBDEncoding ]: 5,
  59. [ GammaEncoding ]: 6
  60. };
  61. var _flatCamera = new OrthographicCamera();
  62. var _blurMaterial = _getBlurShader( MAX_SAMPLES );
  63. var { _lodPlanes, _sizeLods, _sigmas } = _createPlanes();
  64. var _pingPongRenderTarget = null;
  65. var _renderer = null;
  66. // Golden Ratio
  67. var PHI = ( 1 + Math.sqrt( 5 ) ) / 2;
  68. var INV_PHI = 1 / PHI;
  69. // Vertices of a dodecahedron (except the opposites, which represent the
  70. // same axis), used as axis directions evenly spread on a sphere.
  71. var _axisDirections = [
  72. new Vector3( 1, 1, 1 ),
  73. new Vector3( - 1, 1, 1 ),
  74. new Vector3( 1, 1, - 1 ),
  75. new Vector3( - 1, 1, - 1 ),
  76. new Vector3( 0, PHI, INV_PHI ),
  77. new Vector3( 0, PHI, - INV_PHI ),
  78. new Vector3( INV_PHI, 0, PHI ),
  79. new Vector3( - INV_PHI, 0, PHI ),
  80. new Vector3( PHI, INV_PHI, 0 ),
  81. new Vector3( - PHI, INV_PHI, 0 ) ];
  82. var PMREMGenerator = function ( renderer ) {
  83. _renderer = renderer;
  84. };
  85. PMREMGenerator.prototype = {
  86. constructor: PMREMGenerator,
  87. /**
  88. * Generates a PMREM from a supplied Scene, which can be faster than using an
  89. * image if networking bandwidth is low. Optional sigma specifies a blur radius
  90. * in radians to be applied to the scene before PMREM generation. Optional near
  91. * and far planes ensure the scene is rendered in its entirety (the cubeCamera
  92. * is placed at the origin).
  93. */
  94. fromScene: function ( scene, sigma = 0, near = 0.1, far = 100 ) {
  95. var cubeUVRenderTarget = _allocateTargets();
  96. _sceneToCubeUV( scene, near, far, cubeUVRenderTarget );
  97. if ( sigma > 0 ) {
  98. _blur( cubeUVRenderTarget, 0, 0, sigma );
  99. }
  100. _applyPMREM( cubeUVRenderTarget );
  101. _cleanup();
  102. return cubeUVRenderTarget;
  103. },
  104. /**
  105. * Generates a PMREM from an equirectangular texture, which can be either LDR
  106. * (RGBFormat) or HDR (RGBEFormat). The ideal input image size is 1k (1024 x 512),
  107. * as this matches best with the 256 x 256 cubemap output.
  108. */
  109. fromEquirectangular: function ( equirectangular ) {
  110. equirectangular.magFilter = NearestFilter;
  111. equirectangular.minFilter = NearestFilter;
  112. equirectangular.generateMipmaps = false;
  113. var cubeUVRenderTarget = _allocateTargets( equirectangular );
  114. _textureToCubeUV( equirectangular, cubeUVRenderTarget );
  115. _applyPMREM( cubeUVRenderTarget );
  116. _cleanup();
  117. return cubeUVRenderTarget;
  118. },
  119. /**
  120. * Generates a PMREM from an cubemap texture, which can be either LDR
  121. * (RGBFormat) or HDR (RGBEFormat). The ideal input cube size is 256 x 256,
  122. * as this matches best with the 256 x 256 cubemap output.
  123. */
  124. fromCubemap: function ( cubemap ) {
  125. var cubeUVRenderTarget = _allocateTargets( cubemap );
  126. _textureToCubeUV( cubemap, cubeUVRenderTarget );
  127. _applyPMREM( cubeUVRenderTarget );
  128. _cleanup();
  129. return cubeUVRenderTarget;
  130. },
  131. };
  132. function _createPlanes() {
  133. var _lodPlanes = [];
  134. var _sizeLods = [];
  135. var _sigmas = [];
  136. var lod = LOD_MAX;
  137. for ( var i = 0; i < TOTAL_LODS; i ++ ) {
  138. var sizeLod = Math.pow( 2, lod );
  139. _sizeLods.push( sizeLod );
  140. var sigma = 1.0 / sizeLod;
  141. if ( i > LOD_MAX - LOD_MIN ) {
  142. sigma = EXTRA_LOD_SIGMA[ i - LOD_MAX + LOD_MIN - 1 ];
  143. } else if ( i == 0 ) {
  144. sigma = 0;
  145. }
  146. _sigmas.push( sigma );
  147. var texelSize = 1.0 / ( sizeLod - 1 );
  148. var min = - texelSize / 2;
  149. var max = 1 + texelSize / 2;
  150. var uv1 = [ min, min, max, min, max, max, min, min, max, max, min, max ];
  151. var cubeFaces = 6;
  152. var vertices = 6;
  153. var positionSize = 3;
  154. var uvSize = 2;
  155. var faceIndexSize = 1;
  156. var position = new Float32Array( positionSize * vertices * cubeFaces );
  157. var uv = new Float32Array( uvSize * vertices * cubeFaces );
  158. var faceIndex = new Float32Array( faceIndexSize * vertices * cubeFaces );
  159. for ( var face = 0; face < cubeFaces; face ++ ) {
  160. var x = ( face % 3 ) * 2 / 3 - 1;
  161. var y = face > 2 ? 0 : - 1;
  162. var coordinates = [
  163. [ x, y, 0 ],
  164. [ x + 2 / 3, y, 0 ],
  165. [ x + 2 / 3, y + 1, 0 ],
  166. [ x, y, 0 ],
  167. [ x + 2 / 3, y + 1, 0 ],
  168. [ x, y + 1, 0 ]
  169. ];
  170. position.set( [].concat( ...coordinates ),
  171. positionSize * vertices * face );
  172. uv.set( uv1, uvSize * vertices * face );
  173. var fill = [ face, face, face, face, face, face ];
  174. faceIndex.set( fill, faceIndexSize * vertices * face );
  175. }
  176. var planes = new BufferGeometry();
  177. planes.setAttribute(
  178. 'position', new BufferAttribute( position, positionSize ) );
  179. planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) );
  180. planes.setAttribute(
  181. 'faceIndex', new BufferAttribute( faceIndex, faceIndexSize ) );
  182. _lodPlanes.push( planes );
  183. if ( lod > LOD_MIN ) {
  184. lod --;
  185. }
  186. }
  187. return { _lodPlanes, _sizeLods, _sigmas };
  188. }
  189. function _allocateTargets( equirectangular ) {
  190. var params = {
  191. magFilter: NearestFilter,
  192. minFilter: NearestFilter,
  193. generateMipmaps: false,
  194. type: equirectangular ? equirectangular.type : UnsignedByteType,
  195. format: equirectangular ? equirectangular.format : RGBEFormat,
  196. encoding: equirectangular ? equirectangular.encoding : RGBEEncoding,
  197. depthBuffer: false,
  198. stencilBuffer: false
  199. };
  200. var cubeUVRenderTarget = _createRenderTarget(
  201. { ...params, depthBuffer: ( equirectangular ? false : true ) } );
  202. _pingPongRenderTarget = _createRenderTarget( params );
  203. return cubeUVRenderTarget;
  204. }
  205. function _cleanup() {
  206. _pingPongRenderTarget.dispose();
  207. _renderer.setRenderTarget( null );
  208. var size = _renderer.getSize( new Vector2() );
  209. _renderer.setViewport( 0, 0, size.x, size.y );
  210. }
  211. function _sceneToCubeUV( scene, near, far, cubeUVRenderTarget ) {
  212. var fov = 90;
  213. var aspect = 1;
  214. var cubeCamera = new PerspectiveCamera( fov, aspect, near, far );
  215. var upSign = [ 1, 1, 1, 1, - 1, 1 ];
  216. var forwardSign = [ 1, 1, - 1, - 1, - 1, 1 ];
  217. var gammaOutput = _renderer.gammaOutput;
  218. var toneMapping = _renderer.toneMapping;
  219. var toneMappingExposure = _renderer.toneMappingExposure;
  220. _renderer.toneMapping = LinearToneMapping;
  221. _renderer.toneMappingExposure = 1.0;
  222. _renderer.gammaOutput = false;
  223. scene.scale.z *= - 1;
  224. _renderer.setRenderTarget( cubeUVRenderTarget );
  225. for ( var i = 0; i < 6; i ++ ) {
  226. var col = i % 3;
  227. if ( col == 0 ) {
  228. cubeCamera.up.set( 0, upSign[ i ], 0 );
  229. cubeCamera.lookAt( forwardSign[ i ], 0, 0 );
  230. } else if ( col == 1 ) {
  231. cubeCamera.up.set( 0, 0, upSign[ i ] );
  232. cubeCamera.lookAt( 0, forwardSign[ i ], 0 );
  233. } else {
  234. cubeCamera.up.set( 0, upSign[ i ], 0 );
  235. cubeCamera.lookAt( 0, 0, forwardSign[ i ] );
  236. }
  237. _setViewport(
  238. col * SIZE_MAX, i > 2 ? SIZE_MAX : 0, SIZE_MAX, SIZE_MAX );
  239. _renderer.render( scene, cubeCamera );
  240. }
  241. _renderer.toneMapping = toneMapping;
  242. _renderer.toneMappingExposure = toneMappingExposure;
  243. _renderer.gammaOutput = gammaOutput;
  244. scene.scale.z *= - 1;
  245. }
  246. function _textureToCubeUV( texture, cubeUVRenderTarget ) {
  247. var scene = new Scene();
  248. var material = texture.isCubeTexture ? _getCubemapShader() : _getEquirectShader();
  249. scene.add( new Mesh( _lodPlanes[ 0 ], material ) );
  250. var uniforms = material.uniforms;
  251. uniforms[ 'envMap' ].value = texture;
  252. if ( ! texture.isCubeTexture ) {
  253. uniforms[ 'texelSize' ].value.set( 1.0 / texture.image.width, 1.0 / texture.image.height );
  254. }
  255. uniforms[ 'inputEncoding' ].value = ENCODINGS[ texture.encoding ];
  256. uniforms[ 'outputEncoding' ].value = ENCODINGS[ texture.encoding ];
  257. _renderer.setRenderTarget( cubeUVRenderTarget );
  258. _setViewport( 0, 0, 3 * SIZE_MAX, 2 * SIZE_MAX );
  259. _renderer.render( scene, _flatCamera );
  260. }
  261. function _createRenderTarget( params ) {
  262. var cubeUVRenderTarget =
  263. new WebGLRenderTarget( 3 * SIZE_MAX, 3 * SIZE_MAX, params );
  264. cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping;
  265. cubeUVRenderTarget.texture.name = 'PMREM.cubeUv';
  266. return cubeUVRenderTarget;
  267. }
  268. function _setViewport( x, y, width, height ) {
  269. var dpr = _renderer.getPixelRatio();
  270. _renderer.setViewport( x / dpr, y / dpr, width / dpr, height / dpr );
  271. }
  272. function _applyPMREM( cubeUVRenderTarget ) {
  273. var autoClear = _renderer.autoClear;
  274. _renderer.autoClear = false;
  275. for ( var i = 1; i < TOTAL_LODS; i ++ ) {
  276. var sigma = Math.sqrt(
  277. _sigmas[ i ] * _sigmas[ i ] -
  278. _sigmas[ i - 1 ] * _sigmas[ i - 1 ] );
  279. var poleAxis =
  280. _axisDirections[ ( i - 1 ) % _axisDirections.length ];
  281. _blur( cubeUVRenderTarget, i - 1, i, sigma, poleAxis );
  282. }
  283. _renderer.autoClear = autoClear;
  284. }
  285. /**
  286. * This is a two-pass Gaussian blur for a cubemap. Normally this is done
  287. * vertically and horizontally, but this breaks down on a cube. Here we apply
  288. * the blur latitudinally (around the poles), and then longitudinally (towards
  289. * the poles) to approximate the orthogonally-separable blur. It is least
  290. * accurate at the poles, but still does a decent job.
  291. */
  292. function _blur( cubeUVRenderTarget, lodIn, lodOut, sigma, poleAxis ) {
  293. _halfBlur(
  294. cubeUVRenderTarget,
  295. _pingPongRenderTarget,
  296. lodIn,
  297. lodOut,
  298. sigma,
  299. 'latitudinal',
  300. poleAxis );
  301. _halfBlur(
  302. _pingPongRenderTarget,
  303. cubeUVRenderTarget,
  304. lodOut,
  305. lodOut,
  306. sigma,
  307. 'longitudinal',
  308. poleAxis );
  309. }
  310. function _halfBlur( targetIn, targetOut, lodIn, lodOut, sigmaRadians, direction, poleAxis ) {
  311. if ( direction !== 'latitudinal' && direction !== 'longitudinal' ) {
  312. console.error(
  313. 'blur direction must be either latitudinal or longitudinal!' );
  314. }
  315. // Number of standard deviations at which to cut off the discrete approximation.
  316. var STANDARD_DEVIATIONS = 3;
  317. var blurScene = new Scene();
  318. blurScene.add( new Mesh( _lodPlanes[ lodOut ], _blurMaterial ) );
  319. var blurUniforms = _blurMaterial.uniforms;
  320. var pixels = _sizeLods[ lodIn ] - 1;
  321. var radiansPerPixel = isFinite( sigmaRadians ) ? Math.PI / ( 2 * pixels ) : 2 * Math.PI / ( 2 * MAX_SAMPLES - 1 );
  322. var sigmaPixels = sigmaRadians / radiansPerPixel;
  323. var samples = isFinite( sigmaRadians ) ? 1 + Math.floor( STANDARD_DEVIATIONS * sigmaPixels ) : MAX_SAMPLES;
  324. if ( samples > MAX_SAMPLES ) {
  325. console.warn( `sigmaRadians, ${
  326. sigmaRadians}, is too large and will clip, as it requested ${
  327. samples} samples when the maximum is set to ${MAX_SAMPLES}` );
  328. }
  329. var weights = [];
  330. var sum = 0;
  331. for ( var i = 0; i < MAX_SAMPLES; ++ i ) {
  332. var x = i / sigmaPixels;
  333. var weight = Math.exp( - x * x / 2 );
  334. weights.push( weight );
  335. if ( i == 0 ) {
  336. sum += weight;
  337. } else if ( i < samples ) {
  338. sum += 2 * weight;
  339. }
  340. }
  341. weights = weights.map( w => w / sum );
  342. blurUniforms[ 'envMap' ].value = targetIn.texture;
  343. blurUniforms[ 'samples' ].value = samples;
  344. blurUniforms[ 'weights' ].value = weights;
  345. blurUniforms[ 'latitudinal' ].value = direction === 'latitudinal';
  346. if ( poleAxis ) {
  347. blurUniforms[ 'poleAxis' ].value = poleAxis;
  348. }
  349. blurUniforms[ 'dTheta' ].value = radiansPerPixel;
  350. blurUniforms[ 'mipInt' ].value = LOD_MAX - lodIn;
  351. blurUniforms[ 'inputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ];
  352. blurUniforms[ 'outputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ];
  353. var outputSize = _sizeLods[ lodOut ];
  354. var x = 3 * Math.max( 0, SIZE_MAX - 2 * outputSize );
  355. var y = ( lodOut === 0 ? 0 : 2 * SIZE_MAX ) +
  356. 2 * outputSize *
  357. ( lodOut > LOD_MAX - LOD_MIN ? lodOut - LOD_MAX + LOD_MIN : 0 );
  358. _renderer.setRenderTarget( targetOut );
  359. _setViewport( x, y, 3 * outputSize, 2 * outputSize );
  360. _renderer.render( blurScene, _flatCamera );
  361. }
  362. function _getBlurShader( maxSamples ) {
  363. var weights = new Float32Array( maxSamples );
  364. var poleAxis = new Vector3( 0, 1, 0 );
  365. var shaderMaterial = new RawShaderMaterial( {
  366. defines: { 'n': maxSamples },
  367. uniforms: {
  368. 'envMap': { value: null },
  369. 'samples': { value: 1 },
  370. 'weights': { value: weights },
  371. 'latitudinal': { value: false },
  372. 'dTheta': { value: 0 },
  373. 'mipInt': { value: 0 },
  374. 'poleAxis': { value: poleAxis },
  375. 'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
  376. 'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
  377. },
  378. vertexShader: _getCommonVertexShader(),
  379. fragmentShader: `
  380. precision mediump float;
  381. precision mediump int;
  382. varying vec3 vOutputDirection;
  383. uniform sampler2D envMap;
  384. uniform int samples;
  385. uniform float weights[n];
  386. uniform bool latitudinal;
  387. uniform float dTheta;
  388. uniform float mipInt;
  389. uniform vec3 poleAxis;
  390. ${_getEncodings()}
  391. #define ENVMAP_TYPE_CUBE_UV
  392. #include <cube_uv_reflection_fragment>
  393. void main() {
  394. gl_FragColor = vec4(0.0);
  395. for (int i = 0; i < n; i++) {
  396. if (i >= samples)
  397. break;
  398. for (int dir = -1; dir < 2; dir += 2) {
  399. if (i == 0 && dir == 1)
  400. continue;
  401. vec3 axis = latitudinal ? poleAxis : cross(poleAxis, vOutputDirection);
  402. if (all(equal(axis, vec3(0.0))))
  403. axis = cross(vec3(0.0, 1.0, 0.0), vOutputDirection);
  404. axis = normalize(axis);
  405. float theta = dTheta * float(dir * i);
  406. float cosTheta = cos(theta);
  407. // Rodrigues' axis-angle rotation
  408. vec3 sampleDirection = vOutputDirection * cosTheta
  409. + cross(axis, vOutputDirection) * sin(theta)
  410. + axis * dot(axis, vOutputDirection) * (1.0 - cosTheta);
  411. gl_FragColor.rgb +=
  412. weights[i] * bilinearCubeUV(envMap, sampleDirection, mipInt);
  413. }
  414. }
  415. gl_FragColor = linearToOutputTexel(gl_FragColor);
  416. }
  417. `,
  418. blending: NoBlending,
  419. depthTest: false,
  420. depthWrite: false
  421. } );
  422. shaderMaterial.type = 'SphericalGaussianBlur';
  423. return shaderMaterial;
  424. }
  425. function _getEquirectShader() {
  426. var texelSize = new Vector2( 1, 1 );
  427. var shaderMaterial = new RawShaderMaterial( {
  428. uniforms: {
  429. 'envMap': { value: null },
  430. 'texelSize': { value: texelSize },
  431. 'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
  432. 'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
  433. },
  434. vertexShader: _getCommonVertexShader(),
  435. fragmentShader: `
  436. precision mediump float;
  437. precision mediump int;
  438. varying vec3 vOutputDirection;
  439. uniform sampler2D envMap;
  440. uniform vec2 texelSize;
  441. ${_getEncodings()}
  442. #define RECIPROCAL_PI 0.31830988618
  443. #define RECIPROCAL_PI2 0.15915494
  444. void main() {
  445. gl_FragColor = vec4(0.0);
  446. vec3 outputDirection = normalize(vOutputDirection);
  447. vec2 uv;
  448. uv.y = asin(clamp(outputDirection.y, -1.0, 1.0)) * RECIPROCAL_PI + 0.5;
  449. uv.x = atan(outputDirection.z, outputDirection.x) * RECIPROCAL_PI2 + 0.5;
  450. vec2 f = fract(uv / texelSize - 0.5);
  451. uv -= f * texelSize;
  452. vec3 tl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;
  453. uv.x += texelSize.x;
  454. vec3 tr = envMapTexelToLinear(texture2D(envMap, uv)).rgb;
  455. uv.y += texelSize.y;
  456. vec3 br = envMapTexelToLinear(texture2D(envMap, uv)).rgb;
  457. uv.x -= texelSize.x;
  458. vec3 bl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;
  459. vec3 tm = mix(tl, tr, f.x);
  460. vec3 bm = mix(bl, br, f.x);
  461. gl_FragColor.rgb = mix(tm, bm, f.y);
  462. gl_FragColor = linearToOutputTexel(gl_FragColor);
  463. }
  464. `,
  465. blending: NoBlending,
  466. depthTest: false,
  467. depthWrite: false
  468. } );
  469. shaderMaterial.type = 'EquirectangularToCubeUV';
  470. return shaderMaterial;
  471. }
  472. function _getCubemapShader() {
  473. var shaderMaterial = new RawShaderMaterial( {
  474. uniforms: {
  475. 'envMap': { value: null },
  476. 'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
  477. 'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
  478. },
  479. vertexShader: _getCommonVertexShader(),
  480. fragmentShader: `
  481. precision mediump float;
  482. precision mediump int;
  483. varying vec3 vOutputDirection;
  484. uniform samplerCube envMap;
  485. ${_getEncodings()}
  486. #define RECIPROCAL_PI 0.31830988618
  487. #define RECIPROCAL_PI2 0.15915494
  488. void main() {
  489. gl_FragColor = vec4(0.0);
  490. gl_FragColor.rgb = envMapTexelToLinear(textureCube(envMap, vOutputDirection)).rgb;
  491. gl_FragColor = linearToOutputTexel(gl_FragColor);
  492. }
  493. `,
  494. blending: NoBlending,
  495. depthTest: false,
  496. depthWrite: false
  497. } );
  498. shaderMaterial.type = 'CubemapToCubeUV';
  499. return shaderMaterial;
  500. }
  501. function _getCommonVertexShader() {
  502. return `
  503. precision mediump float;
  504. precision mediump int;
  505. attribute vec3 position;
  506. attribute vec2 uv;
  507. attribute float faceIndex;
  508. varying vec3 vOutputDirection;
  509. vec3 getDirection(vec2 uv, float face) {
  510. uv = 2.0 * uv - 1.0;
  511. vec3 direction = vec3(uv, 1.0);
  512. if (face == 0.0) {
  513. direction = direction.zyx;
  514. direction.z *= -1.0;
  515. } else if (face == 1.0) {
  516. direction = direction.xzy;
  517. direction.z *= -1.0;
  518. } else if (face == 3.0) {
  519. direction = direction.zyx;
  520. direction.x *= -1.0;
  521. } else if (face == 4.0) {
  522. direction = direction.xzy;
  523. direction.y *= -1.0;
  524. } else if (face == 5.0) {
  525. direction.xz *= -1.0;
  526. }
  527. return direction;
  528. }
  529. void main() {
  530. vOutputDirection = getDirection(uv, faceIndex);
  531. gl_Position = vec4( position, 1.0 );
  532. }
  533. `;
  534. }
  535. function _getEncodings() {
  536. return `
  537. uniform int inputEncoding;
  538. uniform int outputEncoding;
  539. #include <encodings_pars_fragment>
  540. vec4 inputTexelToLinear(vec4 value){
  541. if(inputEncoding == 0){
  542. return value;
  543. }else if(inputEncoding == 1){
  544. return sRGBToLinear(value);
  545. }else if(inputEncoding == 2){
  546. return RGBEToLinear(value);
  547. }else if(inputEncoding == 3){
  548. return RGBMToLinear(value, 7.0);
  549. }else if(inputEncoding == 4){
  550. return RGBMToLinear(value, 16.0);
  551. }else if(inputEncoding == 5){
  552. return RGBDToLinear(value, 256.0);
  553. }else{
  554. return GammaToLinear(value, 2.2);
  555. }
  556. }
  557. vec4 linearToOutputTexel(vec4 value){
  558. if(outputEncoding == 0){
  559. return value;
  560. }else if(outputEncoding == 1){
  561. return LinearTosRGB(value);
  562. }else if(outputEncoding == 2){
  563. return LinearToRGBE(value);
  564. }else if(outputEncoding == 3){
  565. return LinearToRGBM(value, 7.0);
  566. }else if(outputEncoding == 4){
  567. return LinearToRGBM(value, 16.0);
  568. }else if(outputEncoding == 5){
  569. return LinearToRGBD(value, 256.0);
  570. }else{
  571. return LinearToGamma(value, 2.2);
  572. }
  573. }
  574. vec4 envMapTexelToLinear(vec4 color) {
  575. return inputTexelToLinear(color);
  576. }
  577. `;
  578. }
  579. return PMREMGenerator;
  580. } )();
  581. export { PMREMGenerator };