Threejs制作服务器机房冷却结构

2024-05-01 03:28

本文主要是介绍Threejs制作服务器机房冷却结构,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        这节再绘制一个机房的结构,因为内容比较简单,就只使用一个章节来介绍,

先来一张效果图,

需要两个模型:一个冷却设备,一个服务器机箱,我这里是从网上找来的,首先我们搭建一个场景,

 initScene(){this.scene = new THREE.Scene();},initCamera(){this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);this.camera.position.set(200,-100,200);this.camera.lookAt(200,200,0);this.camera.up.set(0, 0, 1);   // <=== spin // around Z-axis},initLight(){//添加两个平行光const directionalLight1 = new THREE.DirectionalLight(0xffffff, 1.5);directionalLight1.position.set(-300,-300,600)this.scene.add(directionalLight1);const directionalLight2 = new THREE.DirectionalLight(0xffffff, 1.5);directionalLight2.position.set(600,200,600)this.scene.add(directionalLight2);},initRenderer(){this.renderer = new THREE.WebGLRenderer({ antialias: true });this.container = document.getElementById("container")this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);this.renderer.setClearColor('#FFFFFF', 1.0);this.container.appendChild(this.renderer.domElement);},initControl(){this.controls = new OrbitControls(this.camera, this.renderer.domElement);this.controls.enableDamping = true;this.controls.maxPolarAngle = Math.PI / 2.2;      // // 最大角度this.controls.target = new THREE.Vector3(200, 200, 0);this.camera.position.set(200, -100, 200);this.camera.lookAt(200, 200, 0);},initAnimate() {requestAnimationFrame(this.initAnimate);this.renderer.render(this.scene, this.camera);},

        然后添加房间,为了效果更真实,我们会创建一个房间,把服务器放在房间里,把冷却塔放在外面,创建方法和mes中的类似,只不过这个不用设计门了,可以直接搭建四堵墙和地板

 floor:{floorWidth:600, floorLength:600,depth:1},wall:{wallWidth:150, wallLength:300,wallHeight:20},offsetValue:200,initFloor(){let floorGeometry = new THREE.BoxGeometry( this.floor.floorWidth,this.floor.floorLength,this.floor.depth);let floorMaterial = new THREE.MeshPhysicalMaterial({color:'#FFFFFF'});let textureFloor = new THREE.TextureLoader().load('/static/images/floor.jpg', function (texture) {texture.wrapS = texture.wrapT = THREE.RepeatWrapping;})floorMaterial.map = textureFloorlet floor = new THREE.Mesh( floorGeometry, floorMaterial );floor.name = '地板';floor.position.set(this.floor.floorWidth/2,this.floor.floorLength/2,0)this.scene.add(floor)},//初始化墙壁createCubeWall() {let materialTie = new THREE.MeshPhysicalMaterial({color: '#BBBBBB'});  //前  0xafc0ca :灰色let textureWall = new THREE.TextureLoader().load('/static/images/wall.jpg', function (texture) {texture.wrapS = texture.wrapT = THREE.RepeatWrapping;})materialTie.map = textureWalllet wallList = []let wall1 = {width:this.wall.wallLength, height:2, depth:this.wall.wallHeight, angle:0, matArrayB:materialTie, x:this.wall.wallLength/2+this.offsetValue, y:+this.offsetValue, z:this.wall.wallHeight/2, name:"墙面"};let wall2 = {width:this.wall.wallLength, height:2, depth:this.wall.wallHeight, angle:1, matArrayB:materialTie, x:this.wall.wallLength/2+200, y:this.wall.wallWidth+this.offsetValue, z:(this.wall.wallHeight/2), name:"墙面"};let wall3 = {width:this.wall.wallWidth, height:2, depth:this.wall.wallHeight, angle:1.5, matArrayB:materialTie, x:this.offsetValue, y:this.wall.wallWidth/2+this.offsetValue, z:(this.wall.wallHeight/2), name:"墙面"};let wall4 = {width:this.wall.wallWidth, height:2, depth:this.wall.wallHeight, angle:1.5, matArrayB:materialTie, x:this.wall.wallLength+this.offsetValue, y:(this.wall.wallWidth/2)+this.offsetValue, z:(this.wall.wallHeight/2), name:"墙面"};wallList.push(wall1);wallList.push(wall2);wallList.push(wall3);wallList.push(wall4);for(let i=0;i<wallList.length;i++){let cubeGeometry = new THREE.BoxGeometry(wallList[i].width, wallList[i].height, wallList[i].depth);let cube = new THREE.Mesh(cubeGeometry, wallList[i].matArrayB);cube.position.x = wallList[i].x;cube.position.y = wallList[i].y;cube.position.z = wallList[i].z;cube.rotation.z += wallList[i].angle * Math.PI; //-逆时针旋转,+顺时针cube.name = wallList[i].name;this.scene.add(cube);}},

        然后添加两个设备,一个冷却设备,一个服务器主机柜,要注意调整位置,保持一个在房间内一个在房间外,后续会通过冷凝管连接两个设备

initDevice(){const loader = new GLTFLoader()loader.load("/static/models/server.glb", (gltf) => {this.server = gltf.scene;this.server.scale.set(0.3,0.3,0.3);this.server.position.set(300,300,0);this.server.rotation.x = Math.PI/2this.server.rotation.y = Math.PI/2this.scene.add(this.server)   // 加入场景})loader.load("/static/models/tower.glb", (gltf) => {this.tower = gltf.scene;this.tower.scale.set(20,20,20);this.tower.position.set(300,400,30);this.tower.rotation.x = Math.PI/2this.scene.add(this.tower)   // 加入场景})},

添加好设备后,我们就得到这样的场景,

        然后需要添加管道给这两个设备连接起来,并且其中一个为蓝色一个为红色,之前有讲过Threejs绘制管道效果,可以就把那部分拿过来使用,配置好每个坐标点,这里用两个方法一个绘制冷水,一个绘制热水,并且让水流动起来,这部分开发可以参考之前的章节,不过要调整水管的接入点,如果想要直角管,可以在点数组中添加两个拐弯点的坐标,就可以避免后续的点的影响,以达到直角管道的效果。

代码如下:

initColdTube(){const path = new THREE.CatmullRomCurve3([new THREE.Vector3(350, 300, 10),new THREE.Vector3(380, 300, 10),new THREE.Vector3(380, 300, 10),new THREE.Vector3(380, 400, 10),new THREE.Vector3(380, 400, 10),new THREE.Vector3(320, 400, 7),]);let geometry1 = new THREE.TubeGeometry(path, 100, 1, 25, false);let textureLoader = new THREE.TextureLoader();let texture = textureLoader.load('/static/images/cold.png')texture.wrapS = texture.wrapT = THREE.RepeatWrapping;this.coldMaterial = new THREE.MeshBasicMaterial({map:texture,transparent: false,}); //材质对象Materiallet mesh1 = new THREE.Mesh(geometry1, this.coldMaterial); //网格模型对象Meshthis.scene.add(mesh1); //网格模型添加到场景let tubeGeometry2 = new THREE.TubeGeometry(path, 100, 2, 25, false);let tubeMaterial2 = new THREE.MeshPhongMaterial({color: 0xaaaaaa,transparent: true,opacity: 0.5,});let tube2 = new THREE.Mesh(tubeGeometry2, tubeMaterial2);this.scene.add(tube2);},initHotTube(){const path = new THREE.CatmullRomCurve3([new THREE.Vector3(300, 300, 10),new THREE.Vector3(230, 300, 10),new THREE.Vector3(230, 300, 10),new THREE.Vector3(230, 300, 53),new THREE.Vector3(230, 300, 53),new THREE.Vector3(230, 400, 53),new THREE.Vector3(230, 400, 53),new THREE.Vector3(270, 400, 53),]);let geometry1 = new THREE.TubeGeometry(path, 100, 1, 25, false);let textureLoader = new THREE.TextureLoader();let texture = textureLoader.load('/static/images/hot.png')texture.wrapS = texture.wrapT = THREE.RepeatWrapping;this.hotMaterial = new THREE.MeshBasicMaterial({map:texture,transparent: false,}); //材质对象Materiallet mesh1 = new THREE.Mesh(geometry1, this.hotMaterial); //网格模型对象Meshthis.scene.add(mesh1); //网格模型添加到场景let tubeGeometry2 = new THREE.TubeGeometry(path, 100, 2, 25, false);let tubeMaterial2 = new THREE.MeshPhongMaterial({color: 0xaaaaaa,transparent: true,opacity: 0.5,});let tube2 = new THREE.Mesh(tubeGeometry2, tubeMaterial2);this.scene.add(tube2);},

然后再给服务器和冷却设备添加状态和名字,用上个章节中给产线设备添加名字的方式:

initMachineName(x,y,z,name){//创建设备信息const earthDiv = document.createElement('div');earthDiv.className = "label";earthDiv.innerHTML = "<div style='border:1px #FFFFFF solid;border-radius: 5px;width:90px;padding-left:10px'>" +"<span style='font-size: 12px;color:#FFFFFF'>"+name+"</span><br/>" +"<span style='font-size: 12px;color:#FFFFFF'>运行正常</span><br/>" +"<span style='color:green;font-size: 12px;'>温度18℃</span>" +"</div>";const earthLabel = new CSS2DObject(earthDiv);earthLabel.position.set(x,y,z);//相对于父级元素的位置this.scene.add(earthLabel);this.labelRenderer = new CSS2DRenderer();this.labelRenderer.setSize(window.innerWidth, window.innerHeight);document.body.appendChild(this.labelRenderer.domElement)//设置样式this.labelRenderer.domElement.style.position = 'fixed';this.labelRenderer.domElement.style.top = '0px';this.labelRenderer.domElement.style.left = '0px';this.labelRenderer.domElement.style.zIndex = '10';//设置层级},

最终效果如下:

机房冷却

这样一共不到250代码,一个简单的机房数字孪生场景就做好了,如果需要源码可以在评论区留下邮箱,也可以私信一起交流学习。

这篇关于Threejs制作服务器机房冷却结构的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/950615

相关文章

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然

将Java项目提交到云服务器的流程步骤

《将Java项目提交到云服务器的流程步骤》所谓将项目提交到云服务器即将你的项目打成一个jar包然后提交到云服务器即可,因此我们需要准备服务器环境为:Linux+JDK+MariDB(MySQL)+Gi... 目录1. 安装 jdk1.1 查看 jdk 版本1.2 下载 jdk2. 安装 mariadb(my

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

基于Python打造一个可视化FTP服务器

《基于Python打造一个可视化FTP服务器》在日常办公和团队协作中,文件共享是一个不可或缺的需求,所以本文将使用Python+Tkinter+pyftpdlib开发一款可视化FTP服务器,有需要的小... 目录1. 概述2. 功能介绍3. 如何使用4. 代码解析5. 运行效果6.相关源码7. 总结与展望1

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

CentOS 7部署主域名服务器 DNS的方法

《CentOS7部署主域名服务器DNS的方法》文章详细介绍了在CentOS7上部署主域名服务器DNS的步骤,包括安装BIND服务、配置DNS服务、添加域名区域、创建区域文件、配置反向解析、检查配置... 目录1. 安装 BIND 服务和工具2.  配置 BIND 服务3 . 添加你的域名区域配置4.创建区域