ThreeJs模拟工厂生产过程二

简介: 这篇文章详细介绍了如何使用Three.js创建一个模拟工厂车间的3D模型,包括绘制地面和墙面的具体步骤,并特别关注于如何创建带有门的墙面以增加车间的真实性。

上一节已经把产线的基本demo搭建完成,这节先不美化产线,先搭建个车间用来放置这个产线,后期再一起想办法做美化,绘制车间,我们要先绘制地面。地面实际上就是一个很薄的长方体,长方体的长和宽就是车间的面积,高度设置为1,再加一点颜色,作为地面,这里用了一个蓝色的图片作为贴图,如果找不到合适的,也可以把贴图的去掉,用设置的颜色作为地面颜色就好了。我们先定义地面的起画点,再设置地面的宽高,这样方便后期修改,代码如下:

floor:{floorWidth:300, floorLength:300,depth:1},
sceneBegin: { x: 0, y: 0, z: 0 },

 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 = textureFloor
      let floor = new THREE.Mesh( floorGeometry, floorMaterial );
      floor.name = '地板';
      floor.position.set(this.sceneBegin.x+this.floor.floorWidth/2,this.sceneBegin.y+this.floor.floorLength/2,this.sceneBegin.z-10)
      this.scene.add(floor)
    },

绘制好地面后,再绘制墙面,墙面比起地面要复杂的多,在之前绘制仓库的章节里有写过,但是那次的墙面是完整的,正常车间是有门的,所以我们需要绘制5面墙,其中三面是完整的,最后一面墙是用两个模型,中间留下间隔作为门,而且每个墙面的下面要和地面对齐,我们先设定好每面墙的参数,长宽高以及放置的位置,除了那两个特殊墙面外,另外三堵墙,都是和地面一一样宽,但是因为墙面的位置不一样,所以是两面墙和地面一样宽,另外两面墙和地面一样长,因为Threejs默认的位置是这个模型的中心点,所以设置墙面的时候应该是墙面长度的一半,先不考虑门的话,可以先这样写:

 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 = textureWall
      let wallList = []
      let wall1 = {width:this.floor.floorLength, height:2, depth:this.wallHeight, angle:0, matArrayB:materialTie, x:this.floor.floorLength/2, y:0, z:this.wallHeight/2-(this.wallHeight/2), name:"墙面"};
      let wall2 = {width:this.floor.floorLength, height:2, depth:this.wallHeight, angle:1, matArrayB:materialTie, x:this.floor.floorLength/2, y:this.floor.floorWidth, z:this.wallHeight/2-(this.wallHeight/2), name:"墙面"};
      let wall3 = {width:this.floor.floorWidth, height:2, depth:this.wallHeight, angle:1.5, matArrayB:materialTie, x:0, y:(this.floor.floorWidth/2), z:this.wallHeight/2-(this.wallHeight/2), name:"墙面"};
      let wall4 = {width:this.floor.floorWidth, height:2, depth:this.wallHeight, angle:1.5, matArrayB:materialTie, x:this.floor.floorLength, y:(this.floor.floorWidth/2), z:this.wallHeight/2-(this.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);
      }
    },

然后我们需要把其中一面墙拆成两个墙,且不连接,我们这里把wall4缩短,再添加出wall5,设置wall4长度减去40,但是wall5的长度一共20,这样可以留出20的宽度作为门的宽度,代码如下

//初始化墙壁
    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 = textureWall
      let wallList = []
      let wall1 = {width:this.floor.floorLength, height:2, depth:this.wallHeight, angle:0, matArrayB:materialTie, x:this.floor.floorLength/2, y:0, z:this.wallHeight/2-(this.wallHeight/2), name:"墙面"};
      let wall2 = {width:this.floor.floorLength, height:2, depth:this.wallHeight, angle:1, matArrayB:materialTie, x:this.floor.floorLength/2, y:this.floor.floorWidth, z:this.wallHeight/2-(this.wallHeight/2), name:"墙面"};
      let wall3 = {width:this.floor.floorWidth, height:2, depth:this.wallHeight, angle:1.5, matArrayB:materialTie, x:0, y:(this.floor.floorWidth/2), z:this.wallHeight/2-(this.wallHeight/2), name:"墙面"};
      let wall4 = {width:this.floor.floorWidth-40, height:2, depth:this.wallHeight, angle:1.5, matArrayB:materialTie, x:this.floor.floorLength, y:(this.floor.floorWidth/2)+20, z:this.wallHeight/2-(this.wallHeight/2), name:"墙面"};
      let wall5 = {width:20, height:2, depth:this.wallHeight, angle:1.5, matArrayB:materialTie, x:this.floor.floorLength, y:10, z:this.wallHeight/2-10, name:"墙面"};
      wallList.push(wall1);wallList.push(wall2);wallList.push(wall3);wallList.push(wall4);
      wallList.push(wall5);
      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);
      }
    },

效果如下:

车间的绘制差不多就这样,算有个大概的雏形了

相关文章
|
编解码 IDE 算法
2023年电赛---运动目标控制与自动追踪系统(E题)发挥题思路
2023年电赛---运动目标控制与自动追踪系统(E题)发挥题思路
598 0
|
6天前
ThreeJs模拟工厂生产过程六
这篇文章详细介绍了如何在Three.js中模拟工厂生产过程的第六部分,重点是创建和实现车间内线边仓货架的三维模型及其布局。
11 1
ThreeJs模拟工厂生产过程六
|
6天前
|
UED
ThreeJs模拟工厂生产过程三
这篇文章介绍了在Three.js中通过使用mergeGeometries技术来合并大量车间模型,以减少浏览器渲染负担,提高性能,并提供了实现模型合并的具体方法和步骤。
15 5
|
6天前
ThreeJs模拟工厂生产过程一
这篇文章详细介绍了如何使用Three.js模拟工厂生产过程的第一部分,包括创建传送带、生产设备和产品的模型,并实现产品沿传送带移动的动画效果。
15 5
|
6天前
ThreeJs模拟工厂生产过程四
这篇文章详细说明了如何在Three.js中为模拟的工厂产线添加警戒线,以增强产线模型的真实感和安全性表现,通过使用`PlaneGeometry`来创建并定位这些警戒线。
10 1
|
6天前
ThreeJs模拟工厂生产过程七
这篇文章详细介绍了如何在Three.js中为工厂车间的货架动态生成并放置货物,通过循环逻辑和贴图应用使货架上的物品更加逼真,增强了场景的真实感。
9 0
|
6天前
|
JavaScript
ThreeJs模拟工厂生产过程八
这篇文章详细介绍了如何在Three.js中模拟工厂生产过程的第八部分,重点是优化场景中的模型,包括合并货架上的料箱以减少渲染负担,并替换设备模型以增强场景的真实性和互动性。
21 0
|
6天前
|
程序员 图形学
ThreeJs模拟工厂生产过程五
这篇文章详细介绍了如何在Three.js中模拟工业生产过程的第五部分,重点在于添加并实现车间内人物的动态行走动画,使人能够在车间内来回移动,增加了场景的真实感。
16 0
|
4月前
经验大分享:QML动态标注线
经验大分享:QML动态标注线
22 0
|
5月前
|
设计模式 安全 Java
老系统重构系列--如何用一套流程接入所有业务线
**摘要:** 本文介绍了老系统改造的过程,作者提出,ToB业务的挑战在于需要支持多种差异化的业务需求,而模板模式在处理这种需求时可能会导致继承关系复杂和粒度过粗。为了解决这些问题,文章提出了以下步骤: 1. **梳理流程差异点**:识别不同业务流程的差异,以便确定扩展点。 2. **领域模型梳理**:区分核心域和支撑域,确保核心域的稳定性。 3. **二次抽象隔离层**:创建隔离层,避免核心域因新业务接入而变得不稳定。 4. **基于SPI的扩展体系建设**:选择了COLA-SPI实现扩展点,允许业务域定义接口并实现差异化的流程逻辑。
108 0