|
@@ -5,9 +5,15 @@
|
|
|
|
|
|
THREE.LensFlarePlugin = function () {
|
|
|
|
|
|
+ var _gl, _renderer, _precision;
|
|
|
+
|
|
|
var flares = [];
|
|
|
-
|
|
|
- var _gl, _renderer, _precision, _lensFlare = {};
|
|
|
+
|
|
|
+ var vertexBuffer, elementBuffer;
|
|
|
+ var program, attributes, uniforms;
|
|
|
+ var hasVertexTexture;
|
|
|
+
|
|
|
+ var tempTexture, occlusionTexture;
|
|
|
|
|
|
this.init = function ( renderer ) {
|
|
|
|
|
@@ -16,82 +22,75 @@ THREE.LensFlarePlugin = function () {
|
|
|
|
|
|
_precision = renderer.getPrecision();
|
|
|
|
|
|
- _lensFlare.vertices = new Float32Array( 8 + 8 );
|
|
|
- _lensFlare.faces = new Uint16Array( 6 );
|
|
|
-
|
|
|
- var i = 0;
|
|
|
- _lensFlare.vertices[ i ++ ] = - 1; _lensFlare.vertices[ i ++ ] = - 1; // vertex
|
|
|
- _lensFlare.vertices[ i ++ ] = 0; _lensFlare.vertices[ i ++ ] = 0; // uv... etc.
|
|
|
-
|
|
|
- _lensFlare.vertices[ i ++ ] = 1; _lensFlare.vertices[ i ++ ] = - 1;
|
|
|
- _lensFlare.vertices[ i ++ ] = 1; _lensFlare.vertices[ i ++ ] = 0;
|
|
|
-
|
|
|
- _lensFlare.vertices[ i ++ ] = 1; _lensFlare.vertices[ i ++ ] = 1;
|
|
|
- _lensFlare.vertices[ i ++ ] = 1; _lensFlare.vertices[ i ++ ] = 1;
|
|
|
-
|
|
|
- _lensFlare.vertices[ i ++ ] = - 1; _lensFlare.vertices[ i ++ ] = 1;
|
|
|
- _lensFlare.vertices[ i ++ ] = 0; _lensFlare.vertices[ i ++ ] = 1;
|
|
|
+ var vertices = new Float32Array( [
|
|
|
+ -1, -1, 0, 0,
|
|
|
+ 1, -1, 1, 0,
|
|
|
+ 1, 1, 1, 1,
|
|
|
+ -1, 1, 0, 1
|
|
|
+ ] );
|
|
|
|
|
|
- i = 0;
|
|
|
- _lensFlare.faces[ i ++ ] = 0; _lensFlare.faces[ i ++ ] = 1; _lensFlare.faces[ i ++ ] = 2;
|
|
|
- _lensFlare.faces[ i ++ ] = 0; _lensFlare.faces[ i ++ ] = 2; _lensFlare.faces[ i ++ ] = 3;
|
|
|
+ var faces = new Uint16Array( [
|
|
|
+ 0, 1, 2,
|
|
|
+ 0, 2, 3
|
|
|
+ ] );
|
|
|
|
|
|
// buffers
|
|
|
|
|
|
- _lensFlare.vertexBuffer = _gl.createBuffer();
|
|
|
- _lensFlare.elementBuffer = _gl.createBuffer();
|
|
|
+ vertexBuffer = _gl.createBuffer();
|
|
|
+ elementBuffer = _gl.createBuffer();
|
|
|
|
|
|
- _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
|
|
|
- _gl.bufferData( _gl.ARRAY_BUFFER, _lensFlare.vertices, _gl.STATIC_DRAW );
|
|
|
+ _gl.bindBuffer( _gl.ARRAY_BUFFER, vertexBuffer );
|
|
|
+ _gl.bufferData( _gl.ARRAY_BUFFER, vertices, _gl.STATIC_DRAW );
|
|
|
|
|
|
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
|
|
|
- _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.faces, _gl.STATIC_DRAW );
|
|
|
+ _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, elementBuffer );
|
|
|
+ _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faces, _gl.STATIC_DRAW );
|
|
|
|
|
|
// textures
|
|
|
|
|
|
- _lensFlare.tempTexture = _gl.createTexture();
|
|
|
- _lensFlare.occlusionTexture = _gl.createTexture();
|
|
|
+ tempTexture = _gl.createTexture();
|
|
|
+ occlusionTexture = _gl.createTexture();
|
|
|
|
|
|
- _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
|
|
|
+ _gl.bindTexture( _gl.TEXTURE_2D, tempTexture );
|
|
|
_gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, 16, 16, 0, _gl.RGB, _gl.UNSIGNED_BYTE, null );
|
|
|
_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
|
|
|
_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
|
|
|
_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
|
|
|
_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
|
|
|
|
|
|
- _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
|
|
|
+ _gl.bindTexture( _gl.TEXTURE_2D, occlusionTexture );
|
|
|
_gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, 16, 16, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, null );
|
|
|
_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
|
|
|
_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
|
|
|
_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
|
|
|
_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
|
|
|
|
|
|
- if ( _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) <= 0 ) {
|
|
|
+ hasVertexTexture = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) > 0;
|
|
|
|
|
|
- _lensFlare.hasVertexTexture = false;
|
|
|
- _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlare" ], _precision );
|
|
|
+ if ( hasVertexTexture ) {
|
|
|
+
|
|
|
+ program = createProgram( THREE.ShaderFlares[ "lensFlare" ], _precision );
|
|
|
|
|
|
} else {
|
|
|
|
|
|
- _lensFlare.hasVertexTexture = true;
|
|
|
- _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlareVertexTexture" ], _precision );
|
|
|
+ program = createProgram( THREE.ShaderFlares[ "lensFlareVertexTexture" ], _precision );
|
|
|
|
|
|
}
|
|
|
|
|
|
- _lensFlare.attributes = {};
|
|
|
- _lensFlare.uniforms = {};
|
|
|
-
|
|
|
- _lensFlare.attributes.vertex = _gl.getAttribLocation ( _lensFlare.program, "position" );
|
|
|
- _lensFlare.attributes.uv = _gl.getAttribLocation ( _lensFlare.program, "uv" );
|
|
|
+ attributes = {
|
|
|
+ vertex: _gl.getAttribLocation ( program, "position" ),
|
|
|
+ uv: _gl.getAttribLocation ( program, "uv" )
|
|
|
+ }
|
|
|
|
|
|
- _lensFlare.uniforms.renderType = _gl.getUniformLocation( _lensFlare.program, "renderType" );
|
|
|
- _lensFlare.uniforms.map = _gl.getUniformLocation( _lensFlare.program, "map" );
|
|
|
- _lensFlare.uniforms.occlusionMap = _gl.getUniformLocation( _lensFlare.program, "occlusionMap" );
|
|
|
- _lensFlare.uniforms.opacity = _gl.getUniformLocation( _lensFlare.program, "opacity" );
|
|
|
- _lensFlare.uniforms.color = _gl.getUniformLocation( _lensFlare.program, "color" );
|
|
|
- _lensFlare.uniforms.scale = _gl.getUniformLocation( _lensFlare.program, "scale" );
|
|
|
- _lensFlare.uniforms.rotation = _gl.getUniformLocation( _lensFlare.program, "rotation" );
|
|
|
- _lensFlare.uniforms.screenPosition = _gl.getUniformLocation( _lensFlare.program, "screenPosition" );
|
|
|
+ uniforms = {
|
|
|
+ renderType: _gl.getUniformLocation( program, "renderType" ),
|
|
|
+ map: _gl.getUniformLocation( program, "map" ),
|
|
|
+ occlusionMap: _gl.getUniformLocation( program, "occlusionMap" ),
|
|
|
+ opacity: _gl.getUniformLocation( program, "opacity" ),
|
|
|
+ color: _gl.getUniformLocation( program, "color" ),
|
|
|
+ scale: _gl.getUniformLocation( program, "scale" ),
|
|
|
+ rotation: _gl.getUniformLocation( program, "rotation" ),
|
|
|
+ screenPosition: _gl.getUniformLocation( program, "screenPosition" )
|
|
|
+ };
|
|
|
|
|
|
};
|
|
|
|
|
@@ -100,9 +99,6 @@ THREE.LensFlarePlugin = function () {
|
|
|
* Render lens flares
|
|
|
* Method: renders 16x16 0xff00ff-colored points scattered over the light source area,
|
|
|
* reads these back and calculates occlusion.
|
|
|
- * Then _lensFlare.update_lensFlares() is called to re-position and
|
|
|
- * update transparency of flares. Then they are rendered.
|
|
|
- *
|
|
|
*/
|
|
|
|
|
|
this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
|
|
@@ -133,15 +129,10 @@ THREE.LensFlarePlugin = function () {
|
|
|
var screenPosition = new THREE.Vector3( 1, 1, 0 ),
|
|
|
screenPositionPixels = new THREE.Vector2( 1, 1 );
|
|
|
|
|
|
- var uniforms = _lensFlare.uniforms,
|
|
|
- attributes = _lensFlare.attributes;
|
|
|
-
|
|
|
- // set _lensFlare program and reset blending
|
|
|
-
|
|
|
- _gl.useProgram( _lensFlare.program );
|
|
|
+ _gl.useProgram( program );
|
|
|
|
|
|
- _gl.enableVertexAttribArray( _lensFlare.attributes.vertex );
|
|
|
- _gl.enableVertexAttribArray( _lensFlare.attributes.uv );
|
|
|
+ _gl.enableVertexAttribArray( attributes.vertex );
|
|
|
+ _gl.enableVertexAttribArray( attributes.uv );
|
|
|
|
|
|
// loop through all lens flares to update their occlusion and positions
|
|
|
// setup gl and common used attribs/unforms
|
|
@@ -149,11 +140,11 @@ THREE.LensFlarePlugin = function () {
|
|
|
_gl.uniform1i( uniforms.occlusionMap, 0 );
|
|
|
_gl.uniform1i( uniforms.map, 1 );
|
|
|
|
|
|
- _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
|
|
|
+ _gl.bindBuffer( _gl.ARRAY_BUFFER, vertexBuffer );
|
|
|
_gl.vertexAttribPointer( attributes.vertex, 2, _gl.FLOAT, false, 2 * 8, 0 );
|
|
|
_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
|
|
|
|
|
|
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
|
|
|
+ _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, elementBuffer );
|
|
|
|
|
|
_gl.disable( _gl.CULL_FACE );
|
|
|
_gl.depthMask( false );
|
|
@@ -181,7 +172,7 @@ THREE.LensFlarePlugin = function () {
|
|
|
|
|
|
// screen cull
|
|
|
|
|
|
- if ( _lensFlare.hasVertexTexture || (
|
|
|
+ if ( hasVertexTexture || (
|
|
|
screenPositionPixels.x > 0 &&
|
|
|
screenPositionPixels.x < viewportWidth &&
|
|
|
screenPositionPixels.y > 0 &&
|
|
@@ -190,7 +181,7 @@ THREE.LensFlarePlugin = function () {
|
|
|
// save current RGB to temp texture
|
|
|
|
|
|
_gl.activeTexture( _gl.TEXTURE1 );
|
|
|
- _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
|
|
|
+ _gl.bindTexture( _gl.TEXTURE_2D, tempTexture );
|
|
|
_gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
|
|
|
|
|
|
|
|
@@ -209,7 +200,7 @@ THREE.LensFlarePlugin = function () {
|
|
|
// copy result to occlusionMap
|
|
|
|
|
|
_gl.activeTexture( _gl.TEXTURE0 );
|
|
|
- _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
|
|
|
+ _gl.bindTexture( _gl.TEXTURE_2D, occlusionTexture );
|
|
|
_gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
|
|
|
|
|
|
|
|
@@ -219,7 +210,7 @@ THREE.LensFlarePlugin = function () {
|
|
|
_gl.disable( _gl.DEPTH_TEST );
|
|
|
|
|
|
_gl.activeTexture( _gl.TEXTURE1 );
|
|
|
- _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
|
|
|
+ _gl.bindTexture( _gl.TEXTURE_2D, tempTexture );
|
|
|
_gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
|
|
|
|
|
|
|