BinaryLoader.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. import {Version} from "../Version.js";
  3. import {XHRFactory} from "../XHRFactory.js";
  4. //加载 解析点云
  5. export class BinaryLoader{
  6. constructor(version, boundingBox, scale){
  7. if (typeof (version) === 'string') {
  8. this.version = new Version(version);
  9. } else {
  10. this.version = version;
  11. }
  12. this.boundingBox = boundingBox;
  13. this.scale = scale;
  14. }
  15. load(node){
  16. if (node.loaded) {
  17. return;
  18. }
  19. let url = node.getURL();
  20. if (this.version.equalOrHigher('1.4')) {
  21. url += '.bin';
  22. }
  23. let xhr = XHRFactory.createXMLHttpRequest();
  24. xhr.open('GET', url, true);
  25. xhr.responseType = 'arraybuffer';
  26. xhr.overrideMimeType('text/plain; charset=x-user-defined');
  27. xhr.onreadystatechange = () => {
  28. if (xhr.readyState === 4) {
  29. if((xhr.status === 200 || xhr.status === 0) && xhr.response !== null){
  30. let buffer = xhr.response;
  31. this.parse(node, buffer);
  32. } else {
  33. //console.error(`Failed to load file! HTTP status: ${xhr.status}, file: ${url}`);
  34. throw new Error(`Failed to load file! HTTP status: ${xhr.status}, file: ${url}`);
  35. }
  36. }
  37. };
  38. try {
  39. xhr.send(null);
  40. } catch (e) {
  41. console.log('fehler beim laden der punktwolke: ' + e);
  42. }
  43. };
  44. parse(node, buffer, callback){
  45. let pointAttributes = node.pcoGeometry.pointAttributes; //定义自cloud.js
  46. let numPoints = buffer.byteLength / node.pcoGeometry.pointAttributes.byteSize;
  47. if (this.version.upTo('1.5')) {
  48. node.numPoints = numPoints;
  49. }
  50. let workerPath = Potree.scriptPath + '/workers/BinaryDecoderWorker.js';
  51. let worker = Potree.workerPool.getWorker(workerPath);
  52. let maxRest = 3 * ((Potree.settings.splatSH+1)*(Potree.settings.splatSH+1) - 1)
  53. worker.onmessage = function (e) {
  54. let data = e.data;
  55. let buffers = data.attributeBuffers;
  56. let tightBoundingBox = new THREE.Box3(
  57. new THREE.Vector3().fromArray(data.tightBoundingBox.min),
  58. new THREE.Vector3().fromArray(data.tightBoundingBox.max)
  59. );
  60. Potree.workerPool.returnWorker(workerPath, worker);
  61. let geometry = new THREE.BufferGeometry();
  62. for(let property in buffers){
  63. let buffer = buffers[property].buffer;
  64. let batchAttribute = buffers[property].attribute;
  65. if (property === "POSITION_CARTESIAN") {
  66. geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(buffer), 3));
  67. } else if (property === "centersFloat") {//add
  68. geometry.setAttribute("centersFloat", new THREE.BufferAttribute(new Float32Array(buffer), 4 ));
  69. } else if (property === "centersInt") {//add
  70. geometry.setAttribute("centersInt", new THREE.BufferAttribute(new Int32Array(buffer), 4 ));
  71. } else if (property === "rgba") {
  72. geometry.setAttribute("rgba", new THREE.BufferAttribute(new Uint8Array(buffer), 4, true));
  73. } else if (property === "NORMAL_SPHEREMAPPED") {
  74. geometry.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(buffer), 3));
  75. } else if (property === "NORMAL_OCT16") {
  76. geometry.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(buffer), 3));
  77. } else if (property === "NORMAL") {
  78. geometry.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(buffer), 3));
  79. } else if (property === "INDICES") {
  80. let bufferAttribute = new THREE.BufferAttribute(new Uint8Array(buffer), 4);
  81. bufferAttribute.normalized = true;
  82. geometry.setAttribute('indices', bufferAttribute);
  83. } else if (property === "SPACING") {
  84. let bufferAttribute = new THREE.BufferAttribute(new Float32Array(buffer), 1);
  85. geometry.setAttribute('spacing', bufferAttribute);
  86. }else if (property === "IR" || property === "TEMP" ) {//温度
  87. let bufferAttribute = new THREE.BufferAttribute(new Float32Array(buffer), 1);
  88. bufferAttribute.tempRange = batchAttribute.range
  89. geometry.setAttribute(property.toLowerCase(), bufferAttribute);
  90. }else if (property === "SEG") {//分类
  91. let bufferAttribute = new THREE.BufferAttribute(new Float32Array(buffer), 1);
  92. geometry.setAttribute('classification', bufferAttribute);
  93. }else if (property === "covs") {//add
  94. let bufferAttribute = new THREE.BufferAttribute(new Float32Array(buffer), 6);
  95. geometry.setAttribute('covs', bufferAttribute);
  96. }else if (property === "sh") {//add
  97. let bufferAttribute = new THREE.BufferAttribute(new Float32Array(buffer),maxRest);
  98. geometry.setAttribute('sh', bufferAttribute);
  99. }
  100. /* else if(property != 'GS3D'){//改
  101. //geometry.setAttribute("rgba", new THREE.BufferAttribute(new Uint8Array(buffer), 4, true));
  102. const bufferAttribute = new THREE.BufferAttribute(new Float32Array(buffer), 1);
  103. bufferAttribute.potree = {
  104. offset: buffers[property].offset,
  105. scale: buffers[property].scale,
  106. preciseBuffer: buffers[property].preciseBuffer,
  107. range: batchAttribute.range,
  108. };
  109. geometry.setAttribute(property, bufferAttribute);
  110. const attribute = pointAttributes.attributes.find(a => a.name === batchAttribute.name);
  111. attribute.range[0] = Math.min(attribute.range[0], batchAttribute.range[0]);
  112. attribute.range[1] = Math.max(attribute.range[1], batchAttribute.range[1]);
  113. if(node.getLevel() === 0){
  114. attribute.initialRange = batchAttribute.range;
  115. }
  116. } */
  117. }
  118. tightBoundingBox.max.sub(tightBoundingBox.min);
  119. tightBoundingBox.min.set(0, 0, 0);
  120. let numPoints = e.data.buffer.byteLength / pointAttributes.byteSize;
  121. node.numPoints = numPoints;
  122. node.geometry = geometry;
  123. node.mean = new THREE.Vector3(...data.mean);
  124. node.tightBoundingBox = tightBoundingBox;
  125. node.loaded = true;
  126. node.loading = false;
  127. node.estimatedSpacing = data.estimatedSpacing;
  128. Potree.numNodesLoading--;
  129. callback()//add
  130. };
  131. let message = {
  132. buffer: buffer,
  133. pointAttributes: pointAttributes,
  134. version: this.version.version,
  135. min: [ node.boundingBox.min.x, node.boundingBox.min.y, node.boundingBox.min.z ],
  136. offset: [node.pcoGeometry.offset.x, node.pcoGeometry.offset.y, node.pcoGeometry.offset.z],
  137. scale: this.scale,
  138. spacing: node.spacing,
  139. hasChildren: node.hasChildren,
  140. name: node.name
  141. };
  142. worker.postMessage(message, [message.buffer]);
  143. };
  144. }