Lensflare.js 8.5 KB

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