Cesium

Java解析b3dm文件

2023-02-05  本文已影响0人  WebGiser

参考:https://github.com/CesiumGS/3d-tiles/tree/main/specification/TileFormats/Batched3DModel
https://www.cnblogs.com/onsummer/p/13252896.html

image.png

将Obj文件分割成b3dm

参考:https://www.npmjs.com/package/obj23dtiles

npm install -g obj23dtiles
obj23dtiles -i ./bin/barrel/barrel.obj --tileset

java解析b3dm

B3dm.java代码

package com.hello.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

// https://www.cnblogs.com/onsummer/p/13252896.html
// byteLength = 28 + featureTableJSONByteLength + featureTableBinaryByteLength + batchTableJSONByteLength + batchTableBinaryByteLength + glb的字节长度

public class B3dm {
    public static void main(String[] args) throws Exception {
        // 读取所有的字节数据
        String b3dmPath = "C:\\Users\\wzf\\Desktop\\temp\\Batchedtest\\test.b3dm";
        String gltfPath = "C:\\Users\\wzf\\Desktop\\temp\\Batchedtest\\test.gltf";
        File file = new File(b3dmPath);
        File file2 = new File(gltfPath);
        long len = file.length();
        FileInputStream in = new FileInputStream(file);
        FileOutputStream out = new FileOutputStream(file2);
        byte[] bytes = new byte[1024];
        byte[] data = new byte[(int) len];
        int c;
        int num = 0;
        while ((c = in.read(bytes)) != -1) {
            for (int i = 0; i < bytes.length; i++) {
                if (num * 1024 + i < len) {
                    data[num * 1024 + i] = bytes[i];
                }
            }
            num++;
        }

        // 解析header
        String magic = new String(subBytes(data, 0, 4));
        int version = bytesToInt(subBytes(data, 4, 4), 0);
        int byteLen = bytesToInt(subBytes(data, 8, 4), 0);
        int featureTableJsonByteLen = bytesToInt(subBytes(data, 12, 4), 0);
        int featureTableBinByteLen = bytesToInt(subBytes(data, 16, 4), 0);
        int batchTableJsonByteLen = bytesToInt(subBytes(data, 20, 4), 0);
        int batchTableBinByteLen = bytesToInt(subBytes(data, 24, 4), 0);
        System.out.println("magic: " + magic);
        System.out.println("version: " + version);
        System.out.println("byteLen: " + byteLen);
        System.out.println("featureTableJsonByteLen: " + featureTableJsonByteLen);
        System.out.println("featureTableBinByteLen: " + featureTableBinByteLen);
        System.out.println("batchTableJsonByteLen: " + batchTableJsonByteLen);
        System.out.println("batchTableBinByteLen: " + batchTableBinByteLen);
        System.out.println("---------------------------------------------------");

        // 解析featureTable表
        String featureTableJson = new String(subBytes(data, 28, featureTableJsonByteLen));
        System.out.println(featureTableJson);

        System.out.println("---------------------------------------------------");
        int offset = 28 + featureTableJsonByteLen + featureTableBinByteLen;
        String batchTableJson = new String(subBytes(data, offset, batchTableJsonByteLen));
        System.out.println(batchTableJson);

        System.out.println("-----------------------生成gltf----------------------------");
        offset = 28 + featureTableJsonByteLen + featureTableBinByteLen + batchTableJsonByteLen + batchTableBinByteLen;
        out.write(subBytes(data, offset, (int) len - offset));
        out.close();

        in.close();
    }


    // 截取byte数组
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i = begin; i < begin + count; i++) {
            bs[i - begin] = src[i];
        }
        return bs;
    }

    // byte数组转整型
    public static int bytesToInt(byte[] src, int offset) {
        int value = (int) ((src[offset] & 0xFF)
                | ((src[offset + 1] & 0xFF) << 8)
                | ((src[offset + 2] & 0xFF) << 16)
                | ((src[offset + 3] & 0xFF) << 24));
        return value;
    }
}
image.png
上一篇下一篇

猜你喜欢

热点阅读