luxiaotao1123
2021-11-17 85d878b12d6988ca2275d60e7da0ec31814f8fc8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
function Store3D() {
    this.scene = null;//场景
    this.camera = null;//相机
    this.renderer = null;//渲染器
    this.objects = [];//场景中所有对象的集合
    this.firstTime = 1;
    this.storeIsShow=1;//是否显示库房
    this.groupIsShow=1;//是否显示排
    this.shelfIsShow=1;//是否显示架子
    this.goodTypes=[];
    this.objectLockPointer=[];//存储所有的sprite对象
}
 
/**
 * 初始化仓库所有插件
 */
Store3D.prototype.initMain = function () {
    this.initScene();
    this.initCamera();
    this.initRenderer();
    this.initOrbitControl();
    this.initStats();
    this.initAxisHelper();
    this.initBuilding();
    this.initLight();
    this.initObjectSelect();
    this.initAgvCar();
},
    /**
     * 仓库整体开始运行
     */
    Store3D.prototype.start = function () {
        this.initMain();
        this.animate();
    },
 
    /**
     初始化场景,仅仅需要有句话就可以生命一个场景,非常简单
     **/
    Store3D.prototype.initScene = function () {
        this.scene = new THREE.Scene();
    },
    /**
     初始化场景,因为我们做的工厂模型,尽可能的接近于真实情景,采用透视相机
     **/
    Store3D.prototype.initCamera = function () {
        //声明一个透视相机,
        // 视角:60,
        // 纵横比aspect:全屏,使用的是浏览器的宽度/高度
        //近平面near:0.1
        //远平面视角far:10000
        this.camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 10000);
        /*
        设置相机位置,注意threejs中的坐标系采用的是右手坐标系
         */
        this.camera.position.x = 0;
        this.camera.position.y = 1600;
        this.camera.position.z = 1000;
        //相机的朝向
        this.camera.lookAt(0, 0, 0);
        //将相机放到场景中
        this.scene.add(this.camera);
    },
    /**
     * 显示或者隐温度控件
     */
    Store3D.prototype.changeTemperatureShow=function(){
        if (this.tmpIsShow == 1) {
            for (let i = 0; i < this.objectLockPointer.length; i++) {
                let tmpObject = this.objectLockPointer[i];
                if (tmpObject.type == "Temperature") {
                    this.removeObject(tmpObject.id);
                }
                else if(tmpObject.type == "StoreSign")
                {
                    this.addObject(tmpObject);
                }
            }
            this.tmpIsShow =0;
        }
        else {
            for (let i = 0; i < this.objectLockPointer.length; i++) {
                let tmpObject = this.objectLockPointer[i];
                if (tmpObject.type == "Temperature") {
                    this.addObject(tmpObject);
                }
                else if(tmpObject.type == "StoreSign")
                {
                    this.removeObject(tmpObject.id);
                }
            }
            this.tmpIsShow =1;
        }
    },
    /**
     * 根据ID或者名称移除对象
     */
    Store3D.prototype.removeObject = function (nameorid) {
        for (let i = 0; i < this.objects.length; i++) {
            let tmpObject = this.objects[i];
            if (tmpObject.name == nameorid || tmpObject.id == nameorid) {
                this.objects.splice(i, 1);
                this.scene.remove(tmpObject);
            }
 
        }
    },
 
    /**
     声名渲染器
     **/
    Store3D.prototype.initRenderer = function () {
        this.renderer = new THREE.WebGLRenderer(
            {
                antialias: true,//是否开启反锯齿,设置为true开启反锯齿。
                alpha: true,//是否可以设置背景色透明。
                logarithmicDepthBuffer: true//模型的重叠部位便不停的闪烁起来。这便是Z-Fighting问题,为解决这个问题,我们可以采用该种方法
            }
        );
        this.renderer.setSize(window.innerWidth, window.innerHeight);//渲染器的尺寸与windows的尺寸相同
        this.renderer.setClearColor(0x39609B);//设置渲染的背景颜色
        this.renderer.setPixelRatio(window.devicePixelRatio);//设置渲染器的分辨率与浏览器电脑本身的分辨率相同
        //将渲染器添加到我们的网页中,可以将渲染的内容在网页中显示出来
        let container = document.getElementById("container");
        container.appendChild(this.renderer.domElement);
    },
    /**
     * 初始化物体选中控件
     */
    Store3D.prototype.initObjectSelect=function(){
        new ObjectSelect(this.scene, this.camera);
    },
    /**
     * 初始化灯光
     */
    Store3D.prototype.initLight = function () {
        //首先添加个环境光
        let ambient = new THREE.AmbientLight(0xffffff, 1); //AmbientLight,影响整个场景的光源
        ambient.position.set(0, 0, 0);
        this.addObject(ambient);
        //添加平行光,平行光类似于太阳光
        let directionalLight = new THREE.DirectionalLight(0xffffff, 0.3);//模拟远处类似太阳的光源
        directionalLight.position.set(0, 200, 0);
        this.addObject(directionalLight);
        //设置点光源
        let pointLight1 = new THREE.PointLight(0xffffff, 0.3);
        pointLight1.position.set(-500, 200, 0);
        this.addObject(pointLight1);
        let pointLight2 = new THREE.PointLight(0xffffff, 0.3);
        pointLight2.position.set(500, 200, 0);
        this.addObject(pointLight2);
    },
    /**
     初始化相机控件OrbitControl
     */
    Store3D.prototype.initOrbitControl = function () {
        this.orbitControl = new THREE.OrbitControls(this.camera, this.renderer.domElement);
        this.orbitControl.enableDamping = true;
        this.orbitControl.dampingFactor = 0.5;
        // 视角最小距离
        this.orbitControl.minDistance = 0;
        // 视角最远距离
        this.orbitControl.maxDistance = 2000;
        // 最大角度
        this.orbitControl.maxPolarAngle = Math.PI / 2.2;
    },
    /**
     * 初始化性能控件Stats
     */
    Store3D.prototype.initStats = function () {
        this.stats = new Stats();
        this.stats.domElement.style.position = 'absolute';
        this.stats.domElement.style.left = '0px';
        this.stats.domElement.style.up = '0px';
 
        document.body.appendChild(this.stats.domElement);
    },
    /**
     * 初始化坐标系辅助控件
     */
    Store3D.prototype.initAxisHelper = function () {
        let axes = new THREE.AxisHelper(1000);
        this.addObject(axes);
    },
    Store3D.prototype.initLabel = function () {
        let label = new Label("我们是冠军", {
            fontsize: 50
        });
        label.position.set(0, 500, 0);
        this.addObject(label);
    },
    /**
     * 向场景中添加物体,并记录到
     */
    Store3D.prototype.addObject = function (object) {
        this.scene.add(object);
        this.objects.push(object);
    },
    /**
     创建建筑物
     */
    Store3D.prototype.initBuilding = function () {
        let buildingData = buildingObjects.objects;
        for (let i = 0; i < buildingData.length; i++) {
            let objectOption = buildingData[i];
            switch (objectOption.objectType) {
                case "cube":
                    let cube = new Cube(objectOption);
                    this.addObject(cube);
                    break;
                case "wall":
                    let wall = new Wall(objectOption);
                    this.addObject(wall);
                    break;
                case "route":
                    let line=new RouteLine(objectOption);
                    this.addObject(line);
                    break;
            }
        }
    },
    Store3D.prototype.showAgvCar=function(object){
        object.name = "AGV小车";
        object.position.set(-100,40,100);
        this.addObject(object);
    },
 
    Store3D.prototype.initAgvCar=function()
    {
       let agvCar=new AGVCar();
       agvCar.Load(this,'showAgvCar')
    },
    /***********************************************
     * 隐藏或者显示所有的仓库对象
     */
    Store3D.prototype.changeStoreShow = function () {
        if (this.storeIsShow == 1) {
            for (let i = 0; i < this.objects.length; i++) {
                let tmpObject = this.objects[i];
                if (tmpObject.type == "Store") {
                    this.scene.remove(tmpObject);
                }
            }
            this.storeIsShow = 0;
        } else {
            for (let i = 0; i < this.objects.length; i++) {
                let tmpObject = this.objects[i];
                if (tmpObject.type == "Store") {
                    this.scene.add(tmpObject);
                }
            }
            this.storeIsShow = 1;
        }
    },
    /***********************************************
     * 隐藏或者显示所有的巷道对象
     */
    Store3D.prototype.changeGroupShow = function () {
        if (this.groupIsShow == 1) {
            for (let i = 0; i < this.objects.length; i++) {
                let tmpObject = this.objects[i];
                if (tmpObject.type == "StoreGroup") {
                    this.scene.remove(tmpObject);
                }
            }
            this.groupIsShow =0;
        }
        else {
            for (let i = 0; i < this.objects.length; i++) {
                let tmpObject = this.objects[i];
                if (tmpObject.type == "StoreGroup") {
                    this.scene.add(tmpObject);
                }
            }
            this.groupIsShow =1;
        }
    },
    /***********************************************
     * 隐藏或者显示所有仓库框架
     */
    Store3D.prototype.changeShelfShow=function(){
        if(this.shelfIsShow==1)
        {
            for (let i = 0; i < this.objects.length; i++) {
                let tmpObject = this.objects[i];
                if (tmpObject.type == "StoreShelf") {
                    this.scene.remove(tmpObject);
                }
            }
            this.shelfIsShow =0;
        }
        else
        {
            for (let i = 0; i < this.objects.length; i++) {
                let tmpObject = this.objects[i];
                if (tmpObject.type == "StoreShelf") {
                    this.scene.add(tmpObject);
                }
            }
            this.shelfIsShow =1;
        }
    },
    /**
     * 初始化仓库相关对象
     */
    Store3D.prototype.initStoreObjects = function (object) {
 
 
        if (this.firstTime == 1) {
            let Store3DData = eval('(' + window.localStorage.getItem('Store3DData') + ')');
            if (Store3DData !== null) {
                //显示仓库区域
                for (let i = 0; i < Store3DData.Areas.length; i++) {
                    let optionArea = Store3DData.Areas[i];
                    let area = new StoreArea(optionArea);
                    object.addObject(area);
                    //显示仓库
                    for (let j = 0; j < optionArea.Stores.length; j++)//
                    {
                        let optionStore = optionArea.Stores[j];
                        optionStore.Position = CommonFunction.transPosition(optionStore.Position, optionArea.Position);
                        let store = new Store(optionStore);
                        object.addObject(store);
                        let storeSign = new StoreSign(optionStore, this);
                        let storeTemperature=new Temperature(optionStore, this);
                        //显示每行信息
                        for (let k = 0; k < optionStore.Groups.length; k++) {
                            let optionGroup = optionStore.Groups[k];
                            optionGroup.Position = CommonFunction.transPosition(optionGroup.Position, optionStore.Position);
                            let shelf = new StoreShelf(optionGroup);
                            let group=new StoreGroup(optionGroup,1);
                            object.addObject(shelf);
                            object.addObject(group);
                            //显示库位上的货物
                            for (let m = 0; m < optionGroup.Bins.length; m++) {
                                let optionBin = optionGroup.Bins[m];
                                let existGoods=this.getExistedGoodType(optionBin.State);
                                let storeGoods = new StoreGoods(optionGroup, optionBin);
                                if(existGoods==null) {
                                    let goods=storeGoods.create();
                                    object.addObject(goods);
                                    this.goodTypes.push({type: optionBin.State, object: goods});
                                }
                                else
                                {
                                    let goods= storeGoods.clone(existGoods);
                                    object.addObject(goods);
                                }
                            }
                        }
 
                    }
                }
                this.firstTime = 0;
            }
        }
    },
    /**
     * 显示或者隐藏性能控件
     */
    Store3D.prototype.changeStats = function () {
        if (this.stats.domElement.style.display == 'none')
            this.stats.domElement.style.display = 'block';
        else
            this.stats.domElement.style.display = 'none';
    }
 
/**
     * 获取是否已经生成同类型的货物
     */
    Store3D.prototype.getExistedGoodType=function(state){
        for (let i=0;i<this.goodTypes.length;i++)
        {
            let type=this.goodTypes[i];
            if(type.type===state)
            {
                return type.object;
            }
 
        }
        return null;
 
    },
    /**
     * 定时重复刷新
     */
    Store3D.prototype.animate = function () {
        requestAnimationFrame(this.animate.bind(this));
        this.renderer.render(this.scene, this.camera);
        this.stats.update();
        this.initStoreObjects(this);
    }