Lensflare.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. //
  10. var positionScreen = new THREE.Vector3();
  11. // textures
  12. var tempMap = new THREE.DataTexture( new Uint8Array( 16 * 16 * 3 ), 16, 16, THREE.RGBFormat );
  13. tempMap.minFilter = THREE.NearestFilter;
  14. tempMap.magFilter = THREE.NearestFilter;
  15. tempMap.wrapS = THREE.ClampToEdgeWrapping;
  16. tempMap.wrapT = THREE.ClampToEdgeWrapping;
  17. tempMap.needsUpdate = true;
  18. var occlusionMap = new THREE.DataTexture( new Uint8Array( 16 * 16 * 3 ), 16, 16, THREE.RGBFormat );
  19. occlusionMap.minFilter = THREE.NearestFilter;
  20. occlusionMap.magFilter = THREE.NearestFilter;
  21. occlusionMap.wrapS = THREE.ClampToEdgeWrapping;
  22. occlusionMap.wrapT = THREE.ClampToEdgeWrapping;
  23. occlusionMap.needsUpdate = true;
  24. // material
  25. var geometry = THREE.Lensflare.Geometry;
  26. var shader = THREE.Lensflare.Shader;
  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. };
  166. };
  167. THREE.Lensflare.prototype = Object.create( THREE.Mesh.prototype );
  168. THREE.Lensflare.prototype.constructor = THREE.Lensflare;
  169. THREE.Lensflare.prototype.isLensflare = true;
  170. //
  171. THREE.LensflareElement = function ( texture, size, distance, color ) {
  172. this.texture = texture;
  173. this.size = size || 1;
  174. this.distance = distance || 0;
  175. this.color = color || new THREE.Color( 0xffffff );
  176. };
  177. THREE.LensflareElement.Shader = {
  178. uniforms: {
  179. 'map': { value: null },
  180. 'occlusionMap': { value: null },
  181. 'color': { value: null },
  182. 'scale': { value: null },
  183. 'screenPosition': { value: null }
  184. },
  185. vertexShader: [
  186. 'precision highp float;',
  187. 'uniform vec3 screenPosition;',
  188. 'uniform vec2 scale;',
  189. 'uniform sampler2D occlusionMap;',
  190. 'attribute vec3 position;',
  191. 'attribute vec2 uv;',
  192. 'varying vec2 vUV;',
  193. 'varying float vVisibility;',
  194. 'void main() {',
  195. ' vUV = uv;',
  196. ' vec2 pos = position.xy;',
  197. ' vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) );',
  198. ' visibility += texture2D( occlusionMap, vec2( 0.5, 0.1 ) );',
  199. ' visibility += texture2D( occlusionMap, vec2( 0.9, 0.1 ) );',
  200. ' visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) );',
  201. ' visibility += texture2D( occlusionMap, vec2( 0.9, 0.9 ) );',
  202. ' visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) );',
  203. ' visibility += texture2D( occlusionMap, vec2( 0.1, 0.9 ) );',
  204. ' visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) );',
  205. ' visibility += texture2D( occlusionMap, vec2( 0.5, 0.5 ) );',
  206. ' vVisibility = visibility.r / 9.0;',
  207. ' vVisibility *= 1.0 - visibility.g / 9.0;',
  208. ' vVisibility *= visibility.b / 9.0;',
  209. ' gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );',
  210. '}'
  211. ].join( '\n' ),
  212. fragmentShader: [
  213. 'precision highp float;',
  214. 'uniform sampler2D map;',
  215. 'uniform vec3 color;',
  216. 'varying vec2 vUV;',
  217. 'varying float vVisibility;',
  218. 'void main() {',
  219. ' vec4 texture = texture2D( map, vUV );',
  220. ' texture.a *= vVisibility;',
  221. ' gl_FragColor = texture;',
  222. ' gl_FragColor.rgb *= color;',
  223. '}'
  224. ].join( '\n' )
  225. };
  226. THREE.Lensflare.Geometry = ( function () {
  227. var geometry = new THREE.BufferGeometry();
  228. var float32Array = new Float32Array( [
  229. - 1, - 1, 0, 0, 0,
  230. 1, - 1, 0, 1, 0,
  231. 1, 1, 0, 1, 1,
  232. - 1, 1, 0, 0, 1
  233. ] );
  234. var interleavedBuffer = new THREE.InterleavedBuffer( float32Array, 5 );
  235. geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
  236. geometry.addAttribute( 'position', new THREE.InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
  237. geometry.addAttribute( 'uv', new THREE.InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
  238. return geometry;
  239. } )();