-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuv-parser.js
165 lines (127 loc) · 6.17 KB
/
uv-parser.js
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
154
155
156
157
158
159
160
161
162
163
164
165
const argv = require('yargs').argv;
const fs = require('fs');
const path = require('path');
const VERTEX_PER_FACE = 3;
let input = argv.input;
if (!input || !fs.existsSync(input)) {
console.log('input file not found');
process.exit(1);
}
let jsonList = JSON.parse(fs.readFileSync(input, 'utf8'));
jsonList.forEach((model, i) => {
let level = model.level;
let gltfPath = model.gltf_path;
let absoluteGltfPath = path.resolve(gltfPath);
let gltf = JSON.parse(fs.readFileSync(absoluteGltfPath, 'utf8'));
let output = {
gltf: path.basename(absoluteGltfPath),
maps: []
}
if (gltf && gltf.scene) {
console.log('cannot parse gltf file or scene');
process.exit(1);
}
let primitiveUVMaps = new Array();
gltf.scenes[gltf.scene].nodes.forEach((nodeIndex) => {
let node = gltf.nodes[nodeIndex];
if (!node.mesh) {
return;
}
let nodeMesh = gltf.meshes[node.mesh];
let nodeMeshPrimitives = nodeMesh.primitives;
nodeMeshPrimitives.forEach((primitive) => {
let primitiveMaterial = gltf.materials[primitive.material];
if (primitiveMaterial.pbrMetallicRoughness) {
if (primitiveMaterial.pbrMetallicRoughness.baseColorTexture) {
primitiveUVMaps.push({
uvAccessor: gltf.accessors[primitive.attributes["TEXCOORD_" + primitiveMaterial.pbrMetallicRoughness.baseColorTexture.texCoord]],
indexAccessor: gltf.accessors[primitive.indices],
image: gltf.images[gltf.textures[primitiveMaterial.pbrMetallicRoughness.baseColorTexture.index].source]
});
}
if (primitiveMaterial.pbrMetallicRoughness.metallicRoughnessTexture) {
primitiveUVMaps.push({
uvAccessor: gltf.accessors[primitive.attributes["TEXCOORD_" + primitiveMaterial.pbrMetallicRoughness.metallicRoughnessTexture.texCoord]],
indexAccessor: gltf.accessors[primitive.indices],
image: gltf.images[gltf.textures[primitiveMaterial.pbrMetallicRoughness.metallicRoughnessTexture.index].source]
});
}
}
if (primitiveMaterial.normalTexture) {
primitiveUVMaps.push({
uvAccessor: gltf.accessors[primitive.attributes["TEXCOORD_" + primitiveMaterial.normalTexture.texCoord]],
indexAccessor: gltf.accessors[primitive.indices],
image: gltf.images[gltf.textures[primitiveMaterial.normalTexture.index].source]
});
}
if (primitiveMaterial.occlusionTexture) {
primitiveUVMaps.push({
uvAccessor: gltf.accessors[primitive.attributes["TEXCOORD_" + primitiveMaterial.occlusionTexture.texCoord]],
indexAccessor: gltf.accessors[primitive.indices],
image: gltf.images[gltf.textures[primitiveMaterial.occlusionTexture.index].source]
});
}
if (primitiveMaterial.emissiveTexture) {
primitiveUVMaps.push({
uvAccessor: gltf.accessors[primitive.attributes["TEXCOORD_" + primitiveMaterial.emissiveTexture.texCoord]],
indexAccessor: gltf.accessors[primitive.indices],
image: gltf.images[gltf.textures[primitiveMaterial.emissiveTexture.index].source]
});
}
});
});
if (primitiveUVMaps.length == 0) {
console.log('no mapped texture images found');
return;
}
primitiveUVMaps.forEach((map) => {
let mapBufferView = gltf.bufferViews[map.uvAccessor.bufferView];
let indexBufferView = gltf.bufferViews[map.indexAccessor.bufferView];
let uvBufferPath = path.resolve(path.join(path.dirname(absoluteGltfPath), gltf.buffers[mapBufferView.buffer].uri));
let uvBuffer = fs.readFileSync(uvBufferPath);
let indexBufferPath = path.resolve(path.join(path.dirname(absoluteGltfPath), gltf.buffers[indexBufferView.buffer].uri));
let indexBuffer = fs.readFileSync(indexBufferPath);
// assume that all elements in uv maps are VEC2 : 5126 (FLOAT)
if (map.uvAccessor.componentType != 5126 || map.uvAccessor.type != "VEC2") {
console.log('this map is not using float values to map image textures');
return;
}
let uvs = new Array();
for (let i = 0; i < map.uvAccessor.count; i++) {
let u = uvBuffer.readFloatLE(mapBufferView.byteOffset + 2 * i * 4);
let v = uvBuffer.readFloatLE(mapBufferView.byteOffset + (2 * i + 1) * 4);
uvs.push([u, v]);
}
let indices = new Array();
let bytePerIndex = 2;
if (map.indexAccessor.componentType == 5121) { // UNSIGNED_BYTE
bytePerIndex = 1;
} else if (map.indexAccessor.componentType == 5123) { // UNSIGNED_SHORT
bytePerIndex = 2;
} else if (map.indexAccessor.componentType == 5125) { // UNSIGNED_INT
bytePerIndex = 4;
} else {
return;
}
for (let i = 0; i < map.indexAccessor.count; i++) {
if (bytePerIndex == 1) {
index = indexBuffer.readUInt8(indexBufferView.byteOffset + bytePerIndex * i);
} else if (bytePerIndex == 2) {
index = indexBuffer.readUInt16LE(indexBufferView.byteOffset + bytePerIndex * i);
} else {
index = indexBuffer.readUInt32LE(indexBufferView.byteOffset + bytePerIndex * i);
}
indices.push(index);
}
let faceUvs = new Array();
for (let i = 0; i < (indices.length / VERTEX_PER_FACE); i++) {
faceUvs.push([uvs[indices[VERTEX_PER_FACE * i]], uvs[indices[VERTEX_PER_FACE * i + 1]], uvs[indices[VERTEX_PER_FACE * i + 2]]]);
}
output.maps.push({
image: map.image,
faceUvs: faceUvs
});
});
fs.writeFileSync(path.join(path.dirname(absoluteGltfPath), 'uv_coord.json'), JSON.stringify(output, null, 2), { encoding: 'utf8' });
});
process.exit(0);