playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
104 lines (81 loc) • 2.95 kB
JavaScript
var gsplatCopyToWorkbuffer_default = (
/* glsl */
`
// Packed sub-draw params: (sourceBase, colStart, rowWidth, rowStart)
flat varying ivec4 vSubDraw;
uniform vec3 uColorMultiply;
// pre-computed model matrix decomposition
uniform vec3 model_scale;
uniform vec4 model_rotation; // (x,y,z,w) format
uniform uint uId;
void main(void) {
// Compute source index from packed sub-draw varying: (sourceBase, colStart, rowWidth, rowStart)
int localRow = int(gl_FragCoord.y) - vSubDraw.w;
int localCol = int(gl_FragCoord.x) - vSubDraw.y;
uint originalIndex = uint(vSubDraw.x + localRow * vSubDraw.z + localCol);
// Initialize global splat for format read functions
setSplat(originalIndex);
// read center in local space
vec3 modelCenter = getCenter();
// compute world-space center for storage
vec3 worldCenter = (matrix_model * vec4(modelCenter, 1.0)).xyz;
SplatCenter center;
initCenter(modelCenter, center);
// Get source rotation and scale
// getRotation() returns (w,x,y,z) format, convert to (x,y,z,w) for quatMul
vec4 srcRotation = getRotation().yzwx;
vec3 srcScale = getScale();
// Combine: world = model * source (both in x,y,z,w format)
vec4 worldRotation = quatMul(model_rotation, srcRotation);
// Ensure w is positive so sqrt() reconstruction works correctly
// (quaternions q and -q represent the same rotation)
if (worldRotation.w < 0.0) {
worldRotation = -worldRotation;
}
vec3 worldScale = model_scale * srcScale;
// Apply custom center modification
vec3 originalCenter = worldCenter;
modifySplatCenter(worldCenter);
// Apply custom rotation/scale modification
modifySplatRotationScale(originalCenter, worldCenter, worldRotation, worldScale);
// read color
vec4 color = getColor();
// evaluate spherical harmonics
// calculate the model-space view direction
vec3 dir = normalize(center.view * mat3(center.modelView));
// read sh coefficients
vec3 sh[SH_COEFFS];
float scale;
readSHData(sh, scale);
// evaluate
color.xyz += evalSH(sh, dir) * scale;
// Apply custom color modification
modifySplatColor(worldCenter, color);
color.xyz *= uColorMultiply;
// write color + transform using format-specific encoding
writeSplat(worldCenter, worldRotation, worldScale, color);
writePcId(uvec4(uId, 0u, 0u, 0u));
}
`
);
export {
gsplatCopyToWorkbuffer_default as default
};