@driftengine/splats 3.61.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +9 -0
- package/README.md +56 -0
- package/dist/half.d.ts +32 -0
- package/dist/half.js +88 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +38 -0
- package/dist/shaders/generated/splat.wgsl.d.ts +89 -0
- package/dist/shaders/generated/splat.wgsl.js +95 -0
- package/dist/shaders/splat.d.ts +25 -0
- package/dist/shaders/splat.js +337 -0
- package/dist/splat.d.ts +26 -0
- package/dist/splat.js +63 -0
- package/dist/splatBudget.d.ts +40 -0
- package/dist/splatBudget.js +45 -0
- package/dist/splatCapture.d.ts +76 -0
- package/dist/splatCapture.js +108 -0
- package/dist/splatCull.d.ts +25 -0
- package/dist/splatCull.js +80 -0
- package/dist/splatData.d.ts +177 -0
- package/dist/splatData.js +223 -0
- package/dist/splatGl.d.ts +49 -0
- package/dist/splatGl.js +176 -0
- package/dist/splatGpu.d.ts +50 -0
- package/dist/splatGpu.js +180 -0
- package/dist/splatLayout.d.ts +52 -0
- package/dist/splatLayout.js +75 -0
- package/dist/splatMatrix.d.ts +29 -0
- package/dist/splatMatrix.js +68 -0
- package/dist/splatPass.d.ts +83 -0
- package/dist/splatPass.js +206 -0
- package/dist/splatPly.d.ts +14 -0
- package/dist/splatPly.js +242 -0
- package/dist/splatSog.d.ts +110 -0
- package/dist/splatSog.js +285 -0
- package/dist/splatSogDecoder.d.ts +26 -0
- package/dist/splatSogDecoder.js +29 -0
- package/dist/splatSort.d.ts +137 -0
- package/dist/splatSort.js +199 -0
- package/dist/splatSortWorker.d.ts +14 -0
- package/dist/splatSortWorker.js +137 -0
- package/dist/splatSorter.d.ts +112 -0
- package/dist/splatSorter.js +231 -0
- package/dist/splatView.d.ts +52 -0
- package/dist/splatView.js +115 -0
- package/package.json +56 -0
- package/src/fixtures/README.md +36 -0
- package/src/fixtures/cloud.sog +0 -0
- package/src/fixtures/cloud.texels.json +27 -0
- package/src/fixtures/cloud.truth.json +582 -0
- package/src/half.ts +92 -0
- package/src/index.ts +55 -0
- package/src/shaders/generated/splat.wgsl.ts +98 -0
- package/src/shaders/splat.ts +344 -0
- package/src/splat.ts +75 -0
- package/src/splatBudget.ts +48 -0
- package/src/splatCapture.ts +154 -0
- package/src/splatCull.ts +91 -0
- package/src/splatData.ts +398 -0
- package/src/splatGl.ts +262 -0
- package/src/splatGpu.ts +259 -0
- package/src/splatLayout.ts +86 -0
- package/src/splatMatrix.ts +81 -0
- package/src/splatPass.ts +324 -0
- package/src/splatPly.ts +283 -0
- package/src/splatSog.ts +375 -0
- package/src/splatSogDecoder.ts +33 -0
- package/src/splatSort.ts +296 -0
- package/src/splatSortWorker.ts +155 -0
- package/src/splatSorter.ts +285 -0
- package/src/splatView.ts +147 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/*! DriftEngine | Copyright 2026 Drift Technologies | Apache-2.0 | https://github.com/drftrun/driftengine */
|
|
2
|
+
/**
|
|
3
|
+
* `@driftengine/splats` — Gaussian splat captures, read and drawn into the scene.
|
|
4
|
+
*
|
|
5
|
+
* **The barrel is the contract.** Anything not exported here is private to this package, whatever
|
|
6
|
+
* its file permissions suggest.
|
|
7
|
+
*
|
|
8
|
+
* This package owns its readers, its sort, its shaders and its GPU resources, and it reaches the
|
|
9
|
+
* frame through `registerPass` rather than through a verb on `RendererApi` — which is what makes
|
|
10
|
+
* it a package at all. A consumer that never imports it carries none of it, and
|
|
11
|
+
* `scripts/size-gate.test.mjs` asserts that rather than claiming it.
|
|
12
|
+
*/
|
|
13
|
+
export { packSplats, SPLAT_SH1_COEFFICIENTS, SPLAT_WORDS, SPLAT_WORDS_SH1 } from './splatData.ts';
|
|
14
|
+
export type { SplatData, SplatSource } from './splatData.ts';
|
|
15
|
+
export { readSplat, SPLAT_RECORD_BYTES } from './splat.ts';
|
|
16
|
+
export { readSplatPly } from './splatPly.ts';
|
|
17
|
+
/*
|
|
18
|
+
* The `.sog` container: a ZIP of lossless WebP images and a manifest, which is what newer capture
|
|
19
|
+
* tools emit.
|
|
20
|
+
*
|
|
21
|
+
* **Four exports rather than one, because the read has two halves a consumer may want separately.**
|
|
22
|
+
* A bundle unpacks to files and files decode to Gaussians, and an *unbundled* capture — a
|
|
23
|
+
* `meta.json` beside its images, which is what `splat-transform` writes to a directory — arrives at
|
|
24
|
+
* the second half without the first. `browserWebpDecoder` is the ordinary decoder and is separate
|
|
25
|
+
* from both, for the reason `splatSog.ts` opens with: Node has none, and vendoring one is a
|
|
26
|
+
* thousand lines of VP8L this package will not carry.
|
|
27
|
+
*/
|
|
28
|
+
export { browserWebpDecoder } from './splatSogDecoder.ts';
|
|
29
|
+
export { SOG_VERSION, readSogMeta, readSogSource, readSplatSog, unbundleSog } from './splatSog.ts';
|
|
30
|
+
export type { DecodedImage, SogFiles, SogMeta, WebpDecoder } from './splatSog.ts';
|
|
31
|
+
export { createSplatPass } from './splatPass.ts';
|
|
32
|
+
export type { SplatPass, SplatView } from './splatPass.ts';
|
|
33
|
+
export { SPLAT_STRIDE } from './splatLayout.ts';
|
|
34
|
+
export {
|
|
35
|
+
createSplatSortScratch,
|
|
36
|
+
sortSplatsByDepth,
|
|
37
|
+
SPLAT_SIZE_BUCKETS,
|
|
38
|
+
SPLAT_SORT_BUCKETS,
|
|
39
|
+
} from './splatSort.ts';
|
|
40
|
+
export type {
|
|
41
|
+
SplatSortFn,
|
|
42
|
+
SplatSortRequest,
|
|
43
|
+
SplatSortResult,
|
|
44
|
+
SplatSortScratch,
|
|
45
|
+
} from './splatSort.ts';
|
|
46
|
+
export { SplatSorter, createDefaultSplatSort, sortOnMainThread } from './splatSorter.ts';
|
|
47
|
+
export type { SplatSorterOptions } from './splatSorter.ts';
|
|
48
|
+
export { createSplatViewLocal, resolveSplatView } from './splatView.ts';
|
|
49
|
+
export type { SplatViewLocal } from './splatView.ts';
|
|
50
|
+
export { defaultSplatBudget, SPLAT_BUDGET_DEFAULT, SPLAT_BUDGET_WEAK } from './splatBudget.ts';
|
|
51
|
+
export { splatBoundsVisible } from './splatCull.ts';
|
|
52
|
+
/* A capture that fills up as its container blocks arrive, and the one-shot form of the same
|
|
53
|
+
assembly for a caller that read the whole file. */
|
|
54
|
+
export { SplatCapture, splatsFromRecords } from './splatCapture.ts';
|
|
55
|
+
export type { SplatAppend, SplatCaptureOptions } from './splatCapture.ts';
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Generated from ../splat.ts by `npm run wgsl`. Do not edit.
|
|
3
|
+
*
|
|
4
|
+
* The GLSL beside this file is the source of truth. A hand edit here is discarded by
|
|
5
|
+
* the next generation, and `npm run wgsl:check` fails the build when this is stale.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const SPLAT_FRAG_WGSL = "struct Uniforms {\n uOutputTransform: i32,\n uOutputExposure: f32,\n}\n\n@group(0) @binding(1) \nvar<uniform> unnamed: Uniforms;\nvar<private> vCorner_1: vec2<f32>;\nvar<private> vColor_1: vec4<f32>;\nvar<private> fragColor: vec4<f32>;\n\nfn linearToSrgb_u0028_vf3_u003b(c: ptr<function, vec3<f32>>) -> vec3<f32> {\n var low: vec3<f32>;\n var high: vec3<f32>;\n\n let _e54 = (*c);\n low = (_e54 * 12.92f);\n let _e56 = (*c);\n high = ((pow(max(_e56, vec3<f32>(0f, 0f, 0f)), vec3<f32>(0.41666666f, 0.41666666f, 0.41666666f)) * 1.055f) - vec3(0.055f));\n let _e62 = high;\n let _e63 = low;\n let _e64 = (*c);\n return mix(_e62, _e63, step(_e64, vec3<f32>(0.0031308f, 0.0031308f, 0.0031308f)));\n}\n\nfn rrtAndOdtFit_u0028_vf3_u003b(v: ptr<function, vec3<f32>>) -> vec3<f32> {\n var a: vec3<f32>;\n var b: vec3<f32>;\n\n let _e54 = (*v);\n let _e55 = (*v);\n a = ((_e54 * (_e55 + vec3(0.0245786f))) - vec3(0.000090537f));\n let _e61 = (*v);\n let _e62 = (*v);\n b = ((_e61 * ((_e62 * 0.983729f) + vec3(0.432951f))) + vec3(0.238081f));\n let _e69 = a;\n let _e70 = b;\n return (_e69 / _e70);\n}\n\nfn acesFilmic_u0028_vf3_u003b(x: ptr<function, vec3<f32>>) -> vec3<f32> {\n var param: vec3<f32>;\n\n let _e54 = unnamed.uOutputExposure;\n let _e55 = (*x);\n (*x) = (_e55 * _e54);\n let _e57 = (*x);\n param = (mat3x3<f32>(vec3<f32>(0.59719f, 0.076f, 0.0284f), vec3<f32>(0.35458f, 0.90834f, 0.13383f), vec3<f32>(0.04823f, 0.01566f, 0.83777f)) * _e57);\n let _e59 = rrtAndOdtFit_u0028_vf3_u003b((¶m));\n return clamp((mat3x3<f32>(vec3<f32>(1.60475f, -0.10208f, -0.00327f), vec3<f32>(-0.53108f, 1.10813f, -0.07276f), vec3<f32>(-0.07367f, -0.00605f, 1.07602f)) * _e59), vec3(0f), vec3(1f));\n}\n\nfn applyOutputTransform_u0028_vf3_u003b(c_1: ptr<function, vec3<f32>>) -> vec3<f32> {\n var param_1: vec3<f32>;\n var param_2: vec3<f32>;\n\n let _e55 = unnamed.uOutputTransform;\n if (_e55 == 0i) {\n let _e57 = (*c_1);\n return _e57;\n }\n let _e59 = unnamed.uOutputTransform;\n if (_e59 == 2i) {\n let _e61 = (*c_1);\n param_1 = _e61;\n let _e62 = acesFilmic_u0028_vf3_u003b((¶m_1));\n (*c_1) = _e62;\n }\n let _e63 = (*c_1);\n param_2 = _e63;\n let _e64 = linearToSrgb_u0028_vf3_u003b((¶m_2));\n return _e64;\n}\n\nfn main_1() {\n var power: f32;\n var alpha: f32;\n var colour: vec3<f32>;\n var param_3: vec3<f32>;\n\n let _e55 = vCorner_1;\n let _e56 = vCorner_1;\n power = dot(_e55, _e56);\n let _e59 = vColor_1[3u];\n let _e60 = power;\n alpha = (_e59 * exp((-0.5f * _e60)));\n let _e64 = alpha;\n if (_e64 < 0.003921569f) {\n discard;\n }\n let _e66 = vColor_1;\n param_3 = _e66.xyz;\n let _e68 = applyOutputTransform_u0028_vf3_u003b((¶m_3));\n colour = _e68;\n let _e69 = colour;\n let _e70 = alpha;\n let _e71 = (_e69 * _e70);\n let _e72 = alpha;\n fragColor = vec4<f32>(_e71.x, _e71.y, _e71.z, _e72);\n return;\n}\n\n@fragment \nfn main(@location(0) vCorner: vec2<f32>, @location(1) vColor: vec4<f32>) -> @location(0) vec4<f32> {\n vCorner_1 = vCorner;\n vColor_1 = vColor;\n main_1();\n let _e5 = fragColor;\n return _e5;\n}\n";
|
|
9
|
+
|
|
10
|
+
export const SPLAT_VERT_WGSL = "struct Uniforms {\n uSplatCount: i32,\n uSplatStride: i32,\n uSplatTexels: i32,\n uView: mat4x4<f32>,\n uProjection: mat4x4<f32>,\n uViewport: vec2<f32>,\n uModel: mat4x4<f32>,\n uSplatCameraLocal: vec3<f32>,\n uSplatShDegree: i32,\n}\n\nstruct gl_PerVertex {\n @builtin(position) gl_Position: vec4<f32>,\n gl_PointSize: f32,\n}\n\nstruct VertexOutput {\n @builtin(position) gl_Position: vec4<f32>,\n @location(0) member: vec2<f32>,\n @location(1) member_1: vec4<f32>,\n}\n\n@group(0) @binding(0) \nvar<uniform> unnamed: Uniforms;\n@group(0) @binding(16) \nvar uSplatData_t: texture_2d<u32>;\nvar<private> gl_VertexIndex_1: i32;\nvar<private> unnamed_1: gl_PerVertex = gl_PerVertex(vec4<f32>(0f, 0f, 0f, 1f), 1f);\nvar<private> vCorner: vec2<f32>;\nvar<private> vColor: vec4<f32>;\n@group(0) @binding(18) \nvar uSplatOrder_t: texture_2d<u32>;\n@group(0) @binding(17) \nvar uSplatData_s: sampler;\n@group(0) @binding(19) \nvar uSplatOrder_s: sampler;\n\nfn unpackRgba8_u0028_u1_u003b(packed: ptr<function, u32>) -> vec4<f32> {\n let _e50 = (*packed);\n let _e53 = (*packed);\n let _e58 = (*packed);\n let _e63 = (*packed);\n return (vec4<f32>(f32((_e50 & 255u)), f32(((_e53 >> bitcast<u32>(8i)) & 255u)), f32(((_e58 >> bitcast<u32>(16i)) & 255u)), f32(((_e63 >> bitcast<u32>(24i)) & 255u))) / vec4(255f));\n}\n\nfn viewDependentColour_u0028_vu4_u003b_vf3_u003b(sh: ptr<function, vec4<u32>>, direction: ptr<function, vec3<f32>>) -> vec3<f32> {\n var scale: f32;\n var a: vec4<f32>;\n var param: u32;\n var b: vec4<f32>;\n var param_1: u32;\n var c: vec4<f32>;\n var param_2: u32;\n var c0_: vec3<f32>;\n var c1_: vec3<f32>;\n var c2_: vec3<f32>;\n\n let _e62 = (*sh)[3u];\n scale = bitcast<f32>(_e62);\n let _e64 = scale;\n if (_e64 == 0f) {\n return vec3<f32>(0f, 0f, 0f);\n }\n let _e67 = (*sh)[0u];\n param = _e67;\n let _e68 = unpackRgba8_u0028_u1_u003b((¶m));\n a = _e68;\n let _e70 = (*sh)[1u];\n param_1 = _e70;\n let _e71 = unpackRgba8_u0028_u1_u003b((¶m_1));\n b = _e71;\n let _e73 = (*sh)[2u];\n param_2 = _e73;\n let _e74 = unpackRgba8_u0028_u1_u003b((¶m_2));\n c = _e74;\n let _e76 = a[0u];\n let _e78 = a[1u];\n let _e80 = a[2u];\n let _e87 = scale;\n c0_ = ((((vec3<f32>(_e76, _e78, _e80) * 255f) - vec3(128f)) / vec3(127f)) * _e87);\n let _e90 = a[3u];\n let _e92 = b[0u];\n let _e94 = b[1u];\n let _e101 = scale;\n c1_ = ((((vec3<f32>(_e90, _e92, _e94) * 255f) - vec3(128f)) / vec3(127f)) * _e101);\n let _e104 = b[2u];\n let _e106 = b[3u];\n let _e108 = c[0u];\n let _e115 = scale;\n c2_ = ((((vec3<f32>(_e104, _e106, _e108) * 255f) - vec3(128f)) / vec3(127f)) * _e115);\n let _e118 = (*direction)[1u];\n let _e120 = c0_;\n let _e123 = (*direction)[2u];\n let _e124 = c1_;\n let _e128 = (*direction)[0u];\n let _e129 = c2_;\n return ((((_e120 * -(_e118)) + (_e124 * _e123)) - (_e129 * _e128)) * 0.48860252f);\n}\n\nfn fetchTexel_u0028_i1_u003b_i1_u003b(splat: ptr<function, i32>, which: ptr<function, i32>) -> vec4<u32> {\n var at: i32;\n var width: i32;\n\n let _e53 = (*splat);\n let _e55 = unnamed.uSplatTexels;\n let _e57 = (*which);\n at = ((_e53 * _e55) + _e57);\n let _e60 = unnamed.uSplatStride;\n let _e62 = unnamed.uSplatTexels;\n width = (_e60 * _e62);\n let _e64 = at;\n let _e65 = width;\n let _e73 = at;\n let _e74 = width;\n let _e77 = textureLoad(uSplatData_t, vec2<i32>((_e64 - (i32(floor((f32(_e64) / f32(_e65)))) * _e65)), (_e73 / _e74)), 0i);\n return _e77;\n}\n\nfn main_1() {\n var vertex: i32;\n var slot: i32;\n var corner: i32;\n var splat_1: i32;\n var texel0_: vec4<u32>;\n var param_3: i32;\n var param_4: i32;\n var texel1_: vec4<u32>;\n var param_5: i32;\n var param_6: i32;\n var centre: vec3<f32>;\n var param_7: u32;\n var direction_1: vec3<f32>;\n var param_8: i32;\n var param_9: i32;\n var param_10: vec4<u32>;\n var param_11: vec3<f32>;\n var c01_: vec2<f32>;\n var c23_: vec2<f32>;\n var c45_: vec2<f32>;\n var sigma: mat3x3<f32>;\n var world: vec4<f32>;\n var view: vec4<f32>;\n var clip: vec4<f32>;\n var focalX: f32;\n var focalY: f32;\n var invZ: f32;\n var invZ2_: f32;\n var jacobian: mat3x2<f32>;\n var rotation: mat3x3<f32>;\n var world3_: mat3x3<f32>;\n var screen: mat2x2<f32>;\n var a_1: f32;\n var b_1: f32;\n var d: f32;\n var mid: f32;\n var discriminant: f32;\n var lambda1_: f32;\n var lambda2_: f32;\n var major: vec2<f32>;\n var minor: vec2<f32>;\n var radius1_: f32;\n var radius2_: f32;\n var offsets: array<vec2<f32>, 6>;\n var unit: vec2<f32>;\n var offsetPx: vec2<f32>;\n var phi_358_: bool;\n var phi_369_: bool;\n\n let _e95 = gl_VertexIndex_1;\n vertex = _e95;\n let _e96 = vertex;\n slot = (_e96 / 6i);\n let _e98 = vertex;\n corner = (_e98 - (i32(floor((f32(_e98) / f32(6i)))) * 6i));\n let _e106 = slot;\n let _e108 = unnamed.uSplatCount;\n if (_e106 >= _e108) {\n unnamed_1.gl_Position = vec4<f32>(2f, 2f, 2f, 1f);\n vCorner = vec2<f32>(0f, 0f);\n vColor = vec4<f32>(0f, 0f, 0f, 0f);\n return;\n }\n let _e111 = slot;\n let _e113 = unnamed.uSplatStride;\n let _e121 = slot;\n let _e123 = unnamed.uSplatStride;\n let _e126 = textureLoad(uSplatOrder_t, vec2<i32>((_e111 - (i32(floor((f32(_e111) / f32(_e113)))) * _e113)), (_e121 / _e123)), 0i);\n splat_1 = bitcast<i32>(_e126.x);\n let _e129 = splat_1;\n param_3 = _e129;\n param_4 = 0i;\n let _e130 = fetchTexel_u0028_i1_u003b_i1_u003b((¶m_3), (¶m_4));\n texel0_ = _e130;\n let _e131 = splat_1;\n param_5 = _e131;\n param_6 = 1i;\n let _e132 = fetchTexel_u0028_i1_u003b_i1_u003b((¶m_5), (¶m_6));\n texel1_ = _e132;\n let _e133 = texel0_;\n centre = bitcast<vec3<f32>>(_e133.xyz);\n let _e137 = texel0_[3u];\n param_7 = _e137;\n let _e138 = unpackRgba8_u0028_u1_u003b((¶m_7));\n vColor = _e138;\n let _e140 = unnamed.uSplatShDegree;\n if (_e140 > 0i) {\n let _e142 = centre;\n let _e144 = unnamed.uSplatCameraLocal;\n direction_1 = normalize((_e142 - _e144));\n let _e147 = vColor;\n let _e149 = splat_1;\n param_8 = _e149;\n param_9 = 2i;\n let _e150 = fetchTexel_u0028_i1_u003b_i1_u003b((¶m_8), (¶m_9));\n param_10 = _e150;\n let _e151 = direction_1;\n param_11 = _e151;\n let _e152 = viewDependentColour_u0028_vu4_u003b_vf3_u003b((¶m_10), (¶m_11));\n let _e154 = max((_e147.xyz + _e152), vec3<f32>(0f, 0f, 0f));\n let _e155 = vColor;\n vColor = vec4<f32>(_e154.x, _e154.y, _e154.z, _e155.w);\n }\n let _e162 = texel1_[0u];\n c01_ = unpack2x16float(_e162);\n let _e165 = texel1_[1u];\n c23_ = unpack2x16float(_e165);\n let _e168 = texel1_[2u];\n c45_ = unpack2x16float(_e168);\n let _e171 = c01_[0u];\n let _e173 = c01_[1u];\n let _e175 = c23_[0u];\n let _e177 = c01_[1u];\n let _e179 = c23_[1u];\n let _e181 = c45_[0u];\n let _e183 = c23_[0u];\n let _e185 = c45_[0u];\n let _e187 = c45_[1u];\n sigma = mat3x3<f32>(vec3<f32>(_e171, _e173, _e175), vec3<f32>(_e177, _e179, _e181), vec3<f32>(_e183, _e185, _e187));\n let _e193 = unnamed.uModel;\n let _e194 = centre;\n world = (_e193 * vec4<f32>(_e194.x, _e194.y, _e194.z, 1f));\n let _e201 = unnamed.uView;\n let _e202 = world;\n view = (_e201 * _e202);\n let _e205 = unnamed.uProjection;\n let _e206 = view;\n clip = (_e205 * _e206);\n let _e209 = view[2u];\n let _e210 = (_e209 > -0.01f);\n phi_358_ = _e210;\n if !(_e210) {\n let _e213 = clip[0u];\n let _e216 = clip[3u];\n phi_358_ = (abs(_e213) > (_e216 * 1.3f));\n }\n let _e220 = phi_358_;\n phi_369_ = _e220;\n if !(_e220) {\n let _e223 = clip[1u];\n let _e226 = clip[3u];\n phi_369_ = (abs(_e223) > (_e226 * 1.3f));\n }\n let _e230 = phi_369_;\n if _e230 {\n unnamed_1.gl_Position = vec4<f32>(2f, 2f, 2f, 1f);\n vCorner = vec2<f32>(0f, 0f);\n return;\n }\n let _e235 = unnamed.uProjection[0][0u];\n let _e238 = unnamed.uViewport[0u];\n focalX = ((_e235 * _e238) * 0.5f);\n let _e244 = unnamed.uProjection[1][1u];\n let _e247 = unnamed.uViewport[1u];\n focalY = ((_e244 * _e247) * 0.5f);\n let _e251 = view[2u];\n invZ = (1f / _e251);\n let _e253 = invZ;\n let _e254 = invZ;\n invZ2_ = (_e253 * _e254);\n let _e256 = focalX;\n let _e257 = invZ;\n let _e259 = focalY;\n let _e260 = invZ;\n let _e262 = focalX;\n let _e265 = view[0u];\n let _e267 = invZ2_;\n let _e269 = focalY;\n let _e272 = view[1u];\n let _e274 = invZ2_;\n jacobian = mat3x2<f32>(vec2<f32>((_e256 * _e257), 0f), vec2<f32>(0f, (_e259 * _e260)), vec2<f32>(((-(_e262) * _e265) * _e267), ((-(_e269) * _e272) * _e274)));\n let _e281 = unnamed.uView;\n let _e283 = unnamed.uModel;\n let _e284 = (_e281 * _e283);\n rotation = mat3x3<f32>(_e284[0].xyz, _e284[1].xyz, _e284[2].xyz);\n let _e292 = rotation;\n let _e293 = sigma;\n let _e295 = rotation;\n world3_ = ((_e292 * _e293) * transpose(_e295));\n let _e298 = jacobian;\n let _e299 = world3_;\n let _e301 = jacobian;\n screen = ((_e298 * _e299) * transpose(_e301));\n let _e306 = screen[0][0u];\n screen[0][0u] = (_e306 + 0.3f);\n let _e312 = screen[1][1u];\n screen[1][1u] = (_e312 + 0.3f);\n let _e318 = screen[0][0u];\n a_1 = _e318;\n let _e321 = screen[0][1u];\n b_1 = _e321;\n let _e324 = screen[1][1u];\n d = _e324;\n let _e325 = a_1;\n let _e326 = d;\n mid = (0.5f * (_e325 + _e326));\n let _e329 = mid;\n let _e330 = mid;\n let _e332 = a_1;\n let _e333 = d;\n let _e335 = b_1;\n let _e336 = b_1;\n discriminant = sqrt(max(0.1f, ((_e329 * _e330) - ((_e332 * _e333) - (_e335 * _e336)))));\n let _e342 = mid;\n let _e343 = discriminant;\n lambda1_ = (_e342 + _e343);\n let _e345 = mid;\n let _e346 = discriminant;\n lambda2_ = (_e345 - _e346);\n let _e348 = lambda2_;\n if (_e348 <= 0f) {\n unnamed_1.gl_Position = vec4<f32>(2f, 2f, 2f, 1f);\n vCorner = vec2<f32>(0f, 0f);\n return;\n }\n let _e351 = b_1;\n let _e352 = lambda1_;\n let _e353 = a_1;\n major = normalize(vec2<f32>(_e351, (_e352 - _e353)));\n let _e358 = major[1u];\n let _e361 = major[0u];\n minor = vec2<f32>(-(_e358), _e361);\n let _e363 = lambda1_;\n radius1_ = min((2f * sqrt(_e363)), 512f);\n let _e367 = lambda2_;\n radius2_ = min((2f * sqrt(_e367)), 512f);\n offsets = array<vec2<f32>, 6>(vec2<f32>(-1f, -1f), vec2<f32>(1f, -1f), vec2<f32>(-1f, 1f), vec2<f32>(-1f, 1f), vec2<f32>(1f, -1f), vec2<f32>(1f, 1f));\n let _e371 = corner;\n let _e373 = offsets[_e371];\n unit = _e373;\n let _e374 = unit;\n vCorner = (_e374 * 2f);\n let _e377 = unit[0u];\n let _e378 = major;\n let _e380 = radius1_;\n let _e383 = unit[1u];\n let _e384 = minor;\n let _e386 = radius2_;\n offsetPx = (((_e378 * _e377) * _e380) + ((_e384 * _e383) * _e386));\n let _e389 = clip;\n let _e391 = offsetPx;\n let _e393 = unnamed.uViewport;\n let _e397 = clip[3u];\n let _e399 = (_e389.xy + (((_e391 / _e393) * 2f) * _e397));\n let _e401 = clip[2u];\n let _e403 = clip[3u];\n unnamed_1.gl_Position = vec4<f32>(_e399.x, _e399.y, _e401, _e403);\n return;\n}\n\n@vertex \nfn main(@builtin(vertex_index) gl_VertexIndex: u32) -> VertexOutput {\n gl_VertexIndex_1 = i32(gl_VertexIndex);\n main_1();\n let _e8 = unnamed_1.gl_Position.y;\n unnamed_1.gl_Position.y = -(_e8);\n let _e10 = unnamed_1.gl_Position;\n let _e11 = vCorner;\n let _e12 = vColor;\n return VertexOutput(_e10, _e11, _e12);\n}\n";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* What the transform assigned, so the renderer binds the same numbers.
|
|
14
|
+
*
|
|
15
|
+
* A permuted shader has one entry per variant, keyed as `FLAT_FRAG_WGSL` is.
|
|
16
|
+
*/
|
|
17
|
+
export const SPLAT_BINDINGS = {
|
|
18
|
+
"SPLAT_FRAG": {
|
|
19
|
+
"uniforms": 1,
|
|
20
|
+
"uniformSize": 16,
|
|
21
|
+
"fields": {
|
|
22
|
+
"uOutputTransform": {
|
|
23
|
+
"offset": 0,
|
|
24
|
+
"size": 4,
|
|
25
|
+
"type": "int"
|
|
26
|
+
},
|
|
27
|
+
"uOutputExposure": {
|
|
28
|
+
"offset": 4,
|
|
29
|
+
"size": 4,
|
|
30
|
+
"type": "float"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"textures": {}
|
|
34
|
+
},
|
|
35
|
+
"SPLAT_VERT": {
|
|
36
|
+
"uniforms": 0,
|
|
37
|
+
"uniformSize": 240,
|
|
38
|
+
"fields": {
|
|
39
|
+
"uSplatCount": {
|
|
40
|
+
"offset": 0,
|
|
41
|
+
"size": 4,
|
|
42
|
+
"type": "int"
|
|
43
|
+
},
|
|
44
|
+
"uSplatStride": {
|
|
45
|
+
"offset": 4,
|
|
46
|
+
"size": 4,
|
|
47
|
+
"type": "int"
|
|
48
|
+
},
|
|
49
|
+
"uSplatTexels": {
|
|
50
|
+
"offset": 8,
|
|
51
|
+
"size": 4,
|
|
52
|
+
"type": "int"
|
|
53
|
+
},
|
|
54
|
+
"uView": {
|
|
55
|
+
"offset": 16,
|
|
56
|
+
"size": 64,
|
|
57
|
+
"type": "mat4"
|
|
58
|
+
},
|
|
59
|
+
"uProjection": {
|
|
60
|
+
"offset": 80,
|
|
61
|
+
"size": 64,
|
|
62
|
+
"type": "mat4"
|
|
63
|
+
},
|
|
64
|
+
"uViewport": {
|
|
65
|
+
"offset": 144,
|
|
66
|
+
"size": 8,
|
|
67
|
+
"type": "vec2"
|
|
68
|
+
},
|
|
69
|
+
"uModel": {
|
|
70
|
+
"offset": 160,
|
|
71
|
+
"size": 64,
|
|
72
|
+
"type": "mat4"
|
|
73
|
+
},
|
|
74
|
+
"uSplatCameraLocal": {
|
|
75
|
+
"offset": 224,
|
|
76
|
+
"size": 12,
|
|
77
|
+
"type": "vec3"
|
|
78
|
+
},
|
|
79
|
+
"uSplatShDegree": {
|
|
80
|
+
"offset": 236,
|
|
81
|
+
"size": 4,
|
|
82
|
+
"type": "int"
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
"textures": {
|
|
86
|
+
"uSplatData": {
|
|
87
|
+
"texture": 16,
|
|
88
|
+
"sampler": 17,
|
|
89
|
+
"type": "usampler2D"
|
|
90
|
+
},
|
|
91
|
+
"uSplatOrder": {
|
|
92
|
+
"texture": 18,
|
|
93
|
+
"sampler": 19,
|
|
94
|
+
"type": "usampler2D"
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} as const;
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
/** The splat program: a screen-space ellipse per Gaussian, sorted far to near and blended over. */
|
|
2
|
+
|
|
3
|
+
import { OUTPUT_TRANSFORM_GLSL } from '@driftengine/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How far out the quad reaches, in standard deviations.
|
|
7
|
+
*
|
|
8
|
+
* Two sigma leaves `exp(-4)` — under two percent — at the corner, which is below what an eight-bit
|
|
9
|
+
* frame resolves. Three would be more correct and costs 2.25 times the fill for a contribution
|
|
10
|
+
* nothing can see; a splat cloud is fill-bound, so that is the whole argument.
|
|
11
|
+
*/
|
|
12
|
+
const SIGMA_REACH = 2.0;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The largest an ellipse's semi-axis may be, in pixels.
|
|
16
|
+
*
|
|
17
|
+
* **A guard against one splat costing the whole frame**, not a quality dial. A Gaussian seen
|
|
18
|
+
* nearly edge-on projects to an ellipse whose major axis grows without bound as the surface turns
|
|
19
|
+
* into the view, and one such splat covering the screen at a hundred fragments' worth of blending
|
|
20
|
+
* is a frame that drops. What it gives up is that a genuinely enormous splat is clipped rather
|
|
21
|
+
* than drawn; what would make it wrong is a capture authored at a scale where this is common,
|
|
22
|
+
* which is a capture whose splats are the size of the room.
|
|
23
|
+
*/
|
|
24
|
+
const MAX_RADIUS_PX = 512.0;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The low-pass added to both diagonal terms of the screen-space covariance.
|
|
28
|
+
*
|
|
29
|
+
* **A splat smaller than a pixel must not vanish, and this is what keeps it.** Below about one
|
|
30
|
+
* pixel the projected Gaussian falls between sample points and flickers as the camera moves —
|
|
31
|
+
* the same aliasing a mip chain solves for a texture, solved here by widening the lobe to the
|
|
32
|
+
* sampling rate. 0.3 is what every published implementation of this technique uses and it is a
|
|
33
|
+
* variance in pixels squared, so it is a little over half a pixel of standard deviation.
|
|
34
|
+
*
|
|
35
|
+
* What it costs: a genuinely sub-pixel splat reads slightly larger and slightly dimmer than it
|
|
36
|
+
* should, because widening without renormalising spreads the same alpha over more fragments.
|
|
37
|
+
* What would make it wrong is a capture rendered far above its authored resolution, where every
|
|
38
|
+
* splat is many pixels and this is a rounding error on all of them.
|
|
39
|
+
*/
|
|
40
|
+
const LOW_PASS = 0.3;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Common declarations: the two data textures, the camera, and the viewport.
|
|
44
|
+
*
|
|
45
|
+
* **Both stages read `uSplatData`**, which is why it is declared here rather than in the vertex
|
|
46
|
+
* stage alone — the fragment stage needs the colour, and passing it down as a varying would cost
|
|
47
|
+
* four interpolators against one integer fetch.
|
|
48
|
+
*/
|
|
49
|
+
const SHARED = `
|
|
50
|
+
/**
|
|
51
|
+
* Per-splat data, two texels each: position and colour, then the covariance.
|
|
52
|
+
*
|
|
53
|
+
* \`usampler2D\` and \`texelFetch\`, so there is no filtering and no float-texture extension
|
|
54
|
+
* involved — WebGL2 core is enough. Texel 0 is the three position floats reinterpreted as uints
|
|
55
|
+
* plus an RGBA8 colour; texel 1 is the six unique covariance terms as three half pairs.
|
|
56
|
+
*/
|
|
57
|
+
uniform highp usampler2D uSplatData;
|
|
58
|
+
/** The sorted order, one texel a splat. A new order re-uploads four bytes each and nothing else. */
|
|
59
|
+
uniform highp usampler2D uSplatOrder;
|
|
60
|
+
/** How many splats are in the order, so a budget can draw a prefix of it. */
|
|
61
|
+
uniform int uSplatCount;
|
|
62
|
+
/** Splats per row in both textures. A power of two, so the divide is a shift. */
|
|
63
|
+
uniform int uSplatStride;
|
|
64
|
+
/**
|
|
65
|
+
* Texels one splat occupies: two, or three where the capture carries view-dependent colour.
|
|
66
|
+
*
|
|
67
|
+
* **A uniform rather than a constant**, because it is the *file's* width — a SPLT block carries
|
|
68
|
+
* its own wordsPerSplat. A shader that assumed two would read a three-texel capture's second
|
|
69
|
+
* splat at the first one's covariance, which is a cloud of noise rather than a subtle error.
|
|
70
|
+
*/
|
|
71
|
+
uniform int uSplatTexels;
|
|
72
|
+
|
|
73
|
+
uniform mat4 uView;
|
|
74
|
+
uniform mat4 uProjection;
|
|
75
|
+
/** Pixels, so the ellipse can be built in pixel space and converted once. */
|
|
76
|
+
uniform vec2 uViewport;
|
|
77
|
+
/** The capture's own transform, so two captures compose in one scene. */
|
|
78
|
+
uniform mat4 uModel;
|
|
79
|
+
`;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The vertex stage: one ellipse per splat, six vertices, no buffers at all.
|
|
83
|
+
*
|
|
84
|
+
* **Nothing is bound but textures.** The geometry is `gl_VertexID` arithmetic — `id / 6` is the
|
|
85
|
+
* splat and `id % 6` is the corner — so this pass owns no vertex buffer and no vertex array on
|
|
86
|
+
* either backend, and the two compute the same geometry from the same one number. `demo/
|
|
87
|
+
* contributedPass.ts` set that precedent and stated the reason.
|
|
88
|
+
*
|
|
89
|
+
* **What it costs: six vertex invocations a splat rather than the four an indexed quad issues.**
|
|
90
|
+
* At a million splats that is two million extra runs of the arithmetic below. The alternative is a
|
|
91
|
+
* static index buffer at 24 MB per million splats, which is why it is not the starting point.
|
|
92
|
+
*/
|
|
93
|
+
export const SPLAT_VERT = `#version 300 es
|
|
94
|
+
precision highp float;
|
|
95
|
+
precision highp int;
|
|
96
|
+
${SHARED}
|
|
97
|
+
|
|
98
|
+
out vec2 vCorner;
|
|
99
|
+
out vec4 vColor;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The l=1 spherical-harmonic band constant, 0.5 * sqrt(3 / pi).
|
|
103
|
+
*
|
|
104
|
+
* The band-0 constant is the *reader's* — splatPly.ts folds it into the colour, because the DC
|
|
105
|
+
* term is a colour once it has been applied. This one belongs here, because the term it scales is
|
|
106
|
+
* a function of the view direction and cannot be folded into anything.
|
|
107
|
+
*/
|
|
108
|
+
const float SH_C1 = 0.4886025119029199;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Where the camera is, in the **capture's own space**.
|
|
112
|
+
*
|
|
113
|
+
* Not world space, and the difference is the whole of what makes this correct: the coefficients
|
|
114
|
+
* were trained in the capture's frame, so a capture placed into a scene by uModel — turned,
|
|
115
|
+
* moved, scaled — must have its view direction expressed in the frame the coefficients know. The
|
|
116
|
+
* inverse of uView * uModel is one CPU matrix a frame; doing it here would be one per vertex.
|
|
117
|
+
*/
|
|
118
|
+
uniform vec3 uSplatCameraLocal;
|
|
119
|
+
/** 0 for a capture with no harmonics, 1 for the l=1 band. The whole of the off path. */
|
|
120
|
+
uniform int uSplatShDegree;
|
|
121
|
+
|
|
122
|
+
/*
|
|
123
|
+
* **Returns \`uvec4\`, and going through \`vec4\` was a real bug.** A position is a float's bits
|
|
124
|
+
* reinterpreted as a uint — 0x3F800000 is 1,065,353,216 — and float32 carries 24 bits of mantissa,
|
|
125
|
+
* so a round trip through it quantises the low bits of every coordinate. The picture that produces
|
|
126
|
+
* is a capture whose splats are all *nearly* in the right place, which reads as a bad export.
|
|
127
|
+
*/
|
|
128
|
+
uvec4 fetchTexel(int splat, int which) {
|
|
129
|
+
int at = splat * uSplatTexels + which;
|
|
130
|
+
int width = uSplatStride * uSplatTexels;
|
|
131
|
+
return texelFetch(uSplatData, ivec2(at % width, at / width), 0);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/*
|
|
135
|
+
* **RGBA8 out of a uint by hand, because \`unpackUnorm4x8\` is GLSL ES 3.10 and this is 3.00.**
|
|
136
|
+
*
|
|
137
|
+
* It compiles through the generator, which raises the version to 310 before handing the source to
|
|
138
|
+
* glslang — so a 3.10 builtin passes generation and fails at runtime on **WebGL2**, the backend
|
|
139
|
+
* with no fallback beneath it. That inverts the safety property the whole
|
|
140
|
+
* authored-in-GLSL-generated-to-WGSL arrangement exists for, and it is invisible until a browser
|
|
141
|
+
* compiles the shader: the WGSL was correct the whole time.
|
|
142
|
+
*/
|
|
143
|
+
vec4 unpackRgba8(uint packed) {
|
|
144
|
+
return vec4(
|
|
145
|
+
float(packed & 0xFFu),
|
|
146
|
+
float((packed >> 8) & 0xFFu),
|
|
147
|
+
float((packed >> 16) & 0xFFu),
|
|
148
|
+
float((packed >> 24) & 0xFFu)
|
|
149
|
+
) / 255.0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The view-dependent term of one splat's colour, from the texel packSh1 wrote.
|
|
154
|
+
*
|
|
155
|
+
* **The evaluation the reference implementation of this technique uses, in its own sign
|
|
156
|
+
* convention**: −C1·y·c0 + C1·z·c1 − C1·x·c2, with the direction pointing *from* the camera
|
|
157
|
+
* *to* the splat. Reproducing that sign pattern from first principles is where a reimplementation
|
|
158
|
+
* goes wrong, and the failure is a capture whose sheen sits on the wrong side of every surface —
|
|
159
|
+
* plausible from any one angle and unmistakable as soon as the camera moves.
|
|
160
|
+
*
|
|
161
|
+
* Nine bytes and a scale, which packSh1 explains: a per-splat scale spends the sixteenth byte on
|
|
162
|
+
* giving every splat the whole 8-bit range for its own coefficients.
|
|
163
|
+
*/
|
|
164
|
+
vec3 viewDependentColour(uvec4 sh, vec3 direction) {
|
|
165
|
+
float scale = uintBitsToFloat(sh.w);
|
|
166
|
+
if (scale == 0.0) return vec3(0.0);
|
|
167
|
+
vec4 a = unpackRgba8(sh.x);
|
|
168
|
+
vec4 b = unpackRgba8(sh.y);
|
|
169
|
+
vec4 c = unpackRgba8(sh.z);
|
|
170
|
+
/* (byte / 255 * 255 − 128) / 127 undoes the quantisation exactly at the centre. */
|
|
171
|
+
vec3 c0 = (vec3(a.x, a.y, a.z) * 255.0 - 128.0) / 127.0 * scale;
|
|
172
|
+
vec3 c1 = (vec3(a.w, b.x, b.y) * 255.0 - 128.0) / 127.0 * scale;
|
|
173
|
+
vec3 c2 = (vec3(b.z, b.w, c.x) * 255.0 - 128.0) / 127.0 * scale;
|
|
174
|
+
return SH_C1 * (-direction.y * c0 + direction.z * c1 - direction.x * c2);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
void main() {
|
|
178
|
+
int vertex = gl_VertexID;
|
|
179
|
+
int slot = vertex / 6;
|
|
180
|
+
int corner = vertex % 6;
|
|
181
|
+
|
|
182
|
+
/*
|
|
183
|
+
* Off the end of the order: emit a degenerate triangle rather than clamping to the last splat,
|
|
184
|
+
* which would draw it several times over and read as a bright speck that moves with the budget.
|
|
185
|
+
*/
|
|
186
|
+
if (slot >= uSplatCount) {
|
|
187
|
+
gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
|
|
188
|
+
vCorner = vec2(0.0);
|
|
189
|
+
vColor = vec4(0.0);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
int splat = int(texelFetch(uSplatOrder, ivec2(slot % uSplatStride, slot / uSplatStride), 0).r);
|
|
194
|
+
|
|
195
|
+
uvec4 texel0 = fetchTexel(splat, 0);
|
|
196
|
+
uvec4 texel1 = fetchTexel(splat, 1);
|
|
197
|
+
|
|
198
|
+
vec3 centre = uintBitsToFloat(texel0.xyz);
|
|
199
|
+
vColor = unpackRgba8(texel0.w);
|
|
200
|
+
|
|
201
|
+
/*
|
|
202
|
+
* View-dependent colour, added to the DC term the reader already folded into the RGBA8.
|
|
203
|
+
*
|
|
204
|
+
* A uniform gate, so a capture without the band performs no fetch at all — this is provably
|
|
205
|
+
* uniform control flow and not the 2026-08-07 case. Clamped at zero from below because a
|
|
206
|
+
* negative radiance is not a colour and the reference implementation clamps in the same place;
|
|
207
|
+
* **not clamped above**, because the composite downstream is where a range is resolved and
|
|
208
|
+
* clipping here would throw away exactly the highlight this band exists to produce.
|
|
209
|
+
*/
|
|
210
|
+
if (uSplatShDegree > 0) {
|
|
211
|
+
vec3 direction = normalize(centre - uSplatCameraLocal);
|
|
212
|
+
vColor.rgb = max(vColor.rgb + viewDependentColour(fetchTexel(splat, 2), direction), vec3(0.0));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
vec2 c01 = unpackHalf2x16(texel1.x);
|
|
216
|
+
vec2 c23 = unpackHalf2x16(texel1.y);
|
|
217
|
+
vec2 c45 = unpackHalf2x16(texel1.z);
|
|
218
|
+
/* xx xy xz yy yz zz, the order packSplats writes. */
|
|
219
|
+
mat3 sigma = mat3(
|
|
220
|
+
c01.x, c01.y, c23.x,
|
|
221
|
+
c01.y, c23.y, c45.x,
|
|
222
|
+
c23.x, c45.x, c45.y
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
vec4 world = uModel * vec4(centre, 1.0);
|
|
226
|
+
vec4 view = uView * world;
|
|
227
|
+
|
|
228
|
+
/*
|
|
229
|
+
* Behind the near plane, or far outside the frustum: degenerate. A guard band rather than the
|
|
230
|
+
* exact frustum, because a splat whose centre is just outside still has an ellipse reaching in.
|
|
231
|
+
*/
|
|
232
|
+
vec4 clip = uProjection * view;
|
|
233
|
+
if (view.z > -0.01 || abs(clip.x) > clip.w * 1.3 || abs(clip.y) > clip.w * 1.3) {
|
|
234
|
+
gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
|
|
235
|
+
vCorner = vec2(0.0);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/*
|
|
240
|
+
* The 2x3 Jacobian of the perspective projection at this point, in pixels.
|
|
241
|
+
*
|
|
242
|
+
* A perspective divide is not linear, so a 3D Gaussian does not project to a 2D one — it
|
|
243
|
+
* projects to something with no closed form. The standard treatment is to linearise about the
|
|
244
|
+
* splat's centre, which is exact at the centre and increasingly wrong toward the edges of the
|
|
245
|
+
* frame. **What would make it wrong** is a very wide field of view, where a splat at the corner
|
|
246
|
+
* is measurably sheared; at ordinary angles the error is far below a pixel.
|
|
247
|
+
*/
|
|
248
|
+
float focalX = uProjection[0][0] * uViewport.x * 0.5;
|
|
249
|
+
float focalY = uProjection[1][1] * uViewport.y * 0.5;
|
|
250
|
+
float invZ = 1.0 / view.z;
|
|
251
|
+
float invZ2 = invZ * invZ;
|
|
252
|
+
mat3x2 jacobian = mat3x2(
|
|
253
|
+
focalX * invZ, 0.0,
|
|
254
|
+
0.0, focalY * invZ,
|
|
255
|
+
-focalX * view.x * invZ2, -focalY * view.y * invZ2
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
/* Sigma' = J W Sigma W^T J^T, with W the view rotation and the model's. */
|
|
259
|
+
mat3 rotation = mat3(uView * uModel);
|
|
260
|
+
mat3 world3 = rotation * sigma * transpose(rotation);
|
|
261
|
+
mat2 screen = jacobian * world3 * transpose(jacobian);
|
|
262
|
+
|
|
263
|
+
/* The low-pass, on the variances only. See LOW_PASS. */
|
|
264
|
+
screen[0][0] += ${LOW_PASS.toFixed(1)};
|
|
265
|
+
screen[1][1] += ${LOW_PASS.toFixed(1)};
|
|
266
|
+
|
|
267
|
+
/*
|
|
268
|
+
* Eigen-decompose the symmetric 2x2 in closed form: the axes of the ellipse are its
|
|
269
|
+
* eigenvectors and the semi-axes are the square roots of its eigenvalues.
|
|
270
|
+
*/
|
|
271
|
+
float a = screen[0][0];
|
|
272
|
+
float b = screen[0][1];
|
|
273
|
+
float d = screen[1][1];
|
|
274
|
+
float mid = 0.5 * (a + d);
|
|
275
|
+
float discriminant = sqrt(max(0.1, mid * mid - (a * d - b * b)));
|
|
276
|
+
float lambda1 = mid + discriminant;
|
|
277
|
+
float lambda2 = mid - discriminant;
|
|
278
|
+
|
|
279
|
+
/* A non-positive minor eigenvalue is a degenerate ellipse: nothing to draw. */
|
|
280
|
+
if (lambda2 <= 0.0) {
|
|
281
|
+
gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
|
|
282
|
+
vCorner = vec2(0.0);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
vec2 major = normalize(vec2(b, lambda1 - a));
|
|
287
|
+
/* Perpendicular by construction rather than by a second normalize, which would be the same
|
|
288
|
+
vector computed twice and could disagree by an ulp at a degenerate b. */
|
|
289
|
+
vec2 minor = vec2(-major.y, major.x);
|
|
290
|
+
float radius1 = min(${SIGMA_REACH.toFixed(1)} * sqrt(lambda1), ${MAX_RADIUS_PX.toFixed(1)});
|
|
291
|
+
float radius2 = min(${SIGMA_REACH.toFixed(1)} * sqrt(lambda2), ${MAX_RADIUS_PX.toFixed(1)});
|
|
292
|
+
|
|
293
|
+
/* Two triangles over the unit square, in the corner order the fragment stage expects. */
|
|
294
|
+
vec2 offsets[6] = vec2[6](
|
|
295
|
+
vec2(-1.0, -1.0), vec2(1.0, -1.0), vec2(-1.0, 1.0),
|
|
296
|
+
vec2(-1.0, 1.0), vec2(1.0, -1.0), vec2(1.0, 1.0)
|
|
297
|
+
);
|
|
298
|
+
vec2 unit = offsets[corner];
|
|
299
|
+
vCorner = unit * ${SIGMA_REACH.toFixed(1)};
|
|
300
|
+
|
|
301
|
+
vec2 offsetPx = unit.x * major * radius1 + unit.y * minor * radius2;
|
|
302
|
+
/* Pixels to clip space: the ellipse was built in pixels, so this is the only conversion. */
|
|
303
|
+
gl_Position = vec4(
|
|
304
|
+
clip.xy + offsetPx / uViewport * 2.0 * clip.w,
|
|
305
|
+
clip.z,
|
|
306
|
+
clip.w
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
`;
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* The fragment stage: the Gaussian's own falloff, premultiplied.
|
|
313
|
+
*
|
|
314
|
+
* **Premultiplied, and that one line is the difference between composing and not.** The pass
|
|
315
|
+
* blends `ONE, ONE_MINUS_SRC_ALPHA` over a scene target that already holds opaque geometry, so
|
|
316
|
+
* the source must arrive with its alpha already folded in. Emitting straight colour there
|
|
317
|
+
* double-counts the alpha and produces a cloud that is too bright at every silhouette — which
|
|
318
|
+
* reads as a exposure problem rather than as a blend-mode one, and is the failure this plan most
|
|
319
|
+
* expects.
|
|
320
|
+
*/
|
|
321
|
+
export const SPLAT_FRAG = `#version 300 es
|
|
322
|
+
precision highp float;
|
|
323
|
+
precision highp int;
|
|
324
|
+
${OUTPUT_TRANSFORM_GLSL}
|
|
325
|
+
|
|
326
|
+
in vec2 vCorner;
|
|
327
|
+
in vec4 vColor;
|
|
328
|
+
out vec4 fragColor;
|
|
329
|
+
|
|
330
|
+
void main() {
|
|
331
|
+
/* The Gaussian, in units where the quad's corner is SIGMA_REACH standard deviations out. */
|
|
332
|
+
float power = dot(vCorner, vCorner);
|
|
333
|
+
float alpha = vColor.a * exp(-0.5 * power);
|
|
334
|
+
|
|
335
|
+
/*
|
|
336
|
+
* Below what an eight-bit frame resolves. Discarding rather than blending nothing saves the
|
|
337
|
+
* read-modify-write on the tail of every splat, which on a fill-bound pass is most of them.
|
|
338
|
+
*/
|
|
339
|
+
if (alpha < 0.00392156862745098) discard;
|
|
340
|
+
|
|
341
|
+
vec3 colour = applyOutputTransform(vColor.rgb);
|
|
342
|
+
fragColor = vec4(colour * alpha, alpha);
|
|
343
|
+
}
|
|
344
|
+
`;
|
package/src/splat.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/** The `.splat` reader: thirty-two bytes a splat, and nothing else in the file. */
|
|
2
|
+
|
|
3
|
+
import { packSplats } from './splatData.ts';
|
|
4
|
+
import type { SplatData } from './splatData.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Position, scale, colour and rotation, in that order, little-endian.
|
|
8
|
+
*
|
|
9
|
+
* There is no header and no version. The format is a bare array of records, which is what makes
|
|
10
|
+
* the length check below the only structural validation available.
|
|
11
|
+
*/
|
|
12
|
+
export const SPLAT_RECORD_BYTES = 32;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Read a `.splat` capture.
|
|
16
|
+
*
|
|
17
|
+
* **The one thing worth knowing is the quaternion's encoding.** Rotation arrives as four bytes and
|
|
18
|
+
* comes back as `(b - 128) / 128`, so a byte of 128 is zero and the representable range is roughly
|
|
19
|
+
* −1 to +0.9922. `packSplats` normalises what it is handed, which is what makes that asymmetry
|
|
20
|
+
* harmless: the quantised quaternion is a hair off unit length and the normalisation absorbs it.
|
|
21
|
+
*
|
|
22
|
+
* **The file stores wxyz and `SplatSource` takes xyzw**, so the reorder happens here, in the one
|
|
23
|
+
* place the file's own order is on screen. Getting it wrong does not produce a refusal — it
|
|
24
|
+
* produces a capture whose every splat is rotated to somewhere else, which reads as a smear.
|
|
25
|
+
*
|
|
26
|
+
* **Colour is already eight-bit and already linear**, so it divides by 255 and nothing else: there
|
|
27
|
+
* is no logistic and no exponential here, unlike the `.ply` reader. That asymmetry is the format's
|
|
28
|
+
* rather than a choice, and `packSplats` takes linear values from both.
|
|
29
|
+
*/
|
|
30
|
+
export function readSplat(buffer: ArrayBuffer): SplatData {
|
|
31
|
+
const remainder = buffer.byteLength % SPLAT_RECORD_BYTES;
|
|
32
|
+
if (remainder !== 0) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`a .splat file is a whole number of ${SPLAT_RECORD_BYTES}-byte records; this one is ` +
|
|
35
|
+
`${buffer.byteLength} bytes, which leaves ${remainder} over. Either it is truncated or it ` +
|
|
36
|
+
'is not a .splat.',
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const count = buffer.byteLength / SPLAT_RECORD_BYTES;
|
|
41
|
+
const view = new DataView(buffer);
|
|
42
|
+
const bytes = new Uint8Array(buffer);
|
|
43
|
+
|
|
44
|
+
const positions = new Float32Array(count * 3);
|
|
45
|
+
const scales = new Float32Array(count * 3);
|
|
46
|
+
const rotations = new Float32Array(count * 4);
|
|
47
|
+
const colors = new Float32Array(count * 3);
|
|
48
|
+
const opacities = new Float32Array(count);
|
|
49
|
+
|
|
50
|
+
for (let index = 0; index < count; index++) {
|
|
51
|
+
const at = index * SPLAT_RECORD_BYTES;
|
|
52
|
+
const p = index * 3;
|
|
53
|
+
positions[p] = view.getFloat32(at, true);
|
|
54
|
+
positions[p + 1] = view.getFloat32(at + 4, true);
|
|
55
|
+
positions[p + 2] = view.getFloat32(at + 8, true);
|
|
56
|
+
/* Linear already: this format stores the standard deviation, not its logarithm. */
|
|
57
|
+
scales[p] = view.getFloat32(at + 12, true);
|
|
58
|
+
scales[p + 1] = view.getFloat32(at + 16, true);
|
|
59
|
+
scales[p + 2] = view.getFloat32(at + 20, true);
|
|
60
|
+
|
|
61
|
+
colors[p] = (bytes[at + 24] ?? 0) / 255;
|
|
62
|
+
colors[p + 1] = (bytes[at + 25] ?? 0) / 255;
|
|
63
|
+
colors[p + 2] = (bytes[at + 26] ?? 0) / 255;
|
|
64
|
+
opacities[index] = (bytes[at + 27] ?? 0) / 255;
|
|
65
|
+
|
|
66
|
+
/* wxyz in the file, xyzw in `SplatSource`. See the header. */
|
|
67
|
+
const r = index * 4;
|
|
68
|
+
rotations[r] = ((bytes[at + 29] ?? 128) - 128) / 128;
|
|
69
|
+
rotations[r + 1] = ((bytes[at + 30] ?? 128) - 128) / 128;
|
|
70
|
+
rotations[r + 2] = ((bytes[at + 31] ?? 128) - 128) / 128;
|
|
71
|
+
rotations[r + 3] = ((bytes[at + 28] ?? 128) - 128) / 128;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return packSplats({ count, positions, scales, rotations, colors, opacities });
|
|
75
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** How many splats a device should be asked to draw, before anybody has measured that device. */
|
|
2
|
+
|
|
3
|
+
import { isWeakGpuFamily } from '@driftengine/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The count a part this engine has no evidence against is asked to draw.
|
|
7
|
+
*
|
|
8
|
+
* **This is an estimate and it is not measured, which is the first thing to know about it.**
|
|
9
|
+
* `AGENTS.md` says fidelity is cut on a measurement and not on a fear, and the measurement that
|
|
10
|
+
* settles this one is a frame time from a real mid-range Android phone drawing a real capture —
|
|
11
|
+
* which this repository does not have. What is here instead is the arithmetic: a splat pass is
|
|
12
|
+
* fill-bound, a splat at ±2σ covers a few dozen blended fragments, and a mid-range mobile part has
|
|
13
|
+
* something under half a gigapixel a frame of blended fill at 60 Hz. Four hundred thousand splats
|
|
14
|
+
* is what that budget divides into, and it is the range published mobile viewers of this technique
|
|
15
|
+
* report.
|
|
16
|
+
*
|
|
17
|
+
* **What it costs if it is too low** is a capture thinner than the device could have drawn, which
|
|
18
|
+
* is visible as a sparser cloud and nothing worse. **What would make it wrong** is a measurement,
|
|
19
|
+
* in either direction, and a consumer holding one should pass its own number rather than wait for
|
|
20
|
+
* this line to be corrected. `docs/IMPROVEMENTS.md` carries it as an open measurement.
|
|
21
|
+
*/
|
|
22
|
+
export const SPLAT_BUDGET_DEFAULT = 400_000;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The count a part measured to struggle with the good profile is asked to draw.
|
|
26
|
+
*
|
|
27
|
+
* The same estimate against the family `isWeakGpuFamily` names — an Adreno 619 measured at 156 ms
|
|
28
|
+
* a frame on this engine's desktop profile, an Adreno 710 at 112 ms, a UHD 630 unplayable
|
|
29
|
+
* fullscreen. Those are three to ten times the frame time the default is reasoned against, so this
|
|
30
|
+
* is the default cut by that factor rather than a second guess.
|
|
31
|
+
*/
|
|
32
|
+
export const SPLAT_BUDGET_WEAK = 120_000;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The budget to hand `SplatSorter` when the consumer has no number of its own.
|
|
36
|
+
*
|
|
37
|
+
* Takes the renderer's own name — `renderer.rendererName`, which every backend answers — rather
|
|
38
|
+
* than reaching for an adapter itself, because `AGENTS.md`'s rule is that a package takes a
|
|
39
|
+
* platform capability as a parameter and never queries one.
|
|
40
|
+
*
|
|
41
|
+
* **An unknown part is not weak**, which is `isWeakGpuFamily`'s own decision and the reason it is
|
|
42
|
+
* reused rather than re-tabulated here: a device table is wrong about every GPU nobody has tested,
|
|
43
|
+
* and guessing weak on the unknown thins a capture for every part released after this line.
|
|
44
|
+
*/
|
|
45
|
+
export function defaultSplatBudget(rendererName?: string): number {
|
|
46
|
+
if (rendererName !== undefined && isWeakGpuFamily(rendererName)) return SPLAT_BUDGET_WEAK;
|
|
47
|
+
return SPLAT_BUDGET_DEFAULT;
|
|
48
|
+
}
|