华农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.

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