LNXLoader.js
4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { FileLoader, Loader, BufferGeometry, BufferAttribute, Float32BufferAttribute } from 'three';
const intbyte = 4;
const floatbyte = 4;
const charbyte = 1;
const transferToGeometry = (meshstruct) => {
const { vertex, index } = meshstruct;
const geometry = new BufferGeometry();
if (index.length > 0) {
geometry.setIndex(new BufferAttribute(new Uint32Array(index), 1));
}
if (vertex.length > 0) {
geometry.setAttribute('position', new BufferAttribute(new Float32Array(vertex), 3));
}
geometry.setAttribute('uv', new Float32BufferAttribute([], 2));
geometry.computeVertexNormals();
return geometry;
};
const ensureBinary = (buf) => {
if (typeof buf === 'string') {
const arraybuffer = new Uint8Array(buf.length);
for (let i = 0; i < buf.length; i++) {
arraybuffer[i] = buf.charCodeAt(i) & 0xff;
}
return arraybuffer.buffer || arraybuffer;
}
return buf;
};
class LNXLoader extends Loader {
constructor(manager) {
super(manager);
}
load = (url, onLoad, onProgress, onError) => {
const loader = new FileLoader(this.manager);
loader.setPath(this.path);
loader.setResponseType('arraybuffer');
loader.setRequestHeader(this.requestHeader);
loader.setWithCredentials(this.withCredentials);
loader.load(
url,
function (data) {
try {
onLoad(parseLNXData(data));
} catch (e) {
handleLoadError(e, url, onError);
}
},
onProgress,
function (error) {
handleLoadError(error, url, onError);
}
);
};
parse = (binaryData) => {
const data = ensureBinary(binaryData);
const reader = new DataView(data);
// console.log(data);
let headerName = '';
let start = 0;
const documentNameLength = reader.getUint32(start, true);
start += intbyte;
for (let i = 0; i < documentNameLength; i++) {
const str = reader.getUint8(start, true);
headerName += String.fromCharCode(str);
start += charbyte;
}
if (headerName !== 'LuxAlign data structure') {
return {
type: 'LnxModel',
geometry: null
};
}
const version = reader.getFloat32(start, true);
start += floatbyte;
start += 2; // 'pt' string'
const numberOfVertex = reader.getUint32(start, true);
// console.log(numberOfVertex);
start += intbyte;
const vertices = [];
let count = 0;
while (count < numberOfVertex) {
const x = reader.getFloat32(start, true);
start += floatbyte;
const y = reader.getFloat32(start, true);
start += floatbyte;
const z = reader.getFloat32(start, true);
start += floatbyte;
vertices.push(x, y, z);
++count;
}
// console.log(vertices);
start += 3; // 'tri' string'
const numberOfIndex = reader.getUint32(start, true);
// console.log(numberOfIndex);
start += intbyte;
const indices = [];
count = 0;
while (count < numberOfIndex) {
const x = reader.getUint32(start, true);
start += intbyte;
const y = reader.getUint32(start, true);
start += intbyte;
const z = reader.getUint32(start, true);
start += intbyte;
indices.push(x, y, z);
++count;
}
// console.log(indices);
const geometry = transferToGeometry({
vertex: vertices,
index: indices
});
return {
type: 'LnxModel',
geometry
};
};
}
function parseLNXData(data) {
return new LNXLoader().parse(data);
}
function handleLoadError(error, url, onError) {
if (onError) {
onError(error);
} else {
console.error(error);
}
new LNXLoader().manager.itemError(url);
}
export { LNXLoader };