threejs 实现镜面反射,只反射指定物体,背景透明




Reflector.ReflectorShader = {
uniforms: {
'color': {
value: null
},
'tDiffuse': {
value: null
},
'textureMatrix': {
value: null
}
},
vertexShader: /* glsl */`
uniform mat4 textureMatrix;
varying vec4 vUv;
#include <common>
#include <logdepthbuf_pars_vertex>
void main() {
vUv = textureMatrix * vec4( position, 1.0 );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
#include <logdepthbuf_vertex>
}`,
fragmentShader: /* glsl */`
uniform vec3 color;
uniform sampler2D tDiffuse;
varying vec4 vUv;
#include <logdepthbuf_pars_fragment>
float blendOverlay( float base, float blend ) {
return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );
}
vec3 blendOverlay( vec3 base, vec3 blend ) {
return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );
}
void main() {
#include <logdepthbuf_fragment>
vec4 base = texture2DProj( tDiffuse, vUv );
gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );
#include <tonemapping_fragment>
#include <encodings_fragment>
}`
};






import { Reflector } from 'three/examples/jsm/objects/Reflector.js';
import { transparentMirrorShader } from '../xxxx.js'
// 创建镜面反射平面
createMirrorPlane() {
// 创建一个圆形几何体
const radius = 640; // 圆的半径
const segments = 256; // 圆的细分数,细分越多圆形越平滑
const geometry = new THREE.CircleGeometry(radius, segments);
// 创建 Reflector
const reflector = new Reflector(geometry, {
color: 0x000000, // 设置反射颜色
textureWidth: window.innerWidth * window.devicePixelRatio, // 反射纹理宽度
textureHeight: window.innerHeight * window.devicePixelRatio, // 反射纹理高度
shader: transparentMirrorShader,
clipBias: 0.000 // 裁剪偏移量
});
const subModelNameList = ['wall', '天空盒']
reflector.originOnBeforeRender = reflector.onBeforeRender;
reflector.onBeforeRender = function (renderer, scene, camera) {
const scene1 = scene.clone();
scene1.traverse(child => {
if (!child.isScene && !child.isLight && child.name && subModelNameList.some(ele => child.name.includes(ele))) {
if (['wall'].some(ele => child.name.includes(ele))) {
child.visible = true;
} else {
child.visible = false;
}
}
});
reflector.originOnBeforeRender(renderer, scene1, camera);
}
// 设置位置和旋转
reflector.position.set(0, -0.01, 0);
reflector.rotation.x = -Math.PI / 2;
reflector.name = '镜面反射平面';
reflector.material.transparent = true;
// 添加到场景
this.scene.add(reflector);
},
export const transparentMirrorShader = {
uniforms: {
'color': {
value: null
},
'tDiffuse': {
value: null
},
'textureMatrix': {
value: null
}
},
vertexShader: /* glsl */`
uniform mat4 textureMatrix;
varying vec4 vUv;
#include <common>
#include <logdepthbuf_pars_vertex>
void main() {
vUv = textureMatrix * vec4( position, 1.0 );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
#include <logdepthbuf_vertex>
}`,
fragmentShader: /* glsl */`
uniform vec3 color;
uniform sampler2D tDiffuse;
varying vec4 vUv;
#include <logdepthbuf_pars_fragment>
float blendOverlay( float base, float blend ) {
return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );
}
vec3 blendOverlay( vec3 base, vec3 blend ) {
return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );
}
void main() {
#include <logdepthbuf_fragment>
vec4 base = texture2DProj( tDiffuse, vUv );
// 检查是否有有效的反射纹理,如果没有则透明
if (base.a < 0.1) {
discard;
}
gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1 );
#include <tonemapping_fragment>
#include <encodings_fragment>
}`
}
threejs 实现镜面反射,只反射指定物体,背景透明的更多相关文章
- HTML input="file" 浏览时只显示指定文件类型 xls、xlsx、csv
html input="file" 浏览时只显示指定文件类型 xls.xlsx.csv <input id="fileSelect" type=" ...
- <input type="file" />浏览时只显示指定文件类型
<input type="file" />浏览时只显示指定文件类型 <input type="file" accept="appli ...
- MySQL 如何只导出 指定的表 的表结构和数据 ( 转 )
MySQL 如何只导出 指定的表 的表结构和数据 ( 转 ) 2011-01-04 15:03:33 分类: MySQL MySQL 如何只导出 指定的表 的表结构和数据 导出更个库的表结构如下:my ...
- 通过ASP禁止指定IP和只允许指定IP访问网站的代码
过ASP禁止指定IP和只允许指定IP访问网站的代码,需要的朋友可以参考下. 一.禁止指定IP防问网站,并执行相应操作: 代码如下: <% Dim IP,IPString,VisitIP '设置I ...
- log4j配置只打印指定jar或包的DEBUG信息
有的时候查问题的时候需要打印第三方jar里面的debug信息,假如全部jar都打印的话日志文件会很大,这个时候可以配置log4j只打印指定jar的debug信息或者包,同时输出到了一个新的文件中. 比 ...
- findstr 只搜寻指定文件类型
Title:findstr 只搜寻指定文件类型 --2012-05-04 09:27 findstr /i /m /S /C:"关键字" *.php *.asp *.jsp
- 只允许指定的ip访问本机的指定端口22:
只允许指定的ip访问本机的指定端口22: 允许的的ip:192.168.1.123, 192.168.1.124, 192.168.1.100,其他ip都禁止访问. 切换到root用户 1.在tcp协 ...
- charles只获取指定的请求的设置方法
过滤网络请求 通常情况下,需要对网络请求进行过滤,只监控指定服务器的请求.有3种办法: 方法一:在主界面的中部的 Filter 栏中输入需要过滤出来的关键字.例如我们的服务器的地址(host)是:ww ...
- iptables只允许指定ip地址访问指定端口
首先,清除所有预设置 iptables -F#清除预设表filter中的所有规则链的规则 iptables -X#清除预设表filter中使用者自定链中的规则 其次,设置只允许指定ip地址访问指定端口 ...
- linux下通过iptables只允许指定ip地址访问指定端口的设置方法
这篇文章主要介绍了linux下通过iptables只允许指定ip地址访问指定端口的设置方法,需要的朋友可以参考下. 首先,清除所有预设置 iptables -F#清除预设表filter中的所有规则链的 ...
随机推荐
- Apache Shiro 721反序列化漏洞复现
目录 漏洞原理 复现 修复方式 漏洞原理 Shiro 的RememberMe Cookie使用的是 AES-128-CBC 模式加密.其中 128 表示密钥长度为128位,CBC 代表Cipher B ...
- 高性能计算-雅可比算法-MPI重复非阻塞优化(7)
#include <stdio.h> #include <mpi.h> #include <unistd.h> #include <stdlib.h> ...
- 《刚刚问世》系列初窥篇-Java+Playwright自动化测试-6- 元素基础定位方式-上篇 (详细教程)
1.简介 从这篇文章开始,就开始要介绍UI自动化核心的内容,也是最困难的部分了,就是:定位元素,并去对定位到的元素进行一系列相关的操作.想要对元素进行操作,第一步,也是最重要的一步,就是要找到这个元素 ...
- 反汇编动态调试器之x64dbg
转载:https://cloud.tencent.com/developer/article/2337843 x64dbg 是一款开源.免费.功能强大的动态反汇编调试器,它能够在Windows平台上进 ...
- python模块之sqlite3
在Python中操作sqlite3 1)基本使用 import sqlite3 conn = sqlite3.connect('example.db') cursor = conn.cursor() ...
- px转换为rem,响应式js
(function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ...
- 2022-2023 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2022)
F. Foreign Football 一共有\(n\)支队伍,每支队伍的名称为\(s_i\),给定一个\(n \times n\)的矩阵,\(a_{i,j}\)代表第\(i\)支队伍和第\(j\)支 ...
- Ubuntu默认启动到字符界面
修改/etc/default/grub sudo cp /etc/default/grub /etc/default/grub.bak sudo chmod 0777 /etc/default/gru ...
- 鸿蒙应用开发从入门到入行 - 篇3:ArkUI布局基础与制作可交互页面
鸿蒙应用开发从入门到入行 - 篇3:ArkUI布局基础与制作可交互页面 导读:在本篇文章里,您将掌握事件.装饰器.双向绑定等相关知识,并利用所学知识做一个待办列表的案例. 练手案例:登录界面 开始之前 ...
- # 为什么要使用 MediatR 的 3 个理由和 1 个不使用它的原因
为什么要使用 MediatR 的 3 个理由和 1 个不使用它的原因 https://codeopinion.com/why-use-mediatr-3-reasons-why-and-1-reaso ...