Skip to content

Commit 9cdf6da

Browse files
mvaligurskyMartin Valigursky
andauthored
Expose modifyCovariance and unify gsplat shader customization API (playcanvas#8064)
* Expose modifyCovariance and unify gsplat shader customization API * lint --------- Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent c0bf0a7 commit 9cdf6da

16 files changed

Lines changed: 127 additions & 56 deletions

File tree

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
uniform float uTime;
22

3-
vec3 modifyPosition(vec3 center) {
3+
void modifyCenter(inout vec3 center) {
44
// modify center
55
float heightIntensity = center.y * 0.2;
66
center.x += sin(uTime * 5.0 + center.y) * 0.3 * heightIntensity;
7+
}
78

8-
// output y-coordinate
9-
return center;
9+
void modifyCovariance(vec3 originalCenter, vec3 modifiedCenter, inout vec3 covA, inout vec3 covB) {
10+
// no modification
1011
}
1112

12-
vec4 modifyColor(vec3 center, vec4 clr) {
13+
void modifyColor(vec3 center, inout vec4 clr) {
1314
float sineValue = abs(sin(uTime * 5.0 + center.y));
1415

1516
#ifdef CUTOUT
@@ -23,7 +24,5 @@ vec4 modifyColor(vec3 center, vec4 clr) {
2324
float blend = smoothstep(0.9, 1.0, sineValue);
2425
clr.xyz = mix(clr.xyz, gold, blend);
2526
#endif
26-
27-
return clr;
2827
}
2928

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
uniform uTime: f32;
22

3-
fn modifyPosition(center: vec3f) -> vec3f {
3+
fn modifyCenter(center: ptr<function, vec3f>) {
44
// modify center
5-
var result = center;
6-
let heightIntensity = center.y * 0.2;
7-
result.x += sin(uniform.uTime * 5.0 + center.y) * 0.3 * heightIntensity;
5+
let heightIntensity = (*center).y * 0.2;
6+
(*center).x += sin(uniform.uTime * 5.0 + (*center).y) * 0.3 * heightIntensity;
7+
}
88

9-
// output y-coordinate
10-
return result;
9+
fn modifyCovariance(originalCenter: vec3f, modifiedCenter: vec3f, covA: ptr<function, vec3f>, covB: ptr<function, vec3f>) {
10+
// no modification
1111
}
1212

13-
fn modifyColor(center: vec3f, clr: vec4f) -> vec4f {
14-
var result = clr;
13+
fn modifyColor(center: vec3f, clr: ptr<function, vec4f>) {
1514
let sineValue = abs(sin(uniform.uTime * 5.0 + center.y));
1615

1716
#ifdef CUTOUT
1817
// in cutout mode, remove pixels along the wave
1918
if (sineValue < 0.5) {
20-
result.a = 0.0;
19+
(*clr).a = 0.0;
2120
}
2221
#else
2322
// in non-cutout mode, add a golden tint to the wave
2423
let gold = vec3f(1.0, 0.85, 0.0);
2524
let blend = smoothstep(0.9, 1.0, sineValue);
26-
result = vec4f(mix(clr.xyz, gold, blend), clr.a);
25+
(*clr) = vec4f(mix((*clr).xyz, gold, blend), (*clr).a);
2726
#endif
28-
29-
return result;
3027
}
3128

examples/src/examples/gaussian-splatting/picking.shader.glsl.vert

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
uniform float fade;
22

3-
// animate position based on the fade value
4-
vec3 modifyPosition(vec3 pos) {
3+
// animate center position based on the fade value
4+
void modifyCenter(inout vec3 pos) {
55
// Use a sine wave to create a smooth scale down and back up animation
66
float angle = fade * 3.14159265;
77
float shrinkFactor = sin(angle) * 0.3;
88
float scale = 1.0 - shrinkFactor;
9-
return pos * scale;
9+
pos *= scale;
10+
}
11+
12+
void modifyCovariance(vec3 originalCenter, vec3 modifiedCenter, inout vec3 covA, inout vec3 covB) {
13+
// no modification
1014
}
1115

1216
// animate color based on the fade value
13-
vec4 modifyColor(vec3 center, vec4 clr) {
17+
void modifyColor(vec3 center, inout vec4 clr) {
1418

1519
// Check if the color is approximately grayscale
1620
float r = clr.r;
@@ -29,7 +33,5 @@ vec4 modifyColor(vec3 center, vec4 clr) {
2933
// cross fade blue to original orange color based on fade value
3034
clr.rgb = mix(clr.bgr * 0.5, clr.rgb, fade);
3135
}
32-
33-
return clr;
3436
}
3537

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
uniform fade: f32;
22

3-
// animate position based on the fade value
4-
fn modifyPosition(pos: vec3f) -> vec3f {
3+
// animate center position based on the fade value
4+
fn modifyCenter(pos: ptr<function, vec3f>) {
55
// Use a sine wave to create a smooth scale down and back up animation
66
let angle = uniform.fade * 3.14159265;
77
let shrinkFactor = sin(angle) * 0.3;
88
let scale = 1.0 - shrinkFactor;
9-
return pos * scale;
9+
*pos = *pos * scale;
10+
}
11+
12+
fn modifyCovariance(originalCenter: vec3f, modifiedCenter: vec3f, covA: ptr<function, vec3f>, covB: ptr<function, vec3f>) {
13+
// no modification
1014
}
1115

1216
// animate color based on the fade value
13-
fn modifyColor(center: vec3f, clr: vec4f) -> vec4f {
14-
var result = clr;
17+
fn modifyColor(center: vec3f, clr: ptr<function, vec4f>) {
1518

1619
// Check if the color is approximately grayscale
17-
let r = clr.r;
18-
let g = clr.g;
19-
let b = clr.b;
20+
let r = (*clr).r;
21+
let g = (*clr).g;
22+
let b = (*clr).b;
2023

2124
let grayscaleThreshold = 0.01;
2225
let isGrayscale = abs(r - g) < grayscaleThreshold &&
@@ -25,12 +28,10 @@ fn modifyColor(center: vec3f, clr: vec4f) -> vec4f {
2528

2629
if (isGrayscale) {
2730
// If the color is grayscale, make it very bright (the PC logo)
28-
result = vec4f(clr.rgb * 10.0, clr.a);
31+
*clr = vec4f((*clr).rgb * 10.0, (*clr).a);
2932
} else {
3033
// cross fade blue to original orange color based on fade value
31-
result = vec4f(mix(clr.bgr * 0.5, clr.rgb, uniform.fade), clr.a);
34+
*clr = vec4f(mix((*clr).bgr * 0.5, (*clr).rgb, uniform.fade), (*clr).a);
3235
}
33-
34-
return result;
3536
}
3637

src/scene/shader-lib/glsl/chunks/gsplat/vert/gsplat.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export default /* glsl */`
2+
#include "gsplatHelpersVS"
3+
#include "gsplatCustomizeVS"
24
#include "gsplatCommonVS"
35
46
varying mediump vec2 gaussianUV;
@@ -19,8 +21,6 @@ mediump vec4 discardVec = vec4(0.0, 0.0, 2.0, 1.0);
1921
uniform float colorRampIntensity;
2022
#endif
2123
22-
#include "gsplatCustomizeVS"
23-
2424
void main(void) {
2525
// read gaussian details
2626
SplatSource source;
@@ -34,9 +34,13 @@ void main(void) {
3434
#endif
3535
3636
vec3 modelCenter = readCenter(source);
37-
modelCenter = modifyPosition(modelCenter);
3837
3938
SplatCenter center;
39+
center.modelCenterOriginal = modelCenter;
40+
41+
modifyCenter(modelCenter);
42+
center.modelCenterModified = modelCenter;
43+
4044
if (!initCenter(modelCenter, center)) {
4145
gl_Position = discardVec;
4246
return;
@@ -71,7 +75,7 @@ void main(void) {
7175
clr.xyz += evalSH(sh, dir) * scale;
7276
#endif
7377
74-
clr = modifyColor(modelCenter, clr);
78+
modifyColor(modelCenter, clr);
7579
7680
clipCorner(corner, clr.w);
7781

src/scene/shader-lib/glsl/chunks/gsplat/vert/gsplatCorner.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ bool initCornerCov(SplatSource source, SplatCenter center, out SplatCorner corne
7474
bool initCorner(SplatSource source, SplatCenter center, out SplatCorner corner) {
7575
vec3 covA, covB;
7676
readCovariance(source, covA, covB);
77+
modifyCovariance(center.modelCenterOriginal, center.modelCenterModified, covA, covB);
7778
return initCornerCov(source, center, corner, covA, covB);
7879
}
7980
`;
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
export default /* glsl */`
2-
vec3 modifyPosition(vec3 center) {
3-
return center;
2+
void modifyCenter(inout vec3 center) {
3+
// Modify the splat center position
4+
// Example: center.y += 1.0; // offset all splats up by 1 unit
45
}
56
6-
vec4 modifyColor(vec3 center, vec4 color) {
7-
return color;
7+
void modifyCovariance(vec3 originalCenter, vec3 modifiedCenter, inout vec3 covA, inout vec3 covB) {
8+
// Modify the splat size/covariance
9+
// Example to scale all splats by 2x:
10+
// gsplatApplyUniformScale(covA, covB, 2.0);
11+
//
12+
// Example to clamp size to a range:
13+
// float size = gsplatExtractSize(covA, covB);
14+
// float newSize = clamp(size, 0.01, 0.5);
15+
// gsplatApplyUniformScale(covA, covB, newSize / size);
16+
}
17+
18+
void modifyColor(vec3 center, inout vec4 color) {
19+
// Modify the splat color
20+
// Example: color.rgb *= 0.5; // darken all splats
821
}
922
`;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default /* glsl */`
2+
// Helper function to extract the RMS size from covariance
3+
float gsplatExtractSize(vec3 covA, vec3 covB) {
4+
float tr = covA.x + covB.x + covB.z;
5+
return sqrt(max(tr, 0.0) / 3.0);
6+
}
7+
8+
// Helper function to apply uniform scale to covariance
9+
void gsplatApplyUniformScale(inout vec3 covA, inout vec3 covB, float scale) {
10+
float s2 = scale * scale;
11+
covA *= s2;
12+
covB *= s2;
13+
}
14+
`;

src/scene/shader-lib/glsl/chunks/gsplat/vert/gsplatStructs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ struct SplatCenter {
1414
vec4 proj; // center in clip space
1515
mat4 modelView; // model-view matrix
1616
float projMat00; // element [0][0] of the projection matrix
17+
vec3 modelCenterOriginal; // original model center before modification
18+
vec3 modelCenterModified; // model center after modification
1719
};
1820
1921
// stores the offset from center for the current gaussian

src/scene/shader-lib/glsl/collections/shader-chunks-glsl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import gsplatCompressedDataVS from '../chunks/gsplat/vert/gsplatCompressedData.j
5252
import gsplatCompressedSHVS from '../chunks/gsplat/vert/gsplatCompressedSH.js';
5353
import gsplatCustomizeVS from '../chunks/gsplat/vert/gsplatCustomize.js';
5454
import gsplatEvalSHVS from '../chunks/gsplat/vert/gsplatEvalSH.js';
55+
import gsplatHelpersVS from '../chunks/gsplat/vert/gsplatHelpers.js';
5556
import gsplatQuatToMat3VS from '../chunks/gsplat/vert/gsplatQuatToMat3.js';
5657
import gsplatSogsColorVS from '../chunks/gsplat/vert/gsplatSogsColor.js';
5758
import gsplatSogsDataVS from '../chunks/gsplat/vert/gsplatSogsData.js';
@@ -237,6 +238,7 @@ const shaderChunksGLSL = {
237238
gsplatCompressedSHVS,
238239
gsplatCustomizeVS,
239240
gsplatEvalSHVS,
241+
gsplatHelpersVS,
240242
gsplatQuatToMat3VS,
241243
gsplatSogsColorVS,
242244
gsplatSogsDataVS,

0 commit comments

Comments
 (0)