diff --git a/ios/RNGLModelViewer.h b/ios/RNGLModelViewer.h index f2591a9..ea14fd7 100644 --- a/ios/RNGLModelViewer.h +++ b/ios/RNGLModelViewer.h @@ -1,11 +1,84 @@ +// +// react-native-gl-model-viewer +// https://github.com/rastapasta/react-native-gl-model-viewer +// +// RNGLModelViewer.m +// -#if __has_include("RCTBridgeModule.h") -#import "RCTBridgeModule.h" -#else -#import -#endif +#import "RNGLModelViewer.h" -@interface RNGLModelViewer : NSObject +@implementation RNGLModelViewer + +- (void)didMoveToWindow +{ + [super didMoveToWindow]; + + // Setup GLViewModel + self.blendColor = nil; + + if (self.window) { + // Render with our applied props! + [self display]; + + // Restart animation in case it got stopped because of previous moveOut + if (self.animated && !self.isAnimating) { + [self startAnimating]; + } + } else { + // Stop the animation loop until the View gets moved back in + if (self.isAnimating) { + [self stopAnimating]; + } + } +} + +// Apply the GLViewModel updates before any render outside of the animation loop +- (void)display +{ + [self applyProps]; + [super display]; +} + +// GLViewModel render loop, gets called self.frameInterval times per second +- (void)step:(NSTimeInterval)dt +{ + [self applyProps]; +} + +// Apply the current known values of our (maybe Native Animated) props +- (void)applyProps +{ + CATransform3D transform = CATransform3DMakeTranslation(self.translateX, + self.translateY, + self.translateZ); + if (self.scale != 0) { + transform = CATransform3DScale(transform, + self.scale, + self.scale, + self.scale); + } else { + transform = CATransform3DScale(transform, + self.scaleX, + self.scaleY, + self.scaleZ); + } + + if (self.rotateX != 0) + transform = CATransform3DRotate(transform, + self.rotateX, + 1, 0, 0); + + if (self.rotateY != 0) + transform = CATransform3DRotate(transform, + self.rotateY, + 0, 1, 0); + + if (self.rotateZ != 0) + transform = CATransform3DRotate(transform, + self.rotateZ, + 0, 0, 1); + + self.modelTransform = transform; +} @end - \ No newline at end of file diff --git a/ios/RNGLModelViewer.m b/ios/RNGLModelViewer.m index aa006ca..dd63d34 100644 --- a/ios/RNGLModelViewer.m +++ b/ios/RNGLModelViewer.m @@ -1,13 +1,28 @@ +// +// react-native-gl-model-viewer +// https://github.com/rastapasta/react-native-gl-model-viewer +// +// RNGLModelViewer.h +// -#import "RNGLModelViewer.h" +#import -@implementation RNGLModelViewer +@interface RNGLModelViewer : GLModelView -- (dispatch_queue_t)methodQueue -{ - return dispatch_get_main_queue(); -} -RCT_EXPORT_MODULE() +@property (nonatomic, assign) BOOL animated; + +@property (nonatomic, assign) CGFloat rotateX; +@property (nonatomic, assign) CGFloat rotateY; +@property (nonatomic, assign) CGFloat rotateZ; + +@property (nonatomic, assign) CGFloat scale; + +@property (nonatomic, assign) CGFloat scaleX; +@property (nonatomic, assign) CGFloat scaleY; +@property (nonatomic, assign) CGFloat scaleZ; + +@property (nonatomic, assign) CGFloat translateX; +@property (nonatomic, assign) CGFloat translateY; +@property (nonatomic, assign) CGFloat translateZ; @end - \ No newline at end of file diff --git a/ios/RNGLModelViewerManager.h b/ios/RNGLModelViewerManager.h new file mode 100644 index 0000000..ebbf356 --- /dev/null +++ b/ios/RNGLModelViewerManager.h @@ -0,0 +1,18 @@ +// +// react-native-gl-model-viewer +// https://github.com/rastapasta/react-native-gl-model-viewer +// +// RNGLModelViewerManager.h +// + +#if __has_include("RCTViewManager.h") +#import "RCTViewManager.h" +#else +#import +#endif + +#import "RNGLModelViewer.h" + +@interface RNGLModelViewerManager : RCTViewManager + +@end diff --git a/ios/RNGLModelViewerManager.m b/ios/RNGLModelViewerManager.m new file mode 100644 index 0000000..47a781c --- /dev/null +++ b/ios/RNGLModelViewerManager.m @@ -0,0 +1,72 @@ +// +// react-native-gl-model-viewer +// https://github.com/rastapasta/react-native-gl-model-viewer +// +// RNGLModelViewerManager.m +// + +#if __has_include("RCTBridge.h") +#import "RCTBridge.h" +#else +#import +#endif + +#import "RNGLModelViewerManager.h" + +@implementation RNGLModelViewerManager +{ + RNGLModelViewer *glModelViewer; +} + +RCT_EXPORT_MODULE() + +- (UIView *)view +{ + glModelViewer = [[RNGLModelViewer alloc] init]; + return glModelViewer; +} + +RCT_EXPORT_VIEW_PROPERTY(rotateX, CGFloat); +RCT_EXPORT_VIEW_PROPERTY(rotateY, CGFloat); +RCT_EXPORT_VIEW_PROPERTY(rotateZ, CGFloat); + +RCT_EXPORT_VIEW_PROPERTY(scale, CGFloat); + +RCT_EXPORT_VIEW_PROPERTY(scaleX, CGFloat); +RCT_EXPORT_VIEW_PROPERTY(scaleY, CGFloat); +RCT_EXPORT_VIEW_PROPERTY(scaleZ, CGFloat); + +RCT_EXPORT_VIEW_PROPERTY(translateX, CGFloat); +RCT_EXPORT_VIEW_PROPERTY(translateY, CGFloat); +RCT_EXPORT_VIEW_PROPERTY(translateZ, CGFloat); + +// Loads a model - Wavefront (OBJ) or WWDC2010 model format +RCT_CUSTOM_VIEW_PROPERTY(model, NSString, RNGLModelViewer) +{ + view.model = [GLModel modelWithContentsOfFile:[RCTConvert NSString:json]]; +} + +// Loads a texture - PVR + all formats supported by UIImage +RCT_CUSTOM_VIEW_PROPERTY(texture, NSString, RNGLModelViewer) +{ + view.texture = [GLImage imageNamed:[RCTConvert NSString:json]]; +} + +// Starts or stops the animation loop to automatically re-render each 1/60 second +RCT_CUSTOM_VIEW_PROPERTY(animate, BOOL, RNGLModelViewer) +{ + view.animated = [RCTConvert BOOL:json]; + if (view.animated) { + [view startAnimating]; + } else { + [view stopAnimating]; + } +} + +// Applys the current props and rerenders the scene +RCT_EXPORT_METHOD(render) +{ + [glModelViewer display]; +} + +@end