Search code examples
androidcolorsaugmented-realityarcoresceneform

How to change Color of ModelRenderable?


i have a ModelRenderable attached to a Node and rendered in an ArFragment.

I would like to highlight this element to the user for 0.5 sec in a prominent color.

I tried to change the material, but it didn't work out. The rendering freezes without throwing an error. Here is what I tried:

private void addHighlightToNode(Node node) {

    CompletableFuture<Material> materialCompletableFuture =
            MaterialFactory.makeOpaqueWithColor(this, new Color(0, 255, 244));
    ModelRenderable highlightedRenderable = (ModelRenderable) node.getRenderable();
    highlightedRenderable = highlightedRenderable.makeCopy();
    try {
        highlightedRenderable.setMaterial(materialCompletableFuture.get());
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    node.setRenderable(highlightedRenderable);
}

I managed to set the light of the Node to a different color in runtime, but the effect is not close to what I need.

node.setLight(Light.builder(Light.Type.POINT).setColor(new Color(0,255,244)).build());

How can I change the color?


Solution

  • Creating the material is asynchronous, that's why it returns a CompletableFuture. You are calling CompletableFuture.get(), which is a blocking call, but since you are on the UI thread it ends up freezing the app. If you move the setting to be called in thenAccept, it works correctly.

      private void addHighlightToNode(Node node) {
        CompletableFuture<Material> materialCompletableFuture =
                MaterialFactory.makeOpaqueWithColor(this, new Color(0, 255, 244));
    
        materialCompletableFuture.thenAccept(material -> {
          Renderable r2 = node.getRenderable().makeCopy();
          r2.setMaterial(material);
          node.setRenderable(r2);
        });
      }