EddyVerbruggen / nativescript-ar

Augmented Reality NativeScript plugin
https://www.nativescript.org/blog/preview-of-augmented-reality-in-nativescript
MIT License
118 stars 36 forks source link

load models dynamically #7

Closed hamdiwanis closed 6 years ago

hamdiwanis commented 6 years ago

hello, I wanted to ask if it possible to load models dynamically from an API then be able to use it in the plugin?. and thanks for the plugin, :)

EddyVerbruggen commented 6 years ago

I'd be interested to learn if that's possible in a native iOS AR app. Perhaps some online example can be found? I'd be happy to borrow the implementation details of such an example 😉- didn't Google for it yet.

robinbonnes commented 6 years ago

Would be great if we can use this together with the http.getFile() function: https://docs.nativescript.org/cookbook/http to download and store files in the app folder (https://docs.nativescript.org/api-reference/modules/_file_system_.knownfolders#documents)

Then load the assets from these 'knownfolders'. We can even cache models by checking if they exist before downloading them.

EddyVerbruggen commented 6 years ago

@robinbonnes I think handling it in NativeScript is the easy part, but if ARKit doesn't support models being loaded dynamically (as opposed from the shipped bundle) then we can't do it in NativeScript either. So if anyone can find a way (tutorial/code sample/SO answer) how to dynamically load a modal, please reopen this issue.

robinbonnes commented 6 years ago

@EddyVerbruggen What about this? https://the-nerd.be/2014/11/07/dynamically-load-collada-files-in-scenekit-at-runtime/

Steps he made it work:

Example:

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:@"product-1-optimized.scnassets/cube.dae"];

SCNSceneSource *sceneSource = [SCNSceneSource sceneSourceWithURL:documentsDirectoryURL options:nil];
robinbonnes commented 6 years ago

I can try to set up a public API to convert any 3D model (fbx, obj, dae) to .scn by using XCode's scntool converter on Mac.

EDIT:

Made some fast example, didn't test it.

Webserver:


<?php 
if(isset($_FILES['upload'])) {

    // Don't timeout in big files
    set_time_limit(0);

    // Settings
    $converter_url = "http://1.2.3.4/convert_to_scn.php";
    $file_upload = "./dir/to/upload/" . basename($_FILES["upload"]["name"]);
    $file_download = "./dir/to/download/" . pathinfo(basename($_FILES["upload"]["name"]), PATHINFO_FILENAME) . ".scn");

    // Upload file
    if(move_uploaded_file($_FILES["upload"]["tmp_name"], $file_upload)) {
        $file = (function_exists('curl_file_create') ? curl_file_create($file_upload) : '@' . realpath($file_upload));
        $fp = @fopen($file_download, "w");

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $converter_url);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('upload' => $file));
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);

        die("You can find your converted file here: " . $file_download);
    } else {
        die("Error on upload!");
    }
}
?>

<!DOCTYPE html>
<html>
<body>

<form method="post" enctype="multipart/form-data">
    Select file to convert:
    <input type="file" name="upload">
    <input type="submit" value="Convert to scn" name="submit">
</form>

</body>
</html>

convert_to_scn.php: Converter on Mac example (needs wamp server with PHP and port 80 open):


<?php 
if(isset($_FILES['upload'])) {

    // Don't timeout in big files
    set_time_limit(0);

    // Settings
    $import = "./dir/to/import/" . basename($_FILES["upload"]["name"]);
    $export = "./dir/to/export/" . pathinfo(basename($_FILES["upload"]["name"]), PATHINFO_FILENAME) . ".scn");

    // Upload file
    if(move_uploaded_file($_FILES["upload"]["tmp_name"], $import)) {

        // Convert 
        exec ("./scntool --convert " . $import . " --format scn --output " . $export);

        header("Content-Type: scn"); 
        header("Content-Disposition: attachment; filename='" . basename($export) . "'"); 
        readfile($export);
    } else {
        echo "Please pass some file...";
    }
?>

If .dae could be avoided, an alternative would be: https://github.com/eugenebokhan/AssetImportKit/blob/master/3DViewer/README.md

EddyVerbruggen commented 6 years ago

@robinbonnes That sounds great! I can handle the {N} plugin part.