olexale / arkit_flutter_plugin

ARKit Flutter Plugin
MIT License
797 stars 225 forks source link

Use dynamic image in app in ARKitMaterialProperty #127

Closed RobinLbt closed 3 years ago

RobinLbt commented 3 years ago

Hi,

I really like your plugin thank you for all your work !

I would like to know if it's possible to use a dynamic image inside ARKitMaterialProperty (instead of URL or asset image): It's not images I load from the assets, but images I'm building at runtime. I'm doing it using the following code :

 var image = await boundary.toImage(pixelRatio: 1);
    ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    return pngBytes;

What I'm doing is transforming a rendered widget into a image to display it inside the ARView.

I've done with ARCore, but I'm struggling to do it with ARKit since it doesn't take ByteData nor Uint8List as argument, is there any workaround ?

(I'm not projecting them like your example because they need to be placed at very specific places using LatLng coordinates)

RobinLbt commented 3 years ago

I'm trying a workaround using a generated JSON file containing the encoded image:

    ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    String img64 = base64Encode(pngBytes);
    Map<String,dynamic> prepareForJson ={"image": img64};
    return json.encode(prepareForJson);

and then use this JSON file for ARKitMaterialProperty:

final material = ARKitMaterial(
 lightningModeName: ARKitLightModel.lambert,
diffuse: ARKitMaterialProperty(image: jsonFile),
);

final plane = ARKitPlane(
materials: [material],
);

final node =ARKitNode(
 geometry: plane,
position : //something,
);

Unfortunately it doesn't work and I get the following error:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file arkit_plugin/ImageParser.swift, line 11

Looks like the following Swift code cannot parse the image but I have no idea why:


        let data = try Data.init(contentsOf: URL.init(string: name)!)
        return UIImage(data: data)

Any ideas ?

maxTeste commented 3 years ago

@RobinLbt did u find a solution?

RobinLbt commented 3 years ago

@rubenteste no I didn't found any solution yet.

That would be really cool if @olexale could make another argument for the constructor to accept bytes image like ARCore_flutter_plugin

olexale commented 3 years ago

On it. It will help a lot if any of you will provide an example project with the dynamic image in it, so I can test it locally and add to examples later.

olexale commented 3 years ago

Hi @RobinLbt, I have merged the base64 version of this task that you already tested into master and will create a new plugin version with it tomorrow. Unfortunately, your sample with List does not work for me - it fails with RenderObject exceptions on the Flutter side prior to deserialization, so I was not able to debug want was wrong with a more performant solution. Could you please test the current base64 implementation and provide some feedback on how fast it is on the real data? Thanks in advance!

maxTeste commented 3 years ago

hey @olexale I have been using this plugin, and the goal was to use images as anchor and then place objects. But as anchor image I use similar images. But when using similar images he is unable to identify them well, which confuses them ... Is there any way to increase the detail that he uses to compare the images so that he can identify them well?

olexale commented 3 years ago

Hi. You already asked this here.

maxTeste commented 3 years ago

@olexale can u check the comment on the issue closed about my question?

olexale commented 3 years ago

Hi! I have published version 0.6.2 with base64 encoded image support. Could you please check?

RobinLbt commented 3 years ago

Hi @olexale , the performance is pretty good honestly (tested in debug mode on a iPhone 7 with 15/20 images)

olexale commented 3 years ago

Awesome! Then let's keep the current implementation for now. Thanks for testing!