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中的所有规则链的 ...
随机推荐
- [TAD] Triangles of Absolute Differences-反帕斯卡三角形
[IMO2018] Triangles of Absolute Differences-反帕斯卡三角形 前言 叠甲 笔者不是学数竞的,在此感谢我的数竞生为我讲解题目. 笔者学艺不精,且知识面浅薄. 所 ...
- php 读取 csv 转数组列表
我们有个文档需要修改,但是文档列太多,以及数量太大,以至于眼睛看起来很吃力,于是我决定做个简单的转化用脚本读取我想要验证的列的内容是否正确. 于是就产生了一个这样将csv快速的转为数组列表的功能函数 ...
- 纪念第一次在 Github 上提 ISSUE 得到了老哥的回复
背景 第一次在 GitHub 上提 ISSUE,提问的内容就是我的上一篇博文 rustlings v6.0 运行时出现 " You are trying to run Rustlings u ...
- Java基础总结大纲(一)
1.JVM.JRE.和JDK的区别: JVM(Java Virtual Machine):java虚拟机,用于保证java的跨平台的特性.说明:java是跨平台的而JVM不是跨平台的,正对的不同的语言 ...
- PySAGES实记
技术背景 PySAGES是一款可以使用GPU加速的增强采样插件,它可以直接对接到OpenMM上进行增强采样分子动力学模拟,这里我们测试一下相关的安装,并尝试跑一个简单的增强采样示例. 安装PySAGE ...
- linux虚拟ip原理
在 Linux 中,虚拟 IP(Virtual IP,VIP)是指一组与物理网络接口卡 (NIC) 绑定的虚拟 IP 地址,这些 IP 地址并不是物理上存在的,而是通过软件模拟实现的. Linux 实 ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online (The 2nd Universal Cup
The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online (The 2nd Universal Cup. Stage 1: Qingdao) J ...
- MySQL 8.0 为什么会放弃查询缓存?
什么是查询缓存? 查询缓存就是将一次查询结果存储在内存中,假如下一次查询结果在内存中,就直接在内存中读取. 设计初衷 当然是提高性能,通过缓存来减少解析器.优化器.存储引擎的执行时间. MySQL查询 ...
- SPRING 动态注册BEAN
场景 有些情况下,不能直接使用BEAN的方式: @Bean(name = "storage") public DataSourceProxy storageDataSourcePr ...
- git 本地项目与远程地址建立连接
git 本地项目与远程地址建立连接 建立好远程仓库与本地项目地址后 在本地项目文件夹内初始化git仓库 git init 复制远程项目路径地址,后执行: git remote add origin 远 ...