dylanebert HF staff commited on
Commit
cc654e9
·
1 Parent(s): 5f32ba4

checkpoint

Browse files
viewer/src/lib/splat.js/cameras/Camera.ts CHANGED
@@ -1,14 +1,18 @@
1
  import { Object3D } from "../core/Object3D";
2
  import { Matrix4 } from "../math/Matrix4";
 
3
 
4
  class Camera extends Object3D {
5
  projectionMatrix: Matrix4;
6
 
7
- constructor() {
8
  super();
9
 
 
10
  this.projectionMatrix = new Matrix4();
11
  }
 
 
12
  }
13
 
14
  export { Camera };
 
1
  import { Object3D } from "../core/Object3D";
2
  import { Matrix4 } from "../math/Matrix4";
3
+ import { Vector3 } from "../math/Vector3";
4
 
5
  class Camera extends Object3D {
6
  projectionMatrix: Matrix4;
7
 
8
+ constructor(position: Vector3 = new Vector3(0, 0, -5)) {
9
  super();
10
 
11
+ this.position = position;
12
  this.projectionMatrix = new Matrix4();
13
  }
14
+
15
+ updateProjectionMatrix(width: number, height: number): void {}
16
  }
17
 
18
  export { Camera };
viewer/src/lib/splat.js/core/Scene.ts CHANGED
@@ -2,16 +2,19 @@ import { Object3D } from "./Object3D";
2
 
3
  class Scene extends Object3D {
4
  data: Uint8Array;
 
5
 
6
  constructor() {
7
  super();
8
 
9
  this.data = new Uint8Array(0);
 
10
  }
11
 
12
- setData(data: Uint8Array): void {
13
  this.data = data;
14
- console.log(data);
 
15
  }
16
  }
17
 
 
2
 
3
  class Scene extends Object3D {
4
  data: Uint8Array;
5
+ vertexCount: number;
6
 
7
  constructor() {
8
  super();
9
 
10
  this.data = new Uint8Array(0);
11
+ this.vertexCount = 0;
12
  }
13
 
14
+ setData(data: Uint8Array, vertexCount: number): void {
15
  this.data = data;
16
+ this.vertexCount = vertexCount;
17
+ console.log("setData", vertexCount);
18
  }
19
  }
20
 
viewer/src/lib/splat.js/index.ts CHANGED
@@ -1,4 +1,5 @@
1
  export { Camera } from "./cameras/Camera";
 
2
  export { Renderer } from "./renderers/Renderer";
3
  export { Scene } from "./core/Scene";
4
 
 
1
  export { Camera } from "./cameras/Camera";
2
+ export { PerspectiveCamera } from "./cameras/PerspectiveCamera";
3
  export { Renderer } from "./renderers/Renderer";
4
  export { Scene } from "./core/Scene";
5
 
viewer/src/lib/splat.js/loaders/Loader.ts CHANGED
@@ -27,7 +27,10 @@ class Loader {
27
  onProgress?.(bytesRead / contentLength);
28
  }
29
 
30
- scene.setData(data);
 
 
 
31
  }
32
  }
33
 
 
27
  onProgress?.(bytesRead / contentLength);
28
  }
29
 
30
+ const rowLength = 3 * 4 + 3 * 4 + 4 + 4;
31
+ const vertexCount = data.length / rowLength;
32
+
33
+ scene.setData(data, vertexCount);
34
  }
35
  }
36
 
viewer/src/lib/splat.js/math/Matrix3.ts ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Matrix3 {
2
+ buffer: number[];
3
+
4
+ // prettier-ignore
5
+ constructor(n11: number = 1, n12: number = 0, n13: number = 0,
6
+ n21: number = 0, n22: number = 1, n23: number = 0,
7
+ n31: number = 0, n32: number = 0, n33: number = 1) {
8
+ this.buffer = new Array(9);
9
+
10
+ this.set(
11
+ n11, n12, n13,
12
+ n21, n22, n23,
13
+ n31, n32, n33
14
+ );
15
+ }
16
+
17
+ // prettier-ignore
18
+ set(n11: number, n12: number, n13: number,
19
+ n21: number, n22: number, n23: number,
20
+ n31: number, n32: number, n33: number): Matrix3 {
21
+ const e = this.buffer;
22
+
23
+ e[0] = n11; e[1] = n12; e[2] = n13;
24
+ e[3] = n21; e[4] = n22; e[5] = n23;
25
+ e[6] = n31; e[7] = n32; e[8] = n33;
26
+
27
+ return this;
28
+ }
29
+ }
30
+
31
+ export { Matrix3 };
viewer/src/lib/splat.js/math/Matrix4.ts CHANGED
@@ -1,12 +1,12 @@
1
  class Matrix4 {
2
- elements: number[];
3
 
4
  // prettier-ignore
5
  constructor(n11: number = 1, n12: number = 0, n13: number = 0, n14: number = 0,
6
  n21: number = 0, n22: number = 1, n23: number = 0, n24: number = 0,
7
  n31: number = 0, n32: number = 0, n33: number = 1, n34: number = 0,
8
  n41: number = 0, n42: number = 0, n43: number = 0, n44: number = 1) {
9
- this.elements = new Array(16);
10
 
11
  this.set(
12
  n11, n12, n13, n14,
@@ -21,15 +21,38 @@ class Matrix4 {
21
  n21: number, n22: number, n23: number, n24: number,
22
  n31: number, n32: number, n33: number, n34: number,
23
  n41: number, n42: number, n43: number, n44: number) : Matrix4 {
24
- const e = this.elements;
25
 
26
- e[0] = n11; e[4] = n12; e[8] = n13; e[12] = n14;
27
- e[1] = n21; e[5] = n22; e[9] = n23; e[13] = n24;
28
- e[2] = n31; e[6] = n32; e[10] = n33; e[14] = n34;
29
- e[3] = n41; e[7] = n42; e[11] = n43; e[15] = n44;
30
 
31
  return this;
32
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  }
34
 
35
  export { Matrix4 };
 
1
  class Matrix4 {
2
+ buffer: number[];
3
 
4
  // prettier-ignore
5
  constructor(n11: number = 1, n12: number = 0, n13: number = 0, n14: number = 0,
6
  n21: number = 0, n22: number = 1, n23: number = 0, n24: number = 0,
7
  n31: number = 0, n32: number = 0, n33: number = 1, n34: number = 0,
8
  n41: number = 0, n42: number = 0, n43: number = 0, n44: number = 1) {
9
+ this.buffer = new Array(16);
10
 
11
  this.set(
12
  n11, n12, n13, n14,
 
21
  n21: number, n22: number, n23: number, n24: number,
22
  n31: number, n32: number, n33: number, n34: number,
23
  n41: number, n42: number, n43: number, n44: number) : Matrix4 {
24
+ const e = this.buffer;
25
 
26
+ e[0] = n11; e[1] = n12; e[2] = n13; e[3] = n14;
27
+ e[4] = n21; e[5] = n22; e[6] = n23; e[7] = n24;
28
+ e[8] = n31; e[9] = n32; e[10] = n33; e[11] = n34;
29
+ e[12] = n41; e[13] = n42; e[14] = n43; e[15] = n44;
30
 
31
  return this;
32
  }
33
+
34
+ multiply(m: Matrix4): Matrix4 {
35
+ const a = this.buffer;
36
+ const b = m.buffer;
37
+ return new Matrix4(
38
+ b[0] * a[0] + b[1] * a[4] + b[2] * a[8] + b[3] * a[12],
39
+ b[0] * a[1] + b[1] * a[5] + b[2] * a[9] + b[3] * a[13],
40
+ b[0] * a[2] + b[1] * a[6] + b[2] * a[10] + b[3] * a[14],
41
+ b[0] * a[3] + b[1] * a[7] + b[2] * a[11] + b[3] * a[15],
42
+ b[4] * a[0] + b[5] * a[4] + b[6] * a[8] + b[7] * a[12],
43
+ b[4] * a[1] + b[5] * a[5] + b[6] * a[9] + b[7] * a[13],
44
+ b[4] * a[2] + b[5] * a[6] + b[6] * a[10] + b[7] * a[14],
45
+ b[4] * a[3] + b[5] * a[7] + b[6] * a[11] + b[7] * a[15],
46
+ b[8] * a[0] + b[9] * a[4] + b[10] * a[8] + b[11] * a[12],
47
+ b[8] * a[1] + b[9] * a[5] + b[10] * a[9] + b[11] * a[13],
48
+ b[8] * a[2] + b[9] * a[6] + b[10] * a[10] + b[11] * a[14],
49
+ b[8] * a[3] + b[9] * a[7] + b[10] * a[11] + b[11] * a[15],
50
+ b[12] * a[0] + b[13] * a[4] + b[14] * a[8] + b[15] * a[12],
51
+ b[12] * a[1] + b[13] * a[5] + b[14] * a[9] + b[15] * a[13],
52
+ b[12] * a[2] + b[13] * a[6] + b[14] * a[10] + b[15] * a[14],
53
+ b[12] * a[3] + b[13] * a[7] + b[14] * a[11] + b[15] * a[15]
54
+ );
55
+ }
56
  }
57
 
58
  export { Matrix4 };
viewer/src/lib/splat.js/math/Quaternion.ts CHANGED
@@ -19,6 +19,10 @@ class Quaternion {
19
 
20
  return this;
21
  }
 
 
 
 
22
  }
23
 
24
  export { Quaternion };
 
19
 
20
  return this;
21
  }
22
+
23
+ flat(): number[] {
24
+ return [this.x, this.y, this.z, this.w];
25
+ }
26
  }
27
 
28
  export { Quaternion };
viewer/src/lib/splat.js/math/Vector3.ts CHANGED
@@ -16,6 +16,10 @@ class Vector3 {
16
 
17
  return this;
18
  }
 
 
 
 
19
  }
20
 
21
  export { Vector3 };
 
16
 
17
  return this;
18
  }
19
+
20
+ flat(): number[] {
21
+ return [this.x, this.y, this.z];
22
+ }
23
  }
24
 
25
  export { Vector3 };
viewer/src/lib/splat.js/renderers/WebGLRenderer.ts CHANGED
@@ -1,20 +1,211 @@
1
  import type { Camera } from "../cameras/Camera";
 
2
  import type { Scene } from "../core/Scene";
3
  import type { Renderer } from "./Renderer";
4
 
 
 
 
 
 
 
 
5
  export class WebGLRenderer implements Renderer {
6
  canvas: HTMLCanvasElement;
7
 
8
- gl: WebGLRenderingContext;
9
- ext: ANGLE_instanced_arrays;
10
 
11
  constructor(canvas?: HTMLCanvasElement) {
12
  this.canvas =
13
  canvas ?? (document.createElementNS("http://www.w3.org/1999/xhtml", "canvas") as HTMLCanvasElement);
14
- this.gl = this.canvas.getContext("webgl") as WebGLRenderingContext;
15
- this.ext = this.gl.getExtension("ANGLE_instanced_arrays") as ANGLE_instanced_arrays;
16
- }
17
 
18
- render(scene: Scene, camera: Camera): void {}
19
- dispose(): void {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  }
 
1
  import type { Camera } from "../cameras/Camera";
2
+ import type { PerspectiveCamera } from "../cameras/PerspectiveCamera";
3
  import type { Scene } from "../core/Scene";
4
  import type { Renderer } from "./Renderer";
5
 
6
+ import { getViewMatrix } from "./webgl/utils/transformations";
7
+ import { createWorker } from "./webgl/utils/worker";
8
+
9
+ import { vertex } from "./webgl/shaders/vertex.glsl";
10
+ import { frag } from "./webgl/shaders/frag.glsl";
11
+ import type { Matrix4 } from "../math/Matrix4";
12
+
13
  export class WebGLRenderer implements Renderer {
14
  canvas: HTMLCanvasElement;
15
 
16
+ render: (scene: Scene, camera: Camera) => void;
17
+ dispose: () => void;
18
 
19
  constructor(canvas?: HTMLCanvasElement) {
20
  this.canvas =
21
  canvas ?? (document.createElementNS("http://www.w3.org/1999/xhtml", "canvas") as HTMLCanvasElement);
 
 
 
22
 
23
+ const gl = (this.canvas.getContext("webgl") ||
24
+ this.canvas.getContext("experimental-webgl")) as WebGLRenderingContext;
25
+
26
+ let ext: ANGLE_instanced_arrays;
27
+ let worker: Worker;
28
+ let vertexShader: WebGLShader;
29
+ let fragmentShader: WebGLShader;
30
+ let program: WebGLProgram;
31
+
32
+ let u_projection: WebGLUniformLocation;
33
+ let u_viewport: WebGLUniformLocation;
34
+ let u_focal: WebGLUniformLocation;
35
+ let u_view: WebGLUniformLocation;
36
+
37
+ let vertexLocation: number;
38
+ let centerLocation: number;
39
+ let colorLocation: number;
40
+ let covALocation: number;
41
+ let covBLocation: number;
42
+
43
+ let vertexBuffer: WebGLBuffer;
44
+ let centerBuffer: WebGLBuffer;
45
+ let colorBuffer: WebGLBuffer;
46
+ let covABuffer: WebGLBuffer;
47
+ let covBBuffer: WebGLBuffer;
48
+
49
+ function initGLContext(width: number, height: number, scene: Scene, camera: PerspectiveCamera) {
50
+ ext = gl.getExtension("ANGLE_instanced_arrays") as ANGLE_instanced_arrays;
51
+
52
+ worker = new Worker(
53
+ URL.createObjectURL(
54
+ new Blob(["(", createWorker.toString(), ")(self)"], {
55
+ type: "application/javascript",
56
+ })
57
+ )
58
+ );
59
+
60
+ // Viewport
61
+ gl.viewport(0, 0, width, height);
62
+ camera.updateProjectionMatrix(width, height);
63
+
64
+ // Vertex shader
65
+ vertexShader = gl.createShader(gl.VERTEX_SHADER) as WebGLShader;
66
+ gl.shaderSource(vertexShader, vertex);
67
+ gl.compileShader(vertexShader);
68
+ if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
69
+ console.error(gl.getShaderInfoLog(vertexShader));
70
+ }
71
+
72
+ // Fragment shader
73
+ fragmentShader = gl.createShader(gl.FRAGMENT_SHADER) as WebGLShader;
74
+ gl.shaderSource(fragmentShader, frag);
75
+ gl.compileShader(fragmentShader);
76
+ if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
77
+ console.error(gl.getShaderInfoLog(fragmentShader));
78
+ }
79
+
80
+ // Program
81
+ program = gl.createProgram() as WebGLProgram;
82
+ gl.attachShader(program, vertexShader);
83
+ gl.attachShader(program, fragmentShader);
84
+ gl.linkProgram(program);
85
+ gl.useProgram(program);
86
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
87
+ console.error(gl.getProgramInfoLog(program));
88
+ }
89
+
90
+ // Blending
91
+ gl.disable(gl.DEPTH_TEST);
92
+ gl.enable(gl.BLEND);
93
+ gl.blendFuncSeparate(gl.ONE_MINUS_DST_ALPHA, gl.ONE, gl.ONE_MINUS_DST_ALPHA, gl.ONE);
94
+ gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
95
+
96
+ // Uniforms
97
+ u_projection = gl.getUniformLocation(program, "projection") as WebGLUniformLocation;
98
+ gl.uniformMatrix4fv(u_projection, false, camera.projectionMatrix.buffer);
99
+
100
+ u_viewport = gl.getUniformLocation(program, "viewport") as WebGLUniformLocation;
101
+ gl.uniform2fv(u_viewport, new Float32Array([width, height]));
102
+
103
+ u_focal = gl.getUniformLocation(program, "focal") as WebGLUniformLocation;
104
+ gl.uniform2fv(u_focal, new Float32Array([camera.fx, camera.fy]));
105
+
106
+ const viewMatrix = getViewMatrix(camera);
107
+ u_view = gl.getUniformLocation(program, "view") as WebGLUniformLocation;
108
+ gl.uniformMatrix4fv(u_view, false, viewMatrix.buffer);
109
+
110
+ // Vertex buffer
111
+ const triangleVertices = new Float32Array([-2, -2, 2, -2, 2, 2, -2, 2]);
112
+ vertexBuffer = gl.createBuffer() as WebGLBuffer;
113
+
114
+ gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
115
+ gl.bufferData(gl.ARRAY_BUFFER, triangleVertices, gl.STATIC_DRAW);
116
+
117
+ vertexLocation = gl.getAttribLocation(program, "position");
118
+ gl.enableVertexAttribArray(vertexLocation);
119
+ gl.vertexAttribPointer(vertexLocation, 2, gl.FLOAT, false, 0, 0);
120
+
121
+ // Center buffer
122
+ centerBuffer = gl.createBuffer() as WebGLBuffer;
123
+ centerLocation = gl.getAttribLocation(program, "center");
124
+
125
+ gl.enableVertexAttribArray(centerLocation);
126
+ gl.bindBuffer(gl.ARRAY_BUFFER, centerBuffer);
127
+ gl.vertexAttribPointer(centerLocation, 3, gl.FLOAT, false, 0, 0);
128
+ ext.vertexAttribDivisorANGLE(centerLocation, 1);
129
+
130
+ // Color buffer
131
+ colorBuffer = gl.createBuffer() as WebGLBuffer;
132
+ colorLocation = gl.getAttribLocation(program, "color");
133
+
134
+ gl.enableVertexAttribArray(colorLocation);
135
+ gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
136
+ gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 0, 0);
137
+ ext.vertexAttribDivisorANGLE(colorLocation, 1);
138
+
139
+ // Covariance A buffer
140
+ covABuffer = gl.createBuffer() as WebGLBuffer;
141
+ covALocation = gl.getAttribLocation(program, "covA");
142
+
143
+ gl.enableVertexAttribArray(covALocation);
144
+ gl.bindBuffer(gl.ARRAY_BUFFER, covABuffer);
145
+ gl.vertexAttribPointer(covALocation, 3, gl.FLOAT, false, 0, 0);
146
+ ext.vertexAttribDivisorANGLE(covALocation, 1);
147
+
148
+ // Covariance B buffer
149
+ covBBuffer = gl.createBuffer() as WebGLBuffer;
150
+ covBLocation = gl.getAttribLocation(program, "covB");
151
+
152
+ gl.enableVertexAttribArray(covBLocation);
153
+ gl.bindBuffer(gl.ARRAY_BUFFER, covBBuffer);
154
+ gl.vertexAttribPointer(covBLocation, 3, gl.FLOAT, false, 0, 0);
155
+ ext.vertexAttribDivisorANGLE(covBLocation, 1);
156
+
157
+ worker.onmessage = (e) => {
158
+ if (e.data.center) {
159
+ let { covA, covB, center, color } = e.data;
160
+
161
+ gl.bindBuffer(gl.ARRAY_BUFFER, centerBuffer);
162
+ gl.bufferData(gl.ARRAY_BUFFER, center, gl.DYNAMIC_DRAW);
163
+
164
+ gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
165
+ gl.bufferData(gl.ARRAY_BUFFER, color, gl.DYNAMIC_DRAW);
166
+
167
+ gl.bindBuffer(gl.ARRAY_BUFFER, covABuffer);
168
+ gl.bufferData(gl.ARRAY_BUFFER, covA, gl.DYNAMIC_DRAW);
169
+
170
+ gl.bindBuffer(gl.ARRAY_BUFFER, covBBuffer);
171
+ gl.bufferData(gl.ARRAY_BUFFER, covB, gl.DYNAMIC_DRAW);
172
+ }
173
+ };
174
+ }
175
+
176
+ let currentScene: Scene | null = null;
177
+ let currentCamera: Camera | null = null;
178
+
179
+ this.render = function (scene: Scene, camera: Camera) {
180
+ if (scene !== currentScene || camera !== currentCamera) {
181
+ const perspectiveCamera = camera as PerspectiveCamera;
182
+ if (perspectiveCamera == null) {
183
+ throw new Error("Camera is not a PerspectiveCamera");
184
+ }
185
+
186
+ if (currentScene != null || currentCamera != null) {
187
+ this.dispose();
188
+ }
189
+
190
+ currentScene = scene;
191
+ currentCamera = camera;
192
+
193
+ initGLContext(this.canvas.width, this.canvas.height, scene, perspectiveCamera);
194
+ }
195
+ };
196
+
197
+ this.dispose = function () {
198
+ gl.deleteBuffer(vertexBuffer);
199
+ gl.deleteBuffer(centerBuffer);
200
+ gl.deleteBuffer(colorBuffer);
201
+ gl.deleteBuffer(covABuffer);
202
+ gl.deleteBuffer(covBBuffer);
203
+
204
+ gl.deleteShader(vertexShader);
205
+ gl.deleteShader(fragmentShader);
206
+ gl.deleteProgram(program);
207
+
208
+ worker.terminate();
209
+ };
210
+ }
211
  }
viewer/src/lib/splat.js/renderers/webgl/utils/transformations.ts ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Camera } from "$lib/splat.js/cameras/Camera";
2
+ import { Vector3 } from "$lib/splat.js/math/Vector3";
3
+ import { Matrix3 } from "$lib/splat.js/math/Matrix3";
4
+ import { Matrix4 } from "$lib/splat.js/math/Matrix4";
5
+ import type { Quaternion } from "$lib/splat.js/math/Quaternion";
6
+
7
+ export function quaternionToEuler(q: Quaternion) {
8
+ return new Vector3(
9
+ Math.atan2(2 * (q.w * q.x + q.y * q.z), 1 - 2 * (q.x * q.x + q.y * q.y)),
10
+ Math.asin(2 * (q.w * q.y - q.z * q.x)),
11
+ Math.atan2(2 * (q.w * q.z + q.x * q.y), 1 - 2 * (q.y * q.y + q.z * q.z))
12
+ );
13
+ }
14
+
15
+ export function eulerToMatrix(v: Vector3) {
16
+ const x = v.x;
17
+ const y = v.y;
18
+ const z = v.z;
19
+
20
+ const cx = Math.cos(x);
21
+ const sx = Math.sin(x);
22
+ const cy = Math.cos(y);
23
+ const sy = Math.sin(y);
24
+ const cz = Math.cos(z);
25
+ const sz = Math.sin(z);
26
+
27
+ const rotationMatrix = [
28
+ cy * cz + sy * sx * sz,
29
+ -cy * sz + sy * sx * cz,
30
+ sy * cx,
31
+ cx * sz,
32
+ cx * cz,
33
+ -sx,
34
+ -sy * cz + cy * sx * sz,
35
+ sy * sz + cy * sx * cz,
36
+ cy * cx,
37
+ ];
38
+
39
+ return new Matrix4(...rotationMatrix);
40
+ }
41
+
42
+ export function quatToMatrix(q: Quaternion) {
43
+ const x = q.x;
44
+ const y = q.y;
45
+ const z = q.z;
46
+ const w = q.w;
47
+
48
+ const xx = x * x;
49
+ const xy = x * y;
50
+ const xz = x * z;
51
+ const xw = x * w;
52
+ const yy = y * y;
53
+ const yz = y * z;
54
+ const yw = y * w;
55
+ const zz = z * z;
56
+ const zw = z * w;
57
+
58
+ const rotationMatrix = [
59
+ 1 - 2 * (yy + zz),
60
+ 2 * (xy - zw),
61
+ 2 * (xz + yw),
62
+ 2 * (xy + zw),
63
+ 1 - 2 * (xx + zz),
64
+ 2 * (yz - xw),
65
+ 2 * (xz - yw),
66
+ 2 * (yz + xw),
67
+ 1 - 2 * (xx + yy),
68
+ ];
69
+
70
+ return new Matrix3(...rotationMatrix);
71
+ }
72
+
73
+ export function getViewMatrix(camera: Camera) {
74
+ const R = quatToMatrix(camera.rotation).buffer;
75
+ const t = camera.position.flat();
76
+ const camToWorld = [
77
+ [R[0], R[1], R[2], 0],
78
+ [R[3], R[4], R[5], 0],
79
+ [R[6], R[7], R[8], 0],
80
+ [
81
+ -t[0] * R[0] - t[1] * R[3] - t[2] * R[6],
82
+ -t[0] * R[1] - t[1] * R[4] - t[2] * R[7],
83
+ -t[0] * R[2] - t[1] * R[5] - t[2] * R[8],
84
+ 1,
85
+ ],
86
+ ].flat();
87
+ return new Matrix4(...camToWorld);
88
+ }
viewer/src/lib/splat.js/renderers/webgl/utils/worker.ts ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export function createWorker(self: Worker) {
2
+ let buffer: ArrayBuffer;
3
+ let vertexCount = 0;
4
+ let viewProj: Float32Array;
5
+ // 6*4 + 4 + 4 = 8*4
6
+ // XYZ - Position (Float32)
7
+ // XYZ - Scale (Float32)
8
+ // RGBA - colors (uint8)
9
+ // IJKL - quaternion/rot (uint8)
10
+ const rowLength = 3 * 4 + 3 * 4 + 4 + 4;
11
+ let depthIndex = new Uint32Array();
12
+
13
+ function processPlyBuffer(inputBuffer: ArrayBuffer) {
14
+ const ubuf = new Uint8Array(inputBuffer);
15
+ // 10KB ought to be enough for a header...
16
+ const header = new TextDecoder().decode(ubuf.slice(0, 1024 * 10));
17
+ const header_end = "end_header\n";
18
+ const header_end_index = header.indexOf(header_end);
19
+ if (header_end_index < 0) throw new Error("Unable to read .ply file header");
20
+ const vertexCount = parseInt(/element vertex (\d+)\n/.exec(header)![1]);
21
+ console.log("Vertex Count", vertexCount);
22
+ let row_offset: number = 0;
23
+ let offsets: { [key: string]: number } = {};
24
+ let types: { [key: string]: string } = {};
25
+ const TYPE_MAP: { [key: string]: string } = {
26
+ double: "getFloat64",
27
+ int: "getInt32",
28
+ uint: "getUint32",
29
+ float: "getFloat32",
30
+ short: "getInt16",
31
+ ushort: "getUint16",
32
+ uchar: "getUint8",
33
+ };
34
+ for (let prop of header
35
+ .slice(0, header_end_index)
36
+ .split("\n")
37
+ .filter((k) => k.startsWith("property "))) {
38
+ const [_p, type, name] = prop.split(" ");
39
+ const arrayType = TYPE_MAP[type] || "getInt8";
40
+ types[name] = arrayType;
41
+ offsets[name] = row_offset;
42
+ row_offset += parseInt(arrayType.replace(/[^\d]/g, "")) / 8;
43
+ }
44
+
45
+ let dataView = new DataView(inputBuffer, header_end_index + header_end.length);
46
+ let row: number = 0;
47
+ const attrs: any = new Proxy(
48
+ {},
49
+ {
50
+ get(_target, prop: string) {
51
+ if (!types[prop]) throw new Error(prop + " not found");
52
+ const type = types[prop] as keyof DataView;
53
+ const dataViewMethod = dataView[type] as any;
54
+ return dataViewMethod(row * row_offset + offsets[prop], true);
55
+ },
56
+ }
57
+ );
58
+
59
+ // 6*4 + 4 + 4 = 8*4
60
+ // XYZ - Position (Float32)
61
+ // XYZ - Scale (Float32)
62
+ // RGBA - colors (uint8)
63
+ // IJKL - quaternion/rot (uint8)
64
+ const rowLength = 3 * 4 + 3 * 4 + 4 + 4;
65
+ const buffer = new ArrayBuffer(rowLength * vertexCount);
66
+
67
+ for (let j = 0; j < vertexCount; j++) {
68
+ row = j;
69
+
70
+ const position = new Float32Array(buffer, j * rowLength, 3);
71
+ const scales = new Float32Array(buffer, j * rowLength + 4 * 3, 3);
72
+ const rgba = new Uint8ClampedArray(buffer, j * rowLength + 4 * 3 + 4 * 3, 4);
73
+ const rot = new Uint8ClampedArray(buffer, j * rowLength + 4 * 3 + 4 * 3 + 4, 4);
74
+
75
+ if (types["scale_0"]) {
76
+ const qlen = Math.sqrt(attrs.rot_0 ** 2 + attrs.rot_1 ** 2 + attrs.rot_2 ** 2 + attrs.rot_3 ** 2);
77
+
78
+ rot[0] = (attrs.rot_0 / qlen) * 128 + 128;
79
+ rot[1] = (attrs.rot_1 / qlen) * 128 + 128;
80
+ rot[2] = (attrs.rot_2 / qlen) * 128 + 128;
81
+ rot[3] = (attrs.rot_3 / qlen) * 128 + 128;
82
+
83
+ scales[0] = Math.exp(attrs.scale_0);
84
+ scales[1] = Math.exp(attrs.scale_1);
85
+ scales[2] = Math.exp(attrs.scale_2);
86
+ } else {
87
+ scales[0] = 0.01;
88
+ scales[1] = 0.01;
89
+ scales[2] = 0.01;
90
+
91
+ rot[0] = 255;
92
+ rot[1] = 0;
93
+ rot[2] = 0;
94
+ rot[3] = 0;
95
+ }
96
+
97
+ position[0] = attrs.x;
98
+ position[1] = attrs.y;
99
+ position[2] = attrs.z;
100
+
101
+ if (types["f_dc_0"]) {
102
+ const SH_C0 = 0.28209479177387814;
103
+ rgba[0] = (0.5 + SH_C0 * attrs.f_dc_0) * 255;
104
+ rgba[1] = (0.5 + SH_C0 * attrs.f_dc_1) * 255;
105
+ rgba[2] = (0.5 + SH_C0 * attrs.f_dc_2) * 255;
106
+ } else {
107
+ rgba[0] = attrs.red;
108
+ rgba[1] = attrs.green;
109
+ rgba[2] = attrs.blue;
110
+ }
111
+ if (types["opacity"]) {
112
+ rgba[3] = (1 / (1 + Math.exp(-attrs.opacity))) * 255;
113
+ } else {
114
+ rgba[3] = 255;
115
+ }
116
+ }
117
+ return buffer;
118
+ }
119
+
120
+ const runSort = (viewProj: Float32Array) => {
121
+ if (!buffer) return;
122
+
123
+ const f_buffer = new Float32Array(buffer);
124
+ const u_buffer = new Uint8Array(buffer);
125
+
126
+ const covA = new Float32Array(3 * vertexCount);
127
+ const covB = new Float32Array(3 * vertexCount);
128
+
129
+ const center = new Float32Array(3 * vertexCount);
130
+ const color = new Float32Array(4 * vertexCount);
131
+
132
+ let maxDepth = -Infinity;
133
+ let minDepth = Infinity;
134
+ let sizeList = new Int32Array(vertexCount);
135
+ for (let i = 0; i < vertexCount; i++) {
136
+ let depth =
137
+ ((viewProj[2] * f_buffer[8 * i + 0] +
138
+ viewProj[6] * f_buffer[8 * i + 1] +
139
+ viewProj[10] * f_buffer[8 * i + 2]) *
140
+ 4096) |
141
+ 0;
142
+ sizeList[i] = depth;
143
+ if (depth > maxDepth) maxDepth = depth;
144
+ if (depth < minDepth) minDepth = depth;
145
+ }
146
+
147
+ // This is a 16 bit single-pass counting sort
148
+ let depthInv = (256 * 256) / (maxDepth - minDepth);
149
+ let counts0 = new Uint32Array(256 * 256);
150
+ for (let i = 0; i < vertexCount; i++) {
151
+ sizeList[i] = ((sizeList[i] - minDepth) * depthInv) | 0;
152
+ counts0[sizeList[i]]++;
153
+ }
154
+ let starts0 = new Uint32Array(256 * 256);
155
+ for (let i = 1; i < 256 * 256; i++) starts0[i] = starts0[i - 1] + counts0[i - 1];
156
+ depthIndex = new Uint32Array(vertexCount);
157
+ for (let i = 0; i < vertexCount; i++) depthIndex[starts0[sizeList[i]]++] = i;
158
+
159
+ for (let j = 0; j < vertexCount; j++) {
160
+ const i = depthIndex[j];
161
+
162
+ center[3 * j + 0] = f_buffer[8 * i + 0];
163
+ center[3 * j + 1] = f_buffer[8 * i + 1];
164
+ center[3 * j + 2] = f_buffer[8 * i + 2];
165
+
166
+ color[4 * j + 0] = u_buffer[32 * i + 24 + 0] / 255;
167
+ color[4 * j + 1] = u_buffer[32 * i + 24 + 1] / 255;
168
+ color[4 * j + 2] = u_buffer[32 * i + 24 + 2] / 255;
169
+ color[4 * j + 3] = u_buffer[32 * i + 24 + 3] / 255;
170
+
171
+ let scale = [f_buffer[8 * i + 3 + 0], f_buffer[8 * i + 3 + 1], f_buffer[8 * i + 3 + 2]];
172
+ let rot = [
173
+ (u_buffer[32 * i + 28 + 0] - 128) / 128,
174
+ (u_buffer[32 * i + 28 + 1] - 128) / 128,
175
+ (u_buffer[32 * i + 28 + 2] - 128) / 128,
176
+ (u_buffer[32 * i + 28 + 3] - 128) / 128,
177
+ ];
178
+
179
+ const R = [
180
+ 1.0 - 2.0 * (rot[2] * rot[2] + rot[3] * rot[3]),
181
+ 2.0 * (rot[1] * rot[2] + rot[0] * rot[3]),
182
+ 2.0 * (rot[1] * rot[3] - rot[0] * rot[2]),
183
+
184
+ 2.0 * (rot[1] * rot[2] - rot[0] * rot[3]),
185
+ 1.0 - 2.0 * (rot[1] * rot[1] + rot[3] * rot[3]),
186
+ 2.0 * (rot[2] * rot[3] + rot[0] * rot[1]),
187
+
188
+ 2.0 * (rot[1] * rot[3] + rot[0] * rot[2]),
189
+ 2.0 * (rot[2] * rot[3] - rot[0] * rot[1]),
190
+ 1.0 - 2.0 * (rot[1] * rot[1] + rot[2] * rot[2]),
191
+ ];
192
+
193
+ // Compute the matrix product of S and R (M = S * R)
194
+ const M = [
195
+ scale[0] * R[0],
196
+ scale[0] * R[1],
197
+ scale[0] * R[2],
198
+ scale[1] * R[3],
199
+ scale[1] * R[4],
200
+ scale[1] * R[5],
201
+ scale[2] * R[6],
202
+ scale[2] * R[7],
203
+ scale[2] * R[8],
204
+ ];
205
+
206
+ covA[3 * j + 0] = M[0] * M[0] + M[3] * M[3] + M[6] * M[6];
207
+ covA[3 * j + 1] = M[0] * M[1] + M[3] * M[4] + M[6] * M[7];
208
+ covA[3 * j + 2] = M[0] * M[2] + M[3] * M[5] + M[6] * M[8];
209
+ covB[3 * j + 0] = M[1] * M[1] + M[4] * M[4] + M[7] * M[7];
210
+ covB[3 * j + 1] = M[1] * M[2] + M[4] * M[5] + M[7] * M[8];
211
+ covB[3 * j + 2] = M[2] * M[2] + M[5] * M[5] + M[8] * M[8];
212
+ }
213
+
214
+ self.postMessage({ covA, center, color, covB, viewProj }, [
215
+ covA.buffer,
216
+ center.buffer,
217
+ color.buffer,
218
+ covB.buffer,
219
+ ]);
220
+ };
221
+
222
+ const throttledSort = () => {
223
+ if (!sortRunning) {
224
+ sortRunning = true;
225
+ let lastView = viewProj;
226
+ runSort(lastView);
227
+ setTimeout(() => {
228
+ sortRunning = false;
229
+ if (lastView !== viewProj) {
230
+ throttledSort();
231
+ }
232
+ }, 0);
233
+ }
234
+ };
235
+
236
+ let sortRunning: boolean = false;
237
+ self.onmessage = (e) => {
238
+ if (e.data.ply) {
239
+ vertexCount = 0;
240
+ runSort(viewProj);
241
+ buffer = processPlyBuffer(e.data.ply);
242
+ vertexCount = Math.floor(buffer.byteLength / rowLength);
243
+ postMessage({ buffer: buffer });
244
+ } else if (e.data.buffer) {
245
+ buffer = e.data.buffer;
246
+ vertexCount = e.data.vertexCount;
247
+ } else if (e.data.vertexCount) {
248
+ vertexCount = e.data.vertexCount;
249
+ } else if (e.data.view) {
250
+ viewProj = e.data.view;
251
+ throttledSort();
252
+ }
253
+ };
254
+ }
viewer/src/routes/viewer/[slug]/+page.svelte CHANGED
@@ -3,6 +3,7 @@
3
  import type { IViewer } from "./IViewer";
4
  import { BabylonViewer } from "./BabylonViewer";
5
  import { SplatViewer } from "./SplatViewer";
 
6
 
7
  export let data: {
8
  scene: {
 
3
  import type { IViewer } from "./IViewer";
4
  import { BabylonViewer } from "./BabylonViewer";
5
  import { SplatViewer } from "./SplatViewer";
6
+ // import { SplatViewer } from "./SplatViewer.Legacy";
7
 
8
  export let data: {
9
  scene: {
viewer/src/routes/viewer/[slug]/SplatViewer.Legacy.ts CHANGED
@@ -639,9 +639,9 @@ export class SplatViewer implements IViewer {
639
  this.vertexBuffer = this.gl.createBuffer();
640
  this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
641
  this.gl.bufferData(this.gl.ARRAY_BUFFER, triangleVertices, this.gl.STATIC_DRAW);
 
642
  this.a_position = this.gl.getAttribLocation(this.program, "position");
643
  this.gl.enableVertexAttribArray(this.a_position);
644
- this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
645
  this.gl.vertexAttribPointer(this.a_position, 2, this.gl.FLOAT, false, 0, 0);
646
 
647
  // center
@@ -675,6 +675,8 @@ export class SplatViewer implements IViewer {
675
  this.gl.vertexAttribPointer(this.a_covB, 3, this.gl.FLOAT, false, 0, 0);
676
  this.ext.vertexAttribDivisorANGLE(this.a_covB, 1); // Use the extension here
677
 
 
 
678
  this.worker.onmessage = (e) => {
679
  if (e.data.buffer) {
680
  this.splatData = new Uint8Array(e.data.buffer);
@@ -916,7 +918,6 @@ export class SplatViewer implements IViewer {
916
  this.canvas.height = innerHeight;
917
  this.gl.viewport(0, 0, this.canvas.width, this.canvas.height);
918
 
919
- const aspectRatio = this.canvas.width / this.canvas.height;
920
  this.projectionMatrix = getProjectionMatrix(
921
  this.camera.fx,
922
  this.camera.fy,
 
639
  this.vertexBuffer = this.gl.createBuffer();
640
  this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
641
  this.gl.bufferData(this.gl.ARRAY_BUFFER, triangleVertices, this.gl.STATIC_DRAW);
642
+
643
  this.a_position = this.gl.getAttribLocation(this.program, "position");
644
  this.gl.enableVertexAttribArray(this.a_position);
 
645
  this.gl.vertexAttribPointer(this.a_position, 2, this.gl.FLOAT, false, 0, 0);
646
 
647
  // center
 
675
  this.gl.vertexAttribPointer(this.a_covB, 3, this.gl.FLOAT, false, 0, 0);
676
  this.ext.vertexAttribDivisorANGLE(this.a_covB, 1); // Use the extension here
677
 
678
+ console.log(this.vertexBuffer, this.centerBuffer, this.colorBuffer, this.covABuffer, this.covBBuffer);
679
+
680
  this.worker.onmessage = (e) => {
681
  if (e.data.buffer) {
682
  this.splatData = new Uint8Array(e.data.buffer);
 
918
  this.canvas.height = innerHeight;
919
  this.gl.viewport(0, 0, this.canvas.width, this.canvas.height);
920
 
 
921
  this.projectionMatrix = getProjectionMatrix(
922
  this.camera.fx,
923
  this.camera.fy,
viewer/src/routes/viewer/[slug]/SplatViewer.ts CHANGED
@@ -2,17 +2,35 @@ import type { IViewer } from "./IViewer";
2
  import * as SPLAT from "$lib/splat.js";
3
 
4
  export class SplatViewer implements IViewer {
 
 
5
  renderer: SPLAT.Renderer;
 
6
 
7
  constructor(canvas: HTMLCanvasElement) {
 
 
8
  this.renderer = new SPLAT.WebGLRenderer(canvas);
9
  }
10
 
11
  async loadScene(url: string, onProgress?: (progress: number) => void) {
12
- await SPLAT.Loader.LoadAsync(url, this.renderer, onProgress);
 
 
 
 
 
 
 
 
 
 
 
13
  }
14
 
15
- dispose() {}
 
 
16
 
17
  async capture(): Promise<string | null> {
18
  return null;
 
2
  import * as SPLAT from "$lib/splat.js";
3
 
4
  export class SplatViewer implements IViewer {
5
+ scene: SPLAT.Scene;
6
+ camera: SPLAT.Camera;
7
  renderer: SPLAT.Renderer;
8
+ disposed: boolean = false;
9
 
10
  constructor(canvas: HTMLCanvasElement) {
11
+ this.scene = new SPLAT.Scene();
12
+ this.camera = new SPLAT.PerspectiveCamera();
13
  this.renderer = new SPLAT.WebGLRenderer(canvas);
14
  }
15
 
16
  async loadScene(url: string, onProgress?: (progress: number) => void) {
17
+ await SPLAT.Loader.LoadAsync(url, this.scene, onProgress);
18
+
19
+ const frame = () => {
20
+ this.renderer.render(this.scene, this.camera);
21
+
22
+ if (!this.disposed) {
23
+ requestAnimationFrame(frame);
24
+ }
25
+ };
26
+
27
+ this.disposed = false;
28
+ requestAnimationFrame(frame);
29
  }
30
 
31
+ dispose() {
32
+ this.disposed = true;
33
+ }
34
 
35
  async capture(): Promise<string | null> {
36
  return null;