PMREMGenerator.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. _equirectangularToCubeUV( equirectangular, cubeUVRenderTarget );
  115. _applyPMREM( cubeUVRenderTarget );
  116. _cleanup();
  117. return cubeUVRenderTarget;
  118. },
  119. };
  120. function _createPlanes() {
  121. var _lodPlanes = [];
  122. var _sizeLods = [];
  123. var _sigmas = [];
  124. var lod = LOD_MAX;
  125. for ( var i = 0; i < TOTAL_LODS; i ++ ) {
  126. var sizeLod = Math.pow( 2, lod );
  127. _sizeLods.push( sizeLod );
  128. var sigma = 1.0 / sizeLod;
  129. if ( i > LOD_MAX - LOD_MIN ) {
  130. sigma = EXTRA_LOD_SIGMA[ i - LOD_MAX + LOD_MIN - 1 ];
  131. } else if ( i == 0 ) {
  132. sigma = 0;
  133. }
  134. _sigmas.push( sigma );
  135. var texelSize = 1.0 / ( sizeLod - 1 );
  136. var min = - texelSize / 2;
  137. var max = 1 + texelSize / 2;
  138. var uv1 = [ min, min, max, min, max, max, min, min, max, max, min, max ];
  139. var cubeFaces = 6;
  140. var vertices = 6;
  141. var positionSize = 3;
  142. var uvSize = 2;
  143. var faceIndexSize = 1;
  144. var position = new Float32Array( positionSize * vertices * cubeFaces );
  145. var uv = new Float32Array( uvSize * vertices * cubeFaces );
  146. var faceIndex = new Float32Array( faceIndexSize * vertices * cubeFaces );
  147. for ( var face = 0; face < cubeFaces; face ++ ) {
  148. var x = ( face % 3 ) * 2 / 3 - 1;
  149. var y = face > 2 ? 0 : - 1;
  150. var coordinates = [
  151. [ x, y, 0 ],
  152. [ x + 2 / 3, y, 0 ],
  153. [ x + 2 / 3, y + 1, 0 ],
  154. [ x, y, 0 ],
  155. [ x + 2 / 3, y + 1, 0 ],
  156. [ x, y + 1, 0 ]
  157. ];
  158. position.set( [].concat( ...coordinates ),
  159. positionSize * vertices * face );
  160. uv.set( uv1, uvSize * vertices * face );
  161. var fill = [ face, face, face, face, face, face ];
  162. faceIndex.set( fill, faceIndexSize * vertices * face );
  163. }
  164. var planes = new BufferGeometry();
  165. planes.setAttribute(
  166. 'position', new BufferAttribute( position, positionSize ) );
  167. planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) );
  168. planes.setAttribute(
  169. 'faceIndex', new BufferAttribute( faceIndex, faceIndexSize ) );
  170. _lodPlanes.push( planes );
  171. if ( lod > LOD_MIN ) {
  172. lod --;
  173. }
  174. }
  175. return { _lodPlanes, _sizeLods, _sigmas };
  176. }
  177. function _allocateTargets( equirectangular ) {
  178. var params = {
  179. magFilter: NearestFilter,
  180. minFilter: NearestFilter,
  181. generateMipmaps: false,
  182. type: equirectangular ? equirectangular.type : UnsignedByteType,
  183. format: equirectangular ? equirectangular.format : RGBEFormat,
  184. encoding: equirectangular ? equirectangular.encoding : RGBEEncoding,
  185. depthBuffer: false,
  186. stencilBuffer: false
  187. };
  188. var cubeUVRenderTarget = _createRenderTarget(
  189. { ...params, depthBuffer: ( equirectangular ? false : true ) } );
  190. _pingPongRenderTarget = _createRenderTarget( params );
  191. return cubeUVRenderTarget;
  192. }
  193. function _cleanup() {
  194. _pingPongRenderTarget.dispose();
  195. _renderer.setRenderTarget( null );
  196. var size = _renderer.getSize( new Vector2() );
  197. _renderer.setViewport( 0, 0, size.x, size.y );
  198. }
  199. function _sceneToCubeUV(
  200. scene, near, far,
  201. cubeUVRenderTarget ) {
  202. var fov = 90;
  203. var aspect = 1;
  204. var cubeCamera = new PerspectiveCamera( fov, aspect, near, far );
  205. var upSign = [ 1, 1, 1, 1, - 1, 1 ];
  206. var forwardSign = [ 1, 1, - 1, - 1, - 1, 1 ];
  207. var gammaOutput = _renderer.gammaOutput;
  208. var toneMapping = _renderer.toneMapping;
  209. var toneMappingExposure = _renderer.toneMappingExposure;
  210. _renderer.toneMapping = LinearToneMapping;
  211. _renderer.toneMappingExposure = 1.0;
  212. _renderer.gammaOutput = false;
  213. scene.scale.z *= - 1;
  214. _renderer.setRenderTarget( cubeUVRenderTarget );
  215. for ( var i = 0; i < 6; i ++ ) {
  216. var col = i % 3;
  217. if ( col == 0 ) {
  218. cubeCamera.up.set( 0, upSign[ i ], 0 );
  219. cubeCamera.lookAt( forwardSign[ i ], 0, 0 );
  220. } else if ( col == 1 ) {
  221. cubeCamera.up.set( 0, 0, upSign[ i ] );
  222. cubeCamera.lookAt( 0, forwardSign[ i ], 0 );
  223. } else {
  224. cubeCamera.up.set( 0, upSign[ i ], 0 );
  225. cubeCamera.lookAt( 0, 0, forwardSign[ i ] );
  226. }
  227. _setViewport(
  228. col * SIZE_MAX, i > 2 ? SIZE_MAX : 0, SIZE_MAX, SIZE_MAX );
  229. _renderer.render( scene, cubeCamera );
  230. }
  231. _renderer.toneMapping = toneMapping;
  232. _renderer.toneMappingExposure = toneMappingExposure;
  233. _renderer.gammaOutput = gammaOutput;
  234. scene.scale.z *= - 1;
  235. }
  236. function _equirectangularToCubeUV(
  237. equirectangular, cubeUVRenderTarget ) {
  238. var scene = new Scene();
  239. var equirectMaterial = _getEquirectShader();
  240. scene.add( new Mesh( _lodPlanes[ 0 ], equirectMaterial ) );
  241. var uniforms = equirectMaterial.uniforms;
  242. uniforms[ 'envMap' ].value = equirectangular;
  243. uniforms[ 'texelSize' ].value.set(
  244. 1.0 / equirectangular.image.width, 1.0 / equirectangular.image.height );
  245. uniforms[ 'inputEncoding' ].value = ENCODINGS[ equirectangular.encoding ];
  246. uniforms[ 'outputEncoding' ].value = ENCODINGS[ equirectangular.encoding ];
  247. _renderer.setRenderTarget( cubeUVRenderTarget );
  248. _setViewport( 0, 0, 3 * SIZE_MAX, 2 * SIZE_MAX );
  249. _renderer.render( scene, _flatCamera );
  250. }
  251. function _createRenderTarget( params ) {
  252. var cubeUVRenderTarget =
  253. new WebGLRenderTarget( 3 * SIZE_MAX, 3 * SIZE_MAX, params );
  254. cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping;
  255. cubeUVRenderTarget.texture.name = 'PMREM.cubeUv';
  256. return cubeUVRenderTarget;
  257. }
  258. function _setViewport( x, y, width, height ) {
  259. var dpr = _renderer.getPixelRatio();
  260. _renderer.setViewport( x / dpr, y / dpr, width / dpr, height / dpr );
  261. }
  262. function _applyPMREM( cubeUVRenderTarget ) {
  263. var autoClear = _renderer.autoClear;
  264. _renderer.autoClear = false;
  265. for ( var i = 1; i < TOTAL_LODS; i ++ ) {
  266. var sigma = Math.sqrt(
  267. _sigmas[ i ] * _sigmas[ i ] -
  268. _sigmas[ i - 1 ] * _sigmas[ i - 1 ] );
  269. var poleAxis =
  270. _axisDirections[ ( i - 1 ) % _axisDirections.length ];
  271. _blur( cubeUVRenderTarget, i - 1, i, sigma, poleAxis );
  272. }
  273. _renderer.autoClear = autoClear;
  274. }
  275. /**
  276. * This is a two-pass Gaussian blur for a cubemap. Normally this is done
  277. * vertically and horizontally, but this breaks down on a cube. Here we apply
  278. * the blur latitudinally (around the poles), and then longitudinally (towards
  279. * the poles) to approximate the orthogonally-separable blur. It is least
  280. * accurate at the poles, but still does a decent job.
  281. */
  282. function _blur(
  283. cubeUVRenderTarget, lodIn, lodOut,
  284. sigma, poleAxis ) {
  285. _halfBlur(
  286. cubeUVRenderTarget,
  287. _pingPongRenderTarget,
  288. lodIn,
  289. lodOut,
  290. sigma,
  291. 'latitudinal',
  292. poleAxis );
  293. _halfBlur(
  294. _pingPongRenderTarget,
  295. cubeUVRenderTarget,
  296. lodOut,
  297. lodOut,
  298. sigma,
  299. 'longitudinal',
  300. poleAxis );
  301. }
  302. function _halfBlur(
  303. targetIn, targetOut, lodIn,
  304. lodOut, sigmaRadians, direction,
  305. poleAxis ) {
  306. if ( direction !== 'latitudinal' && direction !== 'longitudinal' ) {
  307. console.error(
  308. 'blur direction must be either latitudinal or longitudinal!' );
  309. }
  310. // Number of standard deviations at which to cut off the discrete approximation.
  311. var STANDARD_DEVIATIONS = 3;
  312. var blurScene = new Scene();
  313. blurScene.add( new Mesh( _lodPlanes[ lodOut ], _blurMaterial ) );
  314. var blurUniforms = _blurMaterial.uniforms;
  315. var pixels = _sizeLods[ lodIn ] - 1;
  316. var radiansPerPixel = isFinite( sigmaRadians ) ? Math.PI / ( 2 * pixels ) : 2 * Math.PI / ( 2 * MAX_SAMPLES - 1 );
  317. var sigmaPixels = sigmaRadians / radiansPerPixel;
  318. var samples = isFinite( sigmaRadians ) ? 1 + Math.floor( STANDARD_DEVIATIONS * sigmaPixels ) : MAX_SAMPLES;
  319. if ( samples > MAX_SAMPLES ) {
  320. console.warn( `sigmaRadians, ${
  321. sigmaRadians}, is too large and will clip, as it requested ${
  322. samples} samples when the maximum is set to ${MAX_SAMPLES}` );
  323. }
  324. var weights = [];
  325. var sum = 0;
  326. for ( var i = 0; i < MAX_SAMPLES; ++ i ) {
  327. var x = i / sigmaPixels;
  328. var weight = Math.exp( - x * x / 2 );
  329. weights.push( weight );
  330. if ( i == 0 ) {
  331. sum += weight;
  332. } else if ( i < samples ) {
  333. sum += 2 * weight;
  334. }
  335. }
  336. weights = weights.map( w => w / sum );
  337. blurUniforms[ 'envMap' ].value = targetIn.texture;
  338. blurUniforms[ 'samples' ].value = samples;
  339. blurUniforms[ 'weights' ].value = weights;
  340. blurUniforms[ 'latitudinal' ].value = direction === 'latitudinal';
  341. if ( poleAxis ) {
  342. blurUniforms[ 'poleAxis' ].value = poleAxis;
  343. }
  344. blurUniforms[ 'dTheta' ].value = radiansPerPixel;
  345. blurUniforms[ 'mipInt' ].value = LOD_MAX - lodIn;
  346. blurUniforms[ 'inputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ];
  347. blurUniforms[ 'outputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ];
  348. var outputSize = _sizeLods[ lodOut ];
  349. var x = 3 * Math.max( 0, SIZE_MAX - 2 * outputSize );
  350. var y = ( lodOut === 0 ? 0 : 2 * SIZE_MAX ) +
  351. 2 * outputSize *
  352. ( lodOut > LOD_MAX - LOD_MIN ? lodOut - LOD_MAX + LOD_MIN : 0 );
  353. _renderer.setRenderTarget( targetOut );
  354. _setViewport( x, y, 3 * outputSize, 2 * outputSize );
  355. _renderer.render( blurScene, _flatCamera );
  356. }
  357. function _getBlurShader( maxSamples ) {
  358. var weights = new Float32Array( maxSamples );
  359. var poleAxis = new Vector3( 0, 1, 0 );
  360. var shaderMaterial = new RawShaderMaterial( {
  361. defines: { 'n': maxSamples },
  362. uniforms: {
  363. 'envMap': { value: null },
  364. 'samples': { value: 1 },
  365. 'weights': { value: weights },
  366. 'latitudinal': { value: false },
  367. 'dTheta': { value: 0 },
  368. 'mipInt': { value: 0 },
  369. 'poleAxis': { value: poleAxis },
  370. 'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
  371. 'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
  372. },
  373. vertexShader: _getCommonVertexShader(),
  374. fragmentShader: `
  375. precision mediump float;
  376. precision mediump int;
  377. varying vec3 vOutputDirection;
  378. uniform sampler2D envMap;
  379. uniform int samples;
  380. uniform float weights[n];
  381. uniform bool latitudinal;
  382. uniform float dTheta;
  383. uniform float mipInt;
  384. uniform vec3 poleAxis;
  385. ${_getEncodings()}
  386. #define ENVMAP_TYPE_CUBE_UV
  387. #include <cube_uv_reflection_fragment>
  388. void main() {
  389. gl_FragColor = vec4(0.0);
  390. for (int i = 0; i < n; i++) {
  391. if (i >= samples)
  392. break;
  393. for (int dir = -1; dir < 2; dir += 2) {
  394. if (i == 0 && dir == 1)
  395. continue;
  396. vec3 axis = latitudinal ? poleAxis : cross(poleAxis, vOutputDirection);
  397. if (all(equal(axis, vec3(0.0))))
  398. axis = cross(vec3(0.0, 1.0, 0.0), vOutputDirection);
  399. axis = normalize(axis);
  400. float theta = dTheta * float(dir * i);
  401. float cosTheta = cos(theta);
  402. // Rodrigues' axis-angle rotation
  403. vec3 sampleDirection = vOutputDirection * cosTheta
  404. + cross(axis, vOutputDirection) * sin(theta)
  405. + axis * dot(axis, vOutputDirection) * (1.0 - cosTheta);
  406. gl_FragColor.rgb +=
  407. weights[i] * bilinearCubeUV(envMap, sampleDirection, mipInt);
  408. }
  409. }
  410. gl_FragColor = linearToOutputTexel(gl_FragColor);
  411. }
  412. `,
  413. blending: NoBlending,
  414. depthTest: false,
  415. depthWrite: false
  416. } );
  417. shaderMaterial.type = 'SphericalGaussianBlur';
  418. return shaderMaterial;
  419. }
  420. function _getEquirectShader() {
  421. var texelSize = new Vector2( 1, 1 );
  422. var shaderMaterial = new RawShaderMaterial( {
  423. uniforms: {
  424. 'envMap': { value: null },
  425. 'texelSize': { value: texelSize },
  426. 'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
  427. 'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
  428. },
  429. vertexShader: _getCommonVertexShader(),
  430. fragmentShader: `
  431. precision mediump float;
  432. precision mediump int;
  433. varying vec3 vOutputDirection;
  434. uniform sampler2D envMap;
  435. uniform vec2 texelSize;
  436. ${_getEncodings()}
  437. #define RECIPROCAL_PI 0.31830988618
  438. #define RECIPROCAL_PI2 0.15915494
  439. void main() {
  440. gl_FragColor = vec4(0.0);
  441. vec3 outputDirection = normalize(vOutputDirection);
  442. vec2 uv;
  443. uv.y = asin(clamp(outputDirection.y, -1.0, 1.0)) * RECIPROCAL_PI + 0.5;
  444. uv.x = atan(outputDirection.z, outputDirection.x) * RECIPROCAL_PI2 + 0.5;
  445. vec2 f = fract(uv / texelSize - 0.5);
  446. uv -= f * texelSize;
  447. vec3 tl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;
  448. uv.x += texelSize.x;
  449. vec3 tr = envMapTexelToLinear(texture2D(envMap, uv)).rgb;
  450. uv.y += texelSize.y;
  451. vec3 br = envMapTexelToLinear(texture2D(envMap, uv)).rgb;
  452. uv.x -= texelSize.x;
  453. vec3 bl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;
  454. vec3 tm = mix(tl, tr, f.x);
  455. vec3 bm = mix(bl, br, f.x);
  456. gl_FragColor.rgb = mix(tm, bm, f.y);
  457. gl_FragColor = linearToOutputTexel(gl_FragColor);
  458. }
  459. `,
  460. blending: NoBlending,
  461. depthTest: false,
  462. depthWrite: false
  463. } );
  464. shaderMaterial.type = 'EquirectangularToCubeUV';
  465. return shaderMaterial;
  466. }
  467. function _getCommonVertexShader() {
  468. return `
  469. precision mediump float;
  470. precision mediump int;
  471. attribute vec3 position;
  472. attribute vec2 uv;
  473. attribute float faceIndex;
  474. varying vec3 vOutputDirection;
  475. vec3 getDirection(vec2 uv, float face) {
  476. uv = 2.0 * uv - 1.0;
  477. vec3 direction = vec3(uv, 1.0);
  478. if (face == 0.0) {
  479. direction = direction.zyx;
  480. direction.z *= -1.0;
  481. } else if (face == 1.0) {
  482. direction = direction.xzy;
  483. direction.z *= -1.0;
  484. } else if (face == 3.0) {
  485. direction = direction.zyx;
  486. direction.x *= -1.0;
  487. } else if (face == 4.0) {
  488. direction = direction.xzy;
  489. direction.y *= -1.0;
  490. } else if (face == 5.0) {
  491. direction.xz *= -1.0;
  492. }
  493. return direction;
  494. }
  495. void main() {
  496. vOutputDirection = getDirection(uv, faceIndex);
  497. gl_Position = vec4( position, 1.0 );
  498. }
  499. `;
  500. }
  501. function _getEncodings() {
  502. return `
  503. uniform int inputEncoding;
  504. uniform int outputEncoding;
  505. #include <encodings_pars_fragment>
  506. vec4 inputTexelToLinear(vec4 value){
  507. if(inputEncoding == 0){
  508. return value;
  509. }else if(inputEncoding == 1){
  510. return sRGBToLinear(value);
  511. }else if(inputEncoding == 2){
  512. return RGBEToLinear(value);
  513. }else if(inputEncoding == 3){
  514. return RGBMToLinear(value, 7.0);
  515. }else if(inputEncoding == 4){
  516. return RGBMToLinear(value, 16.0);
  517. }else if(inputEncoding == 5){
  518. return RGBDToLinear(value, 256.0);
  519. }else{
  520. return GammaToLinear(value, 2.2);
  521. }
  522. }
  523. vec4 linearToOutputTexel(vec4 value){
  524. if(outputEncoding == 0){
  525. return value;
  526. }else if(outputEncoding == 1){
  527. return LinearTosRGB(value);
  528. }else if(outputEncoding == 2){
  529. return LinearToRGBE(value);
  530. }else if(outputEncoding == 3){
  531. return LinearToRGBM(value, 7.0);
  532. }else if(outputEncoding == 4){
  533. return LinearToRGBM(value, 16.0);
  534. }else if(outputEncoding == 5){
  535. return LinearToRGBD(value, 256.0);
  536. }else{
  537. return LinearToGamma(value, 2.2);
  538. }
  539. }
  540. vec4 envMapTexelToLinear(vec4 color) {
  541. return inputTexelToLinear(color);
  542. }
  543. `;
  544. }
  545. return PMREMGenerator;
  546. } )();
  547. export { PMREMGenerator };