PMREMGenerator.js 19 KB

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