Lensflare.js 8.8 KB

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