Skip to content

Commit

Permalink
🍺 adding first version of RNGLModelViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
rastapasta committed Apr 15, 2017
1 parent 33d5499 commit 8bc25eb
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 15 deletions.
87 changes: 80 additions & 7 deletions ios/RNGLModelViewer.h
Original file line number Diff line number Diff line change
@@ -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 <React/RCTBridgeModule.h>
#endif
#import "RNGLModelViewer.h"

@interface RNGLModelViewer : NSObject <RCTBridgeModule>
@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

31 changes: 23 additions & 8 deletions ios/RNGLModelViewer.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
//
// react-native-gl-model-viewer
// https://github.com/rastapasta/react-native-gl-model-viewer
//
// RNGLModelViewer.h
//

#import "RNGLModelViewer.h"
#import <GLView/GLModelView.h>

@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

18 changes: 18 additions & 0 deletions ios/RNGLModelViewerManager.h
Original file line number Diff line number Diff line change
@@ -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 <React/RCTViewManager.h>
#endif

#import "RNGLModelViewer.h"

@interface RNGLModelViewerManager : RCTViewManager

@end
72 changes: 72 additions & 0 deletions ios/RNGLModelViewerManager.m
Original file line number Diff line number Diff line change
@@ -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 <React/RCTBridge.h>
#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

0 comments on commit 8bc25eb

Please sign in to comment.