UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

3 lines (2 loc) 3.92 kB
declare const _default: "\n#ifdef GSPLAT_WORKBUFFER_GEOMETRY\n\n // world-space transform data previously written to the work buffer (see gsplatWriteVS)\n var uWorkBufferTransformA: texture_2d<u32>;\n var uWorkBufferTransformB: texture_2d<u32>;\n\n // inverse of matrix_model, to convert stored world-space data back to local space\n uniform matrix_model_inverse: mat4x4f;\n\n // world-space camera position\n uniform uCameraPosition: vec3f;\n\n var<private> wbCoord: vec2i;\n var<private> wbTransformA: vec4u;\n\n // cache transformA at the destination pixel; must be called before any getters\n fn initWorkBufferGeometry(coord: vec2i) {\n wbCoord = coord;\n wbTransformA = textureLoad(uWorkBufferTransformA, coord, 0);\n }\n\n fn workBufferWorldCenter() -> vec3f {\n return vec3f(bitcast<f32>(wbTransformA.x), bitcast<f32>(wbTransformA.y), bitcast<f32>(wbTransformA.z));\n }\n\n // world-space rotation (x,y,z,w), decoded to match the work buffer write encoding\n fn workBufferWorldRotation() -> vec4f {\n #ifdef GSPLAT_WORKBUFFER_COMPACT\n // half-angle projected quaternion, 11+11+10 bits (see containerCompactWrite)\n let data = textureLoad(uWorkBufferTransformB, wbCoord, 0).x;\n let p = vec3f(\n f32(data & 0x7FFu) / 2047.0 * 2.0 - 1.0,\n f32((data >> 11u) & 0x7FFu) / 2047.0 * 2.0 - 1.0,\n f32((data >> 22u) & 0x3FFu) / 1023.0 * 2.0 - 1.0\n );\n let d = dot(p, p);\n return vec4f(sqrt(max(0.0, 2.0 - d)) * p, 1.0 - d);\n #else\n // rotation.xy in transformA.w, rotation.z in transformB.x (see containerPackedWrite)\n let rotXY = unpack2x16float(wbTransformA.w);\n let r = vec3f(rotXY, unpack2x16float(textureLoad(uWorkBufferTransformB, wbCoord, 0).x).x);\n return vec4f(r, sqrt(max(0.0, 1.0 - dot(r, r))));\n #endif\n }\n\n fn workBufferWorldScale() -> vec3f {\n #ifdef GSPLAT_WORKBUFFER_COMPACT\n // log-encoded scale, 3x8 bits: 0 = true zero, 1-255 maps to e^-12..e^9 (see containerCompactWrite)\n let data = wbTransformA.w;\n let sx = f32(data & 0xFFu);\n let sy = f32((data >> 8u) & 0xFFu);\n let sz = f32((data >> 16u) & 0xFFu);\n let logRange = 21.0 / 255.0;\n let logMin = -12.0;\n return vec3f(\n select(exp(sx * logRange + logMin), 0.0, sx == 0.0),\n select(exp(sy * logRange + logMin), 0.0, sy == 0.0),\n select(exp(sz * logRange + logMin), 0.0, sz == 0.0)\n );\n #else\n let b = textureLoad(uWorkBufferTransformB, wbCoord, 0).xy;\n return vec3f(unpack2x16float(b.x).y, unpack2x16float(b.y));\n #endif\n }\n\n // rotate vector by the inverse of unit quaternion q (x,y,z,w)\n fn quatRotateInv(q: vec4f, v: vec3f) -> vec3f {\n let t = -q.xyz;\n return v + 2.0 * cross(t, cross(t, v) + q.w * v);\n }\n\n // Source-format-compatible getters for user modifier code: local-space values reconstructed\n // from the stored world-space data (quantized by the work buffer format, so rotation and\n // scale are approximate).\n fn getCenter() -> vec3f {\n return (uniform.matrix_model_inverse * vec4f(workBufferWorldCenter(), 1.0)).xyz;\n }\n\n // returns (w,x,y,z) to match the source format getRotation convention\n fn getRotation() -> vec4f {\n let worldRotation = workBufferWorldRotation();\n let localRotation = vec4f(quatMul(half4(vec4f(-uniform.model_rotation.xyz, uniform.model_rotation.w)), half4(worldRotation)));\n return localRotation.wxyz;\n }\n\n fn getScale() -> vec3f {\n return workBufferWorldScale() / uniform.model_scale;\n }\n\n#endif\n"; export default _default;