-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreejsModule.js
More file actions
371 lines (359 loc) · 14.4 KB
/
Copy paththreejsModule.js
File metadata and controls
371 lines (359 loc) · 14.4 KB
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
import * as THREE from './build/three.module.js';
import { RGBELoader } from './jsm/loaders/RGBELoader.js';
import {OrbitControls} from './jsm/controls/OrbitControls.js';
import {GLTFLoader} from './jsm/loaders/GLTFLoader.js';
import Mqtt from './mqtt.js'
import {
CSS3DRenderer,
CSS3DObject,
} from "./jsm/CSS3DRenderer.js";
let scene, renderer, camera;
let model, skeleton, mixer, clock;
let greenMaterial, yellowMaterial, redMaterial;
let raycaster, mouse;
let render3D;
let materialGreen, materialYellow, materialRed, emissiveMaterialGreen, emissiveMaterialYellow, emissiveMaterialRed;
let timer = {}
let cssObject = {}
function init(path) {
clock = new THREE.Clock();
scene = new THREE.Scene();
scene.background = new THREE.Color(0xa0a0a0);
// new RGBELoader()
// .setPath( 'textures/equirectangular/' )
// .load( 'venice_sunset_1k.hdr', function ( texture ) {
// texture.mapping = THREE.EquirectangularReflectionMapping;
// scene.environment = texture;
// })
const loader = new GLTFLoader();
loader.load(path, function (gltf) {
model = gltf.scene.children[0];
console.log(model)
setMachineMaterial(model)
setEmergenceLight(model)
const scale = 0.4
model.scale.set(scale,scale,scale);
scene.add(model);
model.updateMatrixWorld(true)
setModelPosition(model)
setShadow(model)
skeleton = new THREE.SkeletonHelper(model);
skeleton.visible = false;
scene.add(skeleton);
mixer = new THREE.AnimationMixer(model);
animate();
});
// renderer
setRenderer()
// camera
setCamera()
// controls
setControls()
// light
setLight()
window.addEventListener('resize', onWindowResize);
// window.addEventListener( 'click', onMouseClick, false );
connectMqtt()
}
function createdCSS3DLabel() {
//增加一个CSS3D对象
let element = document.createElement("div");
element.className = "css3dcontain";
element.style.opacity = 0.75;
let lableTitle = document.createElement("div");
lableTitle.className = "css3dTitle";
// lableTitle.style.opacity = 0.75;
lableTitle.textContent = "数据详情";
element.appendChild(lableTitle);
let lableItem = document.createElement("div");
lableItem.className = "css3dItem";
lableItem.style.opacity = 0.75;
let lableLeft = document.createElement("div");
lableLeft.className = "css3dLeft";
lableLeft.textContent = "产量";
// lableLeft.style.opacity = 0.75;
let lableRight = document.createElement("div");
lableRight.className = "css3dRight";
// lableRight.style.opacity = 0.75;
lableRight.textContent = "10/h";
lableItem.appendChild(lableLeft);
lableItem.appendChild(lableRight);
element.appendChild(lableItem);
return element;
}
function setLight() {
const light = new THREE.AmbientLight( 0xffffff, 0.5 ); // soft white light
scene.add( light );
const PointLightUp = new THREE.PointLight( 0xffffff, 2, 100 );
PointLightUp.position.set(0, 4, -2);
PointLightUp.castShadow = true
PointLightUp.shadow.bias = -0.0005
PointLightUp.shadow.mapSize.width = 1024; // default
PointLightUp.shadow.mapSize.height = 1024; // default
PointLightUp.shadow.camera.near = 0.5; // default
PointLightUp.shadow.camera.far = 1.5 // default
scene.add( PointLightUp );
const PointLightDown = PointLightUp.clone()
PointLightDown.position.set(0, 4, 2)
scene.add( PointLightDown );
}
function setModelPosition(object) {
object.updateMatrixWorld();
const box = new THREE.Box3().setFromObject(object);
const center = box.getCenter(new THREE.Vector3());
object.position.x += object.position.x - center.x;
object.position.y += object.position.y - center.y;
object.position.z += object.position.z - center.z;
}
function setControls () {
const controls = new OrbitControls(camera, renderer.domElement);
controls.enablePan = false;
controls.enableZoom = true;
controls.update();
const controls2 = new OrbitControls(camera, render3D.domElement);
controls2.enablePan = false;
controls2.enableZoom = true;
controls2.update();
}
function setCamera () {
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight,1, 1000 );
camera.position.set(0,6,12);
}
function setRenderer () {
const container = document.getElementById('container');
// model renderer
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.shadowMap.enabled = true;
container.appendChild(renderer.domElement);
// css renderer
render3D = new CSS3DRenderer();
render3D.setSize(
window.innerWidth, window.innerHeight
);
render3D.domElement.style.position = "absolute";
render3D.domElement.style.top = '0';
container.appendChild(render3D.domElement);
}
function setShadow(model) {
model.traverse(function (object) {
if (object.name === '墙体') {
object.children.forEach(item => {
item.receiveShadow = true;
object.castShadow = false
})
} else {
object.castShadow = true;
}
});
}
function setMachineMaterial(model) {
greenMaterial = model.children.filter(item => item.name === '机器3')[0].children[0].material.clone()
const yellowMaterialColor = model.children.filter(item => item.name === '机器2')[0].children[0].material.color
yellowMaterial = model.children.filter(item => item.name === '机器3')[0].children[0].material.clone()
yellowMaterial.color = yellowMaterialColor
redMaterial = model.children.filter(item => item.name === '机器1')[0].children[0].material.clone()
model.traverse(function (object) {
if (object.name.startsWith('机器')) {
object.children[0].material = greenMaterial
}
});
}
// function onMouseClick(event) {
// event.preventDefault();
// raycaster = new THREE.Raycaster()
// mouse = new THREE.Vector2()
// mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
// mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
// raycaster.setFromCamera(mouse, camera)
// let intersects = raycaster.intersectObjects(scene.children);
// console.log(intersects)
// const obj = intersects[0].object
// if (obj.parent.name.startsWith('机器')) {
// if (obj.material === redMaterial) {
// obj.material = yellowMaterial
// } else if (obj.material === yellowMaterial) {
// obj.material = greenMaterial
// } else {
// obj.material = redMaterial
// }
// // get world position
//
// const position = getWorldPosition(obj)
// // create css
// const element = createdCSS3DLabel();
// let css3DObject = new CSS3DObject(element);
// if (position.x > 2) {
// css3DObject.position.x = position.x - 2.05
// } else if (position.x > 1.5) {
// css3DObject.position.x = position.x - 1.05
// } else if (position.x < -2 ) {
// css3DObject.position.x = position.x + 0.96
// } else {
// css3DObject.position.x = position.x - 0.05
// }
// console.log( css3DObject.position.x)
// // css3DObject.position.x = position.x > 1.6 ? position.x - 2 : position.x
// css3DObject.position.y = position.y + 0.2
// css3DObject.position.z = position.z > 2.8 ? position.z - 1.5 : position.z + 0.9
// setModelPosition(css3DObject)
// const scale = 0.01
// css3DObject.scale.set(scale, scale, scale)
// // model.children.push(css3DObject)
// scene.add(css3DObject);
// }
// }
function getWorldPosition(obj) {
obj.geometry.computeBoundingBox();
const boundingBox = obj.geometry.boundingBox;
const position = new THREE.Vector3();
position.subVectors( boundingBox.max, boundingBox.min );
position.multiplyScalar( 1);
position.add( boundingBox.min );
position.applyMatrix4( obj.matrixWorld );
return position
}
function setEmergenceLight(model) {
console.log(model)
const lights = model.children.filter(item => item.name === "三色灯1")[0].children
materialGreen = lights[1].material.clone()
materialYellow = lights[2].material.clone()
materialRed = lights[4].material.clone()
emissiveMaterialGreen = lights[1].material.clone()
emissiveMaterialYellow = lights[2].material.clone()
emissiveMaterialRed = lights[4].material.clone()
emissiveMaterialGreen.emissive = new THREE.Color( 0,255, 0 );
emissiveMaterialYellow.emissive = new THREE.Color( 225,0, 0 );
emissiveMaterialRed.emissive = new THREE.Color( 255,0, 0 );
for(let i = 1; i < 9; i++){
const device = model.children.filter(item => item.name === "三色灯" + i)[0]
device.children[1].material = emissiveMaterialGreen
device.children[2].material = materialYellow
device.children[4].material = materialRed
timer[device+i] = setInterval(() => {
if( device.children[1].material === materialGreen) {
device.children[1].material = emissiveMaterialGreen
} else {
device.children[1].material = materialGreen
}
}, 500)
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
// Render loop
requestAnimationFrame(animate);
// Get the time elapsed since the last frame, used for mixer update
const mixerUpdateDelta = clock.getDelta();
mixer.update(mixerUpdateDelta);
renderer.render(scene, camera);
render3D.render(scene, camera);
}
function connectMqtt() {
const mqttOptions = {
// host: '192.168.53.209',
// port: 11884,
// useSSL: false,
// path: '/',
// clientId: 'reserved?nonce=456×tamp=1625845512921',
// username: 'wHsdx3IOHL77zlK2UYmn',
// password: 'de5dc3536274581e64e178e1365d92d39978d4c81225f29df76f4a48f831cb5f',
// subscription: [
// `/sys/aoZvAxQmzg/aoZvAxQmzh/thing/property/post`,
// ]
host: 'larkea.staging.pivaiot.com',
port: 443,
useSSL: true,
path: '/mqtt',
clientId: 'reserved?nonce=456×tamp=1625845512921',
username: '2eiqA27osEW8EDnDuiqAe8',
password: 'de5dc3536274581e64e178e1365d92d39978d4c81225f29df76f4a48f831cb5f',
subscription: [
`/sys/aoZvAxQmzg/aoZvAxQmzh/thing/property/post`,
]
}
const mqtt = new Mqtt(mqttOptions, (message) => {
const data = JSON.parse(message.payloadString).properties
console.log(data)
const deviceLight = model.children.filter(item => item.name === "三色灯" + data.id)[0]
const device = model.children.filter(item => item.name === "机器" + data.id)[0].children[0]
if(timer[deviceLight+data.id]) {
clearInterval(timer[deviceLight+data.id])
timer[deviceLight+data.id] = ''
}
if(cssObject[device+data.id]) {
scene.remove(cssObject[device+data.id])
cssObject[device+data.id] = ''
}
switch (data.status) {
case 0:
device.material = greenMaterial
deviceLight.children[1].material = emissiveMaterialGreen
deviceLight.children[2].material = materialYellow
deviceLight.children[4].material = materialRed
timer[deviceLight+data.id] = setInterval(() => {
if( deviceLight.children[1].material === materialGreen) {
deviceLight.children[1].material = emissiveMaterialGreen
} else {
deviceLight.children[1].material = materialGreen
}
}, 500)
break
case 1:
device.material = redMaterial
deviceLight.children[1].material = materialGreen
deviceLight.children[2].material = materialYellow
deviceLight.children[4].material = emissiveMaterialRed
timer[deviceLight+data.id] = setInterval(() => {
if( deviceLight.children[4].material === materialRed) {
deviceLight.children[4].material = emissiveMaterialRed
} else {
deviceLight.children[4].material = materialRed
}
}, 500)
const position = getWorldPosition(device)
// create css
const element = createdCSS3DLabel();
let css3DObject = new CSS3DObject(element);
if (position.x > 2) {
css3DObject.position.x = position.x - 2.05
} else if (position.x > 1.5) {
css3DObject.position.x = position.x - 1.05
} else if (position.x < -2 ) {
css3DObject.position.x = position.x + 0.96
} else {
css3DObject.position.x = position.x - 0.05
}
console.log( css3DObject.position.x)
css3DObject.position.y = position.y + 0.2
css3DObject.position.z = position.z > 2.8 ? position.z - 1.5 : position.z + 0.9
setModelPosition(css3DObject)
const scale = 0.01
css3DObject.scale.set(scale, scale, scale)
scene.add(css3DObject);
cssObject[device+data.id] = css3DObject
break
case 2:
device.material = yellowMaterial
deviceLight.children[1].material = materialGreen
deviceLight.children[2].material = emissiveMaterialYellow
deviceLight.children[4].material = materialRed
timer[deviceLight+data.id] = setInterval(() => {
if( deviceLight.children[2].material === materialYellow) {
deviceLight.children[2].material = emissiveMaterialYellow
} else {
deviceLight.children[2].material = materialYellow
}
}, 500)
break
}
})
mqtt.connectMqtt()
}
export {init}