Lensflare.js 8.2 KB

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