-
Notifications
You must be signed in to change notification settings - Fork 61
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
f130f89
commit 865c6c1
Showing
18 changed files
with
345 additions
and
193 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
Modules/@babylonjs/react-native-iosandroid/ios/BabylonEngineView.h
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,17 @@ | ||
#pragma once | ||
#import <UIKit/UIKit.h> | ||
#import <MetalKit/MetalKit.h> | ||
#import <React/RCTBridge.h> | ||
#import <React/RCTComponent.h> | ||
|
||
@interface EngineView : MTKView | ||
|
||
@property (nonatomic, copy) RCTDirectEventBlock onSnapshotDataReturned; | ||
@property (nonatomic, assign) BOOL isTransparent; | ||
@property (nonatomic, assign) NSNumber* antiAliasing; | ||
|
||
- (void)setMSAA:(NSNumber*)value; | ||
- (void)takeSnapshot; | ||
- (void)setIsTransparentFlag:(NSNumber*)isTransparentFlag; | ||
|
||
@end |
96 changes: 96 additions & 0 deletions
96
Modules/@babylonjs/react-native-iosandroid/ios/BabylonEngineView.mm
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,96 @@ | ||
#import "BabylonEngineView.h" | ||
#include "BabylonNativeInterop.h" | ||
|
||
@implementation EngineView { | ||
MTKView* xrView; | ||
} | ||
|
||
- (instancetype)init { | ||
if (self = [super initWithFrame:CGRectZero device:MTLCreateSystemDefaultDevice()]) { | ||
super.translatesAutoresizingMaskIntoConstraints = false; | ||
super.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB; | ||
super.depthStencilPixelFormat = MTLPixelFormatDepth32Float; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)setIsTransparentFlag:(NSNumber*)isTransparentFlag { | ||
BOOL isTransparent = [isTransparentFlag intValue] == 1; | ||
if(isTransparent){ | ||
[self setOpaque:NO]; | ||
} else { | ||
[self setOpaque:YES]; | ||
} | ||
self.isTransparent = isTransparent; | ||
} | ||
|
||
- (void)setMSAA:(NSNumber*)value { | ||
[BabylonNativeInterop updateMSAA:value]; | ||
} | ||
|
||
- (void)setBounds:(CGRect)bounds { | ||
[super setBounds:bounds]; | ||
[BabylonNativeInterop updateView:self]; | ||
} | ||
|
||
- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event { | ||
[BabylonNativeInterop reportTouchEvent:self touches:touches event:event]; | ||
} | ||
|
||
- (void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event { | ||
[BabylonNativeInterop reportTouchEvent:self touches:touches event:event]; | ||
} | ||
|
||
- (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event { | ||
[BabylonNativeInterop reportTouchEvent:self touches:touches event:event]; | ||
} | ||
|
||
- (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event { | ||
[BabylonNativeInterop reportTouchEvent:self touches:touches event:event]; | ||
} | ||
|
||
- (void)drawRect:(CGRect)rect { | ||
if ([BabylonNativeInterop isXRActive]) { | ||
if (!xrView) { | ||
xrView = [[MTKView alloc] initWithFrame:self.bounds device:self.device]; | ||
xrView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | ||
xrView.userInteractionEnabled = false; | ||
[self addSubview:xrView]; | ||
[BabylonNativeInterop updateXRView:xrView]; | ||
} | ||
} else if (xrView) { | ||
[BabylonNativeInterop updateXRView:nil]; | ||
[xrView removeFromSuperview]; | ||
xrView = nil; | ||
} | ||
|
||
[BabylonNativeInterop renderView]; | ||
} | ||
|
||
-(void)dealloc { | ||
[BabylonNativeInterop updateXRView:nil]; | ||
} | ||
|
||
- (void)takeSnapshot { | ||
// We must take the screenshot on the main thread otherwise we might fail to get a valid handle on the view's image. | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
// Start the graphics context. | ||
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES /* opaque */, 0.0f); | ||
|
||
// Draw the current state of the view into the graphics context. | ||
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO]; | ||
|
||
// Grab the image from the graphics context, and convert into a base64 encoded JPG. | ||
UIImage* capturedImage = UIGraphicsGetImageFromCurrentImageContext(); | ||
UIGraphicsEndImageContext(); | ||
NSData* jpgData = UIImageJPEGRepresentation(capturedImage, .8f); | ||
NSString* encodedData = [jpgData base64EncodedStringWithOptions:0]; | ||
|
||
// Fire the onSnapshotDataReturned event if hooked up. | ||
if (self.onSnapshotDataReturned != nil) { | ||
self.onSnapshotDataReturned(@{ @"data":encodedData}); | ||
} | ||
}); | ||
} | ||
|
||
@end |
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
Oops, something went wrong.