How to change Color of ModelRenderable?

邮差的信 提交于 2019-12-24 21:58:44

问题


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?


回答1:


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);
    });
  }


来源:https://stackoverflow.com/questions/51405596/how-to-change-color-of-modelrenderable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!