Unity3d引擎中使用AIGC生成的360全景图(天空盒)

前言

在这里与Skybox AI一起,一键打造体验无限的360°世界,这是这个AIGC一键生成全景图的网站欢迎语。

刚使用它是23年中旬,在没有空去给客户实地拍摄全景图时,可以快速用它生成一些相关的全景图,用作前期沟通的VR demo。当时使用所有都是开放免费的,遗憾的是现在使用多了很多限制,比如很多风格都需要付费,而且生成也需要魔法网络,免费生成次数进行了限制:
Unity3d引擎中使用AIGC生成的360全景图(天空盒)_第1张图片

效果

如下是一些Unity中的效果:

数字绘画赛博朋克

数字绘画长城

科幻风格

写实风格

卡通风格

快速体验

直接访问blockadelabs.com点击体验。

选择一种风格(select a style):

输入对全景图的描述:
Unity3d引擎中使用AIGC生成的360全景图(天空盒)_第2张图片

点击生成后,等待一段时间就能在网站中看到效果,点击下载按钮:
Unity3d引擎中使用AIGC生成的360全景图(天空盒)_第3张图片

产生的全景图是一个2:1的全景图,如下:
Unity3d引擎中使用AIGC生成的360全景图(天空盒)_第4张图片

下载后导入Unity,如果识别不出来图片,需要改成jpg。

在unity内新建一个天空盒/Panoramic着色器的材质球,具体设置参照下图:
Unity3d引擎中使用AIGC生成的360全景图(天空盒)_第5张图片

在场景中新建一个球体Sphere,将材质球拖给它,适当放大球体,将摄像放到球体的正中间,就可以看到全景图的效果。
如果生成的效果不理想得考虑更换风格和描述(这个同Stable Diffusion 的 Prompt 提示词)更改,也可以在此次生成的作品上进行编辑(Edit This)或者再混合(Remix This),直到效果满意。不过,如果你是免费的用户得注意次数,不然就得等下个月了:

Unity3d引擎中使用AIGC生成的360全景图(天空盒)_第6张图片

API接入

这里它提供了API请求的一系列接口,这些接口其实也可以在Unity内使用UnityWebRequest的方式进行请求。

首先,您需要一个 API 密钥才能开始使用。 如果您没有,请前往 https://api.blockadelabs.com 申请。
这是它的安全提示:

不建议在应用程序的前端公开 API 密钥。相反,建议使用我们为您准备的 SDK库之一或开发您自己的后端服务来与前端应用程序进行通信,然后利用服务器到服务器的请求来访问 Blockade Labs API 端点。

您的 API 密钥必须作为参数包含在 HTTP 请求标头中:x-api-key
或者,您可以将其作为 url 查询参数api_key发送(此方法不太安全):https://backend.blockadelabs.com/api/v1/skybox?api_key=7J7eD5TIiJR4Gky…
根据您对 POST 请求的偏好,您可以将参数和值作为 JSON () 或 FormData () 发送。请注意,如果您使用的是 JSON,则需要以 base64 格式对要上传的文件进行编码。application/jsonmultipart/form-data

Unity3d 中你这么写C#脚本:

 UnityWebRequest request = new UnityWebRequest(url, reqtype);
 request.SetRequestHeader("x-api-key", "你的key(如:7J7eD5TIiJR4Gky...)");

生成天空盒

您需要做的就是向 https://backend.blockadelabs.com/api/v1/skybox 发送一个带有参数的 POST 请求,需要传参数提示词prompt:

 JsonData param = new JsonData();
 param["prompt"] = "你的描述(提示词)";
 request.uploadHandler = (UploadHandler)new UploadHandlerRaw(Encoding.UTF8.GetBytes(param.ToJson()));

获得响应示例如下:

{
  "id": 123456, // id of your generation. It can be used to track generation progress or to cancel/delete the generation
  "status": "pending", // initially status is set as 'pending' after you send a generation request. Status will change from one state to another and you will get updates for each one if you are using pusher or webhook: pending -> dispatched -> processing -> complete. Also you can get abort or error.
  "queue_position": 2, // position of your request in a generation queue
  "file_url": "", // full size of generated image url (empty until generation is completed)
  "thumb_url": "", // thumbnail of generated image url (empty until generation is completed)
  "title": "World #123456", // generation title
  "user_id": 1, // your user id
  "username": "[email protected]", // your username
  "error_message": null, // if status=error here you should see the error message
  "obfuscated_id": "460370b7328a5cb8dbddd6ef0d1c9dd4", // hash id of your generation
  "pusher_channel": "status_update_460370b7328a5cb8dbddd6ef0d1c9dd4", // pusher channel name used to track generation progress
  "pusher_event": "status_update", // pusher channel event used to track generation progress
  "created_at": "2023-03-18T10:42:19+00:00", // time created
  "updated_at": "2023-03-18T10:42:19+00:00" // time updated}
}

获得响应后,仍需要等待生成器处理图像,因为生成速度没有那么快。

跟踪生成进度

生成图像时,参数将按以下顺序更改,直到完成,status如下说明:
pending- 生成在队列中(初始状态)
dispatched- 这一次生成被送到了人工智能工作者那里
processing- AI worker 开始生成
complete- 生成已完成,您可以检索生成的天空盒图像
abort- 这一次生成夭折了
error- 生成时出现错误。您可以查看参数以获取更多详细信息。error_message
每当发生更改时,都会通过 Pusher(或可选地通过 webhook)发送相应的事件消息。

要同步生成进度,有三种方式:

1. Pusher

这是推荐方式的同步你生成过程,通过使用官方 Pusher 库(https://pusher.com/docs/channels/channels_libraries/libraries/#official-libraries),您可以将其无缝集成到您的应用程序中。 在发送天空盒或想象生成请求时,您将得到可用于跟踪生成进度的响应。

Pusher 请求参数:

app_id = "1555452"
key = "a6a7b7662238ce4494d5"
cluster = "mt1"

响应示例:

{
  "pusher_channel": "status_update_460370b7328a5cb8dbddd6ef0d1c9dd4",
  "pusher_event": "status_update",
}

2. Webhook

Webhook 是发送到唯一 URL 的通知。

若要启用 Webhook 状态更新,只需在 Skybox 生成请求中发送一个附加参数即可。在每次状态更新时,我们都会向您提供的发送带有进度更新消息 (json) 的请求。webhook_urlPOSTwebhook_url

您可以使用 https://webhook.site/ 测试 Webhook。

3. API数据轮询

不建议使用此方法,因为它可能会触发我们的 API 速率限制器。它只能用于测试目的。

向 API 发出请求并检查更改。 这里更详细地解释。
GET https://backend.blockadelabs.com/api/v1/imagine/requests/{id}status
这是状态更新的响应示例:

{
  "id": 123456, // id of your generation. It can be used to track generation progress or to cancel the generation
  "obfuscated_id": "460370b7328a5cb8dbddd6ef0d1c9dd4", // hash id of your generation
  "user_id": 1, // your user id
  "username": "[email protected]", // your username

  "status": "pending", // initially status is set as 'pending' after you send a generation request. Status will change from one state to another and you will get updates for each one if you are using pusher or webhook: pending -> dispatched -> processing -> complete. Also you can get abort or error.
  "queue_position": 1, // position of your request in a generation queue
  "pusher_channel": "status_update_460370b7328a5cb8dbddd6ef0d1c9dd4", // pusher channel name used to track generation progress
  "pusher_event": "status_update", // pusher channel event used to track generation progress
  "error_message": null, // if status=error here you should find the error message
  
  "type": "skybox", // type of generation (currently "skybox" is the only one)
  "title": "Imagination #123456", // generation title
  "prompt": "prompt text", // prompt text used to generate skybox
  "skybox_style_id": 10, // skybox style id used to generate skybox
  "skybox_style_name": "SciFi", // skybox style name used to generate skybox
  
  "file_url": "https://blockade-platform-production.s3.amazonaws.com/images/imagine/futuristic_microdetailed_vr_scifi_concept_art_cinematic_vr_neon__dbe7f963dc23699c__2757929_dbe7.jpg?ver=1", // generated skybox image (6144x3072 pixels)
  "thumb_url": "https://blockade-platform-production.s3.amazonaws.com/thumbs/imagine/thumb_futuristic_microdetailed_vr_scifi_concept_art_cinematic_vr_neon__dbe7f963dc23699c__2757929_dbe7.jpg?ver=1", // generated skybox thumbnail (672x336 pixels)
  "depth_map_url": "https://blockade-platform-production.s3.amazonaws.com/depths/imagine/futuristic_microdetailed_vr_scifi_concept_art_cinematic_vr_neon__dbe7f963dc23699c__2757929_dbe7.jpg?ver=1", // generated skybox depyh map image (2048x1024 pixels)
  
  "created_at": "2023-03-18T10:42:19+00:00", // time created
  "updated_at": "2023-03-18T10:42:39+00:00", // time updated
}

下载全景图

所有 API 端点都需要在标头中发送或作为 get 参数发送的 API 密钥。用于导出各种文件类型的天空盒的 API 路由:PNG、HDRI(exr、hdr)、深度图、立方体图、视频(纵向、横向、正方形)。

获取导出类型

GET https://backend.blockadelabs.com/api/v1/skybox/export

返回示例:

[
    {
        "id": 1,
        "name": "JPG",
        "key": "equirectangular-jpg",
    },
    {
        "id": 2,
        "name": "PNG",
        "key": "equirectangular-png",
     }
]

请求导出

POST https://backend.blockadelabs.com/api/v1/skybox/export

如果导出请求已经完成,您将立即收到响应,并在响应中收到响应。

响应示例(完整)

{
    "file_url": "https://blockade-platform-production.s3.amazonaws.com/depths/imagine/vr360-stunning-beautiful-vibrant-digital-painting-sega-atari_8414305.jpg", // url for the exported file
    "id": "15e8a0ae53d39e2ef518b2c02f9c43ee", // id of your export request. It can be used to track progress or cancel the request
    "type": "depth-map-png", // requested export type
    "type_id": 6, // requested export type id
    "status": "complete", // initially status is set as 'pending' after you send the export request. Status will change from one state to another and you will get updates for each one if you are using pusher or webhook: pending -> dispatched -> processing -> complete. Also you can get abort or error and in some cases (ie. for JPG and Depth Map export) this will be immediately set to 'complete'.
    "queue_position": 0, // position of your request in a export generation queue
    "error_message": null, // if status=error here you should see the error message
    "pusher_channel": "export_status_update_15e8a0ae53d39e2ef518b2c02f9c43ee", // pusher channel name used to track export generation progress
    "pusher_event": "status_update", // pusher channel event used to track generation
    "created_at": "2023-08-21T09:55:28.000000Z"
}

获取下载链接

GET https://backend.blockadelabs.com/api/v1/skybox/export/{你的导出id}

响应示例:

{
    "file_url": "https://blockade-platform-production.s3.amazonaws.com/depths/imagine/vr360-stunning-beautiful-vibrant-digital-painting-sega-atari_8414305.jpg", // url for the exported file
    "id": "15e8a0ae53d39e2ef518b2c02f9c43ee", // id of your export request. It can be used to track progress or cancel the request
    "type": "depth-map-png", // requested export type
    "type_id": 6, // requested export type id
    "status": "complete", // initially status is set as 'pending' after you send the export request. Status will change from one state to another and you will get updates for each one if you are using pusher or webhook: pending -> dispatched -> processing -> complete. Also you can get abort or error and in some cases (ie. for JPG and Depth Map export) this will be immediately set to 'complete'.
    "queue_position": 0, // position of your request in a export generation queue
    "error_message": null, // if status=error here you should see the error message
    "pusher_channel": "export_status_update_15e8a0ae53d39e2ef518b2c02f9c43ee", // pusher channel name used to track export generation progress
    "pusher_event": "status_update", // pusher channel event used to track generation
    "webhook_url": null, // custom webhook where generator will send updates about export
    "created_at": "2023-08-21T09:55:28.000000Z"
}

这里响应的file_url字段就是下载链接的地址。

费用

和去年相比,这个平台的功能也逐渐完善、强大,系统也从完全开放状态,转变成了接口式的付费使用平台,而且平台的费用分级也很多,详细如下:

Unity3d引擎中使用AIGC生成的360全景图(天空盒)_第7张图片

你可能感兴趣的:(Unity3D,AI,AIGC,Unity全景图生成,AI文生图,Unity文生图,Unity生图接口源码)