@@ -3,7 +3,7 @@ import * as THREE from 'three';
3
3
import './index.module.less' ;
4
4
import React from 'react' ;
5
5
import { OrbitControls } from './OrbitControls' ;
6
-
6
+ import pako from 'pako' ;
7
7
export interface CloudPointProps {
8
8
width : number ;
9
9
height : number ;
@@ -40,13 +40,40 @@ const CloudPoint: React.FC<CloudPointProps> = (props: CloudPointProps) => {
40
40
const cloud = new THREE . Points ( geometry , materials ) ;
41
41
scene . add ( cloud ) ;
42
42
43
+ function decompressData ( compressedData ) {
44
+ const uint8Array = new Uint8Array ( compressedData ) ;
45
+ const decompressedData = pako . inflate ( uint8Array , { to : 'string' } ) ;
46
+ return decompressedData ;
47
+ }
48
+
49
+ function blobToUint8Array ( blob ) {
50
+ return new Promise ( ( resolve , reject ) => {
51
+ const reader = new FileReader ( ) ;
52
+
53
+ reader . onloadend = ( ) => {
54
+ if ( reader . readyState === FileReader . DONE ) {
55
+ const uint8Array = new Uint8Array ( reader . result ) ;
56
+ resolve ( uint8Array ) ;
57
+ } else {
58
+ reject ( new Error ( 'Failed to read Blob as Uint8Array.' ) ) ;
59
+ }
60
+ } ;
61
+
62
+ reader . onerror = ( ) => {
63
+ reject ( new Error ( 'Failed to read Blob as Uint8Array.' ) ) ;
64
+ } ;
65
+
66
+ reader . readAsArrayBuffer ( blob ) ;
67
+ } ) ;
68
+ }
69
+
43
70
// 更新云点数据
44
71
const updatePoint = useCallback (
45
- ( pointJson : string ) => {
72
+ ( points : number [ ] ) => {
46
73
const point = [ ] ;
47
- const pointsArr = JSON . parse ( pointJson ) ;
48
- for ( let i = 0 ; i < pointsArr . length ; i += 3 ) {
49
- point . push ( new THREE . Vector3 ( pointsArr [ i ] , pointsArr [ i + 1 ] , pointsArr [ i + 2 ] ) ) ;
74
+
75
+ for ( let i = 0 ; i < points . length ; i += 3 ) {
76
+ point . push ( new THREE . Vector3 ( points [ i ] , points [ i + 1 ] , points [ i + 2 ] ) ) ;
50
77
}
51
78
geometry . setFromPoints ( point ) ;
52
79
} ,
@@ -69,7 +96,15 @@ const CloudPoint: React.FC<CloudPointProps> = (props: CloudPointProps) => {
69
96
}
70
97
} ;
71
98
ws . current . onmessage = e => {
72
- updatePoint ( e . data ) ;
99
+ blobToUint8Array ( e . data )
100
+ . then ( compressedDataUint8Array => {
101
+ const decompressedData = decompressData ( compressedDataUint8Array ) ;
102
+ const points = JSON . parse ( decompressedData ) ;
103
+ updatePoint ( points ) ;
104
+ } )
105
+ . catch ( error => {
106
+ console . error ( error ) ;
107
+ } ) ;
73
108
} ;
74
109
ws . current . onclose = ( ) => {
75
110
clearInterval ( timerRef . current ) ;
0 commit comments