-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathglbTextDocumentContentProvider.ts
More file actions
82 lines (60 loc) · 2.1 KB
/
Copy pathglbTextDocumentContentProvider.ts
File metadata and controls
82 lines (60 loc) · 2.1 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
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
//
// GlbTextDocumentContentProvider provide json chunk of glb.
//
export class GlbTextDocumentContentProvider implements vscode.TextDocumentContentProvider {
// emitter and its event
onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
onDidChange = this.onDidChangeEmitter.event;
provideTextDocumentContent(uri: vscode.Uri): string {
if (!uri.fsPath.endsWith(".json")) {
return "fsPath must endsWith .json";
}
let fsPath = uri.fsPath.substr(0, uri.fsPath.length - 5);
let data = fs.readFileSync(fsPath);
let offset = 0;
if (data.readInt32LE(offset) != 0x46546C67) {
return `not glb`;
}
offset += 4;
let version = data.readInt32LE(offset)
if (version != 2) {
return `gltf version ${version} not support`;
}
offset += 4;
let length = data.readInt32LE(offset);
offset += 4;
let json = null;
let binOffset = null;
while (offset < length) {
let chunkLength = data.readInt32LE(offset);
offset += 4;
let chunkType = data.readInt32LE(offset);
offset += 4;
let chunkData = data.toString('utf8', offset, offset + chunkLength);
if (chunkType == 0x4E4F534A) {
json = JSON.parse(chunkData);
}
else if (chunkType == 0x004E4942) {
binOffset = offset;
}
else {
// unknown
}
offset += chunkLength;
}
if (binOffset && json) {
// buffer access hack
json.buffers[0]['uri'] = path.basename(fsPath);
for (let bufferView of json.bufferViews) {
bufferView.byteOffset += binOffset;
}
return JSON.stringify(json, null, ' ');
}
else {
return `invalid glb`;
}
}
};