1 Star 0 Fork 3

邢瑞/blender-3d-tiler

forked from Cesium/blender-3d-tiler 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
uv-parser.js 6.17 KB
一键复制 编辑 原始数据 按行查看 历史
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);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xrui94/blender-3d-tiler.git
git@gitee.com:xrui94/blender-3d-tiler.git
xrui94
blender-3d-tiler
blender-3d-tiler
master

搜索帮助