Lensflare.js 8.7 KB

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