Lensflare.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. console.warn( "THREE.Lensflare: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.Lensflare = function () {
  3. THREE.Mesh.call( this, THREE.Lensflare.Geometry, new THREE.MeshBasicMaterial( { opacity: 0, transparent: true } ) );
  4. this.type = 'Lensflare';
  5. this.frustumCulled = false;
  6. this.renderOrder = Infinity;
  7. //
  8. var positionScreen = new THREE.Vector3();
  9. var positionView = new THREE.Vector3();
  10. // textures
  11. var tempMap = new THREE.DataTexture( new Uint8Array( 16 * 16 * 3 ), 16, 16, THREE.RGBFormat );
  12. tempMap.minFilter = THREE.NearestFilter;
  13. tempMap.magFilter = THREE.NearestFilter;
  14. tempMap.wrapS = THREE.ClampToEdgeWrapping;
  15. tempMap.wrapT = THREE.ClampToEdgeWrapping;
  16. var occlusionMap = new THREE.DataTexture( new Uint8Array( 16 * 16 * 3 ), 16, 16, THREE.RGBFormat );
  17. occlusionMap.minFilter = THREE.NearestFilter;
  18. occlusionMap.magFilter = THREE.NearestFilter;
  19. occlusionMap.wrapS = THREE.ClampToEdgeWrapping;
  20. occlusionMap.wrapT = THREE.ClampToEdgeWrapping;
  21. // material
  22. var geometry = THREE.Lensflare.Geometry;
  23. var material1a = new THREE.RawShaderMaterial( {
  24. uniforms: {
  25. 'scale': { value: null },
  26. 'screenPosition': { value: null }
  27. },
  28. vertexShader: [
  29. 'precision highp float;',
  30. 'uniform vec3 screenPosition;',
  31. 'uniform vec2 scale;',
  32. 'attribute vec3 position;',
  33. 'void main() {',
  34. ' gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );',
  35. '}'
  36. ].join( '\n' ),
  37. fragmentShader: [
  38. 'precision highp float;',
  39. 'void main() {',
  40. ' gl_FragColor = vec4( 1.0, 0.0, 1.0, 1.0 );',
  41. '}'
  42. ].join( '\n' ),
  43. depthTest: true,
  44. depthWrite: false,
  45. transparent: false
  46. } );
  47. var material1b = new THREE.RawShaderMaterial( {
  48. uniforms: {
  49. 'map': { value: tempMap },
  50. 'scale': { value: null },
  51. 'screenPosition': { value: null }
  52. },
  53. vertexShader: [
  54. 'precision highp float;',
  55. 'uniform vec3 screenPosition;',
  56. 'uniform vec2 scale;',
  57. 'attribute vec3 position;',
  58. 'attribute vec2 uv;',
  59. 'varying vec2 vUV;',
  60. 'void main() {',
  61. ' vUV = uv;',
  62. ' gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );',
  63. '}'
  64. ].join( '\n' ),
  65. fragmentShader: [
  66. 'precision highp float;',
  67. 'uniform sampler2D map;',
  68. 'varying vec2 vUV;',
  69. 'void main() {',
  70. ' gl_FragColor = texture2D( map, vUV );',
  71. '}'
  72. ].join( '\n' ),
  73. depthTest: false,
  74. depthWrite: false,
  75. transparent: false
  76. } );
  77. // the following object is used for occlusionMap generation
  78. var mesh1 = new THREE.Mesh( geometry, material1a );
  79. //
  80. var elements = [];
  81. var shader = THREE.LensflareElement.Shader;
  82. var material2 = new THREE.RawShaderMaterial( {
  83. uniforms: {
  84. 'map': { value: null },
  85. 'occlusionMap': { value: occlusionMap },
  86. 'color': { value: new THREE.Color( 0xffffff ) },
  87. 'scale': { value: new THREE.Vector2() },
  88. 'screenPosition': { value: new THREE.Vector3() }
  89. },
  90. vertexShader: shader.vertexShader,
  91. fragmentShader: shader.fragmentShader,
  92. blending: THREE.AdditiveBlending,
  93. transparent: true,
  94. depthWrite: false
  95. } );
  96. var mesh2 = new THREE.Mesh( geometry, material2 );
  97. this.addElement = function ( element ) {
  98. elements.push( element );
  99. };
  100. //
  101. var scale = new THREE.Vector2();
  102. var screenPositionPixels = new THREE.Vector2();
  103. var validArea = new THREE.Box2();
  104. var viewport = new THREE.Vector4();
  105. this.onBeforeRender = function ( renderer, scene, camera ) {
  106. renderer.getCurrentViewport( viewport );
  107. var invAspect = viewport.w / viewport.z;
  108. var halfViewportWidth = viewport.z / 2.0;
  109. var halfViewportHeight = viewport.w / 2.0;
  110. var size = 16 / viewport.w;
  111. scale.set( size * invAspect, size );
  112. validArea.min.set( viewport.x, viewport.y );
  113. validArea.max.set( viewport.x + ( viewport.z - 16 ), viewport.y + ( viewport.w - 16 ) );
  114. // calculate position in screen space
  115. positionView.setFromMatrixPosition( this.matrixWorld );
  116. positionView.applyMatrix4( camera.matrixWorldInverse );
  117. if ( positionView.z > 0 ) return; // lensflare is behind the camera
  118. positionScreen.copy( positionView ).applyMatrix4( camera.projectionMatrix );
  119. // horizontal and vertical coordinate of the lower left corner of the pixels to copy
  120. screenPositionPixels.x = viewport.x + ( positionScreen.x * halfViewportWidth ) + halfViewportWidth - 8;
  121. screenPositionPixels.y = viewport.y + ( positionScreen.y * halfViewportHeight ) + halfViewportHeight - 8;
  122. // screen cull
  123. if ( validArea.containsPoint( screenPositionPixels ) ) {
  124. // save current RGB to temp texture
  125. renderer.copyFramebufferToTexture( screenPositionPixels, tempMap );
  126. // render pink quad
  127. var uniforms = material1a.uniforms;
  128. uniforms[ "scale" ].value = scale;
  129. uniforms[ "screenPosition" ].value = positionScreen;
  130. renderer.renderBufferDirect( camera, null, geometry, material1a, mesh1, null );
  131. // copy result to occlusionMap
  132. renderer.copyFramebufferToTexture( screenPositionPixels, occlusionMap );
  133. // restore graphics
  134. var uniforms = material1b.uniforms;
  135. uniforms[ "scale" ].value = scale;
  136. uniforms[ "screenPosition" ].value = positionScreen;
  137. renderer.renderBufferDirect( camera, null, geometry, material1b, mesh1, null );
  138. // render elements
  139. var vecX = - positionScreen.x * 2;
  140. var vecY = - positionScreen.y * 2;
  141. for ( var i = 0, l = elements.length; i < l; i ++ ) {
  142. var element = elements[ i ];
  143. var uniforms = material2.uniforms;
  144. uniforms[ "color" ].value.copy( element.color );
  145. uniforms[ "map" ].value = element.texture;
  146. uniforms[ "screenPosition" ].value.x = positionScreen.x + vecX * element.distance;
  147. uniforms[ "screenPosition" ].value.y = positionScreen.y + vecY * element.distance;
  148. var size = element.size / viewport.w;
  149. var invAspect = viewport.w / viewport.z;
  150. uniforms[ "scale" ].value.set( size * invAspect, size );
  151. material2.uniformsNeedUpdate = true;
  152. renderer.renderBufferDirect( camera, null, geometry, material2, mesh2, null );
  153. }
  154. }
  155. };
  156. this.dispose = function () {
  157. material1a.dispose();
  158. material1b.dispose();
  159. material2.dispose();
  160. tempMap.dispose();
  161. occlusionMap.dispose();
  162. for ( var i = 0, l = elements.length; i < l; i ++ ) {
  163. elements[ i ].texture.dispose();
  164. }
  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.setAttribute( 'position', new THREE.InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
  237. geometry.setAttribute( 'uv', new THREE.InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
  238. return geometry;
  239. } )();