Skip to content

Commit

Permalink
Add picking queries to gltf-viewer iOS sample (#7464)
Browse files Browse the repository at this point in the history
  • Loading branch information
bejado authored Jan 2, 2024
1 parent 2c9559f commit b4021a1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 6 deletions.
13 changes: 13 additions & 0 deletions ios/samples/gltf-viewer/gltf-viewer/FILModelView.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import <UIKit/UIKit.h>

#include <utils/Entity.h>

namespace filament {
class Engine;
class Scene;
Expand Down Expand Up @@ -76,6 +78,17 @@ typedef NSData* _Nonnull (^ResourceCallback)(NSString* _Nonnull);

- (void)destroyModel;

/**
* Issues a pick query at the given view coordinates.
* The coordinates should be in UIKit's coordinate system, with the origin at
* the top-left.
* The callback is triggered with entity of the picked object.
*/
typedef void (^PickCallback)(utils::Entity);
- (void)issuePickQuery:(CGPoint)point callback:(PickCallback)callback;

- (NSString* _Nullable)getEntityName:(utils::Entity)entity;

/**
* Sets up a root transform on the current model to make it fit into a unit cube.
*/
Expand Down
27 changes: 23 additions & 4 deletions ios/samples/gltf-viewer/gltf-viewer/FILModelView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,12 @@ - (void)initCommon {

_swapChain = _engine->createSwapChain((__bridge void*)self.layer);

_materialProvider = createUbershaderProvider(_engine,
UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
_materialProvider =
createUbershaderProvider(_engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
EntityManager& em = EntityManager::get();
NameComponentManager* ncm = new NameComponentManager(em);
_assetLoader = AssetLoader::create({_engine, _materialProvider, ncm, &em});
_resourceLoader = new ResourceLoader(
{.engine = _engine, .normalizeSkinningWeights = true});
_resourceLoader = new ResourceLoader({.engine = _engine, .normalizeSkinningWeights = true});
_stbDecoder = createStbProvider(_engine);
_ktxDecoder = createKtx2Provider(_engine);
_resourceLoader->addTextureProvider("image/png", _stbDecoder);
Expand Down Expand Up @@ -182,6 +181,26 @@ - (void)destroyModel {
_animator = nullptr;
}

- (void)issuePickQuery:(CGPoint)point callback:(PickCallback)callback {
CGPoint pointOriginBottomLeft = CGPointMake(point.x, self.bounds.size.height - point.y);
CGPoint pointScaled = CGPointMake(pointOriginBottomLeft.x * self.contentScaleFactor,
pointOriginBottomLeft.y * self.contentScaleFactor);
_view->pick(pointScaled.x, pointScaled.y,
[callback](View::PickingQueryResult const& result) { callback(result.renderable); });
}

- (NSString* _Nullable)getEntityName:(utils::Entity)entity {
if (!_asset) {
return nil;
}
NameComponentManager* ncm = _assetLoader->getNames();
NameComponentManager::Instance instance = ncm->getInstance(entity);
if (instance) {
return [NSString stringWithUTF8String:ncm->getName(ncm->getInstance(entity))];
}
return nil;
}

- (void)transformToUnitCube {
if (!_asset) {
return;
Expand Down
65 changes: 63 additions & 2 deletions ios/samples/gltf-viewer/gltf-viewer/FILViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#import "FILViewController.h"
#include <UIKit/UIKit.h>

#import "FILModelView.h"

Expand All @@ -35,6 +36,9 @@
using namespace utils;
using namespace ktxreader;

const float kToastAnimationDuration = 0.25f;
const float kToastDelayDuration = 2.0f;

@interface FILViewController ()

- (void)startDisplayLink;
Expand All @@ -50,13 +54,15 @@ @implementation FILViewController {
CFTimeInterval _startTime;
viewer::RemoteServer* _server;
viewer::AutomationEngine* _automation;
UILabel* _toastLabel;

Texture* _skyboxTexture;
Skybox* _skybox;
Texture* _iblTexture;
IndirectLight* _indirectLight;
Entity _sun;

UITapGestureRecognizer* _singleTapRecognizer;
UITapGestureRecognizer* _doubleTapRecognizer;
}

Expand Down Expand Up @@ -94,9 +100,29 @@ - (void)viewDidLoad {
_server = new viewer::RemoteServer();
_automation = viewer::AutomationEngine::createDefault();

_doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reloadModel)];
_doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(reloadModel)];
_doubleTapRecognizer.numberOfTapsRequired = 2;
[self.modelView addGestureRecognizer:_doubleTapRecognizer];
_singleTapRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(issuePickingQuery)];
_singleTapRecognizer.numberOfTapsRequired = 1;
[self.modelView addGestureRecognizer:_singleTapRecognizer];

// Create a label at the top of the screen to toast messages to the user.
CGRect labelRect = self.view.bounds;
labelRect.size.height = 50;
_toastLabel = [[UILabel alloc] initWithFrame:labelRect];
_toastLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_toastLabel.textAlignment = NSTextAlignmentCenter;
_toastLabel.textColor = [UIColor whiteColor];
_toastLabel.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5f];
_toastLabel.numberOfLines = 0;
_toastLabel.lineBreakMode = NSLineBreakByWordWrapping;
_toastLabel.text = @"";
_toastLabel.alpha = 0.0f;
[self.view addSubview:_toastLabel];
}

- (void)viewWillAppear:(BOOL)animated {
Expand Down Expand Up @@ -223,7 +249,8 @@ - (void)loadSettings:(viewer::ReceivedMessage const*)message {
.indirectLight = _indirectLight,
.sunlight = _sun,
};
_automation->applySettings(self.modelView.engine, message->buffer, message->bufferByteCount, content);
_automation->applySettings(
self.modelView.engine, message->buffer, message->bufferByteCount, content);
ColorGrading* const colorGrading = _automation->getColorGrading(self.modelView.engine);
self.modelView.view->setColorGrading(colorGrading);
self.modelView.cameraFocalLength = _automation->getViewerOptions().cameraFocalLength;
Expand Down Expand Up @@ -268,6 +295,40 @@ - (void)reloadModel {
[self createDefaultRenderables];
}

- (void)issuePickingQuery {
CGPoint tapLocation = [_singleTapRecognizer locationInView:self.modelView];
__weak typeof(self) weakSelf = self;
[self.modelView issuePickQuery:tapLocation
callback:^(utils::Entity entity) {
NSString* name = [self.modelView getEntityName:entity];
if (!name) {
name = @"<unnamed>";
}
NSString* message = [NSString
stringWithFormat:@"Picked entity %d (%@) at (%d,%d)",
entity.getId(), name, int(tapLocation.x), int(tapLocation.y)];
[weakSelf toastMessage:message];
}];
}

- (void)toastMessage:(NSString*)message {
_toastLabel.text = message;
_toastLabel.alpha = 0.0f;
[UIView animateWithDuration:kToastAnimationDuration
animations:^{
_toastLabel.alpha = 1.0f;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:kToastAnimationDuration
delay:kToastDelayDuration
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_toastLabel.alpha = 0.0f;
}
completion:nil];
}];
}

- (void)dealloc {
delete _server;
delete _automation;
Expand Down

0 comments on commit b4021a1

Please sign in to comment.