Skip to content

Commit 8d9bb46

Browse files
authored
Convert gsplat material to use chunks (#7164)
1 parent d9719f0 commit 8d9bb46

20 files changed

+803
-1017
lines changed

examples/src/examples/loaders/gsplat-many.example.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ assetListLoader.load(() => {
7171

7272
// instantiate guitar with a custom shader
7373
const guitar = assets.guitar.resource.instantiate({
74-
fragment: files['shader.frag'],
7574
vertex: files['shader.vert']
7675
});
7776
guitar.name = 'guitar';

examples/src/examples/loaders/gsplat-many.shader.frag

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,78 @@
1+
#include "gsplatCommonVS"
2+
3+
varying mediump vec2 gaussianUV;
4+
varying mediump vec4 gaussianColor;
5+
6+
#ifndef DITHER_NONE
7+
varying float id;
8+
#endif
9+
10+
mediump vec4 discardVec = vec4(0.0, 0.0, 2.0, 1.0);
11+
112
uniform float uTime;
2-
varying float height;
313

4-
void animate(inout vec3 center) {
14+
vec3 animatePosition(vec3 center) {
515
// modify center
616
float heightIntensity = center.y * 0.2;
717
center.x += sin(uTime * 5.0 + center.y) * 0.3 * heightIntensity;
818

919
// output y-coordinate
10-
height = center.y;
20+
return center;
1121
}
1222

13-
uniform vec3 view_position;
14-
15-
uniform sampler2D splatColor;
16-
17-
varying mediump vec2 texCoord;
18-
varying mediump vec4 color;
23+
vec4 animateColor(float height, vec4 clr) {
24+
float sineValue = abs(sin(uTime * 5.0 + height));
25+
26+
#ifdef CUTOUT
27+
// in cutout mode, remove pixels along the wave
28+
if (sineValue < 0.5) {
29+
clr.a = 0.0;
30+
}
31+
#else
32+
// in non-cutout mode, add a golden tint to the wave
33+
vec3 gold = vec3(1.0, 0.85, 0.0);
34+
float blend = smoothstep(0.9, 1.0, sineValue);
35+
clr.xyz = mix(clr.xyz, gold, blend);
36+
#endif
1937

20-
mediump vec4 discardVec = vec4(0.0, 0.0, 2.0, 1.0);
38+
return clr;
39+
}
2140

22-
void main(void)
23-
{
24-
// calculate splat uv
25-
if (!calcSplatUV()) {
41+
void main(void) {
42+
// read gaussian center
43+
SplatState state;
44+
if (!readCenter(state)) {
2645
gl_Position = discardVec;
2746
return;
2847
}
2948

30-
// get center
31-
vec3 center = getCenter();
32-
33-
animate(center);
49+
state.center = animatePosition(state.center);
3450

35-
// handle transforms
36-
mat4 model_view = matrix_view * matrix_model;
37-
vec4 splat_cam = model_view * vec4(center, 1.0);
38-
vec4 splat_proj = matrix_projection * splat_cam;
39-
40-
// cull behind camera
41-
if (splat_proj.z < -splat_proj.w) {
51+
// project center to screen space
52+
ProjectedState projState;
53+
if (!projectCenter(state, projState)) {
4254
gl_Position = discardVec;
4355
return;
4456
}
4557

46-
// get covariance
47-
vec3 covA, covB;
48-
getCovariance(covA, covB);
49-
50-
vec4 v1v2 = calcV1V2(splat_cam.xyz, covA, covB, transpose(mat3(model_view)));
51-
52-
// get color
53-
color = texelFetch(splatColor, splatUV, 0);
58+
// read color
59+
vec4 clr = readColor(state);
5460

55-
// calculate scale based on alpha
56-
float scale = min(1.0, sqrt(-log(1.0 / 255.0 / color.a)) / 2.0);
57-
58-
v1v2 *= scale;
59-
60-
// early out tiny splats
61-
if (dot(v1v2.xy, v1v2.xy) < 4.0 && dot(v1v2.zw, v1v2.zw) < 4.0) {
62-
gl_Position = discardVec;
63-
return;
64-
}
61+
// evaluate spherical harmonics
62+
#if SH_BANDS > 0
63+
clr.xyz = max(clr.xyz + evalSH(state, projState), 0.0);
64+
#endif
6565

66-
gl_Position = splat_proj + vec4((vertex_position.x * v1v2.xy + vertex_position.y * v1v2.zw) / viewport * splat_proj.w, 0, 0);
66+
clr = animateColor(state.center.y, clr);
6767

68-
texCoord = vertex_position.xy * scale / 2.0;
68+
applyClipping(projState, clr.w);
6969

70-
#ifdef USE_SH1
71-
vec4 worldCenter = matrix_model * vec4(center, 1.0);
72-
vec3 viewDir = normalize((worldCenter.xyz / worldCenter.w - view_position) * mat3(matrix_model));
73-
color.xyz = max(color.xyz + evalSH(viewDir), 0.0);
74-
#endif
70+
// write output
71+
gl_Position = projState.cornerProj;
72+
gaussianUV = projState.cornerUV;
73+
gaussianColor = vec4(prepareOutputFromGamma(clr.xyz), clr.w);
7574

7675
#ifndef DITHER_NONE
77-
id = float(splatId);
76+
id = float(state.id);
7877
#endif
79-
}
78+
}

src/core/hash.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* @returns {number} Hash value.
66
*/
77
function hashCode(str) {
8+
if (str === null || str === undefined) {
9+
return 0;
10+
}
811
let hash = 0;
912
for (let i = 0, len = str.length; i < len; i++) {
1013
hash = ((hash << 5) - hash) + str.charCodeAt(i);

0 commit comments

Comments
 (0)