Three.js中OutlinePass与后处理冲突问题

需求是鼠标滑过物体的时候物体显示为描边选中状态

方案一:OutlinePass

首先尝试了官方的OutlinePass来添加外边框选中功能
这是没加OutlinePass的效果:



这是使用OutlinePass后的,描边是可以了,但是锯齿严重,且模型发黑:


深入调研了一下发现是OutlinePass与renderer.outputEncoding = THREE.sRGBEncoding冲突,不支持sRGBEncoding,这个问题尚且还能通过一些材质参数调节能调到原来的色调,但是OutlinePass还跟下面的抗锯齿antialias: true冲突。

const renderer = new THREE.WebGLRenderer({
    canvas: canvas,
    antialias: true
})

本以为是后处理跟这些冲突,结果发现不是后处理的问题,就只是OutlinePass的问题。

继续深入调研了一下,发现OutlinePass根本就是个深坑,完全无法用,网上能搜到的方案也仅仅是再加一层fxaa,我试过了,即便再加一层ssaa都达不到antialias: true的效果,所以OutlinePass的方案就完全放弃了。
这是OutlinePass参考代码:

import './style.css'
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js'
import {DRACOLoader} from 'three/examples/jsm/loaders/DRACOLoader.js'
import {EffectComposer} from 'three/examples/jsm/postprocessing/EffectComposer.js';
import {RenderPass} from 'three/examples/jsm/postprocessing/RenderPass.js';
import {OutlinePass} from 'three/examples/jsm/postprocessing/OutlinePass.js';

const sizes = {
    width: window.innerWidth,
    height: window.innerHeight
}

const canvas = document.querySelector('canvas.webgl')

const renderer = new THREE.WebGLRenderer({
    canvas: canvas,
    antialias: true
})
renderer.outputEncoding = THREE.sRGBEncoding
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
const scene = new THREE.Scene()
scene.background = new THREE.Color(0xF5F5F5)
let camera = new THREE.PerspectiveCamera(10, sizes.width / sizes.height, 0.1, 100)
camera.position.z = -12
scene.add(camera)
const light = new THREE.AmbientLight(0xffffff);
scene.add(light);

const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true

const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('draco/')
const gltfLoader = new GLTFLoader()
gltfLoader.setDRACOLoader(dracoLoader)

window.addEventListener('resize', () => {
    sizes.width = window.innerWidth
    sizes.height = window.innerHeight

    camera.aspect = sizes.width / sizes.height;
    camera.updateProjectionMatrix()

    renderer.setSize(sizes.width, sizes.height)
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))

    composer.setSize(sizes.width, sizes.height);
    outlinePass.resolution.set(sizes.width, sizes.height);
})

let model;
controls.model = model;

gltfLoader.load(
    'Skeleton-processed.glb',
    (gltf) => {
        model = gltf.scene
        scene.add(model)
        model.traverse(function (child) {
            if (child.isMesh) {
                child.frustumCulled = false
                // child.texture.encoding = THREE.sRGBEncoding;
                // child.material.color.convertLinearToSRGB();
                // child.material.emissive.convertLinearToSRGB();
                child.material.emissive = new THREE.Color(0xffffff); // 设置发光颜色
                child.material.emissiveIntensity = 0.16; // 设置发光强度
            }
        });
    }
)

// add after defining renderer
const composer = new EffectComposer(renderer);

const renderPass = new RenderPass(scene, camera);
composer.addPass(renderPass);

const outlinePass = new OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), scene, camera);
composer.addPass(outlinePass);
outlinePass.visibleEdgeColor = new THREE.Color(0xffa522); // 设置橙色
outlinePass.edgeStrength = 5; // 将描边的强度设置为 0,即无发光效果
outlinePass.edgeGlow = 0; // 将描边的发光强度设置为 0
outlinePass.usePatternTexture = false; // 禁用纹理以获得纯线的效果

const tick = () => {
    controls.update()
    // renderer.render(scene, camera)
    composer.render()
    window.requestAnimationFrame(tick)
}
tick()


// Add the following to the mouseover event:
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

window.addEventListener('mousemove', (event) => {
    event.preventDefault();

    mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

    raycaster.setFromCamera(mouse, camera);

    const intersects = raycaster.intersectObjects(scene.children, true);

    if (intersects.length > 0) {
        const selectedObject = intersects[0].object;
        outlinePass.selectedObjects = [selectedObject];
    } else {
        outlinePass.selectedObjects = [];
    }
}, false);

方案二:Sobel算子

转头想起图像处理的Sobel算子也可以用来实现描边,折腾半天改了示例项目的shader才实现。
效果是有了,但是锯齿严重多了,本想着反正选中也就一个物体,锯齿就锯齿吧,结果傻眼了,Sobel是基于全屏计算的,无法只描边一个物体。要只显示一个选中的物体,还得折腾,得新开一个renderer,效率就更不用说了,后面肯定很多坑,所以也放弃了这个方案。


参考代码:

import './style.css'
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js'
import {DRACOLoader} from 'three/examples/jsm/loaders/DRACOLoader.js'
import {EffectComposer} from 'three/examples/jsm/postprocessing/EffectComposer.js';
import {RenderPass} from 'three/examples/jsm/postprocessing/RenderPass.js';
import {ShaderPass} from 'three/examples/jsm/postprocessing/ShaderPass.js';
import {SobelOperatorShader} from 'three/examples/jsm/shaders/SobelOperatorShader.js';


const sizes = {
    width: window.innerWidth,
    height: window.innerHeight
}

const canvas = document.querySelector('canvas.webgl')

const renderer = new THREE.WebGLRenderer({
    canvas: canvas,
    antialias: true
})
// renderer.outputEncoding = THREE.sRGBEncoding
renderer.setSize(sizes.width, sizes.height)
// renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
const scene = new THREE.Scene()
scene.background = new THREE.Color(0xF5F5F5)
let camera = new THREE.PerspectiveCamera(10, sizes.width / sizes.height, 0.1, 100)
camera.position.z = -12
scene.add(camera)
const light = new THREE.AmbientLight(0xffffff);
scene.add(light);

const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true

const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('draco/')
const gltfLoader = new GLTFLoader()
gltfLoader.setDRACOLoader(dracoLoader)

window.addEventListener('resize', () => {
    sizes.width = window.innerWidth
    sizes.height = window.innerHeight

    camera.aspect = sizes.width / sizes.height;
    camera.updateProjectionMatrix()

    renderer.setSize(sizes.width, sizes.height)
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))

    composer.setSize(sizes.width, sizes.height);
    composer.setPixelRatio(Math.min(window.devicePixelRatio, 2));

})

let model;
controls.model = model;

gltfLoader.load(
    'Skeleton-processed.glb',
    (gltf) => {
        model = gltf.scene
        scene.add(model)
        model.traverse(function (child) {
            if (child.isMesh) {
                child.frustumCulled = false
                // child.texture.encoding = THREE.sRGBEncoding;
                // child.material.color.convertLinearToSRGB();
                // child.material.emissive.convertLinearToSRGB();
            }
        });
    }
)

const composer = new EffectComposer(renderer);
composer.setSize(sizes.width, sizes.height);
composer.setPixelRatio(Math.min(window.devicePixelRatio, 2));

const renderPass = new RenderPass(scene, camera);
composer.addPass(renderPass);


const tick = () => {
    controls.update()
    // renderer.render(scene, camera)
    composer.render()
    window.requestAnimationFrame(tick)
}
tick()

const outlinePass = new ShaderPass(SobelOperatorShader);
// outlinePass.hiddenEdgeColor.set(0xff0000); // 设置描边的颜色
// outlinePass.visibleEdgeColor.set(0x000000); // 设置背景色

outlinePass.uniforms.resolution.value.x = sizes.width;
outlinePass.uniforms.resolution.value.y = sizes.height;
composer.addPass(outlinePass);

const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

window.addEventListener('mousemove', (event) => {
    event.preventDefault();

    mouse.x = (event.clientX / sizes.width) * 2 - 1;
    mouse.y = -(event.clientY / sizes.height) * 2 + 1;

    raycaster.setFromCamera(mouse, camera);

    const intersects = raycaster.intersectObjects(scene.children, true);

    if (intersects.length > 0) {
        const selectedObject = intersects[0].object;
        outlinePass.selectedObjects = [selectedObject];
    } else {
        outlinePass.selectedObjects = [];
    }
});

需要同时修改import {SobelOperatorShader} from 'three/examples/jsm/shaders/SobelOperatorShader.js';的shader最后输出颜色的步骤:

import {
    Vector2
} from 'three';

/**
 * Sobel Edge Detection (see https://youtu.be/uihBwtPIBxM)
 *
 * As mentioned in the video the Sobel operator expects a grayscale image as input.
 *
 */

const SobelOperatorShader = {

    uniforms: {

        'tDiffuse': {value: null},
        'resolution': {value: new Vector2()}

    },

    vertexShader: /* glsl */`

        varying vec2 vUv;

        void main() {

            vUv = uv;

            gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

        }`,

    fragmentShader: /* glsl */`

        uniform sampler2D tDiffuse;
        uniform vec2 resolution;
        varying vec2 vUv;

        void main() {

            vec2 texel = vec2( 1.0 / resolution.x, 1.0 / resolution.y );

        // kernel definition (in glsl matrices are filled in column-major order)

            const mat3 Gx = mat3( -1, -2, -1, 0, 0, 0, 1, 2, 1 ); // x direction kernel
            const mat3 Gy = mat3( -1, 0, 1, -2, 0, 2, -1, 0, 1 ); // y direction kernel

        // fetch the 3x3 neighbourhood of a fragment

        // first column

            float tx0y0 = texture2D( tDiffuse, vUv + texel * vec2( -1, -1 ) ).r;
            float tx0y1 = texture2D( tDiffuse, vUv + texel * vec2( -1,  0 ) ).r;
            float tx0y2 = texture2D( tDiffuse, vUv + texel * vec2( -1,  1 ) ).r;

        // second column

            float tx1y0 = texture2D( tDiffuse, vUv + texel * vec2(  0, -1 ) ).r;
            float tx1y1 = texture2D( tDiffuse, vUv + texel * vec2(  0,  0 ) ).r;
            float tx1y2 = texture2D( tDiffuse, vUv + texel * vec2(  0,  1 ) ).r;

        // third column

            float tx2y0 = texture2D( tDiffuse, vUv + texel * vec2(  1, -1 ) ).r;
            float tx2y1 = texture2D( tDiffuse, vUv + texel * vec2(  1,  0 ) ).r;
            float tx2y2 = texture2D( tDiffuse, vUv + texel * vec2(  1,  1 ) ).r;

        // gradient value in x direction

            float valueGx = Gx[0][0] * tx0y0 + Gx[1][0] * tx1y0 + Gx[2][0] * tx2y0 +
                Gx[0][1] * tx0y1 + Gx[1][1] * tx1y1 + Gx[2][1] * tx2y1 +
                Gx[0][2] * tx0y2 + Gx[1][2] * tx1y2 + Gx[2][2] * tx2y2;

        // gradient value in y direction

            float valueGy = Gy[0][0] * tx0y0 + Gy[1][0] * tx1y0 + Gy[2][0] * tx2y0 +
                Gy[0][1] * tx0y1 + Gy[1][1] * tx1y1 + Gy[2][1] * tx2y1 +
                Gy[0][2] * tx0y2 + Gy[1][2] * tx1y2 + Gy[2][2] * tx2y2;

        // magnitute of the total gradient

            float G = sqrt( ( valueGx * valueGx ) + ( valueGy * valueGy ) );

            //gl_FragColor = vec4( vec3( G ), 1 );
            // gl_FragColor = vec4( vec3( G > 0.5 ? 1.0 : 0.0 ), 1 );
            vec4 originalColor = texture2D(tDiffuse, vUv);
            vec4 outlineColor = vec4(1.0, 0.0, 0.0, 1.0);  // 描边的颜色,这里使用红色
        
            
    // 判断当前像素是否属于描边区域,如果是则显示描边颜色,否则显示原始颜色
    vec4 finalColor = mix(originalColor, outlineColor, smoothstep(0.5, 0.51, G));

    gl_FragColor = finalColor;
        }`

};

export {SobelOperatorShader};

方案三:菲涅尔

想起来用菲涅尔也可以实现接近描边的选中效果,虽然不那么完美吧,可以进一步尝试优化优化,代码如下:

import './style.css'
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js'
import {DRACOLoader} from 'three/examples/jsm/loaders/DRACOLoader.js'

//region 场景初始化
const sizes = {
    width: window.innerWidth,
    height: window.innerHeight
}

const canvas = document.querySelector('canvas.webgl')

const renderer = new THREE.WebGLRenderer({
    canvas: canvas,
    antialias: true
})
renderer.outputEncoding = THREE.sRGBEncoding
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))

const scene = new THREE.Scene()
scene.background = new THREE.Color(0xF5F5F5)
let camera = new THREE.PerspectiveCamera(10, sizes.width / sizes.height, 0.1, 100)
camera.position.z = -12
scene.add(camera)
const light = new THREE.AmbientLight(0xffffff, 1);
scene.add(light);

const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true


window.addEventListener('resize', () => {
    sizes.width = window.innerWidth
    sizes.height = window.innerHeight

    camera.aspect = sizes.width / sizes.height;
    camera.updateProjectionMatrix()

    renderer.setSize(sizes.width, sizes.height)
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))

})
//endregion

//region 模型加载
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('draco/')
const gltfLoader = new GLTFLoader()
gltfLoader.setDRACOLoader(dracoLoader)
let model;
controls.model = model;
gltfLoader.load(
    'Skeleton-processed.glb',
    (gltf) => {
        model = gltf.scene
        scene.add(model)
        model.traverse(function (child) {
            if (child.isMesh) {
                child.frustumCulled = false
                child.updateMatrixWorld(true)
            }
        });
    }
)
//endregion

//region 选中状态射线检测
const raycaster = new THREE.Raycaster();
let selectedObject = null;
window.addEventListener('mousemove', (event) => {
    camera.updateMatrixWorld(true)
    raycaster.setFromCamera({
        x: (event.clientX / sizes.width) * 2 - 1,
        y: -(event.clientY / sizes.height) * 2 + 1
    }, camera);
});
//endregion

//region 选中状态下的材质
let uniformsRim = {
    rimColor: {value: new THREE.Color(0x2200ee)},
    rimPower: {value: 1.0},
}
const rimMaterial = new THREE.MeshStandardMaterial();
rimMaterial.onBeforeCompile = function (shader) {
    shader.uniforms.rimColor = uniformsRim.rimColor;
    shader.uniforms.rimPower = uniformsRim.rimPower;
    shader.fragmentShader = /* glsl */`
        uniform vec3 rimColor;
        uniform float rimPower;
            ${shader.fragmentShader.replace(
        '#include <dithering_fragment>',
        '#include <dithering_fragment>\n\tfloat dotNV = 1.0-saturate( dot( normal, geometry.viewDir ) );\n\tgl_FragColor.rgb = mix(gl_FragColor.rgb, rimColor, rimPower * dotNV);'
    )}
    `;
    this.needsUpdate = true;
};

let lastMaterial;
//endregion
const update = () => {

    //region 鼠标滑过时显示选中状态
    const intersects = raycaster.intersectObjects(scene.children, true);
    if (intersects.length > 0) {

        console.log(intersects)
        let target = intersects[0].object;

        if (selectedObject !== target) {
            if (selectedObject && lastMaterial) {
                selectedObject.material = lastMaterial;
            }

            selectedObject = target;
            lastMaterial = selectedObject.material;
            rimMaterial.map = selectedObject.material.map;
            selectedObject.material = rimMaterial;
        }
    } else if (selectedObject && lastMaterial) {
        selectedObject.material = lastMaterial;
        selectedObject = null;
        lastMaterial = null;
    }
    //endregion

    controls.update();
    renderer.render(scene, camera);
    window.requestAnimationFrame(update);
};

update()


存在的问题是,并不是完全的描边效果,如下图,不过效率什么的应该不用担心了:



最终方案:outlinepass+ WebGLMultisampleRenderTarget

无奈上面几种都无法满足我对美术效果的期望,这里深度修改了outlinepass源码,结合webgl2的多采样渲染,最终实现了如下图效果:



简述一下实现方案:结合WebGLMultisampleRenderTarget和outlinepass后基本抗锯齿的问题得到了解决,甚至都不需要fxaa,颜色太暗的问题则需要加亮环境光,这部分完成后如果背景为黑色,就还能看,但背景为白色的时候,就需要去修改outlinepass的源码混合通道为NormalBlending,需要一定的shader基础。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容