Lensflare.js 8.7 KB

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