Lensflare.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. name: shader.name,
  90. uniforms: {
  91. 'map': { value: null },
  92. 'occlusionMap': { value: occlusionMap },
  93. 'color': { value: new Color( 0xffffff ) },
  94. 'scale': { value: new Vector2() },
  95. 'screenPosition': { value: new Vector3() }
  96. },
  97. vertexShader: shader.vertexShader,
  98. fragmentShader: shader.fragmentShader,
  99. blending: AdditiveBlending,
  100. transparent: true,
  101. depthWrite: false
  102. } );
  103. const mesh2 = new Mesh( geometry, material2 );
  104. this.addElement = function ( element ) {
  105. elements.push( element );
  106. };
  107. //
  108. const scale = new Vector2();
  109. const screenPositionPixels = new Vector2();
  110. const validArea = new Box2();
  111. const viewport = new Vector4();
  112. this.onBeforeRender = function ( renderer, scene, camera ) {
  113. renderer.getCurrentViewport( viewport );
  114. const renderTarget = renderer.getRenderTarget();
  115. const type = ( renderTarget !== null ) ? renderTarget.texture.type : UnsignedByteType;
  116. if ( currentType !== type ) {
  117. tempMap.dispose();
  118. occlusionMap.dispose();
  119. tempMap.type = occlusionMap.type = type;
  120. currentType = type;
  121. }
  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 ) );
  129. // calculate position in screen space
  130. positionView.setFromMatrixPosition( this.matrixWorld );
  131. positionView.applyMatrix4( camera.matrixWorldInverse );
  132. if ( positionView.z > 0 ) return; // lensflare is behind the camera
  133. positionScreen.copy( positionView ).applyMatrix4( camera.projectionMatrix );
  134. // horizontal and vertical coordinate of the lower left corner of the pixels to copy
  135. screenPositionPixels.x = viewport.x + ( positionScreen.x * halfViewportWidth ) + halfViewportWidth - 8;
  136. screenPositionPixels.y = viewport.y + ( positionScreen.y * halfViewportHeight ) + halfViewportHeight - 8;
  137. // screen cull
  138. if ( validArea.containsPoint( screenPositionPixels ) ) {
  139. // save current RGB to temp texture
  140. renderer.copyFramebufferToTexture( tempMap, screenPositionPixels );
  141. // render pink quad
  142. let uniforms = material1a.uniforms;
  143. uniforms[ 'scale' ].value = scale;
  144. uniforms[ 'screenPosition' ].value = positionScreen;
  145. renderer.renderBufferDirect( camera, null, geometry, material1a, mesh1, null );
  146. // copy result to occlusionMap
  147. renderer.copyFramebufferToTexture( occlusionMap, screenPositionPixels );
  148. // restore graphics
  149. uniforms = material1b.uniforms;
  150. uniforms[ 'scale' ].value = scale;
  151. uniforms[ 'screenPosition' ].value = positionScreen;
  152. renderer.renderBufferDirect( camera, null, geometry, material1b, mesh1, null );
  153. // render elements
  154. const vecX = - positionScreen.x * 2;
  155. const vecY = - positionScreen.y * 2;
  156. for ( let i = 0, l = elements.length; i < l; i ++ ) {
  157. const element = elements[ i ];
  158. const uniforms = material2.uniforms;
  159. uniforms[ 'color' ].value.copy( element.color );
  160. uniforms[ 'map' ].value = element.texture;
  161. uniforms[ 'screenPosition' ].value.x = positionScreen.x + vecX * element.distance;
  162. uniforms[ 'screenPosition' ].value.y = positionScreen.y + vecY * element.distance;
  163. size = element.size / viewport.w;
  164. const invAspect = viewport.w / viewport.z;
  165. uniforms[ 'scale' ].value.set( size * invAspect, size );
  166. material2.uniformsNeedUpdate = true;
  167. renderer.renderBufferDirect( camera, null, geometry, material2, mesh2, null );
  168. }
  169. }
  170. };
  171. this.dispose = function () {
  172. material1a.dispose();
  173. material1b.dispose();
  174. material2.dispose();
  175. tempMap.dispose();
  176. occlusionMap.dispose();
  177. for ( let i = 0, l = elements.length; i < l; i ++ ) {
  178. elements[ i ].texture.dispose();
  179. }
  180. };
  181. }
  182. }
  183. //
  184. class LensflareElement {
  185. constructor( texture, size = 1, distance = 0, color = new Color( 0xffffff ) ) {
  186. this.texture = texture;
  187. this.size = size;
  188. this.distance = distance;
  189. this.color = color;
  190. }
  191. }
  192. LensflareElement.Shader = {
  193. name: 'LensflareElementShader',
  194. uniforms: {
  195. 'map': { value: null },
  196. 'occlusionMap': { value: null },
  197. 'color': { value: null },
  198. 'scale': { value: null },
  199. 'screenPosition': { value: null }
  200. },
  201. vertexShader: /* glsl */`
  202. precision highp float;
  203. uniform vec3 screenPosition;
  204. uniform vec2 scale;
  205. uniform sampler2D occlusionMap;
  206. attribute vec3 position;
  207. attribute vec2 uv;
  208. varying vec2 vUV;
  209. varying float vVisibility;
  210. void main() {
  211. vUV = uv;
  212. vec2 pos = position.xy;
  213. vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) );
  214. visibility += texture2D( occlusionMap, vec2( 0.5, 0.1 ) );
  215. visibility += texture2D( occlusionMap, vec2( 0.9, 0.1 ) );
  216. visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) );
  217. visibility += texture2D( occlusionMap, vec2( 0.9, 0.9 ) );
  218. visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) );
  219. visibility += texture2D( occlusionMap, vec2( 0.1, 0.9 ) );
  220. visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) );
  221. visibility += texture2D( occlusionMap, vec2( 0.5, 0.5 ) );
  222. vVisibility = visibility.r / 9.0;
  223. vVisibility *= 1.0 - visibility.g / 9.0;
  224. vVisibility *= visibility.b / 9.0;
  225. gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );
  226. }`,
  227. fragmentShader: /* glsl */`
  228. precision highp float;
  229. uniform sampler2D map;
  230. uniform vec3 color;
  231. varying vec2 vUV;
  232. varying float vVisibility;
  233. void main() {
  234. vec4 texture = texture2D( map, vUV );
  235. texture.a *= vVisibility;
  236. gl_FragColor = texture;
  237. gl_FragColor.rgb *= color;
  238. }`
  239. };
  240. Lensflare.Geometry = ( function () {
  241. const geometry = new BufferGeometry();
  242. const float32Array = new Float32Array( [
  243. - 1, - 1, 0, 0, 0,
  244. 1, - 1, 0, 1, 0,
  245. 1, 1, 0, 1, 1,
  246. - 1, 1, 0, 0, 1
  247. ] );
  248. const interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
  249. geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
  250. geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
  251. geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
  252. return geometry;
  253. } )();
  254. export { Lensflare, LensflareElement };