Lensflare.js 8.9 KB

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