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

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

//@ts-nocheck
/** @Author: hongbin* @Date: 2023-04-06 11:44:14* @LastEditors: hongbin* @LastEditTime: 2023-04-06 11:49:23* @Description:*/
import {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-ignore
import { 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";/*** Thanks to https://github.com/mrdoob/three.js/issues/14104#issuecomment-429664412 for this fragmentShaderfix** UnrealBloomPass is inspired by the bloom pass of Unreal Engine. It creates a* mip map chain of bloom textures and blurs them with different radii. Because* of the weighted combination of mips, and because larger blurs are done on* higher mips, this effect provides good quality and performance.** Reference:* - https://docs.unrealengine.com/latest/INT/Engine/Rendering/PostProcessEffects/Bloom/*/
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 functionthis.clearColor = new Color(0, 0, 0);// render targetsconst 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 materialif (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 Materialsthis.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 materialthis.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 materialif (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 screenif (this.renderToScreen) {this.fsQuad.material = this.basic;this.basic.map = readBuffer.texture;renderer.setRenderTarget(null);renderer.clear();this.fsQuad.render(renderer);}// 1. Extract Bright Areasthis.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 progressivelylet 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 mipsthis.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 texturethis.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 settingsrenderer.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 <common>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 };

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/58935.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

电缆工厂 3D 可视化管控系统 | 智慧工厂

近年来&#xff0c;我国各类器材制造业已经开始向数字化生产转型&#xff0c;使得生产流程变得更加精准高效。通过应用智能设备、物联网和大数据分析等技术&#xff0c;企业可以更好地监控生产线上的运行和质量情况&#xff0c;及时发现和解决问题&#xff0c;从而提高生产效率…

libevent源码学习3---事件event

libevent源码学习3—事件event libevent 的基本操作单元是事件。每个事件代表一组条件的集合, 这些条件包括: 文件描述符已经就绪, 可以读取或者写入文件描述符变为就绪状态,可以读取或者写入(仅对于边沿触发 IO)超时事件发生某信号用户触发事件 所有事件具有相似的生命周期。…

SpringBoot集成Redis、Redisson保姆教程【附源码】

1. SpringBoot集成Redis 关于Redis的安装,这里就不重复介绍了,需要的朋友可以看我之前的博文Redis多系统安装(Windows、Linux、Ubuntu) Redis原生命令大全,作者整理的很详细,大部分命令转化为java命令基本也是关键词 Redis 命令参考 接下来开始我们的正题,一起学习下…

Windows域环境下,GPO部署的注意事项。

文章目录 GPO部署的注意事项1. 计划和测试2. 命名和文档化3. 安全性4. OU&#xff08;组织单位&#xff09;结构5. GPO优先级6. 禁用不需要的GPO7. 回滚计划8. 日志和审计9. 版本控制10. 性能影响11. 考虑用户体验12. 更新和维护 推荐阅读 在Windows域环境中&#xff0c;使用组…

深度学习推荐系统(二)Deep Crossing及其在Criteo数据集上的应用

深度学习推荐系统(二)Deep Crossing及其在Criteo数据集上的应用 在2016年&#xff0c; 随着微软的Deep Crossing&#xff0c; 谷歌的Wide&Deep以及FNN、PNN等一大批优秀的深度学习模型被提出&#xff0c; 推荐系统全面进入了深度学习时代&#xff0c; 时至今日&#xff0c…

跨站请求伪造(CSRF)

文章目录 渗透测试漏洞原理跨站请求伪造&#xff08;CSRF&#xff09;1. CSRF概述1.1 基本概念1.1.1 关键点1.1.2 目标 1.2 CSRF场景1.2.1 银行账户转账1.2.2 构造虚假网站1.2.3 场景建模1.2.4 实验 1.3 CSRF类别1.3.1 POST方式 1.4 CSRF验证1.4.1 CSRF Poc generator 1.5 XSS与…

SpringAOP详解(下)

proxyFactory代理对象创建方式和代理对象调用方法过程&#xff1a; springaop创建动态代理对象和代理对象调用方法过程&#xff1a; 一、TargetSource的使用 Lazy注解&#xff0c;当加在属性上时&#xff0c;会产生一个代理对象赋值给这个属性&#xff0c;产生代理对象的代码为…

政府科技项目验收全流程分享

科技验收测试 &#xff08;验收申请→主管部门初审→科技厅审核→组织验收→归档备案→信用管理&#xff09;&#xff1a; &#xff08;一&#xff09;验收申请 项目承担单位通过省科技业务管理系统提交验收申请。 按期完成的项目&#xff0c;项目承担单位应当在项目合同书…

C 实现Window/DOS 键盘监听事件

今天是重新复习C语言实现的第一天&#xff0c;今天想编写C 对Windwos/Dos 键盘事件的学习。但是我在安装Visual Studio 2022 没有安装MFC 框架&#xff0c;今天记录下VS追加 MFC框架。 Visual Studio 2022 追加MFC 1、打开vs&#xff0c;点击创建新项目&#xff0c;右侧滑动框…

docker 学习-- 04 实践2 (lnpmr环境)

docker 学习 系列文章目录 docker 学习-- 01 基础知识 docker 学习-- 02 常用命令 docker 学习-- 03 环境安装 docker 学习-- 04 实践 1&#xff08;宝塔&#xff09; docker 学习-- 04 实践 2 &#xff08;lnpmr环境&#xff09; 文章目录 docker 学习 系列文章目录1. 配…

5G智能网关如何解决城市停车痛点难点

2023年上半年&#xff0c;我国汽车新注册登记1175万辆&#xff0c;同比增长5.8%&#xff0c;88个城市汽车保有量超过100万辆&#xff0c;北京、成都等24个城市超过300万辆。随着车辆保有量持续增加&#xff0c;停车难问题长期困扰城市居民&#xff0c;也导致城市路段违停普遍、…

制作鲜花商城小程序的详细步骤

如果你是一个新手商家&#xff0c;想要进入鲜花团购市场&#xff0c;但是不知道如何制作一个小程序商城&#xff0c;那么这篇文章就是为你准备的。以下是制作鲜花团购小程序商城的详细步骤&#xff1a; 1. 登录乔拓云平台后台&#xff0c;进入商城管理页面 首先&#xff0c;你需…

【openGauss2.1.0 TPC-C数据导入】

openGauss2.1.0 TPC-C数据导入 一、下载tpch测试数据二、导入测试数据 一、下载tpch测试数据 使用普通用户如omm登录服务器执行如下命令下载测试数据库&#xff1a;git clone https://gitee.com/xzp-blog/tpch-kit.git二、导入测试数据 进入dbgen目录下&#xff0c;生成makef…

stm32+FreeRTOS遇到的坑

问题 最近把项目移植进FreeRTOS时&#xff0c;发现开始任务都不能启动&#xff1b; 机器是stm32f407&#xff0c;使用的arm固件和FreeRTOS都是最新版本&#xff1b; 裸机运行没有问题&#xff0c;但是使用任务运行就运行不了&#xff1b; 排查 首先尝试了教程用的点灯代码…

虚拟化技术:云计算发展的核心驱动力

文章目录 虚拟化技术的概念和作用虚拟化技术的优势虚拟化技术对未来发展的影响结论 &#x1f389;欢迎来到AIGC人工智能专栏~虚拟化技术&#xff1a;云计算发展的核心驱动力 ☆* o(≧▽≦)o *☆嗨~我是IT陈寒&#x1f379;✨博客主页&#xff1a;IT陈寒的博客&#x1f388;该系…

WebGL矩阵变换库

目录 矩阵变换库&#xff1a; Matrix4对象所支持的方法和属性如表所示&#xff1a; 方法属性规范&#xff1a; 虽然平移、旋转、缩放等变换操作都可以用一个44的矩阵表示&#xff0c;但是在写WebGL程序的时候&#xff0c;手动计算每个矩阵很耗费时间。为了简化编程&#xf…

opencv 案例05-基于二值图像分析(简单缺陷检测)

缺陷检测&#xff0c;分为两个部分&#xff0c;一个部分是提取指定的轮廓&#xff0c;第二个部分通过对比实现划痕检测与缺角检测。本次主要搞定第一部分&#xff0c;学会观察图像与提取图像ROI对象轮廓外接矩形与轮廓。 下面是基于二值图像分析的大致流程 读取图像将图像转换…

SQLite 3.43.0 发布,又有啥新功能?

SQLite 开发团队于 2023 年 08 月 24 日发布了 SQLite 3.43.0 版本。本文给大家分析一下该版本的更新。 全文索引 SQLite 3.43.0 增加了 Contentless-Delete FTS5 索引。这是一种 FTS5 全文索引的变种&#xff0c;不存储被索引的内容&#xff0c;同时支持数据的删除操作。 例…

Rust多线程编程

Rust多线程编程 文章目录 Rust多线程编程使用线程模块创建线程线程传参闭包&#xff08;匿名函数&#xff09;值捕获不可变引用捕获可变引用捕获 线程闭包传参更优雅地传参 回收线程线程同步和通信channel 通道mutex 互斥锁Barrier 栅栏Atomic Types 原子类型 使用线程模块 ru…

Spring Cloud + Spring Boot 项目搭建结构层次示例讲解

Spring Cloud Spring Boot 项目搭建结构层次示例讲解 Spring Cloud 项目搭建结构层次示例Spring Cloud示例&#xff1a; Spring Boot 项目搭建结构层次讲解Spring Boot 项目通常按照一种常见的架构模式组织&#xff0c;可以分为以下几个主要层次&#xff1a;当构建一个 Spring…