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.isLensflare = true;
  9. this.type = 'Lensflare';
  10. this.frustumCulled = false;
  11. this.renderOrder = Infinity; //
  12. const positionScreen = new THREE.Vector3();
  13. const positionView = new THREE.Vector3(); // textures
  14. const tempMap = new THREE.FramebufferTexture( 16, 16, THREE.RGBAFormat );
  15. const occlusionMap = new THREE.FramebufferTexture( 16, 16, THREE.RGBAFormat ); // material
  16. const geometry = Lensflare.Geometry;
  17. const material1a = new THREE.RawShaderMaterial( {
  18. uniforms: {
  19. 'scale': {
  20. value: null
  21. },
  22. 'screenPosition': {
  23. value: null
  24. }
  25. },
  26. vertexShader:
  27. /* glsl */
  28. `
  29. precision highp float;
  30. uniform vec3 screenPosition;
  31. uniform vec2 scale;
  32. attribute vec3 position;
  33. void main() {
  34. gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
  35. }`,
  36. fragmentShader:
  37. /* glsl */
  38. `
  39. precision highp float;
  40. void main() {
  41. gl_FragColor = vec4( 1.0, 0.0, 1.0, 1.0 );
  42. }`,
  43. depthTest: true,
  44. depthWrite: false,
  45. transparent: false
  46. } );
  47. const material1b = new THREE.RawShaderMaterial( {
  48. uniforms: {
  49. 'map': {
  50. value: tempMap
  51. },
  52. 'scale': {
  53. value: null
  54. },
  55. 'screenPosition': {
  56. value: null
  57. }
  58. },
  59. vertexShader:
  60. /* glsl */
  61. `
  62. precision highp float;
  63. uniform vec3 screenPosition;
  64. uniform vec2 scale;
  65. attribute vec3 position;
  66. attribute vec2 uv;
  67. varying vec2 vUV;
  68. void main() {
  69. vUV = uv;
  70. gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
  71. }`,
  72. fragmentShader:
  73. /* glsl */
  74. `
  75. precision highp float;
  76. uniform sampler2D map;
  77. varying vec2 vUV;
  78. void main() {
  79. gl_FragColor = texture2D( map, vUV );
  80. }`,
  81. depthTest: false,
  82. depthWrite: false,
  83. transparent: false
  84. } ); // the following object is used for occlusionMap generation
  85. const mesh1 = new THREE.Mesh( geometry, material1a ); //
  86. const elements = [];
  87. const shader = LensflareElement.Shader;
  88. const material2 = new THREE.RawShaderMaterial( {
  89. uniforms: {
  90. 'map': {
  91. value: null
  92. },
  93. 'occlusionMap': {
  94. value: occlusionMap
  95. },
  96. 'color': {
  97. value: new THREE.Color( 0xffffff )
  98. },
  99. 'scale': {
  100. value: new THREE.Vector2()
  101. },
  102. 'screenPosition': {
  103. value: new THREE.Vector3()
  104. }
  105. },
  106. vertexShader: shader.vertexShader,
  107. fragmentShader: shader.fragmentShader,
  108. blending: THREE.AdditiveBlending,
  109. transparent: true,
  110. depthWrite: false
  111. } );
  112. const mesh2 = new THREE.Mesh( geometry, material2 );
  113. this.addElement = function ( element ) {
  114. elements.push( element );
  115. }; //
  116. const scale = new THREE.Vector2();
  117. const screenPositionPixels = new THREE.Vector2();
  118. const validArea = new THREE.Box2();
  119. const viewport = new THREE.Vector4();
  120. this.onBeforeRender = function ( renderer, scene, camera ) {
  121. renderer.getCurrentViewport( viewport );
  122. const invAspect = viewport.w / viewport.z;
  123. const halfViewportWidth = viewport.z / 2.0;
  124. const halfViewportHeight = viewport.w / 2.0;
  125. let size = 16 / viewport.w;
  126. scale.set( size * invAspect, size );
  127. validArea.min.set( viewport.x, viewport.y );
  128. validArea.max.set( viewport.x + ( viewport.z - 16 ), viewport.y + ( viewport.w - 16 ) ); // calculate position in screen space
  129. positionView.setFromMatrixPosition( this.matrixWorld );
  130. positionView.applyMatrix4( camera.matrixWorldInverse );
  131. if ( positionView.z > 0 ) return; // lensflare is behind the camera
  132. positionScreen.copy( positionView ).applyMatrix4( camera.projectionMatrix ); // horizontal and vertical coordinate of the lower left corner of the pixels to copy
  133. screenPositionPixels.x = viewport.x + positionScreen.x * halfViewportWidth + halfViewportWidth - 8;
  134. screenPositionPixels.y = viewport.y + positionScreen.y * halfViewportHeight + halfViewportHeight - 8; // screen cull
  135. if ( validArea.containsPoint( screenPositionPixels ) ) {
  136. // save current RGB to temp texture
  137. renderer.copyFramebufferToTexture( screenPositionPixels, tempMap ); // render pink quad
  138. let uniforms = material1a.uniforms;
  139. uniforms[ 'scale' ].value = scale;
  140. uniforms[ 'screenPosition' ].value = positionScreen;
  141. renderer.renderBufferDirect( camera, null, geometry, material1a, mesh1, null ); // copy result to occlusionMap
  142. renderer.copyFramebufferToTexture( screenPositionPixels, occlusionMap ); // restore graphics
  143. uniforms = material1b.uniforms;
  144. uniforms[ 'scale' ].value = scale;
  145. uniforms[ 'screenPosition' ].value = positionScreen;
  146. renderer.renderBufferDirect( camera, null, geometry, material1b, mesh1, null ); // render elements
  147. const vecX = - positionScreen.x * 2;
  148. const vecY = - positionScreen.y * 2;
  149. for ( let i = 0, l = elements.length; i < l; i ++ ) {
  150. const element = elements[ i ];
  151. const uniforms = material2.uniforms;
  152. uniforms[ 'color' ].value.copy( element.color );
  153. uniforms[ 'map' ].value = element.texture;
  154. uniforms[ 'screenPosition' ].value.x = positionScreen.x + vecX * element.distance;
  155. uniforms[ 'screenPosition' ].value.y = positionScreen.y + vecY * element.distance;
  156. size = element.size / viewport.w;
  157. const invAspect = viewport.w / viewport.z;
  158. uniforms[ 'scale' ].value.set( size * invAspect, size );
  159. material2.uniformsNeedUpdate = true;
  160. renderer.renderBufferDirect( camera, null, geometry, material2, mesh2, null );
  161. }
  162. }
  163. };
  164. this.dispose = function () {
  165. material1a.dispose();
  166. material1b.dispose();
  167. material2.dispose();
  168. tempMap.dispose();
  169. occlusionMap.dispose();
  170. for ( let i = 0, l = elements.length; i < l; i ++ ) {
  171. elements[ i ].texture.dispose();
  172. }
  173. };
  174. }
  175. } //
  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. } )();