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中的所有规则链的 ...
随机推荐
- 在哪里可以找到官方的mysql容器图像?
如果您在容器上部署MySQL,那么首要任务之一就是找到正确的镜像. 有一定程度的混乱,尤其是当我们试图帮助部署有问题的人时. 例如,当人们说我使用的是官方的docker镜像- 这到底意味着什么?Doc ...
- CF926 Div.2
C. Sasha and the Casino 赌场规则:如果下注 \(y(y > 0)\) 元,如果赢了则除了 \(y\) 元外,额外获得 \(y \times (k - 1)\) 元,否则则 ...
- less 动态样式语言
1.less的介绍 Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展 官方文档 2.less需要编译才能被浏览器解析 浏览器 ...
- Python 调整Excel行高、列宽
在Excel中,默认的行高和列宽可能不足以完全显示某些单元格中的内容,特别是当内容较长时.通过调整行高和列宽,可以确保所有数据都能完整显示,避免内容被截断.合理的行高和列宽可以使表格看起来更加整洁和专 ...
- 一图一知-vue强大的slot
vue常用的slot知识点记录
- DA14531芯片固件逆向系列(2)- 操作系统底层机制分析
首发于先知论坛 https://xz.aliyun.com/t/9186 概述 DA145x软件平台利用了由Riviera Waves许可的小型高效实时内核,内核提供以下功能: 任务创建和状态转换. ...
- DDCA —— 片上网络互联
1. 路由 1.1 网络拓扑示例 Grid(网格) 网络拓扑通常是一个二维矩阵形式,每个节点(处理器)与其上下左右相邻的节点相连. 如果节点在边缘,某些方向上可能没有相邻节点(边界节点). Torus ...
- idea左上角project一片绿的解决方法
idea突然project底色一片绿,真的心里慌得一批.. 解决方法: 打开File-Settings,按图示找到地方,关闭FileColor,即可.
- GienTech动态|入选软件和信息技术服务名牌企业;荣获城市数字化转型优秀案例;参加第四届深圳国际人工智能展
中电金信入选"2023第二届软件和信息技术服务名牌企业" 近日,中国电子信息行业联合会发布了"2023第二届软件和信息技术服务名牌企业"名单,中电金信入 ...
- 【Python】conda基本使用、pip换源、pip超时问题解决
conda问题 重要警告:安装conda的时候,安装目录不要包含空格以及特殊字符,最好不要直接装在C盘根目录, 往期笔记 conda安装: https://www.cnblogs.com/mllt/p ...