华农3D项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

612 lines
19 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. // 从这里开始
  2. var h1 = null
  3. var h2 = null
  4. var h3 = null
  5. var oldtransformNodex = null
  6. // var oldtransformNodeOther = null
  7. // var oldMesh = null
  8. var oldMeshOther = null
  9. var drag = null
  10. // var notmoveGroup = null
  11. // var movedGroup = null
  12. // var movesingle = false
  13. // var dynamicTexture = null
  14. // var dynamicTexture2 = null
  15. // var show = 50
  16. // var showdd = 50
  17. var isRotating = true
  18. var clickParentRotate = false
  19. var camera
  20. BABYLON.DefaultLoadingScreen.prototype.displayLoadingUI = function() {
  21. if (document.getElementById('customLoadingScreenDiv')) {
  22. document.getElementById('customLoadingScreenDiv').style.display = 'initial'
  23. return
  24. }
  25. this._loadingDiv = document.createElement('div')
  26. // this._loadingDiv.style.height = '1750px';
  27. this._loadingDiv.id = 'customLoadingScreenDiv'
  28. thecss = 'lohingifpc'
  29. thecss2 = 'zcpc'
  30. this._loadingDiv.innerHTML = "<div class='oka'><div class='" + thecss + "'><image src='./logok.gif' class='jiazaiimg'/></div></div>"
  31. this._resizeLoadingUI()
  32. window.addEventListener('resize', this._resizeLoadingUI)
  33. document.body.appendChild(this._loadingDiv)
  34. }
  35. // var movebyName = function(nodeName) {
  36. // var x = scene.getTransformNodeByName(nodeName)
  37. // moveTrans(x)
  38. // }
  39. function prepareGroupButton2(transformNodex, color, qu) {
  40. var mesheses = null
  41. if (transformNodex.getClassName() === 'TransformNode') {
  42. mesheses = transformNodex.getChildMeshes(false)
  43. } else {
  44. mesheses = []
  45. mesheses.push(transformNodex)
  46. }
  47. for (var i = 0; i < mesheses.length; i++) {
  48. mesheses[i].actionManager = new BABYLON.ActionManager(scene)
  49. // var theScaling = mesheses[i].scaling
  50. /* if (true) {
  51. try {
  52. mesheses[i].actionManager.registerAction(new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnPointerOutTrigger, mesheses[i], "scaling", new BABYLON.Vector3(theScaling.x, theScaling.y, theScaling.z), 100));
  53. mesheses[i].actionManager.registerAction(new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnPointerOverTrigger, mesheses[i], "scaling", new BABYLON.Vector3(theScaling.x * 1.01, theScaling.y * 1.01, theScaling.z * 1.01), 100));
  54. } catch (error) {
  55. //alert(error)
  56. }
  57. }*/
  58. // var clickbegin = false
  59. mesheses[i].actionManager.registerAction(
  60. new BABYLON.ExecuteCodeAction({
  61. trigger: BABYLON.ActionManager.OnPointerOverTrigger,
  62. parameter: ''
  63. },
  64. function() {
  65. // clickbegin = true
  66. // if (true) {
  67. // if (oldtransformNodeOther != null) {
  68. // oldtransformNodeOther.removeBehavior(drag)
  69. // var mesheses2 = null
  70. // if (oldtransformNodeOther.getClassName() === 'TransformNode') {
  71. // mesheses2 = oldtransformNodeOther.getChildMeshes(false)
  72. // } else {
  73. // mesheses2 = []
  74. // mesheses2.push(oldtransformNodeOther)
  75. // }
  76. // for (var i = 0; i < mesheses2.length; i++) {
  77. // try {
  78. // h2.removeMesh(mesheses2[i])
  79. // } catch (error) {
  80. // // alert(error)
  81. // }
  82. // }
  83. // }
  84. // oldtransformNodeOther = transformNodex
  85. // // transformNodex.addBehavior(drag);
  86. // var mesheses3 = null
  87. // if (transformNodex.getClassName() === 'TransformNode') {
  88. // mesheses3 = transformNodex.getChildMeshes(false)
  89. // } else {
  90. // mesheses3 = []
  91. // mesheses3.push(transformNodex)
  92. // }
  93. // for (var i = 0; i < mesheses3.length; i++) {
  94. // try {
  95. // h2.addMesh(mesheses3[i], color)
  96. // } catch (error) {
  97. // // alert(error)
  98. // }
  99. // }
  100. isRotating = false
  101. camera.useAutoRotationBehavior = isRotating
  102. // }
  103. }
  104. )
  105. )
  106. // 移出事件
  107. mesheses[i].actionManager.registerAction(
  108. new BABYLON.ExecuteCodeAction({
  109. trigger: BABYLON.ActionManager.OnPointerOutTrigger,
  110. parameter: ''
  111. },
  112. function() {
  113. // clickbegin = true
  114. // if (true) {
  115. // var mesheses4 = null
  116. // if (transformNodex.getClassName() === 'Mesh') {
  117. // mesheses4 = []
  118. // mesheses4.push(transformNodex)
  119. // }
  120. // for (var i = 0; i < mesheses4.length; i++) {
  121. // try {
  122. // h2.removeMesh(mesheses4[i])
  123. // } catch (error) {
  124. // // alert(error)
  125. // }
  126. // }
  127. // clickbegin = false
  128. if (clickParentRotate) {
  129. isRotating = true
  130. camera.useAutoRotationBehavior = isRotating
  131. }
  132. // }
  133. }
  134. )
  135. )
  136. mesheses[i].actionManager.registerAction(
  137. new BABYLON.ExecuteCodeAction({
  138. trigger: BABYLON.ActionManager.OnPickTrigger,
  139. parameter: ''
  140. },
  141. function() {
  142. clickbegin = true
  143. // if (true) {
  144. try {
  145. cameraClick(transformNodex)
  146. } catch (error) {
  147. // alert(error)
  148. }
  149. // clickbegin = false
  150. // }
  151. })
  152. )
  153. }
  154. }
  155. BABYLON.DefaultLoadingScreen.prototype.hideLoadingUI = function() {
  156. show = 50
  157. document.getElementById('customLoadingScreenDiv').style.display = 'none'
  158. document.getElementById('customLoadingScreenDiv_first').style.display = 'none'
  159. document.title = '水泵房'
  160. var prepareGroupButton2ByMesh = function(transformNodex, color, qu) {
  161. var mesheses = null
  162. if (transformNodex.getClassName() === 'Mesh') {
  163. mesheses = []
  164. mesheses.push(transformNodex)
  165. }
  166. for (var i = 0; i < mesheses.length; i++) {
  167. mesheses[i].actionManager = new BABYLON.ActionManager(scene)
  168. // var theScaling = mesheses[i].scaling
  169. /* if (true) {
  170. try {
  171. mesheses[i].actionManager.registerAction(new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnPointerOutTrigger, mesheses[i], "scaling", new BABYLON.Vector3(theScaling.x, theScaling.y, theScaling.z), 100));
  172. mesheses[i].actionManager.registerAction(new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnPointerOverTrigger, mesheses[i], "scaling", new BABYLON.Vector3(theScaling.x * 1.01, theScaling.y * 1.01, theScaling.z * 1.01), 100));
  173. } catch (error) {
  174. //alert(error)
  175. }
  176. }*/
  177. // let clickbegin = false
  178. mesheses[i].actionManager.registerAction(
  179. new BABYLON.ExecuteCodeAction({
  180. trigger: BABYLON.ActionManager.OnPointerOverTrigger,
  181. parameter: ''
  182. },
  183. function() {
  184. // clickbegin = true
  185. // if (true) {
  186. if (oldMeshOther != null) {
  187. oldMeshOther.removeBehavior(drag)
  188. var mesheses2 = null
  189. if (oldMeshOther.getClassName() === 'Mesh') {
  190. mesheses2 = []
  191. mesheses2.push(oldMeshOther)
  192. }
  193. for (var i = 0; i < mesheses2.length; i++) {
  194. try {
  195. h2.removeMesh(mesheses2[i])
  196. } catch (error) {
  197. // alert(error)
  198. }
  199. }
  200. }
  201. oldMeshOther = transformNodex
  202. // transformNodex.addBehavior(drag);
  203. var mesheses3 = null
  204. if (transformNodex.getClassName() === 'Mesh') {
  205. mesheses3 = []
  206. mesheses3.push(transformNodex)
  207. }
  208. for (let i = 0; i < mesheses3.length; i++) {
  209. try {
  210. h2.addMesh(mesheses3[i], color)
  211. } catch (error) {
  212. // alert(error)
  213. }
  214. }
  215. // clickbegin = false
  216. isRotating = false
  217. camera.useAutoRotationBehavior = isRotating
  218. // }
  219. }
  220. )
  221. )
  222. // 移出事件
  223. mesheses[i].actionManager.registerAction(
  224. new BABYLON.ExecuteCodeAction({
  225. trigger: BABYLON.ActionManager.OnPointerOutTrigger,
  226. parameter: ''
  227. },
  228. function() {
  229. // clickbegin = true
  230. // if (true) {
  231. var mesheses4 = null
  232. if (transformNodex.getClassName() === 'Mesh') {
  233. mesheses4 = []
  234. mesheses4.push(transformNodex)
  235. }
  236. for (var i = 0; i < mesheses4.length; i++) {
  237. try {
  238. h2.removeMesh(mesheses4[i])
  239. } catch (error) {
  240. // alert(error)
  241. }
  242. }
  243. // clickbegin = false
  244. if (clickParentRotate) {
  245. isRotating = true
  246. camera.useAutoRotationBehavior = isRotating
  247. }
  248. // }
  249. }
  250. )
  251. )
  252. mesheses[i].actionManager.registerAction(
  253. new BABYLON.ExecuteCodeAction({
  254. trigger: BABYLON.ActionManager.OnPickTrigger,
  255. parameter: ''
  256. },
  257. function() {
  258. // clickbegin = true
  259. // if (true) {
  260. try {
  261. // quClick(transformNodex.name)
  262. cameraClick(transformNodex)
  263. } catch (error) {
  264. // alert(error)
  265. }
  266. // clickbegin = false
  267. // }
  268. }
  269. )
  270. )
  271. }
  272. }
  273. // 绑功能点
  274. // var cl = new BABYLON.Color3(0, 0, 1)
  275. // var c2 = new BABYLON.Color3(1, 0, 0)
  276. var smokeAlarms = []
  277. scene.transformNodes.forEach(function(node) {
  278. if (node.name.startsWith('smokealarm')) {
  279. smokeAlarms.push(node)
  280. }
  281. })
  282. var length = smokeAlarms.length
  283. console.log('以 smokealarm 开头的对象数量:', length)
  284. smokeAlarms.forEach(function(jk01, index) {
  285. console.log('smokeAlarms', jk01)
  286. var parts = jk01.name.split('smokealarm')
  287. if (parts.length > 1) {
  288. const paddedIndex = String(index + 1).padStart(3, '0')
  289. jk01.nameID = `S${paddedIndex}`
  290. // jk01.nameID = 'S' + parts[1]
  291. } else {
  292. jk01.nameID = 'S000'
  293. }
  294. jk01.baojing = false
  295. prepareGroupButton2(jk01)
  296. })
  297. // 存储所有 camera 相关的 Mesh
  298. const cameraMeshes = []
  299. scene.meshes.forEach(mesh => {
  300. if (mesh.name.startsWith('camera')) {
  301. cameraMeshes.push(mesh)
  302. }
  303. })
  304. const cameraCount = cameraMeshes.length
  305. console.log('以 camera 开头的 Mesh 数量:', cameraCount)
  306. // 定义颜色
  307. // const colorRed = new BABYLON.Color3(1, 0, 0) // 红
  308. const colorBlue = new BABYLON.Color3(0, 0, 1)
  309. cameraMeshes.forEach((cam, index) => {
  310. const paddedIndex = String(index + 1).padStart(3, '0')
  311. cam.nameID = `C${paddedIndex}`
  312. prepareGroupButton2ByMesh(cam, colorBlue, '摄像头1')
  313. })
  314. var light = new BABYLON.HemisphericLight('HemiLight', new BABYLON.Vector3(0, 1, 0), scene)
  315. light.intensity = 2
  316. light.diffuse = new BABYLON.Color3(0.92, 0.92, 0.92)
  317. // 给vue页面传值,加载完成之后
  318. parent.getIframeLoading('false')
  319. }
  320. BABYLON.DracoCompression.Configuration.decoder.wasmUrl = './js/draco_wasm_wrapper_gltf.js'
  321. BABYLON.DracoCompression.Configuration.decoder.wasmBinaryUrl = './js/draco_decoder_gltf.wasm'
  322. BABYLON.DracoCompression.Configuration.decoder.fallbackUrl = './js/draco_decoder_gltf.js'
  323. // createScene function that creates and return the scene
  324. var createScene = function() {
  325. engine.displayLoadingUI()
  326. // var scene = new BABYLON.Scene(engine)
  327. // camera = new BABYLON.ArcRotateCamera('Camera', 0, 0, 5, new BABYLON.Vector3(0, -1, 0), scene)
  328. // camera.setTarget(new BABYLON.Vector3(0, 0, 0))
  329. // camera.attachControl(canvas, true)
  330. var scene = new BABYLON.Scene(engine)
  331. camera = new BABYLON.ArcRotateCamera(
  332. 'Camera', Math.PI * -218.4 / 180, Math.PI * 40 / 180, 24, // alpha=0(水平角度),beta=90°(垂直俯视),radius=200(距离)
  333. new BABYLON.Vector3(0, 0, 0), // 相机位置会根据 alpha/beta/radius 自动计算,无需手动设置
  334. scene
  335. )
  336. camera.setTarget(new BABYLON.Vector3(0, 0, 0)) // 瞄准原点
  337. camera.attachControl(canvas, true)
  338. camera.lowerRadiusLimit = 5.0 // 这里是最大的位置,值越大,物体越小
  339. camera.upperRadiusLimit = 200
  340. camera.inertia = 0.1 // 缩放的快慢
  341. camera.useAutoRotationBehavior = isRotating // 自动旋转
  342. // 改变场景背景颜色 - 背景颜色opacity值设为0,达到透明背景的效果
  343. scene.clearColor = new BABYLON.Color4(0, 0, 0, 0)
  344. scene.activeCamera = camera
  345. scene.activeCamera.useInputToRestoreState = true
  346. // var camera1_status = scene.activeCamera.storeState()
  347. var assetsManager = new BABYLON.AssetsManager(scene)
  348. // var meshTask = assetsManager.addMeshTask('skull task', '', 'asset/', 'F5.glb')
  349. // meshTask.onSuccess = function(task) {
  350. // task.loadedMeshes[0].scaling = new BABYLON.Vector3(0.4, 0.4, 0.4)
  351. // task.loadedMeshes[0].position = new BABYLON.Vector3(0, 0.35, 0)
  352. // }
  353. // meshTask.onError = function(task, message, exception) {
  354. // console.log(message, exception)
  355. // }
  356. // 加载GLB模型并处理材质
  357. BABYLON.SceneLoader.LoadAssetContainerAsync('asset/', 'F5.glb', scene).then((container) => {
  358. // 遍历所有材质
  359. container.materials.forEach(material => {
  360. // 处理标准材质
  361. if (material instanceof BABYLON.StandardMaterial) {
  362. material.backFaceCulling = true // 启用背面剔除
  363. }
  364. // 处理PBR材质
  365. // if (material instanceof BABYLON.PBRMaterial) {
  366. // material.backFaceCulling = true // 启用背面剔除
  367. // }
  368. })
  369. // 将加载的资产添加到场景中
  370. container.addAllToScene()
  371. }).catch((error) => {
  372. console.error('加载模型时出错:', error)
  373. })
  374. var hdrTexture = new BABYLON.CubeTexture.CreateFromPrefilteredData('textures/environmentSpecular.env', scene)
  375. scene.environmentTexture = hdrTexture
  376. var spriteManagerPlayer = new BABYLON.SpriteManager('playerManager', './img/bl9.png', 10, {
  377. width: 100,
  378. height: 100
  379. }, scene)
  380. // spriteManagerPlayer.renderingGroupId = 2;
  381. spriteManagerPlayer.isPickable = true
  382. scene.onPointerDown = function(evt) {
  383. var pickResult = scene.pickSprite(this.pointerX, this.pointerY)
  384. var pick = scene.pick(scene.pointerX, scene.pointerY)
  385. console.log('pick', pick.pickedPoint)
  386. if (pickResult.pickedSprite != null) {
  387. if (pickResult.hit) {
  388. alert(pickResult.pickedSprite.name)
  389. }
  390. }
  391. }
  392. // renderCanvas.style.backgroundImage = 'url("./textures/' + '4' + '.jpg")'
  393. assetsManager.load()
  394. drag = new BABYLON.PointerDragBehavior({
  395. dragPlaneNormal: new BABYLON.Vector3(0, 0, 1)
  396. })
  397. // drag.useObjectOrienationForDragging = false;
  398. drag.validateDrag = (targetPosition) => {
  399. if (targetPosition.x > 10.5 || targetPosition.x < -10.5) {
  400. return false
  401. }
  402. if (targetPosition.z > 10.5 || targetPosition.z < -10.5) {
  403. return false
  404. }
  405. if (oldtransformNodex != null) {
  406. return true
  407. }
  408. }
  409. drag.onDragEndObservable.add((event) => {
  410. // console.log("dragEnd");
  411. // console.log(event);
  412. // console.log(line03_position)
  413. })
  414. // GUI
  415. h1 = new BABYLON.HighlightLayer('hl1', scene)
  416. h2 = new BABYLON.HighlightLayer('hl2', scene)
  417. h3 = new BABYLON.HighlightLayer('hl2', scene)
  418. var step = 0.1
  419. var currentx = 1
  420. // var step2 = 0.1
  421. // var currentx2 = 0.1
  422. h1.blurHorizontalSize = 0.1
  423. // var showx = 0
  424. // var showx2 = 2
  425. // console.log("x4");
  426. scene.registerAfterRender(() => {
  427. h1.blurHorizontalSize = h1.blurVerticalSize + currentx
  428. var nodealert = scene.getTransformNodeByName('yan')
  429. if (nodealert != null) {
  430. var mesheses3 = null
  431. if (nodealert.getClassName() === 'TransformNode') {
  432. mesheses3 = nodealert.getChildMeshes(false)
  433. } else {
  434. mesheses3 = []
  435. mesheses3.push(nodealert)
  436. }
  437. for (var i = 0; i < mesheses3.length; i++) {
  438. try {
  439. if (nodealert.baojing === true) {
  440. h3.addMesh(mesheses3[i], new BABYLON.Color3(1, 0, 0))
  441. } else {
  442. h3.removeMesh(mesheses3[i])
  443. }
  444. } catch (error) {
  445. // alert(error)
  446. }
  447. }
  448. }
  449. })
  450. if (currentx > 0.5) {
  451. step *= -1
  452. }
  453. if (currentx < 0) {
  454. step *= -1
  455. }
  456. currentx += step
  457. return scene
  458. }
  459. var canvas = document.getElementById('renderCanvas')
  460. // load the 3D engine
  461. var engine = new BABYLON.Engine(canvas, true, {
  462. stencil: true
  463. })
  464. // call the createScene function
  465. var scene = createScene()
  466. scene.autoClear = true
  467. scene.imageProcessingConfiguration.exposure = 1
  468. scene.imageProcessingConfiguration.contrast = 1
  469. scene.environmentIntensity = 0.4
  470. engine.runRenderLoop(function() {
  471. scene.render()
  472. })
  473. window.addEventListener('resize', function() {
  474. engine.resize()
  475. })
  476. // 功能转入报警
  477. // function Myalert(MathineID, baojingx) {
  478. // var theName = yangans.get(MathineID)
  479. // if (theName === 'yan') {
  480. // // console.log(scene);
  481. // xx = scene.getTransformNodeByName('yan').baojing = baojingx
  482. // }
  483. // }
  484. // scene.debugLayer.show();
  485. // 报警 true:表示报警, false :表示不报警
  486. function cameraClick(TheCamera) {
  487. window.parent.postMessage(
  488. {
  489. data: TheCamera.nameID
  490. },
  491. '*'
  492. )
  493. }
  494. function handleAlarm(deviceId, isAlarm) {
  495. var cl = new BABYLON.Color3(0, 0, 1)
  496. var c2 = new BABYLON.Color3(1, 0, 0)
  497. const targetDevice = scene.transformNodes.find(node => node.nameID === deviceId)
  498. if (targetDevice) {
  499. targetDevice.baojing = isAlarm
  500. var mesheses = null
  501. if (targetDevice.getClassName() === 'TransformNode') {
  502. mesheses = targetDevice.getChildMeshes(false)
  503. } else {
  504. mesheses = []
  505. mesheses.push(targetDevice)
  506. }
  507. if (isAlarm) {
  508. console.log('111')
  509. for (var i = 0; i < mesheses.length; i++) {
  510. h2.addMesh(mesheses[i], c2)
  511. }
  512. } else {
  513. console.log('222')
  514. for (var j = 0; j < mesheses.length; j++) {
  515. h2.removeMesh(mesheses[j])
  516. h2.addMesh(mesheses[j], cl)
  517. }
  518. }
  519. } else {
  520. console.log(`未找到设备 ID 为 ${deviceId} 的设备`)
  521. }
  522. }
  523. window.addEventListener(
  524. 'message',
  525. function(e) {
  526. if (e.data.type === 'deviceState') {
  527. console.log('ddd', e.data.data)
  528. const deviceData = e.data.data
  529. if (Array.isArray(deviceData)) {
  530. deviceData.forEach((device) => {
  531. handleAlarm(device.deviceId, device.isAlarm)
  532. })
  533. } else {
  534. handleAlarm(deviceData.deviceId, deviceData.isAlarm)
  535. }
  536. } else if (e.data.type === 'isGetRotate') {
  537. const isGetRotate = e.data.value
  538. console.log('isGetRotate:', isGetRotate)
  539. // 这里可以添加处理 isGetRotate 的逻辑
  540. toggleAutoRotation(isGetRotate)
  541. }
  542. },
  543. false
  544. )
  545. function toggleAutoRotation(isAutoRotating) {
  546. var camera = scene.activeCamera
  547. clickParentRotate = !isAutoRotating
  548. if (!isAutoRotating) {
  549. // 启用自动旋转
  550. camera.useAutoRotationBehavior = true
  551. } else {
  552. // 禁用自动旋转
  553. camera.useAutoRotationBehavior = false
  554. camera.angularSpeed = 0 // 停止旋转
  555. }
  556. }