1
- import { loadPly } from './util/handler-ply'
1
+ import { loadPly } from './loaders/ply/ply-loader'
2
+ import { loadSplat } from './loaders/splat/splat-loader'
2
3
import CameraWebGL from './webgl2/camera-webGL'
3
4
import CubeInstanceWebGL from './webgl2/cubeInstance-webGL'
4
5
import SplatWebGL from './webgl2/splat-webGL'
5
6
6
- const maxGaussians = 200000 ;
7
- // const renderScale = 0.474;
8
7
const renderScale = 1 ;
9
8
10
9
Component ( {
@@ -15,6 +14,8 @@ Component({
15
14
heightScale : 1 , // canvas高度缩放值
16
15
renderByXRFrame : false , // 是否使用 xr-frame渲染
17
16
renderByWebGL2 : true , // 是否使用WebGL2渲染
17
+ workerOn : true ,
18
+ maxGaussians : 600000 ,
18
19
} ,
19
20
lifetimes : {
20
21
/**
@@ -25,7 +26,7 @@ Component({
25
26
26
27
console . log ( '[worker] 排序 worker 的创建' )
27
28
this . worker = wx . createWorker ( 'workers/gaussianSplatting/index.js' ) ;
28
- console . log ( '[worker] 具体 worker' , this . _worker ) ;
29
+ console . log ( '[worker] 具体 worker' , this . worker ) ;
29
30
30
31
} ,
31
32
detached ( ) {
@@ -89,30 +90,110 @@ Component({
89
90
} else if ( this . data . renderByXRFrame ) {
90
91
this . initXRFrame ( ) ;
91
92
}
92
-
93
- // 开始处理 ply 资源
94
- this . initPLY ( ) ;
95
-
96
- // 渲染循环
97
- // this.loopTimer = setInterval(this.requestRender.bind(this), 20);
98
-
99
- // 仅渲染一次,通过触摸驱动
100
- // this.requestRender();
101
93
} ,
102
- initPLY ( ) {
94
+ initPLY ( id ) {
103
95
console . log ( '== PLY Init start ==' )
104
96
105
- // const plySrc = 'http://10.9.169.132:8030/ply/point_cloud.ply';
106
- const plySrc = 'http://10.9.169.132:8030/ply/room.ply' ;
107
- // const plySrc = 'http://10.9.169.132:8030/ply/oneflower.ply';
108
-
109
-
110
- // const filePath = wx.env.USER_DATA_PATH + '/point.ply';
97
+ const host = 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo' ;
98
+ // const host = 'http://10.9.169.120:8030'
99
+
100
+ let type ;
101
+
102
+ // 加载 ply
103
+ // type = 'ply';
104
+ // const pcSrc = `${host}/ply/oneflower.cleaned.ply`;
105
+ // const pcSrc = `${host}/ply/point_cloud.ply`;
106
+ // const pcSrc = `${host}/ply/room.ply`;
107
+ // const pcSrc = `${host}/ply/gs_USJ_Mario_enter.cleaned.ply`;
108
+ // const pcSrc = `${host}/ply/oneflower.ply`;
109
+ // const pcSrc = `${host}/ply/sakura.ply`;
110
+ // const pcSrc = `${host}/ply/sakura.cleaned.ply`;
111
+ // const pcSrc = `${host}/ply/sakura.compressed.ply`;
112
+
113
+ // 加载 splat
114
+ type = 'splat' ;
115
+ const pcSrc = `${ host } /splat/${ id } .splat` ;
111
116
117
+ // Setup Camera
118
+ switch ( id ) {
119
+ case 'room' :
120
+ this . camera . updateCameraInfo (
121
+ // target
122
+ [ 0 , 2 , - 1 ] ,
123
+ // theta
124
+ - Math . PI / 2 ,
125
+ // phi
126
+ Math . PI / 2 ,
127
+ // raidus
128
+ 1
129
+ )
130
+ break ;
131
+ case 'garden' :
132
+ this . camera . updateCameraInfo (
133
+ // target
134
+ [ 0 , 1 , - 1 ] ,
135
+ // theta
136
+ - Math . PI / 2 ,
137
+ // phi
138
+ Math . PI / 2 ,
139
+ // raidus
140
+ 8
141
+ )
142
+ break ;
143
+ case 'stump' :
144
+ this . camera . updateCameraInfo (
145
+ // target
146
+ [ 0 , - 0.5 , 0 ] ,
147
+ // theta
148
+ - Math . PI * 2 / 3 ,
149
+ // phi
150
+ Math . PI * 2 / 3 ,
151
+ // raidus
152
+ 4
153
+ )
154
+ break ;
155
+ case 'oneflower' :
156
+ this . camera . updateCameraInfo (
157
+ // target
158
+ [ 0 , 1.5 , 3 ] ,
159
+ // theta
160
+ - Math . PI / 2 ,
161
+ // phi
162
+ Math . PI / 2 ,
163
+ // raidus
164
+ 8
165
+ )
166
+ break ;
167
+ case 'usj' :
168
+ this . camera . updateCameraInfo (
169
+ // target
170
+ [ 0 , - 1 , 0 ] ,
171
+ // theta
172
+ - Math . PI * 7 / 6 ,
173
+ // phi
174
+ Math . PI / 2 ,
175
+ // raidus
176
+ 4
177
+ )
178
+ break ;
179
+ case 'sakura' :
180
+ this . camera . updateCameraInfo (
181
+ // target
182
+ [ 1.6 , 0.5 , 1 ] ,
183
+ // theta
184
+ Math . PI / 4 ,
185
+ // phi
186
+ Math . PI * 3 / 5 ,
187
+ // raidus
188
+ 0.5
189
+ )
190
+ break ;
191
+ }
112
192
193
+ console . log ( 'splat src' , pcSrc )
113
194
114
195
wx . downloadFile ( {
115
- url : plySrc ,
196
+ url : pcSrc ,
116
197
timeout : 200000 ,
117
198
success : ( res ) => {
118
199
console . log ( "downloadFile 下载回调" , res ) ;
@@ -158,12 +239,23 @@ Component({
158
239
159
240
// console.log('buffer', buffer)
160
241
161
- const plyInfo = loadPly ( buffer ) ;
242
+ let info ;
162
243
163
- console . log ( "plyLoader return" , plyInfo ) ;
244
+ const maxGaussians = this . data . maxGaussians ;
245
+
246
+ switch ( type ) {
247
+ case 'ply' :
248
+ info = loadPly ( buffer , maxGaussians ) ;
249
+ console . log ( "plyLoader return" , info ) ;
250
+ break ;
251
+ case 'splat' :
252
+ info = loadSplat ( buffer , maxGaussians ) ;
253
+ console . log ( "splatLoader return" , info ) ;
254
+ break ;
255
+ }
164
256
165
257
// 初始化 worker 相关
166
- this . initWorker ( plyInfo , {
258
+ this . initWorker ( info , {
167
259
maxGaussians
168
260
} ) ;
169
261
@@ -176,33 +268,6 @@ Component({
176
268
} )
177
269
console . error ( 'file size is 0' )
178
270
}
179
-
180
- // fs.readFile({
181
- // filePath: filePath,
182
- // position: 0,
183
- // success: async (res) => {
184
- // console.log("readFile 读文件回调,结果返回为", res)
185
-
186
- // const plyInfo = loadPly(res.data);
187
-
188
- // console.log("plyLoader return", plyInfo);
189
-
190
- // // 初始化 worker 相关
191
- // this.initWorker(plyInfo, {
192
- // maxGaussians
193
- // });
194
-
195
- // },
196
- // fail(res) {
197
- // wx.hideLoading();
198
- // wx.showToast({
199
- // title: res.errMsg,
200
- // icon: 'none',
201
- // duration: 2000
202
- // })
203
- // console.error(res)
204
- // }
205
- // });
206
271
} ,
207
272
fail ( res ) {
208
273
wx . hideLoading ( ) ;
@@ -235,6 +300,8 @@ Component({
235
300
236
301
this . camera . isWorkerSorting = false ;
237
302
303
+ const start = new Date ( ) . getTime ( )
304
+
238
305
const data = res . result . data
239
306
240
307
const gl = this . gl
@@ -265,7 +332,17 @@ Component({
265
332
266
333
this . gaussiansCount = data . gaussiansCount ;
267
334
268
- this . requestRender ( ) ;
335
+ console . log ( 'gaussiansCount' , this . gaussiansCount )
336
+
337
+
338
+ const end = new Date ( ) . getTime ( )
339
+
340
+ const sortTime = `${ ( ( end - start ) / 1000 ) . toFixed ( 3 ) } s`
341
+ console . log ( `updateBuffer ${ sortTime } ` )
342
+
343
+ // this.requestRender();
344
+ this . canvas . requestAnimationFrame ( this . requestRender . bind ( this ) ) ;
345
+
269
346
// console.log('execFunc_sort end')
270
347
}
271
348
} )
@@ -284,17 +361,8 @@ Component({
284
361
console . log ( 'webgl2 context' , gl ) ;
285
362
286
363
// Setup Camera
287
- // Normal
288
- // const cameraParameters = {
289
- // up: [0, 1.0, 0.0],
290
- // // target: [0, 2.5, 2.5],
291
- // target: [0, 0, 0],
292
- // camera: [-Math.PI/2, Math.PI/2, 10], // theta phi radius
293
- // }
294
- // Room
295
364
const cameraParameters = {
296
365
up : [ 0 , 1.0 , 0.0 ] ,
297
- // target: [0, 2.5, 2.5],
298
366
target : [ 0 , 1 , 0 ] ,
299
367
camera : [ - Math . PI / 2 , Math . PI / 2 , 4 ] , // theta phi radius
300
368
}
@@ -313,8 +381,20 @@ Component({
313
381
} ,
314
382
requestRender ( ) {
315
383
// console.log('requestRender')
384
+
385
+ // 限帧
386
+ let now = Date . now ( )
387
+ const last = this . lastRenderTime || 0 ;
388
+ const mill = now - last
389
+ if ( mill < 30 ) {
390
+ return
391
+ }
392
+ this . lastRenderTime = now
393
+
316
394
const gl = this . gl ;
317
395
396
+ const start = new Date ( ) . getTime ( )
397
+
318
398
// Clear State
319
399
gl . viewport ( 0 , 0 , gl . canvas . width , gl . canvas . height )
320
400
gl . clearColor ( 0 , 0 , 0 , 0.0 ) ;
@@ -323,6 +403,8 @@ Component({
323
403
gl . disable ( gl . DEPTH_TEST )
324
404
gl . enable ( gl . BLEND )
325
405
gl . blendFunc ( gl . ONE_MINUS_DST_ALPHA , gl . ONE )
406
+ // gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
407
+
326
408
327
409
// clear
328
410
gl . clear ( gl . COLOR_BUFFER_BIT ) ;
@@ -333,7 +415,12 @@ Component({
333
415
334
416
// this.drawCubeMesh(gl, projMatrix, viewMatrix)
335
417
418
+
336
419
this . drawSplat ( gl ) ;
420
+
421
+ // const end = new Date().getTime()
422
+ // const sortTime = `${((end - start)/1000).toFixed(4)}s`
423
+ // console.log(`requestRender ${sortTime}`)
337
424
} ,
338
425
drawCubeMesh ( gl , projMatrix , viewMatrix ) {
339
426
// mesh
@@ -413,8 +500,7 @@ Component({
413
500
gl . uniformMatrix4fv ( gl . getUniformLocation ( program , 'projmatrix' ) , false , cam . vpm )
414
501
gl . uniformMatrix4fv ( gl . getUniformLocation ( program , 'viewmatrix' ) , false , cam . vm )
415
502
416
-
417
- // Draw
503
+ // Draw
418
504
gl . drawArraysInstanced ( gl . TRIANGLE_STRIP , 0 , 4 , this . gaussiansCount ) ;
419
505
420
506
} ,
@@ -438,8 +524,33 @@ Component({
438
524
439
525
this . camera . update ( ) ;
440
526
441
- this . requestRender ( ) ;
527
+ // this.requestRender();
528
+ this . canvas . requestAnimationFrame ( this . requestRender . bind ( this ) ) ;
442
529
} ,
530
+ onTapControl ( e ) {
531
+ const dataSet = e . target . dataset ;
532
+
533
+ const id = dataSet . id ;
534
+
535
+ // 开始处理 ply 资源
536
+ this . initPLY ( id ) ;
537
+ } ,
538
+ changeMaxGaussianCount ( e ) {
539
+ this . setData ( {
540
+ maxGaussians : e . detail . value
541
+ } )
542
+
543
+ console . log ( 'slider maxGaussians:' , this . data . maxGaussians ) ;
544
+ } ,
545
+ switchWorker ( e ) {
546
+ this . setData ( {
547
+ workerOn : e . detail . value
548
+ } )
549
+
550
+ this . camera . setWorkerOn ( this . data . workerOn ) ;
551
+
552
+ console . log ( 'switch WorkerOn:' , this . data . workerOn ) ;
553
+ }
443
554
} ,
444
555
} )
445
556
0 commit comments