Lensflare.js 8.6 KB

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