Skip to content

Commit

Permalink
Add spherical harmonics to compressed.ply export (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
slimbuck authored Dec 6, 2024
1 parent 4ef2c0c commit 19b0f2f
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 99 deletions.
4 changes: 3 additions & 1 deletion src/asset-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ class AssetLoader {
}
});

asset.on('error', (err: string) => reject(err));
asset.on('error', (err: string) => {
reject(err);
});

this.registry.add(asset);
this.registry.load(asset);
Expand Down
3 changes: 0 additions & 3 deletions src/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ const ray = new Ray();
const vec = new Vec3();
const vecb = new Vec3();
const va = new Vec3();
const vb = new Vec3();
const vc = new Vec3();
const v4 = new Vec4();

// modulo dealing with negative numbers
const mod = (n: number, m: number) => ((n % m) + m) % m;
Expand Down
41 changes: 19 additions & 22 deletions src/sh-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const dp = (n: number, start: number, a: number[] | Float32Array, b: number[] |
return sum;
};

const coeffsIn = new Float32Array(16);
const coeffsIn = new Float32Array(15);

// Rotate spherical harmonics up to band 3 based on https://github.com/andrewwillmott/sh-lib
//
Expand Down Expand Up @@ -155,38 +155,35 @@ class SHRotation {
src = coeffsIn;
}

// band 0
result[0] = src[0];

// band 1
if (result.length < 4) {
if (result.length < 3) {
return;
}
result[1] = dp(3, 1, src, sh1[0]);
result[2] = dp(3, 1, src, sh1[1]);
result[3] = dp(3, 1, src, sh1[2]);
result[0] = dp(3, 0, src, sh1[0]);
result[1] = dp(3, 0, src, sh1[1]);
result[2] = dp(3, 0, src, sh1[2]);

// band 2
if (result.length < 9) {
if (result.length < 8) {
return;
}
result[4] = dp(5, 4, src, sh2[0]);
result[5] = dp(5, 4, src, sh2[1]);
result[6] = dp(5, 4, src, sh2[2]);
result[7] = dp(5, 4, src, sh2[3]);
result[8] = dp(5, 4, src, sh2[4]);
result[3] = dp(5, 3, src, sh2[0]);
result[4] = dp(5, 3, src, sh2[1]);
result[5] = dp(5, 3, src, sh2[2]);
result[6] = dp(5, 3, src, sh2[3]);
result[7] = dp(5, 3, src, sh2[4]);

// band 3
if (result.length < 16) {
if (result.length < 15) {
return;
}
result[9] = dp(7, 9, src, sh3[0]);
result[10] = dp(7, 9, src, sh3[1]);
result[11] = dp(7, 9, src, sh3[2]);
result[12] = dp(7, 9, src, sh3[3]);
result[13] = dp(7, 9, src, sh3[4]);
result[14] = dp(7, 9, src, sh3[5]);
result[15] = dp(7, 9, src, sh3[6]);
result[8] = dp(7, 8, src, sh3[0]);
result[9] = dp(7, 8, src, sh3[1]);
result[10] = dp(7, 8, src, sh3[2]);
result[11] = dp(7, 8, src, sh3[3]);
result[12] = dp(7, 8, src, sh3[4]);
result[13] = dp(7, 8, src, sh3[5]);
result[14] = dp(7, 8, src, sh3[6]);
};
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/shaders/splat-shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,19 @@ void main(void) {
color.xyz = max(vec3(0.0), color.xyz + evalSH(state, projState));
#endif
// apply tint/brightness
color = color * clrScale + vec4(clrOffset, 0.0);
// don't allow out-of-range alpha
color.a = clamp(color.a, 0.0, 1.0);
// apply locked/selected colors
if ((vertexState & 2u) != 0u) {
// locked
color *= lockedClr;
} else if ((vertexState & 1u) != 0u) {
// selected
color.xyz = mix(color.xyz, selectedClr.xyz * 0.8, selectedClr.a);
} else {
// apply tint/brightness
color = color * clrScale + vec4(clrOffset, 0.0);
}
#endif
Expand Down
Loading

0 comments on commit 19b0f2f

Please sign in to comment.