Lensflare.js 8.8 KB

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