812c151760040730d36090692b167203e6ca3528.svn-base 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import reveal from '../core/reveal'
  2. import * as THREE from 'three'
  3. import { animation, init } from './meshAnimation'
  4. import config from './config'
  5. import FeaturesToAttribute from '../worker/featuresToAttribute.worker'
  6. import Event from '../core/Event'
  7. import {clearFids} from '../core/dyWoker'
  8. const worker = new FeaturesToAttribute()
  9. const cache = reveal.cache
  10. const frustum = new THREE.Frustum();
  11. const cameraViewProjectionMatrix = new THREE.Matrix4();
  12. const hideMeshs = []
  13. const $event = new Event()
  14. worker.addEventListener('message', listener)
  15. function listener(event) {
  16. $event.emit(
  17. event.data.incident,
  18. event.data.animationArray
  19. )
  20. }
  21. const showMesh = function() {
  22. let IN_ID = 0
  23. return function showMesh(mesh, index) {
  24. if (mesh.visible) return;
  25. let incident = 'grentAnimation' + (IN_ID++)
  26. let verticesArray = mesh.geometry.attributes.position.array;
  27. let postObj = {
  28. thing: 'grentAnimationArray',
  29. verticesBuffArrays: verticesArray,
  30. maxHeight: mesh.maxHeight,
  31. incident
  32. }
  33. worker.postMessage(postObj)
  34. $event.once(incident, animationArray => {
  35. init(mesh.geometry.attributes.position.array, mesh.maxHeight)
  36. setTimeout(() => {
  37. mesh.visible = true
  38. animation(mesh, verticesArray, animationArray, config.stepTotal)
  39. }, 300)
  40. threeLayer.renderScene()
  41. })
  42. }
  43. }();
  44. function hideMesh(mesh, index) {
  45. mesh.visible = false
  46. threeLayer.renderScene()
  47. }
  48. function meshStatus_() {
  49. const camera = threeLayer.getCamera();
  50. camera.updateMatrixWorld();
  51. camera.matrixWorldInverse.getInverse(camera.matrixWorld);
  52. cameraViewProjectionMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
  53. frustum.setFromMatrix(cameraViewProjectionMatrix);
  54. for (let url in cache) {
  55. let meshs = cache[url]
  56. if (!Array.isArray(meshs)) continue;
  57. for (let i = 0; i < meshs.length; i++) {
  58. let mesh = meshs[i]
  59. let index = hideMeshs.indexOf(mesh)
  60. let viewMesh = frustum.intersectsObject(mesh)
  61. if (viewMesh) {
  62. mesh.hideCount = 0
  63. } else {
  64. mesh.hideCount = (mesh.hideCount || 0) + 1;
  65. }
  66. if (viewMesh && ~index) {
  67. showMesh(mesh, index)
  68. hideMeshs.splice(index, 1)
  69. } else if (!viewMesh && !~index) {
  70. if (mesh.visible && mesh.hideCount >= 30) {
  71. hideMesh(mesh, index)
  72. hideMeshs.push(mesh)
  73. }
  74. }
  75. if (mesh.hideCount > 300) {
  76. threeLayer.getScene().remove(mesh)
  77. reveal.eliminate(url);
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. function meshStatus() {
  84. const camera = threeLayer.getCamera();
  85. camera.updateMatrixWorld();
  86. camera.matrixWorldInverse.getInverse(camera.matrixWorld);
  87. cameraViewProjectionMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
  88. frustum.setFromMatrix(cameraViewProjectionMatrix);
  89. let scene = threeLayer.getScene()
  90. let children = scene.children
  91. children.forEach(m => {
  92. if (m.type === 'Mesh') {
  93. let viewMesh = frustum.intersectsObject(m)
  94. if (!viewMesh) {
  95. scene.remove(m)
  96. m.isRemove = true
  97. clearFids(m.fids)
  98. }
  99. }
  100. })
  101. }
  102. export default meshStatus