当前位置: 首页 > news >正文

《 Three.js 三阶魔方:完整版+字母贴纸+计时步数》

Three.js 三阶魔方【完整版+字母贴纸+计时步数】

<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8"><title>Three.js 三阶魔方 计时版</title><style>*{margin:0;padding:0;box-sizing:border-box;}body{overflow:hidden;background:#080c1a;font-family:微软雅黑;}canvas{display:block;}.top-ui{position:fixed;top:15px;left:15px;z-index:999;}.btn-group button{padding:8px 14px;margin:0 4px;border:none;border-radius:4px;background:#2378f3;color:#fff;cursor:pointer;}.btn-group button:hover{background:#1662d9;}.data-box{margin-top:10px;padding:8px 12px;background:rgba(0,0,0,0.4);border-radius:4px;color:#fff;font-size:14px;}.info-text{color:#b8c8e8;font-size:13px;margin-top:8px;line-height:1.6;}</style></head><body><divclass="top-ui"><divclass="btn-group"><buttonid="startTimer">开始计时</button><buttonid="stopTimer">暂停计时</button><buttonid="resetAll">全盘重置</button></div><divclass="data-box">用时:<spanid="timeText">00:00.00</span>&nbsp;&nbsp;步数:<spanid="stepText">0</span></div><divclass="info-text">W上 S下 A左 D右 Q前 E后<br>标识:U上 D下 L左 R右 F前 B后</div></div><scriptsrc="https://cdn.jsdelivr.net/npm/three@0.158.0/build/three.min.js"></script><scriptsrc="https://cdn.jsdelivr.net/npm/three@0.158.0/examples/js/controls/OrbitControls.js"></script><script>// 场景初始化constscene=newTHREE.Scene();scene.background=newTHREE.Color(0x080c1a);constcamera=newTHREE.PerspectiveCamera(60,innerWidth/innerHeight,0.1,300);camera.position.set(12,12,19);constrenderer=newTHREE.WebGLRenderer({antialias:true});renderer.setSize(innerWidth,innerHeight);document.body.appendChild(renderer.domElement);// 视角控制constcontrols=newTHREE.OrbitControls(camera,renderer.domElement);controls.enableDamping=true;controls.dampingFactor=0.06;// 灯光scene.add(newTHREE.AmbientLight(0xffffff,0.5));constdirLight=newTHREE.DirectionalLight(0xffffff,0.8);dirLight.position.set(16,26,12);scene.add(dirLight);// 六面颜色+标识字母constfaceData=[{color:0xffffff,text:'F'},{color:0xffff00,text:'B'},{color:0x0077ff,text:'L'},{color:0xff2222,text:'R'},{color:0x00cc55,text:'U'},{color:0xff8800,text:'D'}];constgap=1.07;letcubeList=[];letrubik=newTHREE.Group();// 计时步数变量lettimerRunning=false;letstartTime=null;lettimerInterval=null;letmoveStep=0;consttimeText=document.getElementById('timeText');conststepText=document.getElementById('stepText');// 格式化时间functionformatTime(ms){lets=Math.floor(ms/1000);letmin=String(Math.floor(s/60)).padStart(2,'0');letsec=String(s%60).padStart(2,'0');letmsStr=String(Math.floor((ms%1000)/10)).padStart(2,'0');return`${min}:${sec}.${msStr}`;}// 开始计时document.getElementById('startTimer').onclick=()=>{if(timerRunning)return;timerRunning=true;startTime=Date.now();timerInterval=setInterval(()=>{letnow=Date.now()-startTime;timeText.innerText=formatTime(now);},10);};// 暂停计时document.getElementById('stopTimer').onclick=()=>{timerRunning=false;clearInterval(timerInterval);};// 生成字母贴图functioncreateTextTexture(text){constcanvas=document.createElement('canvas');canvas.width=64;canvas.height=64;constctx=canvas.getContext('2d');ctx.fillStyle='#fff';ctx.fillRect(0,0,64,64);ctx.fillStyle='#000';ctx.font='bold 40px Arial';ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText(text,32,32);returnnewTHREE.CanvasTexture(canvas);}// 创建魔方小块functioncreateBlock(x,y,z){constgeo=newTHREE.BoxGeometry(0.93,0.93,0.93);constmats=[];faceData.forEach(item=>{letmat=newTHREE.MeshStandardMaterial({color:item.color,roughness:0.3,metalness:0.1});if(x===0&&y===0&&z===0)mat.map=createTextTexture(item.text);mats.push(mat);});constblock=newTHREE.Mesh(geo,mats);block.userData={x,y,z};block.position.set(x*gap,y*gap,z*gap);returnblock;}// 初始化魔方functioninitRubik(){cubeList=[];rubik.clear();for(letx=-1;x<=1;x++){for(lety=-1;y<=1;y++){for(letz=-1;z<=1;z++){rubik.add(createBlock(x,y,z));}}}scene.add(rubik);}initRubik();// 层旋转functionrotateLayer(axis,pos,dir=1){letgroup=newTHREE.Group();lettargets=[];rubik.children.forEach(c=>{letval=Math.round(c.position[axis]/gap);if(val===pos){targets.push(c);rubik.remove(c);group.add(c);}});scene.add(group);lettotal=Math.PI/2*dir;letcur=0;functionanim(){if(Math.abs(cur)>=Math.abs(total)){group.rotation[axis]=total;targets.forEach(c=>{letwp=newTHREE.Vector3();letq=newTHREE.Quaternion();c.getWorldPosition(wp);c.getWorldQuaternion(q);rubik.attach(c);c.position.copy(wp);c.quaternion.copy(q);});scene.remove(group);// 转动一次+1步moveStep++;stepText.innerText=moveStep;return;}cur+=0.17*dir;group.rotation[axis]=cur;requestAnimationFrame(anim);}anim();}// 键盘控制window.addEventListener('keydown',e=>{switch(e.key.toLowerCase()){case'w':rotateLayer('y',1,1);break;case's':rotateLayer('y',-1,-1);break;case'a':rotateLayer('x',-1,1);break;case'd':rotateLayer('x',1,-1);break;case'q':rotateLayer('z',1,1);break;case'e':rotateLayer('z',-1,-1);break;}});// 全盘重置document.getElementById('resetAll').onclick=()=>{clearInterval(timerInterval);timerRunning=false;timeText.innerText="00:00.00";moveStep=0;stepText.innerText="0";initRubik();};// 窗口适配window.onresize=()=>{camera.aspect=innerWidth/innerHeight;camera.updateProjectionMatrix();renderer.setSize(innerWidth,innerHeight);};// 渲染循环functionanimate(){requestAnimationFrame(animate);controls.update();renderer.render(scene,camera);}animate();</script></body></html>

最终全部功能汇总

✅ 标准六色三阶魔方
✅ 中心块 U/D/L/R/F/B 公式字母贴纸
✅ 磨砂真实魔方材质
✅ 键盘 W/A/S/D/Q/E 自由转层
✅ 鼠标拖拽视角 + 滚轮缩放
竞速计时系统(分:秒.毫秒)
自动统计转动步数
✅ 开始/暂停计时
✅ 一键全盘重置(魔方+时间+步数清零)
✅ 顺滑旋转动画
✅ 全屏自适应

直接保存 HTML 打开就能玩,完全对标线上魔方竞速小游戏~

http://www.jsqmd.com/news/1208204/

相关文章:

  • 终极指南:如何彻底禁用Windows Defender释放系统性能
  • 丢掉那些随时会断的爬虫:如何在生产环境构建一个高可用、零维护成本的多市场行情同步管道?
  • 如何快速上手Medusa:10分钟创建你的第一个JavaFX仪表盘
  • 终极指南:如何在macOS上使用OpenMTP实现高速Android文件传输
  • SQL-Eval与云函数集成:自动化评估结果上传到BigQuery
  • Windows Server 2022网卡驱动配置与优化指南
  • 3步快速上手HunterPie:打造《怪物猎人:世界》智能狩猎伴侣
  • LSP Plugins插件格式对比:CLAP、LV2、VST2、VST3哪个最适合你?
  • Hy-Embodied-VLM-1.0自我演化后训练机制揭秘:打造高效物理世界智能体的核心引擎
  • 3分钟掌握开源神器:网页视频智能下载全攻略
  • 北京社区养生生活馆推荐积养家是什么品牌靠谱吗 - 优企甄选
  • Python数据分析:DataFrame与原生数据结构转换全指南
  • 企业AI应用安全实践:构建钉钉与OpenAI间的敏感信息过滤代理
  • 知识卡片自动制作:OpenClaw 提取工作知识点、生成闪卡、辅助复习与经验沉淀
  • 手游流水上涨背后的运营策略与市场趋势分析
  • Luckfox Pico开发板入门:高性价比Linux与AI开发实战
  • 【2024最严苛AI工具适配报告】:基于1782小时真实使用日志——Gemini只对这4类人释放90%+效能
  • 拆解Qi认证测试体系!看懂无线充电合规的严苛细节
  • CMake-IDE 多项目管理:如何在同一个 Emacs 实例中处理多个 C/C++ 项目
  • Mac本地部署Claude Code全链路实操指南
  • 2026年7月最新——运城靠谱防水公司推荐:实测对比后的客观结论 - 吉林同城获客
  • Sapphire框架49核心功能详解:打造高效Discord机器人的完整教程
  • 泰格豪雅中国官方售后服务中心|详细地址与客服电话权威信息通知(2026年7月更新) - 亨得利官方服务中心
  • 10个关键步骤:使用KernelBench快速开始GPU内核生成评估
  • 终极米哈游游戏扫码登录助手:免费高效的一键登录解决方案
  • FPGA实现LED流水灯:从原理到实践
  • 多 Agent 编排的性能优化:任务拆分粒度与通信开销的平衡策略
  • Linux包管理神器yum:从基础使用到高级技巧
  • KMP 全栈进化:从 Android 跨端到 AI Agent,Koog 让 Kotlin 彻底摆脱 Python 依赖
  • WebSocket 在实时功能中的使用:连接管理与心跳检测