1
1
// Licensed under the MIT License. See LICENSE in the project root for license information.
2
2
3
3
using Newtonsoft . Json ;
4
+ using System ;
4
5
using System . Collections . Generic ;
6
+ using System . IO ;
5
7
using System . Threading ;
6
8
using System . Threading . Tasks ;
9
+ using UnityEngine ;
10
+ using UnityEngine . Scripting ;
7
11
using Utilities . WebRequestRest ;
8
12
9
13
namespace BlockadeLabs . Skyboxes
10
14
{
11
15
public sealed class SkyboxEndpoint : BlockadeLabsBaseEndpoint
12
16
{
17
+ [ Preserve ]
18
+ private class SkyboxInfoRequest
19
+ {
20
+ [ Preserve ]
21
+ [ JsonConstructor ]
22
+ public SkyboxInfoRequest ( [ JsonProperty ( "request" ) ] SkyboxInfo skyboxInfo )
23
+ {
24
+ SkyboxInfo = skyboxInfo ;
25
+ }
26
+
27
+ [ Preserve ]
28
+ [ JsonProperty ( "request" ) ]
29
+ public SkyboxInfo SkyboxInfo { get ; }
30
+ }
31
+
13
32
public SkyboxEndpoint ( BlockadeLabsClient client ) : base ( client ) { }
14
33
15
- protected override string Root => "skybox" ;
34
+ protected override string Root => string . Empty ;
16
35
17
36
/// <summary>
18
37
/// Returns the list of predefined styles that can influence the overall aesthetic of your skybox generation.
19
38
/// </summary>
20
- /// <param name="cancellationToken"></param>
39
+ /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>. </param>
21
40
/// <returns>A list of <see cref="SkyboxStyle"/>s.</returns>
22
41
public async Task < IReadOnlyList < SkyboxStyle > > GetSkyboxStylesAsync ( CancellationToken cancellationToken = default )
23
42
{
24
- var endpoint = GetUrl ( "/styles" ) ;
43
+ var endpoint = GetUrl ( "skybox /styles" ) ;
25
44
var response = await Rest . GetAsync ( endpoint , parameters : new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
26
45
response . Validate ( ) ;
27
46
return JsonConvert . DeserializeObject < IReadOnlyList < SkyboxStyle > > ( response . Body , client . JsonSerializationOptions ) ;
28
47
}
29
48
30
49
/// <summary>
31
- /// Generate a skybox image
50
+ /// Generate a skybox image.
32
51
/// </summary>
33
- /// <param name="skyboxRequest"></param>
34
- /// <param name="cancellationToken"></param>
35
- /// <returns></returns>
36
- public async Task < SkyboxInfo > GenerateSkyboxAsync ( SkyboxRequest skyboxRequest , CancellationToken cancellationToken = default )
52
+ /// <param name="skyboxRequest"><see cref="SkyboxRequest"/>.</param>
53
+ /// <param name="pollingInterval">Optional, polling interval in seconds.</param>
54
+ /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
55
+ /// <returns><see cref="SkyboxInfo"/>.</returns>
56
+ public async Task < SkyboxInfo > GenerateSkyboxAsync ( SkyboxRequest skyboxRequest , int ? pollingInterval = null , CancellationToken cancellationToken = default )
37
57
{
38
- var jsonContent = JsonConvert . SerializeObject ( skyboxRequest , client . JsonSerializationOptions ) ;
39
- var response = await Rest . PostAsync ( GetUrl ( "/generate" ) , jsonContent , parameters : new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
58
+ var formData = new WWWForm ( ) ;
59
+ formData . AddField ( "prompt" , skyboxRequest . Prompt ) ;
60
+
61
+ if ( ! string . IsNullOrWhiteSpace ( skyboxRequest . NegativeText ) )
62
+ {
63
+ formData . AddField ( "negative_text" , skyboxRequest . NegativeText ) ;
64
+ }
65
+
66
+ if ( skyboxRequest . Seed . HasValue )
67
+ {
68
+ formData . AddField ( "seed" , skyboxRequest . Seed . Value ) ;
69
+ }
70
+
71
+ if ( skyboxRequest . SkyboxStyleId . HasValue )
72
+ {
73
+ formData . AddField ( "skybox_style_id" , skyboxRequest . SkyboxStyleId . Value ) ;
74
+ }
75
+
76
+ if ( skyboxRequest . RemixImagineId . HasValue )
77
+ {
78
+ formData . AddField ( "remix_imagine_id" , skyboxRequest . RemixImagineId . Value ) ;
79
+ }
80
+
81
+ if ( skyboxRequest . Depth )
82
+ {
83
+ formData . AddField ( "return_depth" , skyboxRequest . Depth . ToString ( ) ) ;
84
+ }
85
+
86
+ if ( skyboxRequest . ControlImage != null )
87
+ {
88
+ if ( ! string . IsNullOrWhiteSpace ( skyboxRequest . ControlModel ) )
89
+ {
90
+ formData . AddField ( "control_model" , skyboxRequest . ControlModel ) ;
91
+ }
92
+
93
+ using var imageData = new MemoryStream ( ) ;
94
+ await skyboxRequest . ControlImage . CopyToAsync ( imageData , cancellationToken ) ;
95
+ formData . AddBinaryData ( "control_image" , imageData . ToArray ( ) , skyboxRequest . ControlImageFileName ) ;
96
+ skyboxRequest . Dispose ( ) ;
97
+ }
98
+
99
+ var response = await Rest . PostAsync ( GetUrl ( "skybox" ) , formData , parameters : new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
40
100
response . Validate ( ) ;
41
101
var skyboxInfo = JsonConvert . DeserializeObject < SkyboxInfo > ( response . Body , client . JsonSerializationOptions ) ;
42
102
103
+ while ( ! cancellationToken . IsCancellationRequested )
104
+ {
105
+ await Task . Delay ( pollingInterval ?? 3 * 1000 , cancellationToken )
106
+ . ConfigureAwait ( true ) ; // Configure await to make sure we're still in Unity context
107
+ skyboxInfo = await GetSkyboxInfoAsync ( skyboxInfo , cancellationToken ) ;
108
+
109
+ if ( skyboxInfo . Status is "pending" or "processing" )
110
+ {
111
+ continue ;
112
+ }
113
+
114
+ break ;
115
+ }
116
+
117
+ if ( skyboxInfo . Status != "complete" )
118
+ {
119
+ throw new Exception ( $ "Failed to generate skybox! { skyboxInfo . Id } -> { skyboxInfo . Status } \n Error: { skyboxInfo . ErrorMessage } \n { skyboxInfo } ") ;
120
+ }
121
+
43
122
var downloadTasks = new List < Task > ( 2 )
44
123
{
45
124
Task . Run ( async ( ) =>
@@ -62,14 +141,14 @@ public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, C
62
141
/// <summary>
63
142
/// Returns the skybox metadata for the given skybox id.
64
143
/// </summary>
65
- /// <param name="id"></param>
66
- /// <param name="cancellationToken"></param>
144
+ /// <param name="id">Skybox Id. </param>
145
+ /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>. </param>
67
146
/// <returns><see cref="SkyboxInfo"/>.</returns>
68
147
public async Task < SkyboxInfo > GetSkyboxInfoAsync ( int id , CancellationToken cancellationToken = default )
69
148
{
70
- var response = await Rest . GetAsync ( GetUrl ( $ "/info /{ id } ") , parameters : new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
149
+ var response = await Rest . GetAsync ( GetUrl ( $ "imagine/requests /{ id } ") , parameters : new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
71
150
response . Validate ( ) ;
72
- return JsonConvert . DeserializeObject < SkyboxInfo > ( response . Body , client . JsonSerializationOptions ) ;
151
+ return JsonConvert . DeserializeObject < SkyboxInfoRequest > ( response . Body , client . JsonSerializationOptions ) . SkyboxInfo ;
73
152
}
74
153
}
75
154
}
0 commit comments