Skip to content

Commit

Permalink
refactor(reactive): enhance GLTF serialization
Browse files Browse the repository at this point in the history
### Description

- Add GLTF transferable data to the worker
  - animations (`AnimationClipJSON[]`)
  - cameras (`Camera[]`)
  - parser (`{ json: GLTFParser["json"] }`)
  - scenes (`Group[]`)
  • Loading branch information
Neosoulink committed Jan 7, 2025
1 parent cefd748 commit 64285f2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
13 changes: 13 additions & 0 deletions .changeset/dull-dryers-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"with-reactive": patch
---

# Logs

## refactor(reactive): enhance `GLTF` serialization

- Add GLTF transferable data to the worker
- animations (`AnimationClipJSON[]`)
- cameras (`Camera[]`)
- parser (`{ json: GLTFParser["json"] }`)
- scenes (`Group[]`)
3 changes: 2 additions & 1 deletion packages/reactive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"reflect-metadata": "^0.2.2",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"tsyringe": "^4.8.0"
"tsyringe": "^4.8.0",
"vite": "^6.0.3"
}
}
24 changes: 21 additions & 3 deletions packages/reactive/src/core/app/loader/loader.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
LoadedResourcePayload,
LOADER_SERIALIZED_LOAD_TOKEN
} from "../../../common";
import { AnimationClip, AnimationClipJSON, Camera, Group } from "three";
import { GLTF, GLTFParser } from "three/examples/jsm/loaders/GLTFLoader";

@scoped(Lifecycle.ResolutionScoped)
export class LoaderController {
Expand All @@ -28,12 +30,28 @@ export class LoaderController {
const { payload } = event.data || {};

if (!!payload?.resource && payload.source.type === "gltfModel") {
const resource = payload.resource as any;
const scene = deserializeObject3D(resource?.scene as string);
const resource = payload.resource as unknown as {
animations?: AnimationClipJSON[];
cameras?: string[];
parser?: Partial<GLTFParser> & { json: GLTFParser["json"] };
scene?: string;
scenes?: string[];
userData?: GLTF["userData"];
};
const animations = resource.animations?.map((animation) =>
AnimationClip.parse(animation)
);
const cameras = resource.cameras?.map(
(camera) => deserializeObject3D(camera) as Camera
);
const scene = deserializeObject3D(resource?.scene || "") as Group;
const scenes = resource.scenes?.map(
(scene) => deserializeObject3D(scene) as Group
);

return {
...payload,
resource: { ...resource, scene }
resource: { ...resource, animations, cameras, scene, scenes }
} as LoadedResourcePayload;
}

Expand Down
16 changes: 13 additions & 3 deletions packages/reactive/src/core/register/loader/loader.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { serializeObject3D } from "@quick-threejs/utils";
import { filter, map, Observable, share, Subject } from "rxjs";
import { GLTF } from "three/examples/jsm/loaders/GLTFLoader";
import { VideoTexture } from "three";
import { AnimationClipJSON, VideoTexture } from "three";
import { inject, singleton } from "tsyringe";

import {
LoadedResourcePayload,
SerializedLoadedResourcePayload
} from "../../../common/interfaces/loader.interface";
import { LoaderService } from "./loader.service";
import { JsonSerializable } from "threads";

@singleton()
export class LoaderController {
Expand Down Expand Up @@ -37,8 +38,17 @@ export class LoaderController {
const _resource = payload.resource as GLTF;

resource = {
userData: _resource.userData,
scene: serializeObject3D(_resource.scene)
animations: (payload?.resource as GLTF).animations.map(
// @ts-ignore <<toJSON methods doesn't require a parameter>>
(animation) => animation.toJSON() as AnimationClipJSON
) as unknown as JsonSerializable,
cameras: _resource.cameras.map((camera) =>
serializeObject3D(camera)
),
parser: { json: _resource.parser.json },
scene: serializeObject3D(_resource.scene),
scenes: _resource.scenes.map((scene) => serializeObject3D(scene)),
userData: _resource.userData
};
}

Expand Down
11 changes: 7 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 64285f2

Please sign in to comment.