Lensflare.js 8.9 KB

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