-
Notifications
You must be signed in to change notification settings - Fork 330
Add initial SOG format support #478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
querielo
wants to merge
2
commits into
mkkellogg:main
Choose a base branch
from
querielo:kirill/sog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+372
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,5 +2,6 @@ export const SceneFormat = { | |
| 'Splat': 0, | ||
| 'KSplat': 1, | ||
| 'Ply': 2, | ||
| 'Spz': 3 | ||
| 'Spz': 3, | ||
| 'Sog': 4 | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import * as THREE from 'three'; | ||
| import { SogParser } from './SogParser.js'; | ||
| import { SplatBuffer } from '../SplatBuffer.js'; | ||
| import { SplatBufferGenerator } from '../SplatBufferGenerator.js'; | ||
| // Note: progress utilities available, but not used here to keep loader minimal | ||
| import { unzipStoredEntries } from './ZipReaderBrowser.js'; | ||
| import { LoaderStatus } from '../LoaderStatus.js'; | ||
|
|
||
| async function fetchJSON(url, headers) { | ||
| const resp = await fetch(url, { headers }); | ||
| if (!resp.ok) throw new Error(`Failed to fetch ${url}: ${resp.status} ${resp.statusText}`); | ||
| return resp.json(); | ||
| } | ||
|
|
||
| function finalize(splatArray, optimizeSplatData, minimumAlpha, compressionLevel, sectionSize, sceneCenter, blockSize, bucketSize) { | ||
| if (optimizeSplatData) { | ||
| const gen = SplatBufferGenerator.getStandardGenerator( | ||
| minimumAlpha, compressionLevel, sectionSize, sceneCenter, blockSize, bucketSize | ||
| ); | ||
| return gen.generateFromUncompressedSplatArray(splatArray); | ||
| } else { | ||
| return SplatBuffer.generateFromUncompressedSplatArrays([splatArray], minimumAlpha, 0, new THREE.Vector3()); | ||
| } | ||
| } | ||
|
|
||
| export class SogLoader { | ||
| // Multi-file SOG directory: baseURL ends with '/'; expects meta.json and the image files next to it | ||
| static async loadFromDirectoryURL(baseURL, onProgress, minimumAlpha, compressionLevel, | ||
| optimizeSplatData = true, headers, sectionSize, sceneCenter, blockSize, bucketSize) { | ||
| const isMeta = baseURL.toLowerCase().endsWith('meta.json'); | ||
| const dir = isMeta ? baseURL.slice(0, baseURL.lastIndexOf('/') + 1) : (baseURL.endsWith('/') ? baseURL : (baseURL + '/')); | ||
| const metaURL = isMeta ? baseURL : (dir + 'meta.json'); | ||
| if (onProgress) onProgress(0, '0%', LoaderStatus.Downloading); | ||
| const meta = await fetchJSON(metaURL, headers); | ||
| if (onProgress) onProgress(0, '0%', LoaderStatus.Processing); | ||
| const splatArray = await SogParser.parse(meta, (name) => dir + name); | ||
| const buffer = finalize( | ||
| splatArray, optimizeSplatData, minimumAlpha, compressionLevel, | ||
| sectionSize, sceneCenter, blockSize, bucketSize | ||
| ); | ||
| if (onProgress) onProgress(100, '100%', LoaderStatus.Done); | ||
| return buffer; | ||
| } | ||
|
|
||
| // Bundled .sog: a ZIP whose root contains meta.json and files it references | ||
| static async loadFromZipURL(fileURL, onProgress, minimumAlpha, compressionLevel, | ||
| optimizeSplatData = true, headers, sectionSize, sceneCenter, blockSize, bucketSize) { | ||
| if (onProgress) onProgress(0, '0%', LoaderStatus.Downloading); | ||
| const resp = await fetch(fileURL, { headers }); | ||
| if (!resp.ok) throw new Error(`Failed to fetch ${fileURL}: ${resp.status} ${resp.statusText}`); | ||
| const arrayBuffer = await resp.arrayBuffer(); | ||
| if (onProgress) onProgress(0, '0%', LoaderStatus.Processing); | ||
| const entries = unzipStoredEntries(arrayBuffer); | ||
| const metaBytes = entries.get('meta.json'); | ||
| if (!metaBytes) throw new Error('SOG archive missing meta.json at root'); | ||
| const meta = JSON.parse(new TextDecoder().decode(metaBytes)); | ||
|
|
||
| const resolver = (name) => { | ||
| const bytes = entries.get(name); | ||
| if (!bytes) throw new Error(`SOG archive missing file: ${name}`); | ||
| return new Blob([bytes], { type: 'image/webp' }); | ||
| }; | ||
| const splatArray = await SogParser.parse(meta, resolver); | ||
| const buffer = finalize( | ||
| splatArray, optimizeSplatData, minimumAlpha, compressionLevel, | ||
| sectionSize, sceneCenter, blockSize, bucketSize | ||
| ); | ||
| if (onProgress) onProgress(100, '100%', LoaderStatus.Done); | ||
| return buffer; | ||
| } | ||
|
|
||
| static async loadFromFileHandles(metaJSON, fileResolver, onProgress, minimumAlpha, compressionLevel, | ||
| optimizeSplatData = true, sectionSize, sceneCenter, blockSize, bucketSize) { | ||
| // metaJSON is already-parsed meta; fileResolver(name) -> Blob or URL | ||
| if (onProgress) onProgress(0, '0%', LoaderStatus.Processing); | ||
| const splatArray = await SogParser.parse(metaJSON, fileResolver); | ||
| const buffer = finalize( | ||
| splatArray, optimizeSplatData, minimumAlpha, compressionLevel, | ||
| sectionSize, sceneCenter, blockSize, bucketSize | ||
| ); | ||
| if (onProgress) onProgress(100, '100%', LoaderStatus.Done); | ||
| return buffer; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import * as THREE from 'three'; | ||
| import { UncompressedSplatArray } from '../UncompressedSplatArray.js'; | ||
|
|
||
| async function loadImagePixels(src, typeHint) { | ||
| let blob; | ||
| if (src instanceof Blob) { | ||
| blob = src; | ||
| } else if (typeof src === 'string') { | ||
| const resp = await fetch(src); | ||
| blob = await resp.blob(); | ||
| } else { | ||
| throw new Error('Unsupported image source'); | ||
| } | ||
|
|
||
| try { | ||
| if (typeof ImageDecoder !== 'undefined') { | ||
| const decoder = new ImageDecoder({ data: blob, type: blob.type || typeHint || 'image/webp' }); | ||
| const { image } = await decoder.decode(); | ||
| const width = image.displayWidth || image.codedWidth; | ||
| const height = image.displayHeight || image.codedHeight; | ||
| const data = new Uint8ClampedArray(width * height * 4); | ||
| await image.copyTo(data, { format: 'RGBA' }); | ||
| image.close(); | ||
| return { data, width, height }; | ||
| } | ||
| } catch (e) {} | ||
|
|
||
| const bitmap = await createImageBitmap(blob); | ||
| const width = bitmap.width; | ||
| const height = bitmap.height; | ||
| let ctx; | ||
| let canvas; | ||
| if (typeof OffscreenCanvas !== 'undefined') { | ||
| canvas = new OffscreenCanvas(width, height); | ||
| ctx = canvas.getContext('2d'); | ||
| } else { | ||
| canvas = document.createElement('canvas'); | ||
| canvas.width = width; canvas.height = height; | ||
| ctx = canvas.getContext('2d'); | ||
| } | ||
| ctx.drawImage(bitmap, 0, 0); | ||
| const imageData = ctx.getImageData(0, 0, width, height); | ||
| return { data: imageData.data, width, height }; | ||
| } | ||
|
|
||
| function lerp(a, b, t) { | ||
| return a + (b - a) * t; | ||
| } | ||
| function unlog(n) { | ||
| return Math.sign(n) * (Math.exp(Math.abs(n)) - 1); | ||
| } | ||
|
|
||
| function reconstructQuaternion(r, g, b, a) { | ||
| const comp = (c) => (c / 255 - 0.5) * 2.0 / Math.SQRT2; | ||
| const A = comp(r); | ||
| const B = comp(g); | ||
| const C = comp(b); | ||
| const mode = a - 252; | ||
| const t = A*A + B*B + C*C; | ||
| const D = Math.sqrt(Math.max(0, 1 - t)); | ||
| let qx; | ||
| let qy; | ||
| let qz; | ||
| let qw; | ||
| switch (mode) { | ||
| case 0: | ||
| qx = D; qy = A; qz = B; qw = C; break; | ||
| case 1: | ||
| qx = A; qy = D; qz = B; qw = C; break; | ||
| case 2: | ||
| qx = A; qy = B; qz = D; qw = C; break; | ||
| case 3: | ||
| qx = A; qy = B; qz = C; qw = D; break; | ||
| default: throw new Error('Invalid quaternion mode'); | ||
| } | ||
| const q = new THREE.Quaternion(qx, qy, qz, qw); | ||
| if (q.w < 0) { | ||
| q.x = -q.x; | ||
| q.y = -q.y; | ||
| q.z = -q.z; | ||
| q.w = -q.w; | ||
| } | ||
| return q.normalize(); | ||
| } | ||
|
|
||
| export class SogParser { | ||
| static async parse(meta, baseURLOrResolver) { | ||
| const resolve = async (name) => { | ||
| const url = typeof baseURLOrResolver === 'function' ? await baseURLOrResolver(name) : `${baseURLOrResolver}${name}`; | ||
| return loadImagePixels(url); | ||
| }; | ||
|
|
||
| const [meansL, meansU, quats, scalesImg, sh0Img] = await Promise.all([ | ||
| resolve(meta.means.files[0]), | ||
| resolve(meta.means.files[1]), | ||
| resolve(meta.quats.files[0]), | ||
| resolve(meta.scales.files[0]), | ||
| resolve(meta.sh0.files[0]) | ||
| ]); | ||
|
|
||
| const width = meansL.width; | ||
| const height = meansL.height; | ||
| const capacity = width * height; | ||
| const count = Math.min(meta.count, capacity); | ||
|
|
||
| let degree = 0; | ||
| let shNCoeffsTotal = 0; | ||
| let shNCoeffsWanted = 0; | ||
| if (meta.shN && meta.shN.bands) { | ||
| const bands = meta.shN.bands; | ||
| degree = Math.min(bands, 2); | ||
| shNCoeffsTotal = [0, 3, 8, 15][bands]; | ||
| shNCoeffsWanted = [0, 3, 8][degree]; | ||
| } | ||
| const splats = new UncompressedSplatArray(degree); | ||
|
|
||
| const mins = meta.means.mins; | ||
| const maxs = meta.means.maxs; | ||
| const sh0Codebook = meta.sh0.codebook; | ||
| const scaleCodebook = meta.scales.codebook; | ||
|
|
||
| let labelsImg = null; | ||
| let centroidsImg = null; | ||
| let shNCodebook = null; | ||
| if (degree > 0) { | ||
| const f0 = meta.shN.files[0]; | ||
| const f1 = meta.shN.files[1]; | ||
| const firstIsLabels = /label/i.test(f0); | ||
| const [imgA, imgB] = await Promise.all([ | ||
| resolve(f0), | ||
| resolve(f1) | ||
| ]); | ||
| labelsImg = firstIsLabels ? imgA : imgB; | ||
| centroidsImg = firstIsLabels ? imgB : imgA; | ||
| shNCodebook = meta.shN.codebook; | ||
| } | ||
|
|
||
| for (let i = 0; i < count; i++) { | ||
| const x = i % width; | ||
| const y = (i / width) | 0; | ||
| const idx = (x + y * width) * 4; | ||
|
|
||
| const qx = (meansU.data[idx + 0] << 8) | meansL.data[idx + 0]; | ||
| const qy = (meansU.data[idx + 1] << 8) | meansL.data[idx + 1]; | ||
| const qz = (meansU.data[idx + 2] << 8) | meansL.data[idx + 2]; | ||
| const nx = lerp(mins[0], maxs[0], qx / 65535); | ||
| const ny = lerp(mins[1], maxs[1], qy / 65535); | ||
| const nz = lerp(mins[2], maxs[2], qz / 65535); | ||
| const px = unlog(nx); | ||
| const py = unlog(ny); | ||
| const pz = unlog(nz); | ||
|
|
||
| const sx = Math.exp(scaleCodebook[scalesImg.data[idx + 0]]); | ||
| const sy = Math.exp(scaleCodebook[scalesImg.data[idx + 1]]); | ||
| const sz = Math.exp(scaleCodebook[scalesImg.data[idx + 2]]); | ||
|
|
||
| const q = reconstructQuaternion(quats.data[idx + 0], quats.data[idx + 1], quats.data[idx + 2], quats.data[idx + 3]); | ||
|
|
||
| const SH_C0 = 0.28209479177387814; | ||
| const r = 0.5 + sh0Codebook[sh0Img.data[idx + 0]] * SH_C0; | ||
| const g = 0.5 + sh0Codebook[sh0Img.data[idx + 1]] * SH_C0; | ||
| const b = 0.5 + sh0Codebook[sh0Img.data[idx + 2]] * SH_C0; | ||
| const aByte = sh0Img.data[idx + 3]; | ||
|
|
||
| if (degree === 0) { | ||
| splats.addSplatFromComponents(px, py, pz, sx, sy, sz, q.x, q.y, q.z, q.w, r * 255, g * 255, b * 255, aByte); | ||
| } else { | ||
| const restCount = splats.sphericalHarmonicsCount; | ||
| const rest = new Array(restCount).fill(0); | ||
| const label = labelsImg.data[idx + 0] | (labelsImg.data[idx + 1] << 8); | ||
| if (label < (meta.shN.count || 0) && shNCodebook) { | ||
| for (let j = 0; j < shNCoeffsWanted; j++) { | ||
| const u = (label % 64) * shNCoeffsTotal + j; | ||
| const v = Math.floor(label / 64); | ||
| if (u < centroidsImg.width && v < centroidsImg.height) { | ||
| const cidx = (v * centroidsImg.width + u) * 4; | ||
| const rIdx = centroidsImg.data[cidx + 0]; | ||
| const gIdx = centroidsImg.data[cidx + 1]; | ||
| const bIdx = centroidsImg.data[cidx + 2]; | ||
| rest[j + 0 * shNCoeffsWanted] = shNCodebook[rIdx] ?? 0; | ||
| rest[j + 1 * shNCoeffsWanted] = shNCodebook[gIdx] ?? 0; | ||
| rest[j + 2 * shNCoeffsWanted] = shNCodebook[bIdx] ?? 0; | ||
| } | ||
| } | ||
| } | ||
| splats.addSplatFromComponents(px, py, pz, sx, sy, sz, q.x, q.y, q.z, q.w, r * 255, g * 255, b * 255, aByte, ...rest); | ||
| } | ||
| } | ||
|
|
||
| return splats; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code here createed a sequential layout [R0, R1, R2, ..., G0, G1, G2, ..., B0, B1, B2, ...], but the renderer expects an interleaved lauout.
The code here should be changed to something like this: