-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathContentView.swift
182 lines (137 loc) · 6.09 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// ContentView.swift
// Dream
//
// Created by Sigil Wen on 2023-07-15.
//
import SwiftUI
import RealityKit
import RealityKitContent
import SceneKit
// import SDWebImageSwiftUI
struct ContentView: View {
@State var showImmersiveSpace = false
@Environment(\.openImmersiveSpace) var openImmersiveSpace
@Environment(\.dismissImmersiveSpace) var dismissImmersiveSpace
var scaleAISpellbookCaller: ScaleAISpellbookCaller = ScaleAISpellbookCaller()
@ObservedObject var modalShapE: ModalShapETextTo3D = ModalShapETextTo3D()
var body: some View {
VStack {
Model3D(named: "dream_team")
.scaleEffect(x:10, y:10, z: 10)
.padding()
.colorMultiply(.blue)
.offset(x:0, y: -50)
// SceneKitView()
// .frame(width: 300, height: 300, alignment: .center)
TextField("Enter text here", text: $modalShapE.text3DPrompt)
.multilineTextAlignment(.center)
.padding(.top, 100)
.foregroundColor(.white)
Button(action: {
Task {
do {
print("WE GO JIM")
modalShapE.size = nil
modalShapE.generated3D = nil
modalShapE.gifURL = nil
modalShapE.isGenerating = true
if modalShapE.text3DPrompt == "" {
modalShapE.text3DPrompt = "cute kitty"
}
let res = try await modalShapE.postRequest(prompt: modalShapE.text3DPrompt)
let jsonData = Data(res.utf8)
let decoder = JSONDecoder()
let urls = try decoder.decode([URL].self, from: jsonData)
// Use the list of URLs
for url in urls {
print("URL: \(url)")
}
print(res)
modalShapE.generated3D = URL( string: "https://3dviewer.net/#model=" + urls[1].absoluteString)
modalShapE.gifURL = urls[0]
// // Calling Scale AI Spellbook
let response = try await scaleAISpellbookCaller.callAPI(input: modalShapE.text3DPrompt)
print(response.output)
if let floatValue = Float(response.output) {
print(floatValue) // Prints 3.0
modalShapE.size = floatValue
} else {
print("Invalid float value")
}
modalShapE.isGenerating = false
} catch {
modalShapE.isGenerating = false
print("Failed to call API: \(error)")
}
print("Button tapped!")
}
}) {
Text("Generate")
.foregroundColor(.white)
.padding()
.cornerRadius(10)
}
if modalShapE.isGenerating {
LoadingView()
}
// if let generatedObject = modalShapE.generated3D {
// AnimatedImage(url: generatedObject)
// .resizable()
// .scaledToFit()
// .frame(width: 200, height: 200)
// }
// Toggle("Immersive", isOn: $showImmersiveSpace)
// .toggleStyle(.button)
if let generatedSize = modalShapE.size {
Text("Generated Size for \(modalShapE.text3DPrompt) is \(generatedSize) meters")
}
if let generatedSize = modalShapE.generated3D {
Button(action: {
// Open the link in Safari
UIApplication.shared.open(generatedSize)
}) {
Text("View Link")
}
}
if let generatedGif = modalShapE.gifURL {
Button(action: {
// Open the link in Safari
UIApplication.shared.open(generatedGif)
}) {
Text("View Link")
}
}
}
.padding()
.frame(width: 500)
.onChange(of: showImmersiveSpace) { _, newValue in
Task {
if newValue {
await openImmersiveSpace(id: "ImmersiveSpace")
} else {
await dismissImmersiveSpace()
}
}
}
}
}
#Preview {
ContentView()
}
//struct SceneKitView: UIViewRepresentable {
// let scene = SCNScene(named: "baloon.obj")!
//
// func makeUIView(context: Context) -> SCNView {
// let scnView = SCNView()
// scnView.scene = scene
// scnView.allowsCameraControl = true // For allowing camera control
// scnView.autoenablesDefaultLighting = true
// return scnView
// }
//
// func updateUIView(_ uiView: SCNView, context: Context) {
// // update your view
// print("hello")
// }
//}