JSON图像url而不是base64

qv7cva1a  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(137)

我使用Three.js在数据库中保存和加载对象。我把对象作为JSON插入。
问题是JSON.stringify或toJSON()在base64中转换了图像url(纹理),我想保留http url。
这是最初的JSON:

{
    "metadata": {
        [...]
    },
    "geometries": [
        {
            [...]
        }],
    "materials": [
        {
            [...]
        }],
    "textures": [
        {
            [...]
        }],
    "images": [
        {
            "uuid": "4ED9CE3E-7C8A-4EB7-8CF8-D90B3527DF5F",
            "url": "data:image/png;base64,iVBORw0KGgoATyuRZGZVREd0FAERRRBAEBEFwAYEyKqP/Rfozx+5fDYjvpeT/akv+n/J+/7eMjX9Ke8uojP4JVAYAyuj/Ld3Por7fPQ9i8d7vvr8LKNzLg/dn1u1hvQf3q/v92vRX0X/ [...]"
        }],
    "object": {
        [...]
    }
}

我们想要的JSON类型是:

{
    "metadata": {
        [...]
    },
    "geometries": [
        {
            [...]
        }],
    "materials": [
        {
            [...]
        }],
    "textures": [
        {
            [...]
        }],
    "images": [
        {
            "uuid": "4ED9CE3E-7C8A-4EB7-8CF8-D90B3527DF5F",
            "url": "http://www.domain.com/picture/path.png"
        }],
    "object": {
        [...]
    }
}

有解决办法吗?

zz2j4svz

zz2j4svz1#

在研究了JSON文件并获得纹理路径后,我做了以下操作:

var objectParsed = SELECTED.toJSON(); //transform the selected object in JSON
textureBase64 = objectParsed.images[0].url; //get the value of the base64 encoded image
objectParsed.images[0].url = texMap; // replace the base64 encoded image with the http texture path

objectParsed = JSON.stringify( objectParsed, null, '\t' );
objectParsed = objectParsed.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' ); // make the JSON readable

现在它成功了!:)

nwlls2ji

nwlls2ji2#

我遇到了同样的问题,并通过覆盖Source.toJSON()解决了它,如下所示:

Source.prototype.toJSON = function ( meta ) {

    const isRootObject = ( meta === undefined || typeof meta === 'string' );

    if ( !isRootObject && meta.images[ this.uuid ] !== undefined ) {
        return meta.images[ this.uuid ];
    }

    const output = {
        uuid: this.uuid,
        url: this.data.src
    };

    if ( !isRootObject ) {
        meta.images[ this.uuid ] = output;
    }

    return output;
}

或者,您可以使用this.data.getAttribute('src')存储图像的相对URL。

相关问题