学习threejs,实现炫酷流动霓虹效果

news2026/5/11 16:02:52
‍⚕️主页 gis分享者‍⚕️感谢各位大佬 点赞 收藏⭐ 留言 加关注✅!‍⚕️收录于专栏threejs gis工程师文章目录一、前言1.1 ☘️THREE.ShaderMaterial1.1.1 ☘️注意事项1.1.2 ☘️构造函数1.1.3 ☘️属性1.1.4 ☘️方法二、实现炫酷流动霓虹效果1. ☘️实现思路2. ☘️代码样例一、前言本文详细介绍如何基于threejs在三维场景中实现炫酷流动霓虹效果亲测可用。希望能帮助到您。一起学习加油加油1.1 ☘️THREE.ShaderMaterialTHREE.ShaderMaterial使用自定义shader渲染的材质。 shader是一个用GLSL编写的小程序 在GPU上运行。1.1.1 ☘️注意事项ShaderMaterial 只有使用 WebGLRenderer 才可以绘制正常 因为 vertexShader 和fragmentShader 属性中GLSL代码必须使用WebGL来编译并运行在GPU中。从 THREE r72开始不再支持在ShaderMaterial中直接分配属性。 必须使用BufferGeometry实例使用BufferAttribute实例来定义自定义属性。从 THREE r77开始WebGLRenderTarget 或 WebGLCubeRenderTarget实例不再被用作uniforms。 必须使用它们的texture 属性。内置attributes和uniforms与代码一起传递到shaders。如果您不希望WebGLProgram向shader代码添加任何内容则可以使用RawShaderMaterial而不是此类。您可以使用指令#pragma unroll_loop_start#pragma unroll_loop_end以便通过shader预处理器在GLSL中展开for循环。 该指令必须放在循环的正上方。循环格式必须与定义的标准相对应。循环必须标准化normalized。循环变量必须是i。对于给定的迭代值 UNROLLED_LOOP_INDEX 将替换为 i 的显式值并且可以在预处理器语句中使用。#pragma unroll_loop_startfor(int i0;i10;i){// ...}#pragma unroll_loop_end代码示例constmaterialnewTHREE.ShaderMaterial({uniforms:{time:{value:1.0},resolution:{value:newTHREE.Vector2()}},vertexShader:document.getElementById(vertexShader).textContent,fragmentShader:document.getElementById(fragmentShader).textContent});1.1.2 ☘️构造函数ShaderMaterial( parameters : Object )parameters - (可选)用于定义材质外观的对象具有一个或多个属性。 材质的任何属性都可以从此处传入(包括从Material继承的任何属性)。1.1.3 ☘️属性共有属性请参见其基类Material。.clipping: Boolean定义此材质是否支持剪裁; 如果渲染器传递clippingPlanes uniform则为true。默认值为false。.defaultAttributeValues: Object当渲染的几何体不包含这些属性但材质包含这些属性时这些默认值将传递给shaders。这可以避免在缓冲区数据丢失时出错。this.defaultAttributeValues{color:[1,1,1],uv:[0,0],uv2:[0,0]};.defines: Object使用 #define 指令在GLSL代码为顶点着色器和片段着色器定义自定义常量每个键/值对产生一行定义语句defines:{FOO:15,BAR:true}这将在GLSL代码中产生如下定义语句#defineFOO15#defineBARtrue.extensions: Object一个有如下属性的对象this.extensions{derivatives:false,// set to use derivativesfragDepth:false,// set to use fragment depth valuesdrawBuffers:false,// set to use draw buffersshaderTextureLOD:false// set to use shader texture LOD};.fog: Boolean定义材质颜色是否受全局雾设置的影响; 如果将fog uniforms传递给shader则为true。默认值为false。.fragmentShader: String片元着色器的GLSL代码。这是shader程序的实际代码。在上面的例子中 vertexShader 和 fragmentShader 代码是从DOMHTML文档中获取的 它也可以作为一个字符串直接传递或者通过AJAX加载。.glslVersion: String定义自定义着色器代码的 GLSL 版本。仅与 WebGL 2 相关以便定义是否指定 GLSL 3.0。有效值为 THREE.GLSL1 或 THREE.GLSL3。默认为空。.index0AttributeName: String如果设置则调用gl.bindAttribLocation 将通用顶点索引绑定到属性变量。默认值未定义。.isShaderMaterial: Boolean只读标志用于检查给定对象是否属于 ShaderMaterial 类型。.lights: Boolean材质是否受到光照的影响。默认值为 false。如果传递与光照相关的uniform数据到这个材质则为true。默认是false。.linewidth: Float控制线框宽度。默认值为1。由于OpenGL Core Profile与大多数平台上WebGL渲染器的限制无论如何设置该值线宽始终为1。.flatShading: Boolean定义材质是否使用平面着色进行渲染。默认值为false。.uniforms: Object如下形式的对象{uniform1:{value:1.0},uniform2:{value:2}}指定要传递给shader代码的uniforms键为uniform的名称值(value)是如下形式{value:1.0}这里 value 是uniform的值。名称必须匹配 uniform 的name和GLSL代码中的定义一样。 注意uniforms逐帧被刷新所以更新uniform值将立即更新GLSL代码中的相应值。.uniformsNeedUpdate: Boolean可用于在 Object3D.onBeforeRender() 中更改制服时强制进行制服更新。默认为假。.vertexColors: Boolean定义是否使用顶点着色。默认为假。.vertexShader: String顶点着色器的GLSL代码。这是shader程序的实际代码。 在上面的例子中vertexShader 和 fragmentShader 代码是从DOMHTML文档中获取的 它也可以作为一个字符串直接传递或者通过AJAX加载。.wireframe: Boolean将几何体渲染为线框(通过GL_LINES而不是GL_TRIANGLES)。默认值为false即渲染为平面多边形。.wireframeLinewidth: Float控制线框宽度。默认值为1。由于OpenGL Core Profile与大多数平台上WebGL渲染器的限制无论如何设置该值线宽始终为1。1.1.4 ☘️方法共有方法请参见其基类Material。.clone (): ShaderMaterial this :ShaderMaterial创建该材质的一个浅拷贝。需要注意的是vertexShader和fragmentShader使用引用拷贝 attributes的定义也是如此; 这意味着克隆的材质将共享相同的编译WebGLProgram 但是uniforms 是 值拷贝这样对不同的材质我们可以有不同的uniforms变量。二、实现炫酷流动霓虹效果1. ☘️实现思路通过threejs的ShaderMaterial自定义着色器实现炫酷流动霓虹效果。具体代码参考下面代码样例。2. ☘️代码样例!DOCTYPEHTMLPUBLIC-//W3C//DTD HTML 4.01 Transitional//ENhttp://www.w3.org/TR/html4/loose.dtdhtmlheadmetacharsetUTF-8title炫酷流动霓虹/title!-- Include Three.js --scriptsrchttps://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js/script!-- Include lil-gui for the settings panel --scriptsrchttps://cdn.jsdelivr.net/npm/lil-gui0.19/scriptstylebody{margin:0;padding:0;overflow:hidden;background-color:#000;font-family:sans-serif;}canvas{display:block;width:100vw;height:100vh;}/style/headbody/bodyscript// Support me by PayPal: https://paypal.com/paypalme/sabosugi// --- SCENE SETUP ---constscenenewTHREE.Scene();// Use an Orthographic camera for a 2D full-screen shaderconstcameranewTHREE.OrthographicCamera(-1,1,1,-1,0,1);constrenderernewTHREE.WebGLRenderer({antialias:true,alpha:false});renderer.setSize(window.innerWidth,window.innerHeight);renderer.setPixelRatio(window.devicePixelRatio);document.body.appendChild(renderer.domElement);// --- SHADER MATERIAL ---// Vertex Shader: Simply passes the UV coordinates to the fragment shaderconstvertexShadervarying vec2 vUv; void main() { vUv uv; gl_Position vec4(position, 1.0); };// Fragment Shader: Generates the glowing wavesconstfragmentShaderuniform float uTime; uniform vec2 uResolution; uniform vec3 uColor1; // Primary color (Blue) uniform vec3 uColor2; // Secondary color (Pink) uniform vec3 uBgColor; // Background color uniform float uSpeed; uniform float uComplexity; // Drives the number of mathematical iterations uniform float uDensity; uniform float uIntensity; // Mouse interaction uniforms uniform vec2 uMouse; uniform float uHoverEffect; varying vec2 vUv; // 2D Rotation matrix mat2 rot(float a) { float s sin(a), c cos(a); return mat2(c, -s, s, c); } void main() { // Normalize pixel coordinates vec2 p (gl_FragCoord.xy * 2.0 - uResolution.xy) / min(uResolution.x, uResolution.y); vec2 original_p p; // Store original coordinates for the vignette vec3 finalColor vec3(0.0); float time uTime * uSpeed * 0.5; // Add initial cinematic tilt p * rot(0.2); // --- MOUSE HOVER EFFECT --- if (uHoverEffect 0.0) { // Normalize mouse coordinates to match the screen space vec2 m uMouse; m.x * uResolution.x / min(uResolution.x, uResolution.y); m.y * uResolution.y / min(uResolution.x, uResolution.y); // Apply the same tilt to the mouse coordinates m * rot(0.2); // Calculate distance between pixel and mouse float mouseDist length(p - m); // Use smoothstep for a very soft, bounded area of effect float force smoothstep(1.5, 0.0, mouseDist) * uHoverEffect; // Increased the displacement and twist multipliers to make the effect more visible // while keeping (p - m) to prevent the black hole singularity in the center p (p - m) * force * 0.15; // Increased from 0.03 for a stronger push p * rot(force * 0.12); // Increased from 0.02 for a stronger visible twist } // --- ITERATIVE FUNCTION SYSTEM (Complex Formula) --- // This loop folds space over itself recursively to create high-detail abstract structures. float iterations floor(uComplexity); for (float i 1.0; i 20.0; i) { if (i iterations) break; // Allow dynamic complexity via GUI // Scale and rotate space organically, but more uniformly to keep lines parallel p * rot(sin(time * 0.05) * 0.1 0.08); // --- HARMONIC VORTEX FLUID --- vec2 q p; // 1. Smooth Vortex twist: wider, gentler rotation float dist length(p); q * rot(dist * uDensity * 0.25 - time * 0.3); // 2. Harmonic Folds: Frequencies are mathematically related (1.0x, 2.0x, 0.5x) float freq uDensity * 0.8; // Layer 1 folds (Base frequency, uniform phase shift per iteration) q.x sin(q.y * freq time * 0.5 i * 0.15) * 0.5; q.y cos(q.x * freq - time * 0.5 - i * 0.15) * 0.5; vec2 r q; // Layer 2 ripples (Double frequency, half amplitude for harmony) r.x sin(q.y * freq * 2.0 - time * 0.8) * 0.25; r.y cos(q.x * freq * 2.0 time * 0.8) * 0.25; // Combine into a synchronized, rhythmic ribbon float wave sin(r.x * freq * 1.5 time) * 0.6 cos(r.y * freq * 0.5 - time * 0.7) * 0.4; // Calculate distance to the warped geometric boundary float d abs(r.y - wave); // Complex rendering layer: Hard core dual soft ambient glows float core 0.005 / max(d, 0.002); float soft1 exp(-d * 8.0) * 0.6; float soft2 exp(-d * 2.0) * 0.2; // Dynamic Chromatic Dispersion (Color mapping) float mixFactor sin(r.x * 3.0 r.y * 2.0 time i * 1.6) * 0.5 0.5; vec3 layerColor mix(uColor1, uColor2, mixFactor); // Accumulate lighting using additive blending float attenuation 1.0 / (i * 0.6 1.0); finalColor layerColor * (core soft1 soft2) * uIntensity * attenuation * 30.0; // Fractal zoom for the next iteration (scales the space) p r * 1.05; } // --- POST-PROCESSING --- // 1. Blend with deep background finalColor uBgColor * 0.5; // 2. Cinematic Vignette (darken edges) float vignette 1.0 - smoothstep(0.5, 2.5, length(original_p)); finalColor * vignette; // 3. ACES Filmic HDR Tone Mapping (Approximation) // This prevents the intense glowing colors from washing out into flat white finalColor finalColor * (2.51 * finalColor 0.03) / (finalColor * (2.43 * finalColor 0.59) 0.14); gl_FragColor vec4(finalColor, 1.0); };// Define the uniform variables passed to the shaderconstuniforms{uTime:{value:0.0},uResolution:{value:newTHREE.Vector2(window.innerWidth*window.devicePixelRatio,window.innerHeight*window.devicePixelRatio)},uColor1:{value:newTHREE.Color(#0055ff)},// Electric BlueuColor2:{value:newTHREE.Color(#ff00aa)},// Hot PinkuBgColor:{value:newTHREE.Color(#05030a)},// Very dark violet/blackuSpeed:{value:0.3},uComplexity:{value:8.0},uDensity:{value:3.2535},uIntensity:{value:0.03758},uMouse:{value:newTHREE.Vector2(0,0)},uHoverEffect:{value:0.0}// Disabled by default};constmaterialnewTHREE.ShaderMaterial({vertexShader:vertexShader,fragmentShader:fragmentShader,uniforms:uniforms,depthWrite:false,depthTest:false});// Create a full-screen plane to render the shader onconstgeometrynewTHREE.PlaneGeometry(2,2);constmeshnewTHREE.Mesh(geometry,material);scene.add(mesh);// --- MOUSE TRACKING LOGIC ---// Use a target vector to smoothly interpolate the actual mouse positionconsttargetMousenewTHREE.Vector2(0,0);functionupdateMousePosition(clientX,clientY){// Convert mouse position to normalized device coordinates (-1 to 1)targetMouse.x(clientX/window.innerWidth)*2-1;targetMouse.y-(clientY/window.innerHeight)*21;}window.addEventListener(mousemove,(e){updateMousePosition(e.clientX,e.clientY);});// Support for touch deviceswindow.addEventListener(touchmove,(e){if(e.touches.length0){updateMousePosition(e.touches[0].clientX,e.touches[0].clientY);}},{passive:true});// --- LIL-GUI SETTINGS PANEL ---// State object to hold our GUI valuesconstsettings{speed:0.3,complexity:8,density:3.2535,intensity:0.03758,hoverEffect:false,// Checkbox statecolor1:#0055ff,color2:#ff00aa,bgColor:#05030a};constguinewlil.GUI({title:Shader Settings});// Animation controlsconstanimFoldergui.addFolder(Complex Formula Settings);animFolder.add(settings,speed,0.0,3.0).name(Speed).onChange(vuniforms.uSpeed.valuev);animFolder.add(settings,complexity,1,20,1).name(Math Iterations).onChange(vuniforms.uComplexity.valuev);animFolder.add(settings,density,0.5,5.0).name(Space Folding).onChange(vuniforms.uDensity.valuev);animFolder.add(settings,intensity,0.001,0.05).name(Neon Exposure).onChange(vuniforms.uIntensity.valuev);// Interactive controlsconstinteractiveFoldergui.addFolder(Interaction);interactiveFolder.add(settings,hoverEffect).name(Hover Effect).onChange(v{// Smoothly toggle the uniform value between 0.0 and 1.0uniforms.uHoverEffect.valuev?1.0:0.0;});// Color controlsconstcolorFoldergui.addFolder(Colors);colorFolder.addColor(settings,color1).name(Primary Color).onChange(vuniforms.uColor1.value.set(v));colorFolder.addColor(settings,color2).name(Secondary Color).onChange(vuniforms.uColor2.value.set(v));colorFolder.addColor(settings,bgColor).name(Background Color).onChange(vuniforms.uBgColor.value.set(v));// Collapse the main GUI panel by defaultgui.close();// --- ANIMATION LOOP ---constclocknewTHREE.Clock();functionanimate(){requestAnimationFrame(animate);// Smoothly interpolate the mouse position (Lerp) for a fluid, dragged effectuniforms.uMouse.value.x(targetMouse.x-uniforms.uMouse.value.x)*0.05;uniforms.uMouse.value.y(targetMouse.y-uniforms.uMouse.value.y)*0.05;// Update time uniform for fluid motionuniforms.uTime.valueclock.getElapsedTime();renderer.render(scene,camera);}// --- RESIZE HANDLER ---window.addEventListener(resize,(){// Update renderer sizerenderer.setSize(window.innerWidth,window.innerHeight);// Update resolution uniform for the shaderuniforms.uResolution.value.set(window.innerWidth*window.devicePixelRatio,window.innerHeight*window.devicePixelRatio);});// Start the animation loopanimate();/script/html效果如下源码

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

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

相关文章

SpringBoot-17-MyBatis动态SQL标签之常用标签

文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…

wordpress后台更新后 前端没变化的解决方法

使用siteground主机的wordpress网站,会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后,网站没有变化的情况。 不熟悉siteground主机的新手,遇到这个问题,就很抓狂,明明是哪都没操作错误&#x…

网络编程(Modbus进阶)

思维导图 Modbus RTU(先学一点理论) 概念 Modbus RTU 是工业自动化领域 最广泛应用的串行通信协议,由 Modicon 公司(现施耐德电气)于 1979 年推出。它以 高效率、强健性、易实现的特点成为工业控制系统的通信标准。 包…

UE5 学习系列(二)用户操作界面及介绍

这篇博客是 UE5 学习系列博客的第二篇,在第一篇的基础上展开这篇内容。博客参考的 B 站视频资料和第一篇的链接如下: 【Note】:如果你已经完成安装等操作,可以只执行第一篇博客中 2. 新建一个空白游戏项目 章节操作,重…

IDEA运行Tomcat出现乱码问题解决汇总

最近正值期末周,有很多同学在写期末Java web作业时,运行tomcat出现乱码问题,经过多次解决与研究,我做了如下整理: 原因: IDEA本身编码与tomcat的编码与Windows编码不同导致,Windows 系统控制台…

利用最小二乘法找圆心和半径

#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …

使用docker在3台服务器上搭建基于redis 6.x的一主两从三台均是哨兵模式

一、环境及版本说明 如果服务器已经安装了docker,则忽略此步骤,如果没有安装,则可以按照一下方式安装: 1. 在线安装(有互联网环境): 请看我这篇文章 传送阵>> 点我查看 2. 离线安装(内网环境):请看我这篇文章 传送阵>> 点我查看 说明&#xff1a;假设每台服务器已…

XML Group端口详解

在XML数据映射过程中&#xff0c;经常需要对数据进行分组聚合操作。例如&#xff0c;当处理包含多个物料明细的XML文件时&#xff0c;可能需要将相同物料号的明细归为一组&#xff0c;或对相同物料号的数量进行求和计算。传统实现方式通常需要编写脚本代码&#xff0c;增加了开…

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器的上位机配置操作说明

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器专为工业环境精心打造&#xff0c;完美适配AGV和无人叉车。同时&#xff0c;集成以太网与语音合成技术&#xff0c;为各类高级系统&#xff08;如MES、调度系统、库位管理、立库等&#xff09;提供高效便捷的语音交互体验。 L…

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目&#xff1a;3442. 奇偶频次间的最大差值 I 思路 &#xff1a;哈希&#xff0c;时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况&#xff0c;哈希表这里用数组即可实现。 C版本&#xff1a; class Solution { public:int maxDifference(string s) {int a[26]…

【大模型RAG】拍照搜题技术架构速览:三层管道、两级检索、兜底大模型

摘要 拍照搜题系统采用“三层管道&#xff08;多模态 OCR → 语义检索 → 答案渲染&#xff09;、两级检索&#xff08;倒排 BM25 向量 HNSW&#xff09;并以大语言模型兜底”的整体框架&#xff1a; 多模态 OCR 层 将题目图片经过超分、去噪、倾斜校正后&#xff0c;分别用…

【Axure高保真原型】引导弹窗

今天和大家中分享引导弹窗的原型模板&#xff0c;载入页面后&#xff0c;会显示引导弹窗&#xff0c;适用于引导用户使用页面&#xff0c;点击完成后&#xff0c;会显示下一个引导弹窗&#xff0c;直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…

接口测试中缓存处理策略

在接口测试中&#xff0c;缓存处理策略是一个关键环节&#xff0c;直接影响测试结果的准确性和可靠性。合理的缓存处理策略能够确保测试环境的一致性&#xff0c;避免因缓存数据导致的测试偏差。以下是接口测试中常见的缓存处理策略及其详细说明&#xff1a; 一、缓存处理的核…

龙虎榜——20250610

上证指数放量收阴线&#xff0c;个股多数下跌&#xff0c;盘中受消息影响大幅波动。 深证指数放量收阴线形成顶分型&#xff0c;指数短线有调整的需求&#xff0c;大概需要一两天。 2025年6月10日龙虎榜行业方向分析 1. 金融科技 代表标的&#xff1a;御银股份、雄帝科技 驱动…

观成科技:隐蔽隧道工具Ligolo-ng加密流量分析

1.工具介绍 Ligolo-ng是一款由go编写的高效隧道工具&#xff0c;该工具基于TUN接口实现其功能&#xff0c;利用反向TCP/TLS连接建立一条隐蔽的通信信道&#xff0c;支持使用Let’s Encrypt自动生成证书。Ligolo-ng的通信隐蔽性体现在其支持多种连接方式&#xff0c;适应复杂网…

铭豹扩展坞 USB转网口 突然无法识别解决方法

当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?

编辑&#xff1a;陈萍萍的公主一点人工一点智能 未来机器人的大脑&#xff1a;如何用神经网络模拟器实现更智能的决策&#xff1f;RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战&#xff0c;在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…

Linux应用开发之网络套接字编程(实例篇)

服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …

华为云AI开发平台ModelArts

华为云ModelArts&#xff1a;重塑AI开发流程的“智能引擎”与“创新加速器”&#xff01; 在人工智能浪潮席卷全球的2025年&#xff0c;企业拥抱AI的意愿空前高涨&#xff0c;但技术门槛高、流程复杂、资源投入巨大的现实&#xff0c;却让许多创新构想止步于实验室。数据科学家…

深度学习在微纳光子学中的应用

深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向&#xff1a; 逆向设计 通过神经网络快速预测微纳结构的光学响应&#xff0c;替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…