-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0c9707
commit 8731cae
Showing
18 changed files
with
452 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import SwiftUI | ||
import CompositorServices | ||
|
||
struct ContentStageConfiguration: CompositorLayerConfiguration { | ||
func makeConfiguration(capabilities: LayerRenderer.Capabilities, configuration: inout LayerRenderer.Configuration) { | ||
configuration.depthFormat = .depth32Float | ||
configuration.colorFormat = .bgra8Unorm_srgb | ||
|
||
let foveationEnabled = capabilities.supportsFoveation | ||
configuration.isFoveationEnabled = foveationEnabled | ||
|
||
let options: LayerRenderer.Capabilities.SupportedLayoutsOptions = foveationEnabled ? [.foveationEnabled] : [] | ||
let supportedLayouts = capabilities.supportedLayouts(options: options) | ||
|
||
configuration.layout = supportedLayouts.contains(.layered) ? .layered : .dedicated | ||
} | ||
} | ||
|
||
class Renderer { | ||
let layerRenderer: LayerRenderer | ||
var libNativeBridge: LibNativeBridge | ||
|
||
init(_ layerRenderer: LayerRenderer) { | ||
self.layerRenderer = layerRenderer | ||
self.libNativeBridge = LibNativeBridge(layerRenderer) | ||
} | ||
|
||
func startRenderLoop() { | ||
let renderThread = Thread { | ||
self.renderLoop() | ||
} | ||
renderThread.name = "Render Thread" | ||
renderThread.start() | ||
} | ||
|
||
|
||
func renderLoop() { | ||
while true { | ||
if layerRenderer.state == .invalidated { | ||
print("Layer is invalidated") | ||
|
||
libNativeBridge.shutdown() | ||
return | ||
} else if layerRenderer.state == .paused { | ||
layerRenderer.waitUntilRunning() | ||
continue | ||
} else { | ||
autoreleasepool { | ||
libNativeBridge.initialize() | ||
libNativeBridge.render() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
@main | ||
struct ExampleApp: App { | ||
@State private var showImmersiveSpace = false | ||
@State private var immersiveSpaceIsShown = false | ||
|
||
@Environment(\.openImmersiveSpace) var openImmersiveSpace | ||
@Environment(\.dismissImmersiveSpace) var dismissImmersiveSpace | ||
|
||
var body: some Scene { | ||
WindowGroup { | ||
VStack { | ||
Toggle("Show Immersive Space", isOn: $showImmersiveSpace) | ||
.toggleStyle(.button) | ||
.padding(.top, 50) | ||
} | ||
.onChange(of: showImmersiveSpace) { _, newValue in | ||
Task { | ||
if newValue { | ||
switch await openImmersiveSpace(id: "ImmersiveSpace") { | ||
case .opened: | ||
immersiveSpaceIsShown = true | ||
case .error, .userCancelled: | ||
fallthrough | ||
@unknown default: | ||
immersiveSpaceIsShown = false | ||
showImmersiveSpace = false | ||
} | ||
} else if immersiveSpaceIsShown { | ||
await dismissImmersiveSpace() | ||
immersiveSpaceIsShown = false | ||
} | ||
} | ||
} | ||
|
||
} | ||
ImmersiveSpace(id: "ImmersiveSpace") { | ||
CompositorLayer(configuration: ContentStageConfiguration()) { layerRenderer in | ||
let renderer = Renderer(layerRenderer) | ||
renderer.startRenderLoop() | ||
} | ||
}.immersionStyle(selection: .constant(.mixed), in: .mixed, .full) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>NSPhotoLibraryAddUsageDescription</key> | ||
<string>Need photo library permission for debug code to save photo captures</string> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>$(DEVELOPMENT_LANGUAGE)</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>APPL</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>1</string> | ||
<key>UIApplicationSceneManifest</key> | ||
<dict> | ||
<key>UIApplicationPreferredDefaultSceneSessionRole</key> | ||
<string>UIWindowSceneSessionRoleApplication</string> | ||
<key>UIApplicationSupportsMultipleScenes</key> | ||
<true/> | ||
<key>UISceneConfigurations</key> | ||
<dict/> | ||
</dict> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#import "LibNativeBridge.hpp" | ||
#import <Babylon/AppRuntime.h> | ||
#import <Babylon/Graphics/Device.h> | ||
#import <Babylon/ScriptLoader.h> | ||
#import <Babylon/Plugins/NativeEngine.h> | ||
#import <Babylon/Plugins/NativeInput.h> | ||
#import <Babylon/Plugins/NativeOptimizations.h> | ||
#import <Babylon/Plugins/NativeXr.h> | ||
#import <Babylon/Polyfills/Canvas.h> | ||
#import <Babylon/Polyfills/Console.h> | ||
#import <Babylon/Polyfills/Window.h> | ||
#import <Babylon/Polyfills/XMLHttpRequest.h> | ||
|
||
std::optional<Babylon::Graphics::Device> device{}; | ||
std::optional<Babylon::Graphics::DeviceUpdate> update{}; | ||
std::optional<Babylon::AppRuntime> runtime{}; | ||
std::optional<Babylon::Polyfills::Canvas> nativeCanvas{}; | ||
std::optional<Babylon::Plugins::NativeXr> nativeXr{}; | ||
Babylon::Plugins::NativeInput* nativeInput{}; | ||
bool isXrActive{}; | ||
|
||
|
||
bool LibNativeBridge::initialize() { | ||
if (m_initialized) { | ||
return true; | ||
} | ||
|
||
Babylon::Graphics::Configuration graphicsConfig{}; | ||
graphicsConfig.Window = m_layerRenderer; | ||
// Pass in visionOS default widht and height. | ||
#if TARGET_OS_SIMULATOR | ||
graphicsConfig.Width = static_cast<size_t>(2732); | ||
graphicsConfig.Height = static_cast<size_t>(2048); | ||
#else | ||
graphicsConfig.Width = static_cast<size_t>(1920); | ||
graphicsConfig.Height = static_cast<size_t>(1824); | ||
#endif | ||
|
||
device.emplace(graphicsConfig); | ||
update.emplace(device->GetUpdate("update")); | ||
|
||
device->StartRenderingCurrentFrame(); | ||
update->Start(); | ||
|
||
runtime.emplace(); | ||
|
||
runtime->Dispatch([](Napi::Env env) | ||
{ | ||
device->AddToJavaScript(env); | ||
|
||
Babylon::Polyfills::Console::Initialize(env, [](const char* message, auto) { | ||
NSLog(@"%s", message); | ||
}); | ||
|
||
nativeCanvas.emplace(Babylon::Polyfills::Canvas::Initialize(env)); | ||
|
||
Babylon::Polyfills::Window::Initialize(env); | ||
|
||
Babylon::Polyfills::XMLHttpRequest::Initialize(env); | ||
|
||
Babylon::Plugins::NativeEngine::Initialize(env); | ||
|
||
Babylon::Plugins::NativeOptimizations::Initialize(env); | ||
// nativeXr.emplace(Babylon::Plugins::NativeXr::Initialize(env)); | ||
// nativeXr->UpdateWindow((__bridge void*)m_layerRenderer); | ||
|
||
nativeInput = &Babylon::Plugins::NativeInput::CreateForJavaScript(env); | ||
}); | ||
|
||
Babylon::ScriptLoader loader{ *runtime }; | ||
loader.LoadScript("app:///Scripts/ammo.js"); | ||
loader.LoadScript("app:///Scripts/recast.js"); | ||
loader.LoadScript("app:///Scripts/babylon.max.js"); | ||
loader.LoadScript("app:///Scripts/babylonjs.loaders.js"); | ||
loader.LoadScript("app:///Scripts/babylonjs.materials.js"); | ||
loader.LoadScript("app:///Scripts/babylon.gui.js"); | ||
loader.LoadScript("app:///Scripts/experience.js"); | ||
m_initialized = true; | ||
|
||
return true; | ||
} | ||
|
||
void LibNativeBridge::render() { | ||
if (device && m_initialized) | ||
{ | ||
update->Finish(); | ||
device->FinishRenderingCurrentFrame(); | ||
device->StartRenderingCurrentFrame(); | ||
update->Start(); | ||
} | ||
} | ||
|
||
void LibNativeBridge::shutdown() { | ||
if (!m_initialized) { | ||
return; | ||
} | ||
|
||
if (device) | ||
{ | ||
update->Finish(); | ||
device->FinishRenderingCurrentFrame(); | ||
} | ||
|
||
nativeInput = {}; | ||
nativeCanvas.reset(); | ||
runtime.reset(); | ||
update.reset(); | ||
device.reset(); | ||
m_initialized = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef BgfxAdapter_hpp | ||
#define BgfxAdapter_hpp | ||
|
||
#include <CompositorServices/CompositorServices.h> | ||
#include <ARKit/ARKit.h> | ||
|
||
class LibNativeBridge { | ||
private: | ||
bool m_initialized = false; | ||
cp_layer_renderer_t m_layerRenderer = NULL; | ||
|
||
public: | ||
LibNativeBridge(cp_layer_renderer_t layerRenderer) : m_layerRenderer(layerRenderer) { | ||
} | ||
|
||
~LibNativeBridge() { | ||
shutdown(); | ||
} | ||
|
||
bool initialize(void); | ||
void shutdown(void); | ||
void render(void); | ||
}; | ||
|
||
#endif /* BgfxAdapter_hpp */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.