返回顶部
首页 > 资讯 > 操作系统 >解决Three.js辉光背景不透明
  • 563
分享到

解决Three.js辉光背景不透明

javascriptthree.js 2023-08-30 11:08:34 563人浏览 八月长安
摘要

使用此pass canvas元素的background都能看到 不过相应的辉光颜色和背景颜色不相容的地方看起来颜色会怪一些 如图 不过如果是纯色就没什么问题了 //@ts-nocheckimport { AdditiveBle

使用此pass canvas元素的background都能看到 不过相应的辉光颜色和背景颜色不相容的地方看起来颜色会怪一些
如图
不过如果是纯色就没什么问题了
在这里插入图片描述

//@ts-nocheckimport {    AdditiveBlending,    Color,    LinearFilter,    MeshBasicMaterial,    RGBAFORMat,    ShaderMaterial,    Texture,    UniformsUtils,    Vector2,    Vector3,    webGLRenderer,    WEBGLRenderTarget,} from "three";import { Pass } from "three/examples/jsm/postprocessing/Pass";// typescript definitions doesn't have FullScreenQuad//@ts-ignoreimport { FullScreenQuad } from "three/examples/jsm/postprocessing/Pass";import { CopyShader } from "three/examples/jsm/shaders/CopyShader.js";import { LuminosityHighPassshader } from "three/examples/jsm/shaders/LuminosityHighPassShader.js";class TransparentBackgroundFixedUnrealBloomPass extends Pass {    strength: number;    radius: number;    threshold: number;    resolution: Vector2;    clearColor: Color;    renderTargetsHorizontal: any[];    renderTargetsVertical: any[];    nMips: number;    renderTargetBright: WebGLRenderTarget;    highPassUniforms: any;    materialHighPassFilter: ShaderMaterial;    separableBlurMaterials: any[];    compositeMaterial: ShaderMaterial;    bloomTintColors: Vector3[];    copyUniforms: any;    materialCopy: ShaderMaterial;    _oldClearColor: Color;    oldClearAlpha: number;    basic: MeshBasicMaterial;    fsQuad: Pass.FullScreenQuad;    static BlurDirectionX: any;    static BlurDirectionY: any;    constructor(        resolution: Vector2,        strength: number,        radius: number,        threshold: number    ) {        super();        this.strength = strength !== undefined ? strength : 1;        this.radius = radius;        this.threshold = threshold;        this.resolution =            resolution !== undefined                ? new Vector2(resolution.x, resolution.y)                : new Vector2(256, 256);        // create color only once here, reuse it later inside the render function        this.clearColor = new Color(0, 0, 0);        // render targets        const pars = {            minFilter: LinearFilter,            magFilter: LinearFilter,            format: RGBAFormat,        };        this.renderTargetsHorizontal = [];        this.renderTargetsVertical = [];        this.nMips = 5;        let resx = Math.round(this.resolution.x / 2);        let resy = Math.round(this.resolution.y / 2);        this.renderTargetBright = new WebGLRenderTarget(resx, resy, pars);        this.renderTargetBright.texture.name = "UnrealBloomPass.bright";        this.renderTargetBright.texture.generateMipmaps = false;        for (let i = 0; i < this.nMips; i++) {            const renderTargetHorizonal = new WebGLRenderTarget(                resx,                resy,                pars            );            renderTargetHorizonal.texture.name = "UnrealBloomPass.h" + i;            renderTargetHorizonal.texture.generateMipmaps = false;            this.renderTargetsHorizontal.push(renderTargetHorizonal);            const renderTargetVertical = new WebGLRenderTarget(                resx,                resy,                pars            );            renderTargetVertical.texture.name = "UnrealBloomPass.v" + i;            renderTargetVertical.texture.generateMipmaps = false;            this.renderTargetsVertical.push(renderTargetVertical);            resx = Math.round(resx / 2);            resy = Math.round(resy / 2);        }        // luminosity high pass material        if (LuminosityHighPassShader === undefined)            console.error(                "THREE.UnrealBloomPass relies on LuminosityHighPassShader"            );        const highPassShader = LuminosityHighPassShader;        this.highPassUniforms = UniformsUtils.clone(highPassShader.uniforms);        this.highPassUniforms["luminosityThreshold"].value = threshold;        this.highPassUniforms["smoothWidth"].value = 0.01;        this.materialHighPassFilter = new ShaderMaterial({            uniforms: this.highPassUniforms,            vertexShader: highPassShader.vertexShader,            fragmentShader: highPassShader.fragmentShader,            defines: {},        });        // Gaussian Blur Materials        this.separableBlurMaterials = [];        const kernelSizeArray = [3, 5, 7, 9, 11];        resx = Math.round(this.resolution.x / 2);        resy = Math.round(this.resolution.y / 2);        for (let i = 0; i < this.nMips; i++) {            this.separableBlurMaterials.push(                this.getSeperableBlurMaterial(kernelSizeArray[i])            );            this.separableBlurMaterials[i].uniforms["texSize"].value =                new Vector2(resx, resy);            resx = Math.round(resx / 2);            resy = Math.round(resy / 2);        }        // Composite material        this.compositeMaterial = this.getCompositeMaterial(this.nMips);        this.compositeMaterial.uniforms["blurTexture1"].value =            this.renderTargetsVertical[0].texture;        this.compositeMaterial.uniforms["blurTexture2"].value =            this.renderTargetsVertical[1].texture;        this.compositeMaterial.uniforms["blurTexture3"].value =            this.renderTargetsVertical[2].texture;        this.compositeMaterial.uniforms["blurTexture4"].value =            this.renderTargetsVertical[3].texture;        this.compositeMaterial.uniforms["blurTexture5"].value =            this.renderTargetsVertical[4].texture;        this.compositeMaterial.uniforms["bloomStrength"].value = strength;        this.compositeMaterial.uniforms["bloomRadius"].value = 0.1;        this.compositeMaterial.needsUpdate = true;        const bloomFactors = [1.0, 0.8, 0.6, 0.4, 0.2];        this.compositeMaterial.uniforms["bloomFactors"].value = bloomFactors;        this.bloomTintColors = [            new Vector3(1, 1, 1),            new Vector3(1, 1, 1),            new Vector3(1, 1, 1),            new Vector3(1, 1, 1),            new Vector3(1, 1, 1),        ];        this.compositeMaterial.uniforms["bloomTintColors"].value =            this.bloomTintColors;        // copy material        if (CopyShader === undefined) {            console.error("THREE.UnrealBloomPass relies on CopyShader");        }        const copyShader = CopyShader;        this.copyUniforms = UniformsUtils.clone(copyShader.uniforms);        this.copyUniforms["opacity"].value = 1.0;        this.materialCopy = new ShaderMaterial({            uniforms: this.copyUniforms,            vertexShader: copyShader.vertexShader,            fragmentShader: copyShader.fragmentShader,            blending: AdditiveBlending,            depthTest: false,            depthWrite: false,            transparent: true,        });        this.enabled = true;        this.needsSwap = false;        this._oldClearColor = new Color();        this.oldClearAlpha = 1;        this.basic = new MeshBasicMaterial();        this.fsQuad = new FullScreenQuad(null);    }    dispose() {        for (let i = 0; i < this.renderTargetsHorizontal.length; i++) {            this.renderTargetsHorizontal[i].dispose();        }        for (let i = 0; i < this.renderTargetsVertical.length; i++) {            this.renderTargetsVertical[i].dispose();        }        this.renderTargetBright.dispose();    }    setSize(width: number, height: number) {        let resx = Math.round(width / 2);        let resy = Math.round(height / 2);        this.renderTargetBright.setSize(resx, resy);        for (let i = 0; i < this.nMips; i++) {            this.renderTargetsHorizontal[i].setSize(resx, resy);            this.renderTargetsVertical[i].setSize(resx, resy);            this.separableBlurMaterials[i].uniforms["texSize"].value =                new Vector2(resx, resy);            resx = Math.round(resx / 2);            resy = Math.round(resy / 2);        }    }    render(        renderer: WebGLRenderer,        writeBuffer: any,        readBuffer: { texture: Texture },        deltaTime: any,        maskActive: any    ) {        renderer.getClearColor(this._oldClearColor);        this.oldClearAlpha = renderer.getClearAlpha();        const oldAutoClear = renderer.autoClear;        renderer.autoClear = false;        renderer.setClearColor(this.clearColor, 0);        if (maskActive) renderer.state.buffers.stencil.setTest(false);        // Render input to screen        if (this.renderToScreen) {            this.fsQuad.material = this.basic;            this.basic.map = readBuffer.texture;            renderer.setRenderTarget(null);            renderer.clear();            this.fsQuad.render(renderer);        }        // 1. Extract Bright Areas        this.highPassUniforms["tDiffuse"].value = readBuffer.texture;        this.highPassUniforms["luminosityThreshold"].value = this.threshold;        this.fsQuad.material = this.materialHighPassFilter;        renderer.setRenderTarget(this.renderTargetBright);        renderer.clear();        this.fsQuad.render(renderer);        // 2. Blur All the mips progressively        let inputRenderTarget = this.renderTargetBright;        for (let i = 0; i < this.nMips; i++) {            this.fsQuad.material = this.separableBlurMaterials[i];            this.separableBlurMaterials[i].uniforms["colorTexture"].value =                inputRenderTarget.texture;            this.separableBlurMaterials[i].uniforms["direction"].value =                TransparentBackgroundFixedUnrealBloomPass.BlurDirectionX;            renderer.setRenderTarget(this.renderTargetsHorizontal[i]);            renderer.clear();            this.fsQuad.render(renderer);            this.separableBlurMaterials[i].uniforms["colorTexture"].value =                this.renderTargetsHorizontal[i].texture;            this.separableBlurMaterials[i].uniforms["direction"].value =                TransparentBackgroundFixedUnrealBloomPass.BlurDirectionY;            renderer.setRenderTarget(this.renderTargetsVertical[i]);            renderer.clear();            this.fsQuad.render(renderer);            inputRenderTarget = this.renderTargetsVertical[i];        }        // Composite All the mips        this.fsQuad.material = this.compositeMaterial;        this.compositeMaterial.uniforms["bloomStrength"].value = this.strength;        this.compositeMaterial.uniforms["bloomRadius"].value = this.radius;        this.compositeMaterial.uniforms["bloomTintColors"].value =            this.bloomTintColors;        renderer.setRenderTarget(this.renderTargetsHorizontal[0]);        renderer.clear();        this.fsQuad.render(renderer);        // Blend it additively over the input texture        this.fsQuad.material = this.materialCopy;        this.copyUniforms["tDiffuse"].value =            this.renderTargetsHorizontal[0].texture;        if (maskActive) renderer.state.buffers.stencil.setTest(true);        if (this.renderToScreen) {            renderer.setRenderTarget(null);            this.fsQuad.render(renderer);        } else {            renderer.setRenderTarget(readBuffer);            this.fsQuad.render(renderer);        }        // Restore renderer settings        renderer.setClearColor(this._oldClearColor, this.oldClearAlpha);        renderer.autoClear = oldAutoClear;    }    getSeperableBlurMaterial(kernelRadius: number) {        return new ShaderMaterial({            defines: {                KERNEL_RADIUS: kernelRadius,                SIGMA: kernelRadius,            },            uniforms: {                colorTexture: { value: null },                texSize: { value: new Vector2(0.5, 0.5) },                direction: { value: new Vector2(0.5, 0.5) },            },            vertexShader: `varying vec2 vUv;void main() {vUv = uv;gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );}`,            fragmentShader: `#include varying vec2 vUv;uniform sampler2D colorTexture;uniform vec2 texSize;uniform vec2 direction;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 fSigma = float(SIGMA);          float weightSum = gaussianPdf(0.0, fSigma);          float alphaSum = 0.0;          vec3 diffuseSum = texture2D( colorTexture, vUv).rgb * weightSum;          for( int i = 1; i < KERNEL_RADIUS; i ++ ) {            float x = float(i);            float w = gaussianPdf(x, fSigma);            vec2 uvOffset = direction * invSize * x;            vec4 sample1 = texture2D( colorTexture, vUv + uvOffset);            vec4 sample2 = texture2D( colorTexture, vUv - uvOffset);            diffuseSum += (sample1.rgb + sample2.rgb) * w;            alphaSum += (sample1.a + sample2.a) * w;            weightSum += 2.0 * w;          }          gl_FraGColor = vec4(diffuseSum/weightSum, alphaSum/weightSum);        }`,        });    }    getCompositeMaterial(nMips: number) {        return new ShaderMaterial({            defines: {                NUM_MIPS: nMips,            },            uniforms: {                blurTexture1: { value: null },                blurTexture2: { value: null },                blurTexture3: { value: null },                blurTexture4: { value: null },                blurTexture5: { value: null },                dirtTexture: { value: null },                bloomStrength: { value: 1.0 },                bloomFactors: { value: null },                bloomTintColors: { value: null },                bloomRadius: { value: 0.0 },            },            vertexShader: `varying vec2 vUv;void main() {vUv = uv;gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );}`,            fragmentShader: `varying vec2 vUv;uniform sampler2D blurTexture1;uniform sampler2D blurTexture2;uniform sampler2D blurTexture3;uniform sampler2D blurTexture4;uniform sampler2D blurTexture5;uniform sampler2D dirtTexture;uniform float bloomStrength;uniform float bloomRadius;uniform float bloomFactors[NUM_MIPS];uniform vec3 bloomTintColors[NUM_MIPS];float lerpBloomFactor(const in float factor) {float mirrorFactor = 1.2 - factor;return mix(factor, mirrorFactor, bloomRadius);}void main() {gl_FragColor = bloomStrength * ( lerpBloomFactor(bloomFactors[0]) * vec4(bloomTintColors[0], 1.0) * texture2D(blurTexture1, vUv) +lerpBloomFactor(bloomFactors[1]) * vec4(bloomTintColors[1], 1.0) * texture2D(blurTexture2, vUv) +lerpBloomFactor(bloomFactors[2]) * vec4(bloomTintColors[2], 1.0) * texture2D(blurTexture3, vUv) +lerpBloomFactor(bloomFactors[3]) * vec4(bloomTintColors[3], 1.0) * texture2D(blurTexture4, vUv) +lerpBloomFactor(bloomFactors[4]) * vec4(bloomTintColors[4], 1.0) * texture2D(blurTexture5, vUv) );}`,        });    }}TransparentBackgroundFixedUnrealBloomPass.BlurDirectionX = new Vector2(    1.0,    0.0);TransparentBackgroundFixedUnrealBloomPass.BlurDirectionY = new Vector2(    0.0,    1.0);export { TransparentBackgroundFixedUnrealBloomPass as UnrealBloomPass };

来源地址:https://blog.csdn.net/printf_hello/article/details/132567667

--结束END--

本文标题: 解决Three.js辉光背景不透明

本文链接: https://lsjlt.com/news/382677.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • 解决Three.js辉光背景不透明
    使用此pass canvas元素的background都能看到 不过相应的辉光颜色和背景颜色不相容的地方看起来颜色会怪一些 如图 不过如果是纯色就没什么问题了 //@ts-nocheckimport { AdditiveBle...
    99+
    2023-08-30
    javascript three.js
  • css背景色透明但内容不透明的解决方法
    这篇文章主要讲解了“css背景色透明但内容不透明的解决方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“css背景色透明但内容不透明的解决方法”吧! ...
    99+
    2024-04-02
  • CSS怎么实现背景透明文字不透明
    本篇内容介绍了“CSS怎么实现背景透明文字不透明”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! ...
    99+
    2024-04-02
  • PNG图片在IE6中背景不透明怎么解决
    本篇内容介绍了“PNG图片在IE6中背景不透明怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! ...
    99+
    2024-04-02
  • css实现背景透明文字不透明的方法
    小编给大家分享一下css实现背景透明文字不透明的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!什么是csscss是一种用来表现HTML或XML等文件样式的计算...
    99+
    2023-06-14
  • css怎么实现背景半透明但文字不透明
    这篇文章主要讲解了“css怎么实现背景半透明但文字不透明”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“css怎么实现背景半透明但文字不透明”吧!具体代码:&...
    99+
    2024-04-02
  • 怎么在css中设置文字透明背景不透明
    这期内容当中小编将会给大家带来有关怎么在css中设置文字透明背景不透明,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。css设置文字透明背景不透明<!DOCTYPE html><...
    99+
    2023-06-14
  • 使用CSS3怎么实现背景透明文字不透明
    本篇文章为大家展示了使用CSS3怎么实现背景透明文字不透明,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。<!DOCTYPE html><html lang=&q...
    99+
    2023-06-08
  • css怎么实现背景图片透明文字不透明
    要实现背景图片透明,文字不透明的效果,可以使用CSS的伪元素和定位技巧来实现。首先,将要显示的文字包裹在一个容器元素内。例如,使用一...
    99+
    2023-08-09
    css
  • Css怎么实现背景色透明或半透明但内容不透明
    这篇文章主要讲解了“Css怎么实现背景色透明或半透明但内容不透明”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Css怎么实现背景色透明或半透明但内容不透明”...
    99+
    2024-04-02
  • html能不能设置透明色背景
    本篇内容介绍了“html能不能设置透明色背景”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!html中设置透明色背景的方法:1、直接使用“ba...
    99+
    2023-06-07
  • css如何实现文字不透明背景半透明效果
    这篇文章主要讲解了“css如何实现文字不透明背景半透明效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“css如何实现文字不透明背景半透明效果”吧! ...
    99+
    2024-04-02
  • css怎么实现背景图片半透明内容不透明
    这篇文章将为大家详细讲解有关css怎么实现背景图片半透明内容不透明,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。效果展示半透明不透明常见的失败做法最常见的做法事设置元素的opacity,这种设置出来的效果...
    99+
    2023-06-08
  • 如何解决PNG图片在IE6中背景不透明问题
    这篇文章将为大家详细讲解有关如何解决PNG图片在IE6中背景不透明问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。 JS代码  function c...
    99+
    2024-04-02
  • css怎么实现背景半透明文字不透明的效果
    这篇文章给大家分享的是有关css怎么实现背景半透明文字不透明的效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。效果如下:<!DOCTYPE html><html> &...
    99+
    2023-06-08
  • CSS如何实现背景图片透明而文字不透明效果
    这篇文章给大家分享的是有关CSS如何实现背景图片透明而文字不透明效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。摘要: 方法一(毛玻璃效果):背景图 + 伪类 + flite:blur(3px)方法二(半透明效...
    99+
    2023-06-08
  • HTML设置video为不透明背景的方法
    小编给大家分享一下HTML设置video为不透明背景的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!HTML设置video为不透明背景的方法:使用loop,a...
    99+
    2023-06-06
  • 如何解决chrome浏览器中input背景透明问题
    本篇内容主要讲解“如何解决chrome浏览器中input背景透明问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何解决chrome浏览器中input背景透明...
    99+
    2024-04-02
  • 怎么解决chrome浏览器中input背景透明问题
    这篇文章主要介绍“怎么解决chrome浏览器中input背景透明问题”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么解决chrome浏览器中input背景透明问题...
    99+
    2024-04-02
  • EasyX绘制透明背景图的方法详解
    目录三元光栅操作优化方案三元光栅操作 根据在网上的搜索总结得到两种方案,最常见的绘制带有透明背景的图像的方案都是采用如下的源图像和掩码图像叠加来消去边缘部分: IMAGE img[2...
    99+
    2023-01-06
    EasyX绘制透明背景图 EasyX 透明背景图 EasyX 透明背景
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作