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中的所有规则链的 ...
随机推荐
- windows下git安装以及基本使用
Git的基本使用 Windows下Git的基本使用与操作 安装Git 这个是一个git的windows系统的命令行版本 https://git-scm.com/downloads 安装 tortois ...
- uniapp对接个推推送以及模拟器调试
uniapp 推送需要配置的内容https://ask.dcloud.net.cn/article/35622 推送流程 参考 https://blog.csdn.net/m0_67401660/ar ...
- OpenWRT/iStoreOS 不从头编译内核安装4G LTE网卡 Quectel EM05-CE记录
我的机器是x86装了iStoreOS,有4G网卡Quectel EM05 https://www.quectel.com/cn/product/lte-em05 主要参考资料如下 https://ww ...
- 推荐手绘工具神器Excalidraw素描草图风格白板,支持AI-开源免费
推荐手绘工具神器Excalidraw素描草图风格白板,支持AI-开源免费 原创 IT软件部落 IT软件部落 Excalidraw 一个开源的虚拟手绘风格的白板,是一个很好的素描工具.它真的很容易使用, ...
- clickhouse之安装与基本使用
近期要做一个数据统计功能,公司选择了clickhouse作为数据库:下面记录一下该数据库的特性和使用教程. clickhouse是一个列式数据库,主要用于数据分析:从目前使用看来,特点如下: 列式存储 ...
- 2018-2019 9th BSUIR Open Programming Championship
I. Equal Mod Segments \(1 \leq n \leq 1e5\) \(1 \leq a_i \leq 3e5\) 题解:ST表 + 扫描线 + 二维偏序 取模存在一个不错的性质: ...
- AtCoder Beginner Contest 152
Flatten 给定\(n\)个正整数\(a_i\),,现在让你求出\(n\)个整数\(b_i\),使得任取\(1\le i < j \le n\),\(a_ib_i=a_jb_j\)始终成立, ...
- 某开源ERP最新版SQL与RCE的审计过程
文章首发于 https://forum.butian.net/share/134 前言 代码路径 https://gitee.com/jishenghua/JSH_ERP 软件版本 华夏ERP_v2. ...
- orangepi zero3 使用dd命令进行SD卡系统备份与还原
1. 使用dd命令备份整个sd卡 首先使用 df -h命令查看sd卡挂载名,如下所示,sd卡挂载为 /dev/sdc meng@meng:~/桌面/code$ df -h 文件系统 大小 已用 可用 ...
- Teams 无法访问的问题
排除以下 login.partner.microsoftonline.cn; login.microsoftonline.com; microsoft.com; live.com; hotmail.c ...