-
Notifications
You must be signed in to change notification settings - Fork 369
Update babylon-support branch to use es6 imports, module-level GTLF loader function, auto-detect plugin extension, and add/remove observables #1397
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
base: babylon-support
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,8 +91,8 @@ | |
| "@types/three": "^0.170.0", | ||
| "@vitejs/plugin-react": "^4.3.2", | ||
| "@vitest/eslint-plugin": "^1.5.1", | ||
| "babylonjs": "^7.0.0", | ||
| "babylonjs-loaders": "^7.0.0", | ||
| "@babylonjs/core": "^8.39.3", | ||
| "@babylonjs/loaders": "^8.39.3", | ||
|
Comment on lines
-94
to
+95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's update the "peerDependencies" section, too. I'll let you decide which versions are needed for the current babylon support but |
||
| "cesium": "^1.132.0", | ||
| "concurrently": "^6.2.1", | ||
| "eslint": "^9.0.0", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| import { LoaderBase } from '3d-tiles-renderer/core'; | ||
| import { Matrix, Quaternion, SceneLoader } from 'babylonjs'; | ||
| import 'babylonjs-loaders'; | ||
| import { Matrix, Quaternion, ImportMeshAsync } from '@babylonjs/core'; | ||
| import "@babylonjs/loaders/glTF/2.0"; | ||
|
|
||
| // GLB magic bytes: "glTF" in ASCII (0x67, 0x6C, 0x54, 0x46) | ||
| const GLB_MAGIC = 0x46546C67; | ||
| const _worldMatrix = /* @__PURE__ */ Matrix.Identity(); | ||
|
|
||
| export class GLTFLoader extends LoaderBase { | ||
|
|
||
| constructor( scene ) { | ||
|
|
@@ -13,6 +16,57 @@ export class GLTFLoader extends LoaderBase { | |
|
|
||
| } | ||
|
|
||
| /** | ||
| * Detect if buffer contains GLB binary data by checking magic bytes | ||
| * @param {ArrayBuffer|Uint8Array} buffer - The file buffer | ||
| * @returns {boolean} True if GLB format | ||
| */ | ||
| isGLB( buffer ) { | ||
|
|
||
| // Handle both ArrayBuffer and typed arrays (e.g., Uint8Array) | ||
| const arrayBuffer = buffer instanceof ArrayBuffer ? buffer : buffer.buffer; | ||
| const byteOffset = buffer instanceof ArrayBuffer ? 0 : buffer.byteOffset; | ||
| const byteLength = buffer.byteLength; | ||
|
|
||
| if ( byteLength < 4 ) { | ||
|
|
||
| return false; | ||
|
|
||
| } | ||
|
|
||
| const view = new DataView( arrayBuffer, byteOffset, byteLength ); | ||
| const magic = view.getUint32( 0, true ); // little-endian | ||
| return magic === GLB_MAGIC; | ||
|
|
||
| } | ||
|
Comment on lines
+19
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to be sure: there's no way for Babylon to determine whether a file is a binary or ascii glTF automatically? |
||
|
|
||
| /** | ||
| * Detect file extension from URI or buffer content | ||
| * @param {ArrayBuffer} buffer - The file buffer | ||
| * @param {string} uri - The file URI | ||
| * @returns {string} '.glb' or '.gltf' | ||
| */ | ||
| detectExtension( buffer, uri ) { | ||
|
|
||
| // First check magic bytes in buffer (most reliable) | ||
| if ( this.isGLB( buffer ) ) { | ||
|
|
||
| return '.glb'; | ||
|
|
||
| } | ||
|
|
||
| // Fallback to URI extension | ||
| const lowerUri = uri.toLowerCase(); | ||
| if ( lowerUri.endsWith( '.glb' ) ) { | ||
|
|
||
| return '.glb'; | ||
|
|
||
| } | ||
|
|
||
| // Default to gltf | ||
| return '.gltf'; | ||
| } | ||
|
|
||
| async parse( buffer, uri ) { | ||
|
|
||
| const { scene, workingPath, adjustmentTransform } = this; | ||
|
|
@@ -25,18 +79,15 @@ export class GLTFLoader extends LoaderBase { | |
|
|
||
| } | ||
|
|
||
| // Detect format from buffer magic bytes or URI extension | ||
| const pluginExtension = this.detectExtension( buffer, uri ); | ||
| // Use unique filename to prevent texture caching issues | ||
| // TODO: What is the correct method for loading gltf files in babylon? | ||
| const container = await SceneLoader.LoadAssetContainerAsync( | ||
| rootUrl, | ||
| const container = await ImportMeshAsync( | ||
| new File( [ buffer ], uri ), | ||
| scene, | ||
| null, | ||
| '.glb', | ||
| { pluginExtension } | ||
| ); | ||
|
|
||
| container.addAllToScene(); | ||
|
|
||
| // retrieve the primary scene | ||
| const root = container.meshes[ 0 ]; | ||
|
|
||
|
|
||
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.
Is this supposed to import from
@babylonjs/core?