Create stunning AI-generated skybox assets within Unity for use as skyboxes, HDRI style lighting, and world meshes. Speed up your prototyping or use in your existing creative workflow!
In order to use this package you need to provide an API key from Blockade Labs in the API section. Get one at https://api.blockadelabs.com.
Unity 2020.3 LTS or higher.
- Add this package to your assets from the Unity Asset Store
- Go to
Window > Package Manager
- Change the project scope to
My Assets
- Find
Blockade Labs SDK
and clickInstall
- CLI:
openupm add com.blockadelabs.sdk
- Registry:
- Name:
OpenUPM
- URL:
https://package.openupm.com
- Scope:
com.blockadelabs
- Name:
- Go to
Window > Package Manager
+ > Add package from git URL...
- Enter
https://github.com/Blockade-Games/BlockadeLabs-SDK-Unity.git
The Blockade SDK can be used standalone or optionally together with a Pusher websockets package. If installed, the Pusher package will use websockets to listen for any changes in the Asset Generation Process. This changes the API request update strategy from Polling to Callbacks which will reduce the chances of being rate limited by the API.
You can learn more about the Pusher package here.
The Pusher library requires .NET Framework runtime:
- Set
Edit > Project Settings > Player > Api Compatibility Level
to.NET Framework
.
Option 1: Install the Pusher package with git:
- Go to
Window > Package Manager
+ > Add package from git URL...
- Enter
https://github.com/pusher/pusher-websocket-unity.git#upm
Option 2: Install the Pusher package with OpenUPM:
- CLI:
openupm add com.pusher.pusherwebsocketunity
- Registry:
- Add scope:
com.pusher.pusherwebsocketunity
- Add scope:
Note
After installing the Pusher package for Unity 2021+ versions you might encounter an error saying:
Assembly 'Packages/com.pusher.pusherwebsocketunity/Packages/PusherClient.2.1.0/lib/net472/PusherClient.dll' will not be loaded due to errors: PusherClient references strong named Newtonsoft.Json Assembly references: 12.0.0.0 Found in project: 13.0.0.0.
To resolve the issue go to Edit > Project Settings > Player > Other Settings > Configuration > Assembly Version Validation
and disable Version Validation
.
Refer to the changelog file here.
After importing the package an onboarding window should pop up with directions to open the SkyboxAI scene.
If the onboarding window does not open for you, you can access the SkyboxAI scene by the following:
- menu:
Tools > Blockade Labs > Open SkyboxAI Scene
- open:
Packages/Blockade Labs SDK/Scenes/SkyboxScene
Note
The SkyboxSceneAI scene uses Text Mesh Pro elements for runtime UI. If you haven't imported TMP Essentials
you will be prompted to do so after you load the scene. When you are done importing TMP Essentials,
reload the scene by either double clicking on it in the Assets/Samples/Blockade Labs SDK/Scenes
.
The scene contains two notable gameObjects:
Blockade Labs Skybox Generator
generates skybox textures and materials.Blockade Labs Skybox Mesh
generates and configures a mesh which combines the skybox with a generated depth map.
There are 4 ways to provide your API keys, in order of precedence:
Warning
We recommended using the environment variables to load the API key instead of having it hard coded in your source. It is not recommended use this method in production, but only for accepting user credentials, local testing and quick start scenarios.
- Pass keys directly with constructor
⚠️ - Unity Scriptable Object
⚠️ - Load key from configuration file
- Use System Environment Variables
var api = new BlockadeLabsClient("yourApiKey");
Or create a BlockadeLabsAuthentication
object manually
var api = new BlockadeLabsClient(new BlockadeLabsAuthentication("yourApiKey"));
You can save the key directly into a scriptable object that is located in the Assets/Resources
folder.
You can create a new one by using the context menu of the project pane and creating a new BlockadeLabsConfiguration
scriptable object.
Create > BlockadeLabs > BlockadeLabsConfiguration
Attempts to load api keys from a configuration file, by default .blockadelabs
in the current directory, optionally traversing up the directory tree or in the user's home directory.
To create a configuration file, create a new text file named .blockadelabs
and containing the line:
{
"apiKey": "yourApiKey",
}
You can also load the file directly with known path by calling a static method in Authentication:
var api = new BlockadeLabsClient(new BlockadeLabsAuthentication().LoadFromDirectory("your/path/to/.blockadelabs"));;
Use your system's environment variables specify an api key to use.
- Use
BLOCKADELABS_API_KEY
for your api key.
var api = new BlockadeLabsClient(new BlockadeLabsAuthentication().LoadFromEnvironment());
You can generate a new skybox that will replace the existing one by following these steps.
- Select the
Blockade Labs Skybox Generator
gameObject. - Add your Blockade Labs'
API key
in the designated inspector field. - Click the
Apply
button. - After the plugin is successfully initialized, some additional fields will become available.
- Select the desired style.
- Fill in the
Prompt
field. - Click the
Generate Skybox
Button. - Generated textures and materials are placed in the
Assets/Blockade Labs SDK
for use in your project. - You should see your new skybox in the game view. You can also click
Move Scene Camera to Skybox
to see the skybox in the scene view.
For detailed information on how skyboxes work in Unity, see using skyboxes.
- Go to
Window > Rendering > Lighting
. - Go to the
Environment
tab. - Drag the generated
skybox material
into the theSkybox Material
field. - If you want to use the skybox as background lighting in your scene, ensure
Environment Lighting Source
is set toSkybox
, then clickGenerate Lighting
.
- Add a global volume to your scene:
GameObject > Volume > Global Volume
. - Drag the generated
HDRP volume profile
in to theProfile
field.
If you are building a 'Fully Immersive' visionOS app, then the steps are the same as the render pipeline which you are using.
If you are building a 'PolySpatial Mixed Reality' app, then the scene sky is not rendered. However, if you use Mesh Creator to create skybox mesh, then you can render it in mixed reality. Since PolySpatial only supports Shader Graph materials, you need to change the type of the generated depth material:
- Install the
Shader Graph
package from the Unity Package Manager. - Go to the "skybox depth material" you want to use and duplicate it to create a material for visionOS.
- Change the material type from
Blockade Skybox Depth
toBlock Skybox Depth Shader Graph
.
If you want the Blockade Labs Skybox Generator
component to always generate shader graph materials, update the Depth Material
field.
The Blockade Labs Skybox Generator
component generates a color texture and a depth texture, which are assigned to the skybox material.
The Blockade Labs Skybox Mesh
component generates a Tetrahedron mesh to apply this material. You can configure the Mesh Density
and Depth Scale
fields. The mesh and material will be configured to apply the generated depth map to deform the mesh.
Try zooming in and out with the scroll wheel in play mode to see the effect of the depth scale!
If you want to use the generated mesh in your own scene, click Save Prefab
, then drag the new prefab into your scene.
If you're interested in having runtime generated content generated on demand, you can use the public API surface directly.
Warning
It is highly encouraged to use a proxy service when generating runtime content, to keep your API key secure and to minimize unauthorized content generation. DO NOT store or check in your API key into source control or ship it with your application. Ensure that you have not hard coded your API key in source control, and DO NOT check in your BlockadeLabsConfiguration
into source control.
Using either the BlockadeLabs-SDK-DotNet or BlockadeLabs-SDK-Unity packages directly in your front-end app may expose your API keys and other sensitive information. To mitigate this risk, it is recommended to set up an intermediate API that makes requests to BlockadeLabs on behalf of your front-end app. This library can be utilized for both front-end and intermediary host configurations, ensuring secure communication with the BlockadeLabs API.
In the front end example, you will need to securely authenticate your users using your preferred OAuth provider. Once the user is authenticated, exchange your custom auth token with your API key on the backend.
Follow these steps:
- Setup a new project using either the BlockadeLabs-SDK-DotNet or BlockadeLabs-SDK-Unity packages.
- Authenticate users with your OAuth provider.
- After successful authentication, create a new
BlockadeLabsAuthentication
object and pass in the custom token as your apiKey. - Create a new
BlockadeLabsClientSettings
object and specify the domain where your intermediate API is located. - Pass your new
auth
andsettings
objects to theBlockadeLabsClient
constructor when you create the client instance.
Here's an example of how to set up the front end:
var authToken = await LoginAsync();
var auth = new BlockadeLabsAuthentication(authToken);
var settings = new BlockadeLabsClientSettings(domain: "api.your-custom-domain.com");
using var api = new BlockadeLabsClient(auth, settings);
This setup allows your front end application to securely communicate with your backend that will be using the BlockadeLabs-SDK-DotNet-Proxy, which then forwards requests to the BlockadeLabs API. This ensures that your BlockadeLabs API keys and other sensitive information remain secure throughout the process.
In this example, we demonstrate how to set up and use BlockadeLabsProxy
in a new ASP.NET Core web app. The proxy server will handle authentication and forward requests to the BlockadeLabs API, ensuring that your API keys and other sensitive information remain secure.
- Create a new ASP.NET Core minimal web API project.
- Add the BlockadeLabs-SDK-DotNet nuget package to your project.
- Powershell install:
Install-Package BlockadeLabs-SDK-DotNet-Proxy
- dotnet:
dotnet add package BlockadeLabs-SDK-DotNet-Proxy
- Manually editing .csproj:
<PackageReference Include="BlockadeLabs-SDK-DotNet-Proxy" />
- Powershell install:
- Create a new class that inherits from
AbstractAuthenticationFilter
and override theValidateAuthenticationAsync
method. This will implement theIAuthenticationFilter
that you will use to check user session token against your internal server. - In
Program.cs
, create a new proxy web application by callingBlockadeLabsProxy.CreateWebApplication
method, passing your customAuthenticationFilter
as a type argument. - Create
BlockadeLabsAuthentication
as you would normally and load your API key from environment variable.
using BlockadeLabsSDK;
using BlockadeLabsSDK.Proxy;
using System.Security.Authentication;
public partial class Program
{
private class AuthenticationFilter : AbstractAuthenticationFilter
{
public override async Task ValidateAuthenticationAsync(IHeaderDictionary request)
{
await Task.CompletedTask; // remote resource call
// You will need to implement your own class to properly test
// custom issued tokens you've setup for your end users.
if (!request["x-api-key"].ToString().Contains(TestUserToken))
{
throw new AuthenticationException("User is not authorized");
}
}
}
public static void Main(string[] args)
{
var auth = BlockadeLabsAuthentication.LoadFromEnvironment();
using var blockadeLabsClient = new BlockadeLabsClient(auth);
BlockadeLabsProxy.CreateWebApplication<AuthenticationFilter>(args, blockadeLabsClient).Run();
}
}
Once you have set up your proxy server, your end users can now make authenticated requests to your proxy api instead of directly to the BlockadeLabs API. The proxy server will handle authentication and forward requests to the BlockadeLabs API, ensuring that your API keys and other sensitive information remain secure.
Returns the list of predefined styles that can influence the overall aesthetic of your skybox generation.
var api = new BlockadeLabsClient();
var skyboxStyles = await api.SkyboxEndpoint.GetSkyboxStylesAsync(SkyboxModel.Model3);
foreach (var skyboxStyle in skyboxStyles)
{
Debug.Log($"{skyboxStyle.Name}");
}
Returns the list of predefined styles that can influence the overall aesthetic of your skybox generation, sorted by style family. This route can be used in order to build a menu of styles sorted by family.
var api = new BlockadeLabsClient();
var skyboxFamilyStyles = await BlockadeLabsClient.SkyboxEndpoint.GetSkyboxStyleFamiliesAsync(SkyboxModel.Model3);
foreach (var skyboxStyle in skyboxFamilyStyles)
{
Debug.Log($"{skyboxStyle.Name}");
}
Returns the list of all available export types.
var api = new BlockadeLabsClient();
var exportOptions = await api.SkyboxEndpoint.GetAllSkyboxExportOptionsAsync();
foreach (var exportOption in exportOptions)
{
Debug.Log($"{exportOption.Id}: {exportOption.Name} | {exportOption.Key}");
}
var request = new SkyboxRequest("mars", enhancePrompt: true);
// Generates ALL export options for the skybox
var skyboxInfo = await api.SkyboxEndpoint.GenerateSkyboxAsync(request, exportOptions);
Debug.Log($"Successfully created skybox: {skyboxInfo.Id}");
Generate a skybox.
var api = new BlockadeLabsClient();
var skyboxStyles = await BlockadeLabsClient.SkyboxEndpoint.GetSkyboxStylesAsync(SkyboxModel.Model3);
var request = new SkyboxRequest(skyboxStyles.First(), "mars", enhancePrompt: true);
// You can also get progress callbacks when the generation progress has changed/updated
var progress = new Progress<SkyboxInfo>(async progress =>
{
Debug.Log(progress);
});
var skyboxInfo = await api.SkyboxEndpoint.GenerateSkyboxAsync(request, progressCallback: progress);
Debug.Log($"Successfully created skybox: {skyboxInfo.Id}");
if (skyboxInfo.TryGetAsset<Texture2D>(SkyboxExportOption.Equirectangular_PNG, out var texture))
{
skyboxMaterial.mainTexture = texture;
}
Returns the skybox metadata for the given skybox id.
var skyboxId = 42;
var api = new BlockadeLabsClient();
var skyboxInfo = await api.SkyboxEndpoint.GetSkyboxInfoAsync(skyboxId);
Debug.Log($"Skybox: {result.Id}");
// Note: If you wish to use the returned skybox info textures you'll need to additionally call await SkyboxInfo.LoadAssetsAsync(); before you can assign them to a material property.
await skyboxInfo.LoadAssetsAsync();
if (skyboxInfo.TryGetAsset<Texture2D>(SkyboxExportOption.Equirectangular_PNG, out var texture))
{
skyboxMaterial.mainTexture = texture;
}
Exports the skybox with the requested export type.
Note
You can also specify the export types when initially generating a skybox.
var skyboxId = 42;
var api = new BlockadeLabsClient();
var skyboxInfo = await api.SkyboxEndpoint.GetSkyboxInfoAsync(skyboxId);
skyboxInfo = await api.SkyboxEndpoint.ExportSkyboxAsync(skyboxInfo, DefaultExportOptions.DepthMap_PNG);
await skyboxInfo.LoadAssetsAsync();
if (skyboxInfo.TryGetAsset<Texture2D>(SkyboxExportOption.DepthMap_PNG, out var texture))
{
skyboxMaterial.depthTexture = texture;
}
Deletes a skybox by id.
var skyboxId = 42;
var result = await api.SkyboxEndpoint.DeleteSkyboxAsync(skybox);
// result == true
Gets the previously generated skyboxes.
var history = await api.SkyboxEndpoint.GetSkyboxHistoryAsync();
Debug.Log($"Found {history.TotalCount} skyboxes");
foreach (var skybox in history.Skyboxes)
{
Debug.Log($"{skybox.Id} {skybox.Title} status: {skybox.Status}");
}
Cancels a pending skybox generation request by id.
var skyboxId = 42;
var result = await CancelSkyboxGenerationAsync(skyboxId);
// result == true
Note
This is automatically done when cancelling a skybox generation using cancellation token.
Cancels ALL pending skybox generation requests.
var result = await api.SkyboxEndpoint.CancelAllPendingSkyboxGenerationsAsync();
Debug.Log(result ? "All pending generations successfully cancelled" : "No pending generations");