// 选择模型、显示外边框
THREE.OutlinePass = function ( resolution, scene, camera, selectedObjects ) {
//场景、相机
this.renderScene = scene;
this.renderCamera = camera;
//要选择的对象
this.selectedObjects = selectedObjects !== undefined ? selectedObjects : [];
//可是区域的颜色、隐藏区域的颜色
this.visibleEdgeColor = new THREE.Color( 1, 1, 1 );
this.hiddenEdgeColor = new THREE.Color( 0.1, 0.04, 0.02 );
this.edgeGlow = 0.0;
this.usePatternTexture = false;
this.edgeThickness = 1.0; //边缘大小
this.edgeStrength = 3.0;
this.downSampleRatio = 2;
this.pulsePeriod = 0;
THREE.Pass.call( this );
//分辨率
this.resolution = ( resolution !== undefined ) ? new THREE.Vector2( resolution.x, resolution.y ) : new THREE.Vector2( 256, 256 );
//纹理格式
var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat };
//
var resx = Math.round( this.resolution.x / this.downSampleRatio );
var resy = Math.round( this.resolution.y / this.downSampleRatio );
this.maskBufferMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
this.maskBufferMaterial.side = THREE.DoubleSide;
this.renderTargetMaskBuffer = new THREE.WebGLRenderTarget( this.resolution.x, this.resolution.y, pars );
this.renderTargetMaskBuffer.texture.name = "OutlinePass.mask";
this.renderTargetMaskBuffer.texture.generateMipmaps = false;
this.depthMaterial = new THREE.MeshDepthMaterial();
this.depthMaterial.side = THREE.DoubleSide;
this.depthMaterial.depthPacking = THREE.RGBADepthPacking;
this.depthMaterial.blending = THREE.NoBlending;
this.prepareMaskMaterial = this.getPrepareMaskMaterial();
this.prepareMaskMaterial.side = THREE.DoubleSide;
this.prepareMaskMaterial.fragmentShader = replaceDepthToViewZ( this.prepareMaskMaterial.fragmentShader, this.renderCamera );
this.renderTargetDepthBuffer = new THREE.WebGLRenderTarget( this.resolution.x, this.resolution.y, pars );
this.renderTargetDepthBuffer.texture.name = "OutlinePass.depth";
this.renderTargetDepthBuffer.texture.generateMipmaps = false;
this.renderTargetMaskDownSampleBuffer = new THREE.WebGLRenderTarget( resx, resy, pars );
this.renderTargetMaskDownSampleBuffer.texture.name = "OutlinePass.depthDownSample";
this.renderTargetMaskDownSampleBuffer.texture.generateMipmaps = false;
this.renderTargetBlurBuffer1 = new THREE.WebGLRenderTarget( resx, resy, pars );
this.renderTargetBlurBuffer1.texture.name = "OutlinePass.blur1";
this.renderTargetBlurBuffer1.texture.generateMipmaps = false;
this.renderTargetBlurBuffer2 = new THREE.WebGLRenderTarget( Math.round( resx / 2 ), Math.round( resy / 2 ), pars );
this.renderTargetBlurBuffer2.texture.name = "OutlinePass.blur2";
this.renderTargetBlurBuffer2.texture.generateMipmaps = false;
this.edgeDetectionMaterial = this.getEdgeDetectionMaterial();
this.renderTargetEdgeBuffer1 = new THREE.WebGLRenderTarget( resx, resy, pars );
this.renderTargetEdgeBuffer1.texture.name = "OutlinePass.edge1";
this.renderTargetEdgeBuffer1.texture.generateMipmaps = false;
this.renderTargetEdgeBuffer2 = new THREE.WebGLRenderTarget( Math.round( resx / 2 ), Math.round( resy / 2 ), pars );
this.renderTargetEdgeBuffer2.texture.name = "OutlinePass.edge2";
this.renderTargetEdgeBuffer2.texture.generateMipmaps = false;
var MAX_EDGE_THICKNESS = 4; //最大边缘大小
var MAX_EDGE_GLOW = 4;
//水平模糊滤镜
this.separableBlurMaterial1 = this.getSeperableBlurMaterial( MAX_EDGE_THICKNESS );
this.separableBlurMaterial1.uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
this.separableBlurMaterial1.uniforms[ "kernelRadius" ].value = 1;
//垂直模糊滤镜
this.separableBlurMaterial2 = this.getSeperableBlurMaterial( MAX_EDGE_GLOW );
this.separableBlurMaterial2.uniforms[ "texSize" ].value = new THREE.Vector2( Math.round( resx / 2 ), Math.round( resy / 2 ) );
this.separableBlurMaterial2.uniforms[ "kernelRadius" ].value = MAX_EDGE_GLOW;
// Overlay material
this.overlayMaterial = this.getOverlayMaterial();
// copy material
if ( THREE.CopyShader === undefined )
console.error( "THREE.OutlinePass relies on THREE.CopyShader" );
var copyShader = THREE.CopyShader;
this.copyUniforms = THREE.UniformsUtils.clone( copyShader.uniforms );
this.copyUniforms[ "opacity" ].value = 1.0;
this.materialCopy = new THREE.ShaderMaterial( {
uniforms: this.copyUniforms,
vertexShader: copyShader.vertexShader,
fragmentShader: copyShader.fragmentShader,
blending: THREE.NoBlending, //禁用混合
depthTest: false, //关闭深度测试
depthWrite: false, //关闭深度写
transparent: true //设置透明
} );
this.enabled = true;
this.needsSwap = false;
this.oldClearColor = new THREE.Color();
this.oldClearAlpha = 1;
this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 ); //创建正交投影
this.scene = new THREE.Scene(); //创建新的场景
this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null ); //平面几何数据
this.quad.frustumCulled = false; // Avoid getting clipped //关闭cpu剔除
this.scene.add( this.quad ); //添加窗口四边形到场景
this.tempPulseColor1 = new THREE.Color();
this.tempPulseColor2 = new THREE.Color();
this.textureMatrix = new THREE.Matrix4();
function replaceDepthToViewZ( string, camera ) {
var type = camera.isPerspectiveCamera ? 'perspective' : 'orthographic';
return string.replace( /DEPTH_TO_VIEW_Z/g, type + 'DepthToViewZ' );
}
};
THREE.OutlinePass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
constructor: THREE.OutlinePass,
dispose: function () {
this.renderTargetMaskBuffer.dispose();
this.renderTargetDepthBuffer.dispose();
this.renderTargetMaskDownSampleBuffer.dispose();
this.renderTargetBlurBuffer1.dispose();
this.renderTargetBlurBuffer2.dispose();
this.renderTargetEdgeBuffer1.dispose();
this.renderTargetEdgeBuffer2.dispose();
},
setSize: function ( width, height ) {
this.renderTargetMaskBuffer.setSize( width, height );
var resx = Math.round( width / this.downSampleRatio );
var resy = Math.round( height / this.downSampleRatio );
this.renderTargetMaskDownSampleBuffer.setSize( resx, resy );
this.renderTargetBlurBuffer1.setSize( resx, resy );
this.renderTargetEdgeBuffer1.setSize( resx, resy );
this.separableBlurMaterial1.uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
resx = Math.round( resx / 2 );
resy = Math.round( resy / 2 );
this.renderTargetBlurBuffer2.setSize( resx, resy );
this.renderTargetEdgeBuffer2.setSize( resx, resy );
this.separableBlurMaterial2.uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
},
//改变选择物体的可见性
changeVisibilityOfSelectedObjects: function ( bVisible ) {
function gatherSelectedMeshesCallBack( object ) {
if ( object.isMesh ) {
if ( bVisible ) {
object.visible = object.userData.oldVisible;
delete object.userData.oldVisible;
} else {
object.userData.oldVisible = object.visible;
object.visible = bVisible;
}
}
}
for ( var i = 0; i < this.selectedObjects.length; i ++ ) {
var selectedObject = this.selectedObjects[ i ];
selectedObject.traverse( gatherSelectedMeshesCallBack );
}
},
//改变没有选择的物体的可见性
changeVisibilityOfNonSelectedObjects: function ( bVisible ) {
var selectedMeshes = [];
function gatherSelectedMeshesCallBack( object ) {
if ( object.isMesh ) selectedMeshes.push( object );
}
for ( var i = 0; i < this.selectedObjects.length; i ++ ) {
var selectedObject = this.selectedObjects[ i ];
selectedObject.traverse( gatherSelectedMeshesCallBack );
}
function VisibilityChangeCallBack( object ) {
if ( object.isMesh || object.isLine || object.isSprite ) {
var bFound = false;
for ( var i = 0; i < selectedMeshes.length; i ++ ) {
var selectedObjectId = selectedMeshes[ i ].id;
if ( selectedObjectId === object.id ) {
bFound = true;
break;
}
}
if ( ! bFound ) {
var visibility = object.visible;
if ( ! bVisible || object.bVisible ) object.visible = bVisible;
object.bVisible = visibility;
}
}
}
this.renderScene.traverse( VisibilityChangeCallBack );
},
//设置屏幕取深度的矩阵
updateTextureMatrix: function () {
this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
0.0, 0.5, 0.0, 0.5,
0.0, 0.0, 0.5, 0.5,
0.0, 0.0, 0.0, 1.0 ); //将ndc坐标[-1~1]转化到[0~1]范围
this.textureMatrix.multiply( this.renderCamera.projectionMatrix ); //投影空间
this.textureMatrix.multiply( this.renderCamera.matrixWorldInverse ); //视图空间
},
//渲染
render: function ( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) {
if ( this.selectedObjects.length > 0 ) {
//保存老的渲染设置【颜色、透明度、自动清除状态】
this.oldClearColor.copy( renderer.getClearColor() );
this.oldClearAlpha = renderer.getClearAlpha();
var oldAutoClear = renderer.autoClear;
renderer.autoClear = false;
//屏幕模板测试
if ( maskActive ) renderer.context.disable( renderer.context.STENCIL_TEST );
//设置新的清除颜色
renderer.setClearColor( 0xffffff, 1 );
// Make selected objects invisible
//隐藏要选择的模型
this.changeVisibilityOfSelectedObjects( false );
//保存背景
var currentBackground = this.renderScene.background;
this.renderScene.background = null; //不设置背景
// 1. Draw Non Selected objects in the depth buffer
// 渲染背景的深度
this.renderScene.overrideMaterial = this.depthMaterial;
renderer.setRenderTarget( this.renderTargetDepthBuffer );
renderer.clear();
renderer.render( this.renderScene, this.renderCamera );
// Make selected objects visible
//显示选择的物体
this.changeVisibilityOfSelectedObjects( true );
// Update Texture Matrix for Depth compare
//更新纹理矩阵【深度转换 模型视图变换、投影变换、屏幕坐标转换】
this.updateTextureMatrix();
// Make non selected objects invisible, and draw only the selected objects, by comparing the depth buffer of non selected objects
//隐藏不可见物体
this.changeVisibilityOfNonSelectedObjects( false );
//根据不可见物体的深度,设置可见物体的深度
this.renderScene.overrideMaterial = this.prepareMaskMaterial;
this.prepareMaskMaterial.uniforms[ "cameraNearFar" ].value = new THREE.Vector2( this.renderCamera.near, this.renderCamera.far );
this.prepareMaskMaterial.uniforms[ "depthTexture" ].value = this.renderTargetDepthBuffer.texture;
this.prepareMaskMaterial.uniforms[ "textureMatrix" ].value = this.textureMatrix;
renderer.setRenderTarget( this.renderTargetMaskBuffer );
renderer.clear();
renderer.render( this.renderScene, this.renderCamera );
this.renderScene.overrideMaterial = null;
//设置背景物体可见
this.changeVisibilityOfNonSelectedObjects( true );
//重置原来的背景
this.renderScene.background = currentBackground;
// 2. Downsample to Half resolution
// 下采样到一半的分辨率
this.quad.material = this.materialCopy;
this.copyUniforms[ "tDiffuse" ].value = this.renderTargetMaskBuffer.texture; //经过选中物体和未选中物体深度比较后的纹理
renderer.setRenderTarget( this.renderTargetMaskDownSampleBuffer ); //设置下采样的渲染目标
renderer.clear();//清空下采样渲染目标
renderer.render( this.scene, this.camera ); //渲染四边形,填充屏幕
this.tempPulseColor1.copy( this.visibleEdgeColor ); //设置选中物体中可见部分的颜色 脉冲
this.tempPulseColor2.copy( this.hiddenEdgeColor ); //设置选中物体中不可见部分的颜色 脉冲
if ( this.pulsePeriod > 0 ) {
var scalar = ( 1 + 0.25 ) / 2 + Math.cos( performance.now() * 0.01 / this.pulsePeriod ) * ( 1.0 - 0.25 ) / 2;
this.tempPulseColor1.multiplyScalar( scalar );
this.tempPulseColor2.multiplyScalar( scalar );
}
// 3. Apply Edge Detection Pass //使用边缘监测
this.quad.material = this.edgeDetectionMaterial;
this.edgeDetectionMaterial.uniforms[ "maskTexture" ].value = this.renderTargetMaskDownSampleBuffer.texture; //设置下采样的渲染目标
this.edgeDetectionMaterial.uniforms[ "texSize" ].value = new THREE.Vector2( this.renderTargetMaskDownSampleBuffer.width, this.renderTargetMaskDownSampleBuffer.height );
this.edgeDetectionMaterial.uniforms[ "visibleEdgeColor" ].value = this.tempPulseColor1; //设置可见部分的颜色
this.edgeDetectionMaterial.uniforms[ "hiddenEdgeColor" ].value = this.tempPulseColor2; //设置不可见部分的颜色
renderer.setRenderTarget( this.renderTargetEdgeBuffer1 ); //设置边缘监测的渲染目标
renderer.clear(); //清空边缘监测的渲染目标
renderer.render( this.scene, this.camera ); //渲染四边形,填充屏幕
// 4. Apply Blur on Half res
// 在下采样纹理中应用模糊
this.quad.material = this.separableBlurMaterial1;
this.separableBlurMaterial1.uniforms[ "colorTexture" ].value = this.renderTargetEdgeBuffer1.texture; //边缘检测后的纹理
this.separableBlurMaterial1.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionX; //水平方向的模糊
this.separableBlurMaterial1.uniforms[ "kernelRadius" ].value = this.edgeThickness; //模糊半径
renderer.setRenderTarget( this.renderTargetBlurBuffer1 );
renderer.clear();
renderer.render( this.scene, this.camera );
this.separableBlurMaterial1.uniforms[ "colorTexture" ].value = this.renderTargetBlurBuffer1.texture; //水平模糊后的纹理
this.separableBlurMaterial1.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionY; //垂直方向的模糊
renderer.setRenderTarget( this.renderTargetEdgeBuffer1 ); //模糊半径
renderer.clear();
renderer.render( this.scene, this.camera );
// Apply Blur on quarter res
this.quad.material = this.separableBlurMaterial2;
this.separableBlurMaterial2.uniforms[ "colorTexture" ].value = this.renderTargetEdgeBuffer1.texture; //水平扩大上次的模糊范围
this.separableBlurMaterial2.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionX;
renderer.setRenderTarget( this.renderTargetBlurBuffer2 );
renderer.clear();
renderer.render( this.scene, this.camera );
this.separableBlurMaterial2.uniforms[ "colorTexture" ].value = this.renderTargetBlurBuffer2.texture; //垂直扩大上次的模糊范围
this.separableBlurMaterial2.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionY;
renderer.setRenderTarget( this.renderTargetEdgeBuffer2 );
renderer.clear();
renderer.render( this.scene, this.camera );
// Blend it additively over the input texture
this.quad.material = this.overlayMaterial;
this.overlayMaterial.uniforms[ "maskTexture" ].value = this.renderTargetMaskBuffer.texture; //深度比较后的纹理
this.overlayMaterial.uniforms[ "edgeTexture1" ].value = this.renderTargetEdgeBuffer1.texture; //边缘水平模糊后的纹理
this.overlayMaterial.uniforms[ "edgeTexture2" ].value = this.renderTargetEdgeBuffer2.texture; //边缘垂直模糊后的纹理
this.overlayMaterial.uniforms[ "patternTexture" ].value = this.patternTexture; //填充图案纹理
this.overlayMaterial.uniforms[ "edgeStrength" ].value = this.edgeStrength; //内边缘的强度
this.overlayMaterial.uniforms[ "edgeGlow" ].value = this.edgeGlow; //外边缘的颜色
this.overlayMaterial.uniforms[ "usePatternTexture" ].value = this.usePatternTexture; //不使用图案填充
if ( maskActive ) renderer.context.enable( renderer.context.STENCIL_TEST ); //恢复模板测试
renderer.setRenderTarget( readBuffer ); //恢复渲染缓冲
renderer.render( this.scene, this.camera ); //渲染四边形,合并背景
renderer.setClearColor( this.oldClearColor, this.oldClearAlpha ); //恢复清空颜色和透明度
renderer.autoClear = oldAutoClear; //恢复自动清空
}
if ( this.renderToScreen ) { //
this.quad.material = this.materialCopy;
this.copyUniforms[ "tDiffuse" ].value = readBuffer.texture;
renderer.setRenderTarget( null );
renderer.render( this.scene, this.camera );
}
},
//获取边缘遮罩层
getPrepareMaskMaterial: function () {
return new THREE.ShaderMaterial( {
uniforms: {
"depthTexture": { value: null }, //背景的深度
"cameraNearFar": { value: new THREE.Vector2( 0.5, 0.5 ) }, //相机的近裁切、远裁切面
"textureMatrix": { value: new THREE.Matrix4() } //纹理投影矩阵
},
vertexShader: [
'varying vec4 projTexCoord;', //投影纹理坐标【将来会转换为屏幕坐标】
'varying vec4 vPosition;',
'uniform mat4 textureMatrix;',
'void main() {',
' vPosition = modelViewMatrix * vec4( position, 1.0 );', //相机空间坐标
' vec4 worldPosition = modelMatrix * vec4( position, 1.0 );', //世界坐标
' projTexCoord = textureMatrix * worldPosition;', //世界坐标经过 视图变换、投影变换、视口变换后转换为屏幕的像素坐标
' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
'}'
].join( '\n' ),
fragmentShader: [
'#include
'varying vec4 vPosition;',
'varying vec4 projTexCoord;',
'uniform sampler2D depthTexture;',
'uniform vec2 cameraNearFar;',
'void main() {',
' float depth = unpackRGBAToDepth(texture2DProj( depthTexture, projTexCoord ));', //还原背景深度
' float viewZ = - DEPTH_TO_VIEW_Z( depth, cameraNearFar.x, cameraNearFar.y );', //转换深度到视图空间
' float depthTest = (-vPosition.z > viewZ) ? 1.0 : 0.0;', //比较背景深度和选择物体的深度,选择的物体比背景深度大返回1,否则返回0
' gl_FragColor = vec4(0.0, depthTest, 1.0, 1.0);', //深度比较后的结果使用纹理的g通道存储
'}'
].join( '\n' )
} );
},
//边缘监测材质
getEdgeDetectionMaterial: function () {
return new THREE.ShaderMaterial( {
uniforms: {
"maskTexture": { value: null }, //遮罩纹理 深度比较后的纹理
"texSize": { value: new THREE.Vector2( 0.5, 0.5 ) }, //遮罩纹理大小
"visibleEdgeColor": { value: new THREE.Vector3( 1.0, 1.0, 1.0 ) }, //可见部分的颜色
"hiddenEdgeColor": { value: new THREE.Vector3( 1.0, 1.0, 1.0 ) }, //不可见部分的颜色
},
vertexShader:
"varying vec2 vUv;\n\
void main() {\n\
vUv = uv;\n\
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
}",
fragmentShader:[
'varying vec2 vUv;',
'uniform sampler2D maskTexture;',
'uniform vec2 texSize;',
'uniform vec3 visibleEdgeColor;',
'uniform vec3 hiddenEdgeColor;',
'void main() {',
'vec2 invSize = 1.0 / texSize;',
'vec4 uvOffset = vec4(1.0, 0.0, 0.0, 1.0) * vec4(invSize, invSize);', //uv偏移
'vec4 c1 = texture2D( maskTexture, vUv + uvOffset.xy);', //右偏移一个像素
'vec4 c2 = texture2D( maskTexture, vUv - uvOffset.xy);', //左偏移一个像素
'vec4 c3 = texture2D( maskTexture, vUv + uvOffset.yw);', //上偏移一个像素
'vec4 c4 = texture2D( maskTexture, vUv - uvOffset.yw);', //下偏移一个像素
'float diff1 = (c1.r - c2.r)*0.5;', //边缘检测 填充背景用的是0xffffff, 填充选择物体时用的是vec4(0.0, depthTest, 1.0, 1.0)
'float diff2 = (c3.r - c4.r)*0.5;', //边缘检测
'float d = length( vec2(diff1, diff2) );', //
'float a1 = min(c1.g, c2.g);', //获取最小的深度
'float a2 = min(c3.g, c4.g);', //获取最小的深度
'float visibilityFactor = min(a1, a2);', //获取最小的深度
'vec3 edgeColor = 1.0 - visibilityFactor > 0.001 ? visibleEdgeColor : hiddenEdgeColor;', //根据深度纹理设置可见、不可见部分的颜色
'gl_FragColor = vec4(edgeColor, 1.0) * vec4(d);', //颜色值 * 深度变化
'}'
].join( '\n' )
} );
},
getSeperableBlurMaterial: function ( maxRadius ) {
return new THREE.ShaderMaterial( {
defines: {
"MAX_RADIUS": maxRadius, //模糊半径
},
uniforms: {
"colorTexture": { value: null }, //边缘检测后纹理
"texSize": { value: new THREE.Vector2( 0.5, 0.5 ) }, //边缘检测后纹理大小
"direction": { value: new THREE.Vector2( 0.5, 0.5 ) }, //模糊方向
"kernelRadius": { value: 1.0 } //模糊权重矩阵
},
vertexShader:[
'varying vec2 vUv;',
'void main() {',
' vUv = uv;',
' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
'}'].join( '\n' ),
fragmentShader:[
'#include
'varying vec2 vUv;',
'uniform sampler2D colorTexture;',
'uniform vec2 texSize;',
'uniform vec2 direction;',
'uniform float kernelRadius;',
'float gaussianPdf(in float x, in float sigma) {',
' return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;',
'}',
'void main() {',
' vec2 invSize = 1.0 / texSize;',
' float weightSum = gaussianPdf(0.0, kernelRadius);', //权重和
' vec3 diffuseSum = texture2D( colorTexture, vUv).rgb * weightSum;', //
' vec2 delta = direction * invSize * kernelRadius/float(MAX_RADIUS);', //模糊方向 * 像素偏移 * 模糊权重
' vec2 uvOffset = delta;',
' for( int i = 1; i <= MAX_RADIUS; i ++ ) {', //遍历像素周围【水平或者垂直方向】的像素,计算模糊结果
' float w = gaussianPdf(uvOffset.x, kernelRadius);', //计算权重
' vec3 sample1 = texture2D( colorTexture, vUv + uvOffset).rgb;', //采样像素
' vec3 sample2 = texture2D( colorTexture, vUv - uvOffset).rgb;', //采样像素
' diffuseSum += ((sample1 + sample2) * w);', //合并采样 * 权重
' weightSum += (2.0 * w);', //权重的和
' uvOffset += delta;', //范围一步一步的扩大
' }',
' gl_FragColor = vec4(diffuseSum/weightSum, 1.0);', //结果平均化
'}'
].join( '\n' ),
} );
},
getOverlayMaterial: function () {
return new THREE.ShaderMaterial( {
uniforms: {
"maskTexture": { value: null }, //选择物体的遮罩纹理
"edgeTexture1": { value: null }, //水平边缘检测模糊后的纹理
"edgeTexture2": { value: null }, //垂直边缘检测模糊后的纹理
"patternTexture": { value: null }, //填充的图案
"edgeStrength": { value: 1.0 }, //边缘的强度
"edgeGlow": { value: 1.0 }, //外边缘的颜色
"usePatternTexture": { value: 0.0 } //开启图案填充
},
vertexShader:
"varying vec2 vUv;\n\
void main() {\n\
vUv = uv;\n\
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
}",
fragmentShader:[
'varying vec2 vUv;',
'uniform sampler2D maskTexture;',
'uniform sampler2D edgeTexture1;',
'uniform sampler2D edgeTexture2;',
'uniform sampler2D patternTexture;',
'uniform float edgeStrength;',
'uniform float edgeGlow;',
'uniform bool usePatternTexture;',
'void main() {',
'vec4 edgeValue1 = texture2D(edgeTexture1, vUv);',
'vec4 edgeValue2 = texture2D(edgeTexture2, vUv);',
'vec4 maskColor = texture2D(maskTexture, vUv);',
'vec4 patternColor = texture2D(patternTexture, 6.0 * vUv);', //使用6倍的图案密度填充
'float visibilityFactor = 1.0 - maskColor.g > 0.0 ? 1.0 : 0.5;', //可见部分使用0.5的颜色权重
'vec4 edgeValue = edgeValue1 + edgeValue2 * edgeGlow;', //边缘的颜色,内边缘使用edgeValue1, 外边缘使用edgeValue2 * edgeGlow
'vec4 finalColor = edgeStrength * maskColor.r * edgeValue;', //颜色强度 * 边缘标志 * 边缘的颜色
'if(usePatternTexture)', //使用图案填充
' finalColor += + visibilityFactor * (1.0 - maskColor.r) * (1.0 - patternColor.r);', //可见部分的权重 * 选择物体的区域 * 图案
'gl_FragColor = finalColor;',
'}'
].join( '\n' ),
blending: THREE.AdditiveBlending,
depthTest: false,
depthWrite: false,
transparent: true //混合背景
} );
}
} );
THREE.OutlinePass.BlurDirectionX = new THREE.Vector2( 1.0, 0.0 );
THREE.OutlinePass.BlurDirectionY = new THREE.Vector2( 0.0, 1.0 );