Skip to content

Commit

Permalink
Update gsplat example shaders to latest structure (playcanvas#7021)
Browse files Browse the repository at this point in the history
  • Loading branch information
slimbuck authored Oct 10, 2024
1 parent 5ec6e33 commit 154ca8d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
22 changes: 15 additions & 7 deletions examples/src/examples/loaders/gsplat-many.shader.frag
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
uniform float uTime;
varying float height;

void main(void)
{
// get splat color and alpha
gl_FragColor = evalSplat();

void animate(inout vec4 clr) {
float sineValue = abs(sin(uTime * 5.0 + height));

#ifdef CUTOUT

// in cutout mode, remove pixels along the wave
if (sineValue < 0.5) {
gl_FragColor.a = 0.0;
clr.a = 0.0;
}

#else

// in non-cutout mode, add a golden tint to the wave
vec3 gold = vec3(1.0, 0.85, 0.0);
float blend = smoothstep(0.9, 1.0, sineValue);
gl_FragColor.xyz = mix(gl_FragColor.xyz, gold, blend);
clr.xyz = mix(clr.xyz, gold, blend);

#endif
}

varying mediump vec2 texCoord;
varying mediump vec4 color;

void main(void)
{
vec4 clr = evalSplat(texCoord, color);

animate(clr);

gl_FragColor = clr;
}
61 changes: 48 additions & 13 deletions examples/src/examples/loaders/gsplat-many.shader.vert
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
uniform float uTime;
varying float height;

void animate() {
void animate(inout vec3 center) {
// modify center
float heightIntensity = center.y * 0.2;
center.x += sin(uTime * 5.0 + center.y) * 0.3 * heightIntensity;
Expand All @@ -10,7 +10,14 @@ void animate() {
height = center.y;
}

vec4 discardVec = vec4(0.0, 0.0, 2.0, 1.0);
uniform vec3 view_position;

uniform sampler2D splatColor;

varying mediump vec2 texCoord;
varying mediump vec4 color;

mediump vec4 discardVec = vec4(0.0, 0.0, 2.0, 1.0);

void main(void)
{
Expand All @@ -20,25 +27,53 @@ void main(void)
return;
}

// read data
readData();
// get center
vec3 center = getCenter();

// animate
animate();
animate(center);

vec4 pos;
if (!evalSplat(pos)) {
// handle transforms
mat4 model_view = matrix_view * matrix_model;
vec4 splat_cam = model_view * vec4(center, 1.0);
vec4 splat_proj = matrix_projection * splat_cam;

// cull behind camera
if (splat_proj.z < -splat_proj.w) {
gl_Position = discardVec;
return;
}

gl_Position = pos;
// get covariance
vec3 covA, covB;
getCovariance(covA, covB);

vec4 v1v2 = calcV1V2(splat_cam.xyz, covA, covB, transpose(mat3(model_view)));

texCoord = vertex_position.xy;
color = getColor();
// get color
color = texelFetch(splatColor, splatUV, 0);

// calculate scale based on alpha
float scale = min(1.0, sqrt(-log(1.0 / 255.0 / color.a)) / 2.0);

v1v2 *= scale;

// early out tiny splats
if (dot(v1v2.xy, v1v2.xy) < 4.0 && dot(v1v2.zw, v1v2.zw) < 4.0) {
gl_Position = discardVec;
return;
}

gl_Position = splat_proj + vec4((vertex_position.x * v1v2.xy + vertex_position.y * v1v2.zw) / viewport * splat_proj.w, 0, 0);

texCoord = vertex_position.xy * scale / 2.0;

#ifdef USE_SH1
vec4 worldCenter = matrix_model * vec4(center, 1.0);
vec3 viewDir = normalize((worldCenter.xyz / worldCenter.w - view_position) * mat3(matrix_model));
color.xyz = max(color.xyz + evalSH(viewDir), 0.0);
#endif

#ifndef DITHER_NONE
id = float(splatId);
#endif

}
}
8 changes: 4 additions & 4 deletions src/scene/gsplat/shader-generator-gsplat.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ const splatCoreVS = /* glsl */ `
}
// fetch quantized spherical harmonic coefficients
void fetchScale(in highp usampler2D sampler, out float scale, out vec3 a, out vec3 b, out vec3 c) {
uvec4 t = texelFetch(sampler, splatUV, 0);
void fetchScale(in highp usampler2D s, out float scale, out vec3 a, out vec3 b, out vec3 c) {
uvec4 t = texelFetch(s, splatUV, 0);
scale = uintBitsToFloat(t.x);
a = unpack111011(t.y) * 2.0 - 1.0;
b = unpack111011(t.z) * 2.0 - 1.0;
c = unpack111011(t.w) * 2.0 - 1.0;
}
// fetch quantized spherical harmonic coefficients
void fetch(in highp usampler2D sampler, out vec3 a, out vec3 b, out vec3 c, out vec3 d) {
uvec4 t = texelFetch(sampler, splatUV, 0);
void fetch(in highp usampler2D s, out vec3 a, out vec3 b, out vec3 c, out vec3 d) {
uvec4 t = texelFetch(s, splatUV, 0);
a = unpack111011(t.x) * 2.0 - 1.0;
b = unpack111011(t.y) * 2.0 - 1.0;
c = unpack111011(t.z) * 2.0 - 1.0;
Expand Down

0 comments on commit 154ca8d

Please sign in to comment.