提交 ebf93289 编写于 作者: A alteredq

Resurrected lens flares as custom plugin.

Still a bit dirty but less than it was before. Maybe also sprites could be pluginized.
上级 81297f68
此差异已折叠。
因为 它太大了无法显示 source diff 。你可以改为 查看blob
此差异已折叠。
3D assets and textures for ROME "3 Dreams of Black" at http://ro.me are licensed
under a Creative CommonsAttribution-NonCommercial-ShareAlike 3.0 Unported License
( http://creativecommons.org/licenses/by-nc-sa/3.0/ ).
\ No newline at end of file
/**
* @author mikael emtinger / http://gomo.se/
* @author alteredq / http://alteredqualia.com/
*/
THREE.LensFlare = function ( texture, size, distance, blending, color ) {
THREE.Object3D.call( this );
this.lensFlares = [];
this.positionScreen = new THREE.Vector3();
this.customUpdateCallback = undefined;
if( texture !== undefined ) {
this.add( texture, size, distance, blending, color );
}
};
THREE.LensFlare.prototype = new THREE.Object3D();
THREE.LensFlare.prototype.constructor = THREE.LensFlare;
THREE.LensFlare.prototype.supr = THREE.Object3D.prototype;
/*
* Add: adds another flare
*/
THREE.LensFlare.prototype.add = function ( texture, size, distance, blending, color, opacity ) {
if( size === undefined ) size = -1;
if( distance === undefined ) distance = 0;
if( opacity === undefined ) opacity = 1;
if( color === undefined ) color = new THREE.Color( 0xffffff );
if( blending === undefined ) blending = THREE.BillboardBlending;
distance = Math.min( distance, Math.max( 0, distance ) );
this.lensFlares.push( { texture: texture, // THREE.Texture
size: size, // size in pixels (-1 = use texture.width)
distance: distance, // distance (0-1) from light source (0=at light source)
x: 0, y: 0, z: 0, // screen position (-1 => 1) z = 0 is ontop z = 1 is back
scale: 1, // scale
rotation: 1, // rotation
opacity: opacity, // opacity
color: color, // color
blending: blending } ); // blending
};
/*
* Update lens flares update positions on all flares based on the screen position
* Set myLensFlare.customUpdateCallback to alter the flares in your project specific way.
*/
THREE.LensFlare.prototype.updateLensFlares = function () {
var f, fl = this.lensFlares.length;
var flare;
var vecX = -this.positionScreen.x * 2;
var vecY = -this.positionScreen.y * 2;
for( f = 0; f < fl; f ++ ) {
flare = this.lensFlares[ f ];
flare.x = this.positionScreen.x + vecX * flare.distance;
flare.y = this.positionScreen.y + vecY * flare.distance;
flare.wantedRotation = flare.x * Math.PI * 0.25;
flare.rotation += ( flare.wantedRotation - flare.rotation ) * 0.25;
}
};
/**
* @author mikael emtinger / http://gomo.se/
*
*/
THREE.ShaderFlares = {
'lensFlareVertexTexture': {
vertexShader: [
"uniform vec3 screenPosition;",
"uniform vec2 scale;",
"uniform float rotation;",
"uniform int renderType;",
"uniform sampler2D occlusionMap;",
"attribute vec2 position;",
"attribute vec2 uv;",
"varying vec2 vUV;",
"varying float vVisibility;",
"void main() {",
"vUV = uv;",
"vec2 pos = position;",
"if( renderType == 2 ) {",
"vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) ) +",
"texture2D( occlusionMap, vec2( 0.5, 0.1 ) ) +",
"texture2D( occlusionMap, vec2( 0.9, 0.1 ) ) +",
"texture2D( occlusionMap, vec2( 0.9, 0.5 ) ) +",
"texture2D( occlusionMap, vec2( 0.9, 0.9 ) ) +",
"texture2D( occlusionMap, vec2( 0.5, 0.9 ) ) +",
"texture2D( occlusionMap, vec2( 0.1, 0.9 ) ) +",
"texture2D( occlusionMap, vec2( 0.1, 0.5 ) ) +",
"texture2D( occlusionMap, vec2( 0.5, 0.5 ) );",
"vVisibility = ( visibility.r / 9.0 ) *",
"( 1.0 - visibility.g / 9.0 ) *",
"( visibility.b / 9.0 ) *",
"( 1.0 - visibility.a / 9.0 );",
"pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
"pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
"}",
"gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
"}"
].join( "\n" ),
fragmentShader: [
"#ifdef GL_ES",
"precision highp float;",
"#endif",
"uniform sampler2D map;",
"uniform float opacity;",
"uniform int renderType;",
"uniform vec3 color;",
"varying vec2 vUV;",
"varying float vVisibility;",
"void main() {",
// pink square
"if( renderType == 0 ) {",
"gl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );",
// restore
"} else if( renderType == 1 ) {",
"gl_FragColor = texture2D( map, vUV );",
// flare
"} else {",
"vec4 texture = texture2D( map, vUV );",
"texture.a *= opacity * vVisibility;",
"gl_FragColor = texture;",
"gl_FragColor.rgb *= color;",
"}",
"}"
].join( "\n" )
},
'lensFlare': {
vertexShader: [
"uniform vec3 screenPosition;",
"uniform vec2 scale;",
"uniform float rotation;",
"uniform int renderType;",
"attribute vec2 position;",
"attribute vec2 uv;",
"varying vec2 vUV;",
"void main() {",
"vUV = uv;",
"vec2 pos = position;",
"if( renderType == 2 ) {",
"pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
"pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
"}",
"gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
"}"
].join( "\n" ),
fragmentShader: [
"#ifdef GL_ES",
"precision highp float;",
"#endif",
"uniform sampler2D map;",
"uniform sampler2D occlusionMap;",
"uniform float opacity;",
"uniform int renderType;",
"uniform vec3 color;",
"varying vec2 vUV;",
"void main() {",
// pink square
"if( renderType == 0 ) {",
"gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );",
// restore
"} else if( renderType == 1 ) {",
"gl_FragColor = texture2D( map, vUV );",
// flare
"} else {",
"float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 ) ).a +",
"texture2D( occlusionMap, vec2( 0.9, 0.5 ) ).a +",
"texture2D( occlusionMap, vec2( 0.5, 0.9 ) ).a +",
"texture2D( occlusionMap, vec2( 0.1, 0.5 ) ).a;",
"visibility = ( 1.0 - visibility / 4.0 );",
"vec4 texture = texture2D( map, vUV );",
"texture.a *= opacity * visibility;",
"gl_FragColor = texture;",
"gl_FragColor.rgb *= color;",
"}",
"}"
].join( "\n" )
}
};
......@@ -69,6 +69,10 @@ THREE.WebGLRenderer = function ( parameters ) {
this.maxMorphTargets = 8;
// custom render plugins
this.renderPlugins = [];
// info
this.info = {
......@@ -274,6 +278,16 @@ THREE.WebGLRenderer = function ( parameters ) {
};
// Plugins
this.addPlugin = function ( plugin ) {
plugin.init( this );
this.renderPlugins.push( plugin );
};
// Deallocation
this.deallocateObject = function ( object ) {
......@@ -3164,7 +3178,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( scene.overrideMaterial ) {
setBlending( scene.overrideMaterial.blending );
this.setBlending( scene.overrideMaterial.blending );
setDepthTest( scene.overrideMaterial.depthTest );
setDepthWrite( scene.overrideMaterial.depthWrite );
setPolygonOffset( scene.overrideMaterial.polygonOffset, scene.overrideMaterial.polygonOffsetFactor, scene.overrideMaterial.polygonOffsetUnits );
......@@ -3176,7 +3190,7 @@ THREE.WebGLRenderer = function ( parameters ) {
// opaque pass (front-to-back order)
setBlending( THREE.NormalBlending );
this.setBlending( THREE.NormalBlending );
renderObjects( scene.__webglObjects, true, "opaque", camera, lights, fog, false );
renderObjectsImmediate( scene.__webglObjectsImmediate, "opaque", camera, lights, fog, false );
......@@ -3196,6 +3210,24 @@ THREE.WebGLRenderer = function ( parameters ) {
}
// custom render plugins
if ( this.renderPlugins.length ) {
for ( i = 0, il = this.renderPlugins.length; i < il; i ++ ) {
this.renderPlugins[ i ].render( scene, camera, _viewportWidth, _viewportHeight );
_currentProgram = null;
_oldBlending = -1;
_oldDepthTest = -1;
_oldDepthWrite = -1;
_currentGeometryGroupHash = -1;
}
}
// Generate mipmap if we're using any kind of mipmap filtering
if ( renderTarget && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
......@@ -3321,7 +3353,7 @@ THREE.WebGLRenderer = function ( parameters ) {
// render regular objects
setDepthTest( true );
setBlending( THREE.NormalBlending ); // maybe blending should be just disabled?
_this.setBlending( THREE.NormalBlending ); // maybe blending should be just disabled?
//_gl.cullFace( _gl.FRONT );
......@@ -3441,7 +3473,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( ! material ) continue;
if ( useBlending ) setBlending( material.blending );
if ( useBlending ) _this.setBlending( material.blending );
setDepthTest( material.depthTest );
setDepthWrite( material.depthWrite );
......@@ -3482,7 +3514,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( ! material ) continue;
if ( useBlending ) setBlending( material.blending );
if ( useBlending ) this.setBlending( material.blending );
setDepthTest( material.depthTest );
setDepthWrite( material.depthWrite );
......@@ -3702,8 +3734,8 @@ THREE.WebGLRenderer = function ( parameters ) {
}
setBlending( object.blending );
setTexture( object.map, 0 );
_this.setBlending( object.blending );
_this.setTexture( object.map, 0 );
_gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
......@@ -3803,6 +3835,7 @@ THREE.WebGLRenderer = function ( parameters ) {
scene.__webglObjects = [];
scene.__webglObjectsImmediate = [];
scene.__webglSprites = [];
scene.__webglFlares = [];
}
......@@ -3958,6 +3991,10 @@ THREE.WebGLRenderer = function ( parameters ) {
scene.__webglSprites.push( object );
} else if ( object instanceof THREE.LensFlare ) {
scene.__webglFlares.push( object );
}
object.__webglActive = true;
......@@ -4118,6 +4155,10 @@ THREE.WebGLRenderer = function ( parameters ) {
removeInstancesDirect( scene.__webglSprites, object );
} else if ( object instanceof THREE.LensFlare ) {
removeInstancesDirect( scene.__webglFlares, object );
} else if ( object instanceof THREE.MarchingCubes || object.immediateRenderCallback ) {
removeInstances( scene.__webglObjectsImmediate, object );
......@@ -4771,7 +4812,7 @@ THREE.WebGLRenderer = function ( parameters ) {
} else {
setTexture( texture, value );
_this.setTexture( texture, value );
}
......@@ -4799,7 +4840,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if ( !texture ) continue;
setTexture( texture, uniform._array[ i ] );
_this.setTexture( texture, uniform._array[ i ] );
}
......@@ -5110,7 +5151,7 @@ THREE.WebGLRenderer = function ( parameters ) {
};
function setBlending ( blending ) {
this.setBlending = function ( blending ) {
if ( blending !== _oldBlending ) {
......@@ -5446,6 +5487,13 @@ THREE.WebGLRenderer = function ( parameters ) {
// Textures
function isPowerOfTwo ( value ) {
return ( value & ( value - 1 ) ) === 0;
};
function setTextureParameters ( textureType, texture, image ) {
if ( isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ) ) {
......@@ -5472,7 +5520,7 @@ THREE.WebGLRenderer = function ( parameters ) {
};
function setTexture ( texture, slot ) {
this.setTexture = function ( texture, slot ) {
if ( texture.needsUpdate ) {
......@@ -5731,7 +5779,7 @@ THREE.WebGLRenderer = function ( parameters ) {
};
// fallback filters for non-power-of-2 textures
// Fallback filters for non-power-of-2 textures
function filterFallback ( f ) {
......@@ -5752,6 +5800,8 @@ THREE.WebGLRenderer = function ( parameters ) {
};
// Map three.js constants to WebGL constants
function paramThreeToGL ( p ) {
switch ( p ) {
......@@ -5788,12 +5838,6 @@ THREE.WebGLRenderer = function ( parameters ) {
};
function isPowerOfTwo ( value ) {
return ( value & ( value - 1 ) ) === 0;
};
// Allocations
function allocateBones ( object ) {
......
......@@ -129,8 +129,11 @@ EXTRAS_FILES = [
'extras/loaders/UTF8Loader.js',
'extras/objects/Axes.js',
'extras/objects/MarchingCubes.js',
'extras/objects/LensFlare.js',
'extras/plugins/LensFlarePlugin.js',
'extras/renderers/AnaglyphWebGLRenderer.js',
'extras/renderers/CrosseyedWebGLRenderer.js'
'extras/renderers/CrosseyedWebGLRenderer.js',
'extras/shaders/ShaderFlares.js'
]
CANVAS_FILES = [
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册