first commit

This commit is contained in:
Nick Fisher
2021-09-15 20:07:11 +08:00
commit a0f877be48
292 changed files with 100157 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
#import <Flutter/Flutter.h>
@interface MimeticFilamentPlugin : NSObject<FlutterPlugin>
@end

View File

@@ -0,0 +1,12 @@
#import "MimeticFilamentPlugin.h"
#import "filament/FilamentNativeViewFactory.h"
FilamentNativeViewFactory* factory;
@implementation MimeticFilamentPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
factory =
[[FilamentNativeViewFactory alloc] initWithRegistrar:registrar];
[registrar registerViewFactory:factory withId:@"mimetic.app/filament_view"];
}
@end

View File

@@ -0,0 +1,29 @@
#ifndef FilamentNativeViewFactory_h
#define FilamentNativeViewFactory_h
#endif /* FilamentNativeViewFactory_h */
#import <Flutter/Flutter.h>
#import "FilamentViewer.hpp"
@interface FilamentMethodCallHandler : FlutterMethodChannel
- (void)handleMethodCall:(FlutterMethodCall* _Nonnull)call result:( FlutterResult _Nonnull)result;
- (mimetic::FilamentViewer*) _viewer;
@end
@interface FilamentNativeViewFactory : NSObject <FlutterPlatformViewFactory>
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar;
- (mimetic::ResourceBuffer)loadResource:(const char* const)path;
- (void)freeResource:(void*)mem size:(size_t)size misc:(void*)misc;
@end
@interface FilamentNativeView : NSObject <FlutterPlatformView>
- (instancetype)initWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args
registrar:(NSObject<FlutterPluginRegistrar>*)registrar;
- (UIView*)view;
@end

View File

@@ -0,0 +1,125 @@
#import "FilamentNativeViewFactory.h"
#import "FilamentViewController.h"
static const id VIEW_TYPE = @"mimetic.app/filament_view";
static const FilamentNativeViewFactory* _factory;
static mimetic::ResourceBuffer loadResource(const char* const name) {
return [_factory loadResource:name];
}
static void* freeResource(void* mem, size_t size, void* misc) {
[_factory freeResource:mem size:size misc:misc ];
return nullptr;
}
@implementation FilamentNativeViewFactory {
NSObject<FlutterPluginRegistrar>* _registrar;
}
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
self = [super init];
if (self) {
_registrar = registrar;
}
_factory = self;
return self;
}
- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args {
return [[FilamentNativeView alloc] initWithFrame:frame
viewIdentifier:viewId
arguments:args
registrar:_registrar];
}
@end
@implementation FilamentMethodCallHandler {
FilamentViewController *_controller;
FlutterMethodChannel* _channel;
mimetic::FilamentViewer* _viewer;
void* _layer;
}
- (instancetype)initWithController:(FilamentViewController*)controller
registrar:(NSObject<FlutterPluginRegistrar>*)registrar
viewId:(int64_t)viewId
layer:(void*)layer
{
_layer = layer;
_controller = controller;
NSString* channelName = [NSString stringWithFormat:@"%@_%d",VIEW_TYPE,viewId];
_channel = [FlutterMethodChannel
methodChannelWithName:channelName
binaryMessenger:[registrar messenger]];
[_channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) {
[self handleMethodCall:call result:result];
}];
return self;
}
- (void)handleMethodCall:(FlutterMethodCall* _Nonnull)call result:(FlutterResult _Nonnull )result {
if([@"initialize" isEqualToString:call.method]) {
[self initialize];
} else {
result(FlutterMethodNotImplemented);
}
}
- (mimetic::ResourceBuffer)loadResource:(const char* const)path {
NSString* documentPath = [NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString* pathComponent = [NSString stringWithUTF8String:path];
NSString* nsPath = [documentPath stringByAppendingPathComponent:pathComponent];
if (![[NSFileManager defaultManager] fileExistsAtPath:nsPath]) {
NSLog(@"Error: no file exists at %@", nsPath);
exit(-1);
}
NSData* buffer = [NSData dataWithContentsOfFile:nsPath];
mimetic::ResourceBuffer rbuf([buffer bytes], [buffer length]);
return rbuf;
}
- (void)freeResource:(void*)mem size:(size_t)s misc:(void *)m {
// TODO
}
-(void)initialize {
_viewer = new mimetic::FilamentViewer(_layer, loadResource, freeResource);
}
@end
@implementation FilamentNativeView {
FilamentView *_view;
FilamentViewController *_controller;
FilamentMethodCallHandler *_handler;
}
- (instancetype)initWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args
registrar:(NSObject<FlutterPluginRegistrar>*)registrar {
if (self = [super init]) {
_view = [[FilamentView alloc] init];
_controller = [[FilamentViewController alloc] initWithRegistrar:registrar];
_controller.modelView = _view;
[_controller viewDidLoad];
[_controller startDisplayLink];
_handler = [[FilamentMethodCallHandler alloc] initWithController:_controller registrar:registrar viewId:viewId layer:(__bridge void*)[_view layer]];
}
return self;
}
- (UIView*)view {
return _view;
}
@end

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
/**
* FILModelView is simply a UIView with an OpenGL layer.
*
*
*/
@interface FilamentView : UIView
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// These defines are set in the "Preprocessor Macros" build setting for each scheme.
#include "FilamentView.h"
#import <Foundation/Foundation.h>
using namespace std;
@interface FilamentView ()
- (void)initCommon;
@end
@implementation FilamentView {
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initCommon];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder*)coder {
if (self = [super initWithCoder:coder]) {
[self initCommon];
}
return self;
}
- (void)initCommon {
[self initializeGLLayer];
}
- (void)initializeGLLayer {
CAEAGLLayer* glLayer = (CAEAGLLayer*)self.layer;
glLayer.opaque = YES;
}
+ (Class)layerClass {
return [CAEAGLLayer class];
}
@end

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <UIKit/UIKit.h>
#import "FilamentView.h"
#import "Flutter/Flutter.h"
@interface FilamentViewController : UIViewController
@property(weak, nonatomic) IBOutlet FilamentView* modelView;
- (void)startDisplayLink;
- (void)stopDisplayLink;
-initWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar;
@end

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FilamentViewController.h"
#import "FilamentView.h"
#import <Flutter/Flutter.h>
@implementation FilamentViewController {
CADisplayLink* _displayLink;
NSObject<FlutterPluginRegistrar>* _registrar;
}
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
if (self = [super init]) {
_registrar = registrar;
}
return self;
}
#pragma mark UIViewController methods
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
[self startDisplayLink];
}
- (void)viewWillDisappear:(BOOL)animated {
[self stopDisplayLink];
}
- (void)startDisplayLink {
[self stopDisplayLink];
// Call our render method 60 times a second.
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render)];
_displayLink.preferredFramesPerSecond = 60;
[_displayLink addToRunLoop:NSRunLoop.currentRunLoop forMode:NSDefaultRunLoopMode];
}
- (void)stopDisplayLink {
[_displayLink invalidate];
_displayLink = nil;
}
- (void)render {
}
- (void)dealloc {
}
@end