@threlte/gltf 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,208 @@
1
+ 'use strict'
2
+ import THREE from 'three'
3
+ import draco from 'draco3dgltf'
4
+ const decoder = draco.createDecoderModule()
5
+ const DRACOLoader = (THREE.DRACOLoader = function (t) {
6
+ ;(this.timeLoaded = 0),
7
+ (this.manager = t || THREE.DefaultLoadingManager),
8
+ (this.materials = null),
9
+ (this.verbosity = 0),
10
+ (this.attributeOptions = {}),
11
+ (this.drawMode = THREE.TrianglesDrawMode),
12
+ (this.nativeAttributeMap = { position: 'POSITION', normal: 'NORMAL', color: 'COLOR', uv: 'TEX_COORD' })
13
+ })
14
+
15
+ export default DRACOLoader
16
+
17
+ THREE.DRACOLoader.prototype = {
18
+ constructor: THREE.DRACOLoader,
19
+ load: function (t, e, r, o) {
20
+ var i = this,
21
+ n = new THREE.FileLoader(i.manager)
22
+ n.setPath(this.path),
23
+ n.setResponseType('arraybuffer'),
24
+ n.load(
25
+ t,
26
+ function (t) {
27
+ i.decodeDracoFile(t, e)
28
+ },
29
+ r,
30
+ o
31
+ )
32
+ },
33
+ setPath: function (t) {
34
+ return (this.path = t), this
35
+ },
36
+ setVerbosity: function (t) {
37
+ return (this.verbosity = t), this
38
+ },
39
+ setDrawMode: function (t) {
40
+ return (this.drawMode = t), this
41
+ },
42
+ setSkipDequantization: function (t, e) {
43
+ var r = !0
44
+ return void 0 !== e && (r = e), (this.getAttributeOptions(t).skipDequantization = r), this
45
+ },
46
+ decodeDracoFile: function (t, e, r, o) {
47
+ decoder.then((decoder) => this.decodeDracoFileInternal(t, decoder, e, r, o))
48
+ },
49
+ decodeDracoFileInternal: function (t, e, r, o, i) {
50
+ var n = new e.DecoderBuffer()
51
+ n.Init(new Int8Array(t), t.byteLength)
52
+ var a = new e.Decoder(),
53
+ s = a.GetEncodedGeometryType(n)
54
+ if (s == e.TRIANGULAR_MESH) this.verbosity > 0 && console.log('Loaded a mesh.')
55
+ else {
56
+ if (s != e.POINT_CLOUD) {
57
+ var u = 'THREE.DRACOLoader: Unknown geometry type.'
58
+ //throw (console.error(u), new Error(u))
59
+ }
60
+ this.verbosity > 0 && console.log('Loaded a point cloud.')
61
+ }
62
+ r(this.convertDracoGeometryTo3JS(e, a, s, n, o, i))
63
+ },
64
+ addAttributeToGeometry: function (t, e, r, o, i, n, a, s) {
65
+ if (0 === n.ptr) {
66
+ var u = 'THREE.DRACOLoader: No attribute ' + o
67
+ throw (console.error(u), new Error(u))
68
+ }
69
+ var d,
70
+ A,
71
+ c = n.num_components(),
72
+ l = r.num_points() * c
73
+ switch (i) {
74
+ case Float32Array:
75
+ ;(d = new t.DracoFloat32Array()),
76
+ e.GetAttributeFloatForAllPoints(r, n, d),
77
+ (s[o] = new Float32Array(l)),
78
+ (A = THREE.Float32BufferAttribute)
79
+ break
80
+ case Int8Array:
81
+ ;(d = new t.DracoInt8Array()),
82
+ e.GetAttributeInt8ForAllPoints(r, n, d),
83
+ (s[o] = new Int8Array(l)),
84
+ (A = THREE.Int8BufferAttribute)
85
+ break
86
+ case Int16Array:
87
+ ;(d = new t.DracoInt16Array()),
88
+ e.GetAttributeInt16ForAllPoints(r, n, d),
89
+ (s[o] = new Int16Array(l)),
90
+ (A = THREE.Int16BufferAttribute)
91
+ break
92
+ case Int32Array:
93
+ ;(d = new t.DracoInt32Array()),
94
+ e.GetAttributeInt32ForAllPoints(r, n, d),
95
+ (s[o] = new Int32Array(l)),
96
+ (A = THREE.Int32BufferAttribute)
97
+ break
98
+ case Uint8Array:
99
+ ;(d = new t.DracoUInt8Array()),
100
+ e.GetAttributeUInt8ForAllPoints(r, n, d),
101
+ (s[o] = new Uint8Array(l)),
102
+ (A = THREE.Uint8BufferAttribute)
103
+ break
104
+ case Uint16Array:
105
+ ;(d = new t.DracoUInt16Array()),
106
+ e.GetAttributeUInt16ForAllPoints(r, n, d),
107
+ (s[o] = new Uint16Array(l)),
108
+ (A = THREE.Uint16BufferAttribute)
109
+ break
110
+ case Uint32Array:
111
+ ;(d = new t.DracoUInt32Array()),
112
+ e.GetAttributeUInt32ForAllPoints(r, n, d),
113
+ (s[o] = new Uint32Array(l)),
114
+ (A = THREE.Uint32BufferAttribute)
115
+ break
116
+ default:
117
+ u = 'THREE.DRACOLoader: Unexpected attribute type.'
118
+ throw (console.error(u), new Error(u))
119
+ }
120
+ for (var b = 0; b < l; b++) s[o][b] = d.GetValue(b)
121
+ a.setAttribute(o, new A(s[o], c)), t.destroy(d)
122
+ },
123
+ convertDracoGeometryTo3JS: function (t, e, r, o, i, n) {
124
+ var a, s, u
125
+ if (
126
+ (!0 === this.getAttributeOptions('position').skipDequantization && e.SkipAttributeTransform(t.POSITION),
127
+ r === t.TRIANGULAR_MESH
128
+ ? ((a = new t.Mesh()), (s = e.DecodeBufferToMesh(o, a)))
129
+ : ((a = new t.PointCloud()), (s = e.DecodeBufferToPointCloud(o, a))),
130
+ !s.ok() || 0 == a.ptr)
131
+ ) {
132
+ return new THREE.BufferGeometry()
133
+ var d = 'THREE.DRACOLoader: Decoding failed: '
134
+ throw ((d += s.error_msg()), console.error(d), t.destroy(e), t.destroy(a), new Error(d))
135
+ }
136
+ t.destroy(o),
137
+ r == t.TRIANGULAR_MESH
138
+ ? ((u = a.num_faces()), this.verbosity > 0 && console.log('Number of faces loaded: ' + u.toString()))
139
+ : (u = 0)
140
+ var A = a.num_points(),
141
+ c = a.num_attributes()
142
+ this.verbosity > 0 &&
143
+ (console.log('Number of points loaded: ' + A.toString()),
144
+ console.log('Number of attributes loaded: ' + c.toString()))
145
+ var l = e.GetAttributeId(a, t.POSITION)
146
+ if (-1 == l) {
147
+ d = 'THREE.DRACOLoader: No position attribute found.'
148
+ throw (console.error(d), t.destroy(e), t.destroy(a), new Error(d))
149
+ }
150
+ var b = e.GetAttribute(a, l),
151
+ f = {},
152
+ y = new THREE.BufferGeometry()
153
+ if (i)
154
+ for (var E in i) {
155
+ var h = n[E],
156
+ w = i[E],
157
+ p = e.GetAttributeByUniqueId(a, w)
158
+ //this.addAttributeToGeometry(t, e, a, E, h, p, y, f)
159
+ }
160
+ else
161
+ for (var E in this.nativeAttributeMap) {
162
+ var T = e.GetAttributeId(a, t[this.nativeAttributeMap[E]])
163
+ if (-1 !== T) {
164
+ this.verbosity > 0 && console.log('Loaded ' + E + ' attribute.')
165
+ p = e.GetAttribute(a, T)
166
+ //this.addAttributeToGeometry(t, e, a, E, Float32Array, p, y, f)
167
+ }
168
+ }
169
+ if (r == t.TRIANGULAR_MESH)
170
+ if (this.drawMode === THREE.TriangleStripDrawMode) {
171
+ var R = new t.DracoInt32Array()
172
+ e.GetTriangleStripsFromMesh(a, R)
173
+ f.indices = new Uint32Array(R.size())
174
+ for (var I = 0; I < R.size(); ++I) f.indices[I] = R.GetValue(I)
175
+ t.destroy(R)
176
+ } else {
177
+ var v = 3 * u
178
+ f.indices = new Uint32Array(v)
179
+ var D = new t.DracoInt32Array()
180
+ for (I = 0; I < u; ++I) {
181
+ e.GetFaceFromMesh(a, I, D)
182
+ var m = 3 * I
183
+ ;(f.indices[m] = D.GetValue(0)), (f.indices[m + 1] = D.GetValue(1)), (f.indices[m + 2] = D.GetValue(2))
184
+ }
185
+ t.destroy(D)
186
+ }
187
+ ;(y.drawMode = this.drawMode),
188
+ r == t.TRIANGULAR_MESH &&
189
+ y.setIndex(
190
+ new (f.indices.length > 65535 ? THREE.Uint32BufferAttribute : THREE.Uint16BufferAttribute)(f.indices, 1)
191
+ )
192
+ var G = new t.AttributeQuantizationTransform()
193
+ if (G.InitFromAttribute(b)) {
194
+ ;(y.attributes.position.isQuantized = !0),
195
+ (y.attributes.position.maxRange = G.range()),
196
+ (y.attributes.position.numQuantizationBits = G.quantization_bits()),
197
+ (y.attributes.position.minValues = new Float32Array(3))
198
+ for (I = 0; I < 3; ++I) y.attributes.position.minValues[I] = G.min_value(I)
199
+ }
200
+ return t.destroy(G), t.destroy(e), t.destroy(a), y
201
+ },
202
+ isVersionSupported: function (t, e) {
203
+ e(decoder.isVersionSupported(t))
204
+ },
205
+ getAttributeOptions: function (t) {
206
+ return void 0 === this.attributeOptions[t] && (this.attributeOptions[t] = {}), this.attributeOptions[t]
207
+ },
208
+ }