Lensflare.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. ( function () {
  2. class Lensflare extends THREE.Mesh {
  3. constructor() {
  4. super( Lensflare.Geometry, new THREE.MeshBasicMaterial( {
  5. opacity: 0,
  6. transparent: true
  7. } ) );
  8. this.type = 'Lensflare';
  9. this.frustumCulled = false;
  10. this.renderOrder = Infinity; //
  11. const positionScreen = new THREE.Vector3();
  12. const positionView = new THREE.Vector3(); // textures
  13. const tempMap = new THREE.FramebufferTexture( 16, 16, THREE.RGBFormat );
  14. const occlusionMap = new THREE.FramebufferTexture( 16, 16, THREE.RGBFormat ); // material
  15. const geometry = Lensflare.Geometry;
  16. const material1a = new THREE.RawShaderMaterial( {
  17. uniforms: {
  18. 'scale': {
  19. value: null
  20. },
  21. 'screenPosition': {
  22. value: null
  23. }
  24. },
  25. vertexShader:
  26. /* glsl */
  27. `
  28. precision highp float;
  29. uniform vec3 screenPosition;
  30. uniform vec2 scale;
  31. attribute vec3 position;
  32. void main() {
  33. gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
  34. }`,
  35. fragmentShader:
  36. /* glsl */
  37. `
  38. precision highp float;
  39. void main() {
  40. gl_FragColor = vec4( 1.0, 0.0, 1.0, 1.0 );
  41. }`,
  42. depthTest: true,
  43. depthWrite: false,
  44. transparent: false
  45. } );
  46. const material1b = new THREE.RawShaderMaterial( {
  47. uniforms: {
  48. 'map': {
  49. value: tempMap
  50. },
  51. 'scale': {
  52. value: null
  53. },
  54. 'screenPosition': {
  55. value: null
  56. }
  57. },
  58. vertexShader:
  59. /* glsl */
  60. `
  61. precision highp float;
  62. uniform vec3 screenPosition;
  63. uniform vec2 scale;
  64. attribute vec3 position;
  65. attribute vec2 uv;
  66. varying vec2 vUV;
  67. void main() {
  68. vUV = uv;
  69. gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
  70. }`,
  71. fragmentShader:
  72. /* glsl */
  73. `
  74. precision highp float;
  75. uniform sampler2D map;
  76. varying vec2 vUV;
  77. void main() {
  78. gl_FragColor = texture2D( map, vUV );
  79. }`,
  80. depthTest: false,
  81. depthWrite: false,
  82. transparent: false
  83. } ); // the following object is used for occlusionMap generation
  84. const mesh1 = new THREE.Mesh( geometry, material1a ); //
  85. const elements = [];
  86. const shader = LensflareElement.Shader;
  87. const material2 = new THREE.RawShaderMaterial( {
  88. uniforms: {
  89. 'map': {
  90. value: null
  91. },
  92. 'occlusionMap': {
  93. value: occlusionMap
  94. },
  95. 'color': {
  96. value: new THREE.Color( 0xffffff )
  97. },
  98. 'scale': {
  99. value: new THREE.Vector2()
  100. },
  101. 'screenPosition': {
  102. value: new THREE.Vector3()
  103. }
  104. },
  105. vertexShader: shader.vertexShader,
  106. fragmentShader: shader.fragmentShader,
  107. blending: THREE.AdditiveBlending,
  108. transparent: true,
  109. depthWrite: false
  110. } );
  111. const mesh2 = new THREE.Mesh( geometry, material2 );
  112. this.addElement = function ( element ) {
  113. elements.push( element );
  114. }; //
  115. const scale = new THREE.Vector2();
  116. const screenPositionPixels = new THREE.Vector2();
  117. const validArea = new THREE.Box2();
  118. const viewport = new THREE.Vector4();
  119. this.onBeforeRender = function ( renderer, scene, camera ) {
  120. renderer.getCurrentViewport( viewport );
  121. const invAspect = viewport.w / viewport.z;
  122. const halfViewportWidth = viewport.z / 2.0;
  123. const halfViewportHeight = viewport.w / 2.0;
  124. let size = 16 / viewport.w;
  125. scale.set( size * invAspect, size );
  126. validArea.min.set( viewport.x, viewport.y );
  127. validArea.max.set( viewport.x + ( viewport.z - 16 ), viewport.y + ( viewport.w - 16 ) ); // calculate position in screen space
  128. positionView.setFromMatrixPosition( this.matrixWorld );
  129. positionView.applyMatrix4( camera.matrixWorldInverse );
  130. if ( positionView.z > 0 ) return; // lensflare is behind the camera
  131. positionScreen.copy( positionView ).applyMatrix4( camera.projectionMatrix ); // horizontal and vertical coordinate of the lower left corner of the pixels to copy
  132. screenPositionPixels.x = viewport.x + positionScreen.x * halfViewportWidth + halfViewportWidth - 8;
  133. screenPositionPixels.y = viewport.y + positionScreen.y * halfViewportHeight + halfViewportHeight - 8; // screen cull
  134. if ( validArea.containsPoint( screenPositionPixels ) ) {
  135. // save current RGB to temp texture
  136. renderer.copyFramebufferToTexture( screenPositionPixels, tempMap ); // render pink quad
  137. let uniforms = material1a.uniforms;
  138. uniforms[ 'scale' ].value = scale;
  139. uniforms[ 'screenPosition' ].value = positionScreen;
  140. renderer.renderBufferDirect( camera, null, geometry, material1a, mesh1, null ); // copy result to occlusionMap
  141. renderer.copyFramebufferToTexture( screenPositionPixels, occlusionMap ); // restore graphics
  142. uniforms = material1b.uniforms;
  143. uniforms[ 'scale' ].value = scale;
  144. uniforms[ 'screenPosition' ].value = positionScreen;
  145. renderer.renderBufferDirect( camera, null, geometry, material1b, mesh1, null ); // render elements
  146. const vecX = - positionScreen.x * 2;
  147. const vecY = - positionScreen.y * 2;
  148. for ( let i = 0, l = elements.length; i < l; i ++ ) {
  149. const element = elements[ i ];
  150. const uniforms = material2.uniforms;
  151. uniforms[ 'color' ].value.copy( element.color );
  152. uniforms[ 'map' ].value = element.texture;
  153. uniforms[ 'screenPosition' ].value.x = positionScreen.x + vecX * element.distance;
  154. uniforms[ 'screenPosition' ].value.y = positionScreen.y + vecY * element.distance;
  155. size = element.size / viewport.w;
  156. const invAspect = viewport.w / viewport.z;
  157. uniforms[ 'scale' ].value.set( size * invAspect, size );
  158. material2.uniformsNeedUpdate = true;
  159. renderer.renderBufferDirect( camera, null, geometry, material2, mesh2, null );
  160. }
  161. }
  162. };
  163. this.dispose = function () {
  164. material1a.dispose();
  165. material1b.dispose();
  166. material2.dispose();
  167. tempMap.dispose();
  168. occlusionMap.dispose();
  169. for ( let i = 0, l = elements.length; i < l; i ++ ) {
  170. elements[ i ].texture.dispose();
  171. }
  172. };
  173. }
  174. }
  175. Lensflare.prototype.isLensflare = true; //
  176. class LensflareElement {
  177. constructor( texture, size = 1, distance = 0, color = new THREE.Color( 0xffffff ) ) {
  178. this.texture = texture;
  179. this.size = size;
  180. this.distance = distance;
  181. this.color = color;
  182. }
  183. }
  184. LensflareElement.Shader = {
  185. uniforms: {
  186. 'map': {
  187. value: null
  188. },
  189. 'occlusionMap': {
  190. value: null
  191. },
  192. 'color': {
  193. value: null
  194. },
  195. 'scale': {
  196. value: null
  197. },
  198. 'screenPosition': {
  199. value: null
  200. }
  201. },
  202. vertexShader:
  203. /* glsl */
  204. `
  205. precision highp float;
  206. uniform vec3 screenPosition;
  207. uniform vec2 scale;
  208. uniform sampler2D occlusionMap;
  209. attribute vec3 position;
  210. attribute vec2 uv;
  211. varying vec2 vUV;
  212. varying float vVisibility;
  213. void main() {
  214. vUV = uv;
  215. vec2 pos = position.xy;
  216. vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) );
  217. visibility += texture2D( occlusionMap, vec2( 0.5, 0.1 ) );
  218. visibility += texture2D( occlusionMap, vec2( 0.9, 0.1 ) );
  219. visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) );
  220. visibility += texture2D( occlusionMap, vec2( 0.9, 0.9 ) );
  221. visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) );
  222. visibility += texture2D( occlusionMap, vec2( 0.1, 0.9 ) );
  223. visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) );
  224. visibility += texture2D( occlusionMap, vec2( 0.5, 0.5 ) );
  225. vVisibility = visibility.r / 9.0;
  226. vVisibility *= 1.0 - visibility.g / 9.0;
  227. vVisibility *= visibility.b / 9.0;
  228. gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );
  229. }`,
  230. fragmentShader:
  231. /* glsl */
  232. `
  233. precision highp float;
  234. uniform sampler2D map;
  235. uniform vec3 color;
  236. varying vec2 vUV;
  237. varying float vVisibility;
  238. void main() {
  239. vec4 texture = texture2D( map, vUV );
  240. texture.a *= vVisibility;
  241. gl_FragColor = texture;
  242. gl_FragColor.rgb *= color;
  243. }`
  244. };
  245. Lensflare.Geometry = function () {
  246. const geometry = new THREE.BufferGeometry();
  247. const float32Array = new Float32Array( [ - 1, - 1, 0, 0, 0, 1, - 1, 0, 1, 0, 1, 1, 0, 1, 1, - 1, 1, 0, 0, 1 ] );
  248. const interleavedBuffer = new THREE.InterleavedBuffer( float32Array, 5 );
  249. geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
  250. geometry.setAttribute( 'position', new THREE.InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
  251. geometry.setAttribute( 'uv', new THREE.InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
  252. return geometry;
  253. }();
  254. THREE.Lensflare = Lensflare;
  255. THREE.LensflareElement = LensflareElement;
  256. } )();