Lensflare.js 8.7 KB

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