PMREMGenerator.js 19 KB

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