Skip to content

Commit d2211d6

Browse files
committed
(feat)vk: 高斯溅射切换到共享arrayBuffer通信
1 parent 773456f commit d2211d6

File tree

3 files changed

+147
-46
lines changed

3 files changed

+147
-46
lines changed

miniprogram/packageAPI/pages/ar/gaussian-splatting/gaussian-splatting.js

Lines changed: 111 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Component({
1515
renderByXRFrame: false, // 是否使用 xr-frame渲染
1616
renderByWebGL2: true, // 是否使用WebGL2渲染
1717
workerOn: true,
18-
maxGaussians: 600000,
18+
maxGaussians: 800000,
1919
},
2020
lifetimes: {
2121
/**
@@ -83,7 +83,7 @@ Component({
8383
console.log('== PLY Init start ==')
8484

8585
const host = 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo';
86-
// const host = 'http://10.9.169.120:8030'
86+
// const host = 'http://10.9.169.125:8030'
8787

8888
let type;
8989

@@ -167,9 +167,9 @@ Component({
167167
case 'sakura':
168168
this.camera.updateCameraInfo(
169169
// target
170-
[1.6, 0.5, 1],
170+
[1.6, 0, 1],
171171
// theta
172-
Math.PI / 4,
172+
Math.PI * 3 / 11,
173173
// phi
174174
Math.PI * 3 / 5,
175175
// raidus
@@ -242,9 +242,25 @@ Component({
242242
break;
243243
}
244244

245+
// 提供渲染的高斯球数
246+
const renderCount = this.renderCount = info.count;
247+
248+
// 全部用 f32 存储
249+
this.sabPositions = wx.createSharedArrayBuffer(renderCount * 4 * 3)
250+
this.sabOpacities= wx.createSharedArrayBuffer(renderCount * 4)
251+
this.sabCov3Da = wx.createSharedArrayBuffer(renderCount * 4 * 3)
252+
this.sabCov3Db = wx.createSharedArrayBuffer(renderCount * 4 * 3)
253+
this.sabcolors = wx.createSharedArrayBuffer(renderCount * 4 * 3)
254+
255+
console.log('创建 worker 共享内存', this.sabPositions, this.sabOpacities, this.sabCov3Da, this.sabCov3Db, this.sabcolors)
256+
245257
// 初始化 worker 相关
246258
this.initWorker(info, {
247-
maxGaussians
259+
sabPositions: this.sabPositions,
260+
sabOpacities: this.sabOpacities,
261+
sabCov3Da: this.sabCov3Da,
262+
sabCov3Db: this.sabCov3Db,
263+
sabcolors: this.sabcolors,
248264
});
249265

250266
} else {
@@ -288,47 +304,55 @@ Component({
288304

289305
this.camera.isWorkerSorting = false;
290306

291-
const start = new Date().getTime()
292-
293307
const data = res.result.data
294308

309+
const start = new Date().getTime()
310+
295311
const gl = this.gl
296312

297313
const updateBuffer = (buffer, data) => {
298314
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
299315
gl.bufferData(gl.ARRAY_BUFFER, data, gl.DYNAMIC_DRAW)
300316
}
317+
// const colors = new Float32Array(data.colors);
318+
// const positions = new Float32Array(data.positions);
319+
// const opacities = new Float32Array(data.opacities);
320+
// const cov3Da = new Float32Array(data.cov3Da);
321+
// const cov3Db = new Float32Array(data.cov3Db);
301322

302-
const colors = new Float32Array(data.colors);
303-
const positions = new Float32Array(data.positions);
304-
const opacities = new Float32Array(data.opacities);
305-
const cov3Da = new Float32Array(data.cov3Da);
306-
const cov3Db = new Float32Array(data.cov3Db);
323+
const positions = new Float32Array(this.sabPositions.buffer);
324+
const opacities = new Float32Array(this.sabOpacities.buffer);
325+
const cov3Da = new Float32Array(this.sabCov3Da.buffer);
326+
const cov3Db = new Float32Array(this.sabCov3Db.buffer);
327+
const colors = new Float32Array(this.sabcolors.buffer);
328+
329+
// console.log('positions', positions);
330+
// console.log('opacities', opacities);
331+
// console.log('cov3Da', cov3Da);
332+
// console.log('cov3Db', cov3Db);
333+
// console.log('colors', colors);
307334

308335

309-
updateBuffer(this.splat.buffers.color, colors)
310336
updateBuffer(this.splat.buffers.center, positions)
311337
updateBuffer(this.splat.buffers.opacity, opacities)
312338
updateBuffer(this.splat.buffers.covA, cov3Da)
313339
updateBuffer(this.splat.buffers.covB, cov3Db)
340+
updateBuffer(this.splat.buffers.color, colors)
314341

315-
// console.log(this.splat.buffers.color, colors)
316342
// console.log(this.splat.buffers.center, positions)
317343
// console.log(this.splat.buffers.opacity, opacities)
318344
// console.log(this.splat.buffers.covA, cov3Da)
319345
// console.log(this.splat.buffers.covB, cov3Db)
346+
// console.log(this.splat.buffers.color, colors)
320347

348+
// 设定绘制的高斯球数量
321349
this.gaussiansCount = data.gaussiansCount;
322350

323-
console.log('gaussiansCount', this.gaussiansCount)
324-
325-
326351
const end = new Date().getTime()
327352

328353
const sortTime = `${((end - start)/1000).toFixed(3)}s`
329354
console.log(`updateBuffer ${sortTime}`)
330355

331-
// this.requestRender();
332356
this.canvas.requestAnimationFrame(this.requestRender.bind(this));
333357

334358
// console.log('execFunc_sort end')
@@ -392,18 +416,15 @@ Component({
392416
gl.enable(gl.BLEND)
393417
gl.blendFunc(gl.ONE_MINUS_DST_ALPHA, gl.ONE)
394418
// gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
395-
396419

397420
// clear
398421
gl.clear(gl.COLOR_BUFFER_BIT);
399422

400-
// camera
423+
// drawCubeMesh
401424
// const projMatrix = this.camera.projMatrix;
402425
// const viewMatrix = this.camera.viewMatrix;
403-
404426
// this.drawCubeMesh(gl, projMatrix, viewMatrix)
405427

406-
407428
this.drawSplat(gl);
408429

409430
// const end = new Date().getTime()
@@ -495,24 +516,82 @@ Component({
495516
// webGL触摸相关逻辑
496517
onTouchStartWebGL(e) {
497518
// console.log(e);
498-
this.camera.lastTouch.clientX = e.touches[0].clientX
499-
this.camera.lastTouch.clientY = e.touches[0].clientY
519+
520+
if (e.touches.length === 1) {
521+
this.camera.lastTouch.x1 = e.touches[0].clientX
522+
this.camera.lastTouch.y1 = e.touches[0].clientY
523+
this.camera.lastTouch.x2 = null;
524+
this.camera.lastTouch.y2 = null;
525+
this.camera.lastTouch.distance = 0;
526+
} else if (e.touches.length === 2) {
527+
const touch1 = e.touches[0];
528+
const touch2 = e.touches[1];
529+
530+
this.camera.lastTouch.x1 = touch1.clientX
531+
this.camera.lastTouch.y1 = touch1.clientY
532+
this.camera.lastTouch.x2 = touch2.clientX
533+
this.camera.lastTouch.y2 = touch2.clientY
534+
535+
const distanceX = touch1.clientX - touch2.clientX;
536+
const distanceY = touch1.clientY - touch2.clientY;
537+
this.camera.lastTouch.distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
538+
}
539+
500540
},
501541
onTouchMoveWebGL(e) {
502542
// console.log(e);
503543

504-
const touch = e.touches[0]
505-
const movementX = touch.clientX - this.camera.lastTouch.clientX
506-
const movementY = touch.clientY - this.camera.lastTouch.clientY
507-
this.camera.lastTouch.clientX = touch.clientX
508-
this.camera.lastTouch.clientY = touch.clientY
544+
const moveScale = 1;
545+
546+
if (e.touches.length === 1) {
547+
const touch = e.touches[0];
548+
// 单指移动镜头
549+
const movementX = touch.clientX - this.camera.lastTouch.x1
550+
const movementY = touch.clientY - this.camera.lastTouch.y1
551+
this.camera.lastTouch.x1 = touch.clientX
552+
this.camera.lastTouch.y1 = touch.clientY
509553

510-
this.camera.theta -= movementX * 0.01 * .5 * .3
511-
this.camera.phi = Math.max(1e-6, Math.min(Math.PI - 1e-6, this.camera.phi + movementY * 0.01 * .5))
554+
if (Math.abs(movementX) < 100 && Math.abs(movementY) < 100) {
555+
// 只处理小移动
556+
this.camera.theta -= movementX * 0.01 * .3 * moveScale
557+
this.camera.phi = Math.max(1e-6, Math.min(Math.PI - 1e-6, this.camera.phi + movementY * 0.01 * moveScale))
558+
}
559+
560+
} else if (e.touches.length === 2) {
561+
// 支持单指变双指,兼容双指操作但是两根手指触屏时间不一致的情况
562+
const touch1 = e.touches[0];
563+
const touch2 = e.touches[1];
564+
565+
const distanceX = touch1.clientX - touch2.clientX;
566+
const distanceY = touch1.clientY - touch2.clientY;
567+
const distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
568+
569+
if (this.camera.lastTouch.x2 === null && this.camera.lastTouch.y2 === null) {
570+
this.camera.lastTouch.x1 = touch1.clientX
571+
this.camera.lastTouch.y1 = touch1.clientY
572+
this.camera.lastTouch.x2 = touch2.clientX
573+
this.camera.lastTouch.y2 = touch2.clientY
574+
575+
this.camera.lastTouch.distance = distance;
576+
} else {
577+
// 双指开始滑动
578+
let deltaScale = distance - this.camera.lastTouch.distance;
579+
this.camera.lastTouch.distance = distance;
580+
581+
if (deltaScale < -2) {
582+
deltaScale = -2;
583+
} else if (deltaScale > 2) {
584+
deltaScale = 2;
585+
}
586+
587+
const newRaidus = this.camera.radius - deltaScale * 0.2;
588+
this.camera.radius = newRaidus > 0 ? newRaidus: this.camera.radius;
589+
}
590+
591+
}
512592

513593
this.camera.update();
514594

515-
// this.requestRender();
516595
this.canvas.requestAnimationFrame(this.requestRender.bind(this));
517596
},
518597
onTapControl(e) {

miniprogram/packageAPI/pages/ar/gaussian-splatting/loaders/splat/splat-loader.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const perPI = Math.PI / 180;
1111
export function loadSplat(content, maxGaussians) {
1212
console.log('loadSplat', content);
1313

14+
const start = new Date().getTime()
15+
1416
// Create arrays for gaussian properties
1517
const positions = []
1618
const opacities = []
@@ -65,6 +67,10 @@ export function loadSplat(content, maxGaussians) {
6567
positions.push(...inCenter)
6668
}
6769

70+
const end = new Date().getTime();
71+
72+
const sortTime = `${((end - start)/1000).toFixed(3)}s`
73+
console.log(`parse ${splatCount} gaussians in ${sortTime}.`)
6874

6975
return { positions, opacities, colors, cov3Ds, count: splatCount };
7076

miniprogram/workers/gaussianSplatting/index.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,29 @@ function init(plyInfo, config) {
1212
// console.log('plyInfo', plyInfo);
1313
gaussians = plyInfo;
1414
gaussians.totalCount = plyInfo.count;
15-
gaussians.count = Math.min(gaussians.totalCount, config.maxGaussians)
15+
gaussians.count = gaussians.totalCount;
1616

1717
depthIndex = new Uint32Array(gaussians.count);
1818

1919
console.log(`[Worker] Received ${gaussians.count} gaussians`)
2020

21-
data.positions = new Float32Array(gaussians.count * 3)
22-
data.opacities = new Float32Array(gaussians.count)
23-
data.cov3Da = new Float32Array(gaussians.count * 3)
24-
data.cov3Db = new Float32Array(gaussians.count * 3)
25-
data.colors = new Float32Array(gaussians.count * 3)
21+
// data.positions = new Float32Array(gaussians.count * 3)
22+
// data.opacities = new Float32Array(gaussians.count)
23+
// data.cov3Da = new Float32Array(gaussians.count * 3)
24+
// data.cov3Db = new Float32Array(gaussians.count * 3)
25+
// data.colors = new Float32Array(gaussians.count * 3)
26+
27+
data.positions = new Float32Array(config.sabPositions.buffer);
28+
data.opacities = new Float32Array(config.sabOpacities.buffer);
29+
data.cov3Da = new Float32Array(config.sabCov3Da.buffer);
30+
data.cov3Db = new Float32Array(config.sabCov3Db.buffer);
31+
data.colors = new Float32Array(config.sabcolors.buffer);
32+
33+
// console.log(`[Worker] init data positions`, data.positions)
34+
// console.log(`[Worker] init data opacities`, data.opacities)
35+
// console.log(`[Worker] init data cov3Da`,data.cov3Da)
36+
// console.log(`[Worker] init data cov3Db`, data.cov3Db)
37+
// console.log(`[Worker] init data colors`, data.colors)
2638

2739
}
2840

@@ -43,6 +55,10 @@ function sort(params) {
4355
// Sort the gaussians!
4456
sortGaussiansByDepth(depthIndex, gaussians, viewMatrix)
4557

58+
const sortEnd = new Date().getTime();
59+
60+
const sortTime = `${((sortEnd - start)/1000).toFixed(3)}s`
61+
4662
// Update arrays containing the data
4763
for (let j = 0; j < gaussians.count; j++) {
4864
const i = depthIndex[j]
@@ -55,7 +71,6 @@ function sort(params) {
5571
data.positions[j*3+1] = gaussians.positions[i*3+1]
5672
data.positions[j*3+2] = gaussians.positions[i*3+2]
5773

58-
5974
data.opacities[j] = gaussians.opacities[i]
6075

6176
// Split the covariance matrix into two vec3
@@ -71,16 +86,17 @@ function sort(params) {
7186

7287
const end = new Date().getTime();
7388

74-
const sortTime = `${((end - start)/1000).toFixed(3)}s`
75-
console.log(`[Worker] Sorted ${gaussians.count} gaussians in ${sortTime}. Algorithm: ${sortingAlgorithm}`)
89+
const writeTime = `${((end - sortEnd)/1000).toFixed(3)}s`
90+
console.log(`[Worker] Sorted ${gaussians.count} gaussians in ${sortTime}.`)
91+
console.log(`[Worker] Writed ${gaussians.count} gaussians in ${writeTime}.`)
7692

7793
return {
7894
data: {
79-
colors: data.colors.buffer,
80-
positions: data.positions.buffer,
81-
opacities: data.opacities.buffer,
82-
cov3Da: data.cov3Da.buffer,
83-
cov3Db: data.cov3Db.buffer,
95+
// colors: data.colors.buffer,
96+
// positions: data.positions.buffer,
97+
// opacities: data.opacities.buffer,
98+
// cov3Da: data.cov3Da.buffer,
99+
// cov3Db: data.cov3Db.buffer,
84100
gaussiansCount: gaussians.count,
85101
}
86102
};

0 commit comments

Comments
 (0)