Skip to content
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

fix(DRACOExporter): vendor draco_encoder #259

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@
"vite": "^4.3.8"
},
"dependencies": {
"@types/draco3d": "^1.4.0",
"@types/offscreencanvas": "^2019.6.4",
"@types/webxr": "^0.5.2",
"chevrotain": "^10.1.2",
"draco3d": "^1.4.1",
"fflate": "^0.6.9",
"ktx-parse": "^0.4.5",
"mmd-parser": "^1.0.4",
Expand Down
103 changes: 103 additions & 0 deletions src/_polyfill/draco_encoder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Type definitions for draco3d 1.4
// Project: https://github.com/google/draco#readme
// Definitions by: Don McCurdy <https://github.com/donmccurdy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

type TypedArray = Float32Array | Uint32Array | Uint16Array | Uint8Array | Int16Array | Int8Array

export function createEncoderModule(object?: Record<string, unknown>): EncoderModule

export interface BaseModule {
Mesh: new () => Mesh

DracoFloat32Array: new () => DracoFloat32Array
DracoInt8Array: new () => DracoInt8Array
DracoInt16Array: new () => DracoInt16Array
DracoInt32Array: new () => DracoInt32Array
DracoUInt8Array: new () => DracoUInt8Array
DracoUInt16Array: new () => DracoUInt16Array
DracoUInt32Array: new () => DracoUInt32Array

_malloc: (ptr: number) => number
_free: (ptr: number) => void
destroy: (object: unknown) => void

// Heap.
HEAPF32: Float32Array
HEAP32: Int32Array
HEAP16: Int16Array
HEAP8: Int8Array
HEAPU32: Uint32Array
HEAPU16: Uint16Array
HEAPU8: Uint8Array
}

export interface EncoderModule extends BaseModule {
Encoder: new () => Encoder
MeshBuilder: new () => MeshBuilder

POSITION: number
NORMAL: number
TEX_COORD: number
COLOR: number
GENERIC: number

MESH_SEQUENTIAL_ENCODING: number
MESH_EDGEBREAKER_ENCODING: number
}

export interface Encoder {
SetAttributeQuantization(attribute: number, bits: number): void
SetAttributeExplicitQuantization(
attribute: number,
bits: number,
itemSize: number,
origin: [number, number, number],
range: number,
): void
SetSpeedOptions(encodeSpeed: number, decodeSpeed: number): void
SetEncodingMethod(method: number): void
SetTrackEncodedProperties(track: boolean): void
EncodeMeshToDracoBuffer(mesh: Mesh, array: DracoInt8Array): number
GetNumberOfEncodedPoints(): number
GetNumberOfEncodedFaces(): number
}

export interface DracoArray {
GetValue: (index: number) => number
}

// tslint:disable-next-line:no-empty-interface
export interface DracoFloat32Array extends DracoArray {}
// tslint:disable-next-line:no-empty-interface
export interface DracoInt8Array extends DracoArray {}
// tslint:disable-next-line:no-empty-interface
export interface DracoInt16Array extends DracoArray {}
// tslint:disable-next-line:no-empty-interface
export interface DracoInt32Array extends DracoArray {}
// tslint:disable-next-line:no-empty-interface
export interface DracoUInt8Array extends DracoArray {}
// tslint:disable-next-line:no-empty-interface
export interface DracoUInt16Array extends DracoArray {}
// tslint:disable-next-line:no-empty-interface
export interface DracoUInt32Array extends DracoArray {}

export interface Attribute {
num_components: () => number
}

export interface Mesh {
ptr: number
num_faces: () => number
num_points: () => number
}

export interface MeshBuilder {
AddFacesToMesh(mesh: Mesh, numFaces: number, faces: Uint16Array | Uint32Array): void
AddUInt8Attribute(mesh: Mesh, attribute: number, count: number, itemSize: number, array: TypedArray): number
AddInt8Attribute(mesh: Mesh, attribute: number, count: number, itemSize: number, array: TypedArray): number
AddUInt16Attribute(mesh: Mesh, attribute: number, count: number, itemSize: number, array: TypedArray): number
AddInt16Attribute(mesh: Mesh, attribute: number, count: number, itemSize: number, array: TypedArray): number
AddUInt32Attribute(mesh: Mesh, attribute: number, count: number, itemSize: number, array: TypedArray): number
AddFloatAttribute(mesh: Mesh, attribute: number, count: number, itemSize: number, array: TypedArray): number
}
27 changes: 27 additions & 0 deletions src/_polyfill/draco_encoder.js

Large diffs are not rendered by default.

10 changes: 2 additions & 8 deletions src/exporters/DRACOExporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { EncoderModule } from 'draco3d'
import { BufferGeometry, Mesh, Points } from 'three'
import { createEncoderModule } from '../_polyfill/draco_encoder'

/**
* Export draco compressed files from threejs geometry objects.
Expand All @@ -15,8 +15,6 @@ import { BufferGeometry, Mesh, Points } from 'three'
* - exportNormals
*/

declare const DracoEncoderModule: () => EncoderModule

class DRACOExporter {
// Encoder methods

Expand Down Expand Up @@ -52,13 +50,9 @@ class DRACOExporter {
throw new Error('DRACOExporter: The first parameter of parse() is now an instance of Mesh or Points.')
}

if (DracoEncoderModule === undefined) {
throw new Error('THREE.DRACOExporter: required the draco_encoder to work.')
}

const geometry = object.geometry

const dracoEncoder = DracoEncoderModule()
const dracoEncoder = createEncoderModule()
const encoder = new dracoEncoder.Encoder()
let builder
let dracoObject
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@
resolved "https://registry.yarnpkg.com/@esbuild/win32-x64/-/win32-x64-0.17.19.tgz#8cfaf2ff603e9aabb910e9c0558c26cf32744061"
integrity sha512-lAhycmKnVOuRYNtRtatQR1LPQf2oYCkRGkSFnseDAKPl8lu5SOsK/e1sXe5a0Pc5kHIHe6P2I/ilntNv2xf3cA==

"@types/draco3d@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@types/draco3d/-/draco3d-1.4.0.tgz#6a57c6295d69b3c69ee72a93f132763afd74c77b"
integrity sha512-rXFiyNFUu/sad/VWmc6xdO4GWcc55ny7cq08cxmDFcDIeNg0WXT279ZW7mug+rO51TPS2xlmMY631VKauoot7A==

"@types/offscreencanvas@^2019.6.4":
version "2019.6.4"
resolved "https://registry.yarnpkg.com/@types/offscreencanvas/-/offscreencanvas-2019.6.4.tgz#64f6d120b53925028299c744fcdd32d2cd525963"
Expand Down Expand Up @@ -240,11 +235,6 @@ core-util-is@~1.0.0:
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=

draco3d@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/draco3d/-/draco3d-1.4.1.tgz#2abdcf7b59caaac50f7e189aec454176c57146b2"
integrity sha512-9Rxonc70xiovBC+Bq1h57SNZIHzWTibU1VfIGp5z3Xx8dPtv4yT5uGhiH7P5uvJRR2jkrvHafRxR7bTANkvfpg==

emoji-regex@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
Expand Down