Skip to content

Commit

Permalink
Merge pull request #95 from Carifio24/gltf-improvements
Browse files Browse the repository at this point in the history
Improvements to glTF scatter export
  • Loading branch information
Carifio24 authored Dec 16, 2024
2 parents c03b26f + 35d786a commit 8f8ec1c
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
apt:
- '^libxcb.*-dev'
- libxkbcommon-x11-0
- libegl1-mesa
- libegl1-mesa-dev
- libhdf5-dev
envs: |
- linux: py39-test-all
Expand Down
2 changes: 1 addition & 1 deletion glue_ar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup_common():
from .common.usd_builder import USDBuilder # noqa: F401
from .common.stl_builder import STLBuilder # noqa: F401

from .compression import compress_gltfpack, compress_gltf_pipeline # noqa: F401
from .compression import compress_draco, compress_meshoptimizer # noqa: F401


def setup_qt():
Expand Down
300 changes: 239 additions & 61 deletions glue_ar/common/scatter_gltf.py

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions glue_ar/common/tests/gltf_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from glue_ar.utils import iterator_count


BufferFormat = Union[Literal["f"], Literal["I"]]
BufferFormat = Union[Literal["f"], Literal["I"], Literal["H"]]


def get_data(gltf: GLTF, buffer: Buffer, buffer_view: Optional[BufferView] = None) -> bytes:
Expand Down Expand Up @@ -41,8 +41,9 @@ def count_vertices(gltf: GLTF, buffer: Buffer, buffer_view: BufferView):
return count_points(gltf, buffer, buffer_view, 'f')


def count_indices(gltf: GLTF, buffer: Buffer, buffer_view: BufferView):
return count_points(gltf, buffer, buffer_view, 'I')
def count_indices(gltf: GLTF, buffer: Buffer, buffer_view: BufferView, use_short=False):
format = 'H' if use_short else 'I'
return count_points(gltf, buffer, buffer_view, format)


def unpack_points(gltf: GLTF,
Expand Down
8 changes: 5 additions & 3 deletions glue_ar/common/tests/test_scatter_gltf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from glue_ar.common.tests.gltf_helpers import count_indices, count_vertices, unpack_vertices
from glue_ar.common.tests.helpers import APP_VIEWER_OPTIONS
from glue_ar.common.tests.test_scatter import BaseScatterTest
from glue_ar.gltf_utils import SHORT_MAX
from glue_ar.utils import export_label_for_layer, hex_to_components, layers_to_export, mask_for_bounds, \
xyz_bounds, xyz_for_layer

Expand Down Expand Up @@ -68,16 +69,17 @@ def test_basic_export(self, app_type: str, viewer_type: str):
phi_resolution=phi_resolution)
points_count = sphere_points_count(theta_resolution=theta_resolution,
phi_resolution=phi_resolution)

assert count_indices(gltf, model.buffers[0], model.bufferViews[0]) == triangles_count
use_short = points_count <= SHORT_MAX
assert count_indices(gltf, model.buffers[0], model.bufferViews[0], use_short=use_short) == triangles_count
assert count_vertices(gltf, model.buffers[0], model.bufferViews[1]) == points_count

assert model.bufferViews[0].target == BufferTarget.ELEMENT_ARRAY_BUFFER.value
assert model.bufferViews[1].target == BufferTarget.ARRAY_BUFFER.value

indices_accessor = model.accessors[0]
assert indices_accessor.bufferView == 0
assert indices_accessor.componentType == ComponentType.UNSIGNED_INT.value
expected_indices_type = ComponentType.UNSIGNED_SHORT if use_short else ComponentType.UNSIGNED_INT
assert indices_accessor.componentType == expected_indices_type.value
assert indices_accessor.count == triangles_count * 3
assert indices_accessor.type == AccessorType.SCALAR.value
assert indices_accessor.min == [0]
Expand Down
11 changes: 5 additions & 6 deletions glue_ar/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@


NODE_MODULES_DIR = join(PACKAGE_DIR, "js", "node_modules")
GLTF_PIPELINE_FILEPATH = join(NODE_MODULES_DIR, "gltf-pipeline", "bin", "gltf-pipeline.js")
GLTFPACK_FILEPATH = join(NODE_MODULES_DIR, "gltfpack", "cli.js")
GLTF_TRANSFORM_FILEPATH = join(NODE_MODULES_DIR, "@gltf-transform", "cli", "bin", "cli.js")


@compressor("draco")
def compress_gltf_pipeline(filepath: str):
run(["node", GLTF_PIPELINE_FILEPATH, "-i", filepath, "-o", filepath, "-d"], capture_output=True)
def compress_draco(filepath: str):
run([GLTF_TRANSFORM_FILEPATH, "optimize", filepath, filepath, "--compress", "draco"], capture_output=True)


@compressor("meshoptimizer")
def compress_gltfpack(filepath: str):
run(["node", GLTFPACK_FILEPATH, "-i", filepath, "-o", filepath], capture_output=True)
def compress_meshoptimizer(filepath: str):
run([GLTF_TRANSFORM_FILEPATH, "optimize", filepath, filepath], capture_output=True)
9 changes: 7 additions & 2 deletions glue_ar/gltf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"meshoptimizer": "EXT_meshopt_compression",
}

SHORT_MAX = 65_535


def create_material_for_color(
color: List[int],
Expand All @@ -40,10 +42,13 @@ def add_points_to_bytearray(arr: bytearray, points: Iterable[Iterable[Union[int,
arr.extend(struct.pack('f', coordinate))


def add_triangles_to_bytearray(arr: bytearray, triangles: Iterable[Iterable[int]]):
def add_triangles_to_bytearray(arr: bytearray,
triangles: Iterable[Iterable[int]],
short: bool = False):
format = "H" if short else "I"
for triangle in triangles:
for index in triangle:
arr.extend(struct.pack('I', index))
arr.extend(struct.pack(format, index))


T = TypeVar("T", bound=Union[int, float])
Expand Down
3 changes: 1 addition & 2 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"dependencies": {
"gltf-pipeline": "^4.1.0",
"gltfpack": "^0.21.0"
"@gltf-transform/cli": "^4.1.1"
},
"scripts": {
"glue-ar-export": "npm install && node glue-ar-export.js"
Expand Down

0 comments on commit 8f8ec1c

Please sign in to comment.