Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ draw-server
docs/public/playground
docs/src/content/docs/api/*.md
viamrobotics-motion-tools-*.tgz
.wireit

# Logs
*-debug.log*
Expand Down
42 changes: 40 additions & 2 deletions src/lib/attribute.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { BufferAttribute, BufferGeometry } from 'three'

import type { Metadata } from './metadata'

import { ColorFormat } from './buf/draw/v1/metadata_pb'
import { colorStride, STRIDE } from './buffer'
import type { LODLevel } from './loaders/pcd/messages'

Check failure on line 5 in src/lib/attribute.ts

View workflow job for this annotation

GitHub Actions / lint

Expected "./loaders/pcd/messages" (type-sibling) to come before "./buffer" (value-sibling)
import type { Metadata } from './metadata'

export const createBufferGeometry = (positions: Float32Array, metadata?: Metadata) => {
const geometry = new BufferGeometry()
Expand All @@ -20,6 +21,43 @@
return geometry
}

export interface LODGeometryLevel {
geometry: BufferGeometry
distance: number
}

export const createLODGeometries = (levels: LODLevel[]): LODGeometryLevel[] => {
return levels.map((level) => ({
geometry: createBufferGeometry(level.positions, {
colors: level.colors ?? undefined,
colorFormat: ColorFormat.RGB,
}),
distance: level.distance,
}))
}

export const updateLODGeometries = (
existing: LODGeometryLevel[],
levels: LODLevel[]
): LODGeometryLevel[] => {
if (existing.length !== levels.length) {
for (const { geometry } of existing) {
geometry.dispose()
}
return createLODGeometries(levels)
}

for (let i = 0; i < levels.length; i++) {
updateBufferGeometry(existing[i]!.geometry, levels[i]!.positions, {
colors: levels[i]!.colors ?? undefined,
colorFormat: ColorFormat.RGB,
})
existing[i]!.distance = levels[i]!.distance
}

return existing
}

export const updateBufferGeometry = (
geometry: BufferGeometry,
positions: Float32Array,
Expand Down
42 changes: 37 additions & 5 deletions src/lib/components/Entities/Points.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import type { Snippet } from 'svelte'

import { T, useTask, useThrelte } from '@threlte/core'
import { OrthographicCamera, Points, PointsMaterial } from 'three'
import { Detailed } from '@threlte/extras'
import { LOD, OrthographicCamera, Points, PointsMaterial } from 'three'

import { asColor, isSingleColor } from '$lib/buffer'
import { traits, useTrait } from '$lib/ecs'
Expand All @@ -23,10 +24,11 @@

const worldMatrix = useTrait(() => entity, traits.WorldMatrix)
const geometry = useTrait(() => entity, traits.BufferGeometry)
const lodData = useTrait(() => entity, traits.PointCloudLOD)
const opacity = useTrait(() => entity, traits.Opacity)
const entityColor = useTrait(() => entity, traits.Color)
const colors = useTrait(() => entity, traits.Colors)
const entityPointSize = useTrait(() => entity, traits.PointSize)
const opacity = useTrait(() => entity, traits.Opacity)
const invisible = useTrait(() => entity, traits.InheritedInvisible)
const renderOrder = useTrait(() => entity, traits.RenderOrder)
const materialProps = useTrait(() => entity, traits.Material)
Expand All @@ -35,12 +37,22 @@
entityPointSize.current ? entityPointSize.current * 0.001 : settings.current.pointSize
)
const orthographic = $derived(settings.current.cameraMode === 'orthographic')
const hasLOD = $derived(lodData.current !== undefined && lodData.current.levels.length > 0)

const points = new Points()
points.matrixAutoUpdate = false
const material = points.material as PointsMaterial
material.toneMapped = false

let lodRef = $state<LOD>()

const lodPoints = $derived(
lodData.current?.levels.map((level) => ({
points: new Points(level.geometry, material),
distance: level.distance,
})) ?? []
)

$effect.pre(() => {
material.size = pointSize
})
Expand Down Expand Up @@ -95,8 +107,10 @@

$effect.pre(() => {
if (worldMatrix.current) {
points.matrix.copy(worldMatrix.current)
points.updateMatrixWorld()
const target = hasLOD && lodRef ? lodRef : points
target.matrixAutoUpdate = false
target.matrix.copy(worldMatrix.current)
target.updateMatrixWorld()
}
})

Expand All @@ -121,7 +135,25 @@
})
</script>

{#if geometry.current}
{#if hasLOD}
<Detailed
bind:ref={lodRef}
name={entity}
visible={invisible.current !== true}
renderOrder={renderOrder.current}
{...events}
>
{#each lodPoints as { points: childPoints, distance } (childPoints)}
<T
is={childPoints}
{distance}
bvh={{ maxDepth: 40, maxLeafSize: 20 }}
/>
{/each}

{@render children?.()}
</Detailed>
{:else if geometry.current}
<T
is={points}
name={entity}
Expand Down
7 changes: 6 additions & 1 deletion src/lib/ecs/traits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { type Entity, trait } from 'koota'
import { Matrix4, BufferGeometry as ThreeBufferGeometry } from 'three'

import { createBufferGeometry, updateBufferGeometry } from '$lib/attribute'
import { createBufferGeometry, updateBufferGeometry, type LODGeometryLevel } from '$lib/attribute'

Check failure on line 7 in src/lib/ecs/traits.ts

View workflow job for this annotation

GitHub Actions / lint

Expected "LODGeometryLevel" to come before "updateBufferGeometry"
import { ColorFormat } from '$lib/buf/draw/v1/metadata_pb'
import { createBox, createCapsule, createSphere } from '$lib/geometry'
import { parsePcdInWorker } from '$lib/loaders/pcd'
Expand Down Expand Up @@ -172,6 +172,11 @@

export const BufferGeometry = trait(() => new ThreeBufferGeometry())

export const PointCloudLOD = trait(() => ({
levels: [] as LODGeometryLevel[],
diagonal: 0,
}))

export const GLTF = trait(() => ({
source: { url: '' } as { url: string } | { gltf: ThreeGltf } | { glb: Uint8Array },
animationName: '',
Expand Down
65 changes: 53 additions & 12 deletions src/lib/hooks/usePointclouds.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Entity } from 'koota'
import type { ConfigurableTrait, Entity } from 'koota'

import { CameraClient } from '@viamrobotics/sdk'
import {
Expand All @@ -8,11 +8,16 @@ import {
} from '@viamrobotics/svelte-sdk'
import { getContext, setContext, untrack } from 'svelte'

import { createBufferGeometry, updateBufferGeometry } from '$lib/attribute'
import {
createBufferGeometry,
createLODGeometries,
updateBufferGeometry,
updateLODGeometries,
} from '$lib/attribute'
import { ColorFormat } from '$lib/buf/draw/v1/metadata_pb'
import { RefetchRates } from '$lib/components/overlay/RefreshRate.svelte'
import { hierarchy, traits, useWorld } from '$lib/ecs'
import { parsePcdInWorker } from '$lib/loaders/pcd'
import { parsePcdWithLOD } from '$lib/loaders/pcd'
import { useLogs } from '$lib/plugins'

import { useEnvironment } from './useEnvironment.svelte'
Expand Down Expand Up @@ -141,37 +146,73 @@ export const providePointclouds = (partID: () => string) => {
}
}

parsePcdInWorker(data)
.then(({ positions, colors }) => {
parsePcdWithLOD(data)
.then(({ levels, boundingBoxDiagonal }) => {
if (disposed) {
return
}

const existing = entities.get(queryKey)
const finest = levels.find((l) => l.level === 0) ?? levels[0]!
const metadata = {
colors,
colors: finest.colors ?? undefined,
colorFormat: ColorFormat.RGB,
}

if (existing) {
hierarchy.setParent(existing, name)
const geometry = existing.get(traits.BufferGeometry)
const existingLOD = existing.get(traits.PointCloudLOD)

if (geometry) {
updateBufferGeometry(geometry, positions, metadata)
return
updateBufferGeometry(geometry, finest.positions, metadata)
}

if (levels.length > 1) {
if (existingLOD) {
// Update geometry buffers in place without setting the trait
// to avoid triggering re-renders and component remounts.
// BVH is not recomputed here — it drifts slightly between
// frames but avoids expensive main-thread recomputation.
existingLOD.levels = updateLODGeometries(existingLOD.levels, levels)
existingLOD.diagonal = boundingBoxDiagonal
} else {
existing.add(
traits.PointCloudLOD({
levels: createLODGeometries(levels),
diagonal: boundingBoxDiagonal,
})
)
}
} else if (existingLOD) {
for (const { geometry } of existingLOD.levels) {
geometry.dispose()
}
existing.remove(traits.PointCloudLOD)
}

return
}

const geometry = createBufferGeometry(positions, metadata)
const geometry = createBufferGeometry(finest.positions, metadata)

const entity = world.spawn(
const entityTraits: ConfigurableTrait[] = [
...hierarchy.parentTraits(name),
traits.Name(`${name} pointcloud`),
traits.BufferGeometry(geometry),
traits.Points
)
traits.Points,
]

if (levels.length > 1) {
entityTraits.push(
traits.PointCloudLOD({
levels: createLODGeometries(levels),
diagonal: boundingBoxDiagonal,
})
)
}

const entity = world.spawn(...entityTraits)
entities.set(queryKey, entity)
})
.catch((error) => {
Expand Down
Loading
Loading