Sceneform-android-sdk: How to change the alpha of a node using ObjectAnimator ?

Created on 13 Nov 2018  路  4Comments  路  Source: google-ar/sceneform-android-sdk

We can rotate a node using object animator as explained in the example :

```
private static ObjectAnimator createAnimator() {

    Quaternion orientation1 = Quaternion.axisAngle(new Vector3(0.0f, 1.0f, 0.0f), 0);
    Quaternion orientation2 = Quaternion.axisAngle(new Vector3(0.0f, 1.0f, 0.0f), 120);
    Quaternion orientation3 = Quaternion.axisAngle(new Vector3(0.0f, 1.0f, 0.0f), 240);
    Quaternion orientation4 = Quaternion.axisAngle(new Vector3(0.0f, 1.0f, 0.0f), 360);

    ObjectAnimator orbitAnimation = new ObjectAnimator();

    orbitAnimation.setObjectValues(orientation1, orientation2, orientation3, orientation4);

    // Next, give it the localRotation property.
    orbitAnimation.setPropertyName("localRotation");

    // Use Sceneform's QuaternionEvaluator.
    orbitAnimation.setEvaluator(new QuaternionEvaluator());

    //  Allow orbitAnimation to repeat forever
    orbitAnimation.setRepeatCount(ObjectAnimator.INFINITE);
    orbitAnimation.setRepeatMode(ObjectAnimator.RESTART);
    orbitAnimation.setInterpolator(new LinearInterpolator());
    orbitAnimation.setAutoCancel(true);

    return orbitAnimation;
}`

But how to change the 'alpha' of a node? Is it possible using ObjectAnimator or any other ways?

question

Most helpful comment

Provided you are using the material that comes from MaterialFactory.makeTransparentWithColor
you can set the color with a call like:
renderable.getMaterial().setFloat4(MaterialFactory.MATERIAL_COLOR,
new Color(animatedRed, animatedGreen, animatedBlue, animatedAlpha));

If using an Animator as Romain suggested above this might look like:

MaterialFactory.makeTransparentWithColor(this, new Color(android.graphics.Color.RED))
        .thenAccept(
            material -> {
              renderable =
                  ShapeFactory.makeSphere(0.1f, new Vector3(0.0f, 0.15f, 0.0f), material);
            })
        .exceptionally(
            throwable -> {
              DemoUtils.displayError(this, "Unable to build material.", throwable);
              return null;
            });
    ValueAnimator alphaAnimator = ValueAnimator.ofFloat(0.0f, 1.0f, 0.0f);
    alphaAnimator.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        float animatedAlpha = (float) animation.getAnimatedValue();
        if(sphereRenderable != null) {
          sphereRenderable.getMaterial().setFloat4(MaterialFactory.MATERIAL_COLOR,
              new Color(0.0f, 0.0f, 1.0f*animatedAlpha, animatedAlpha));
        }
      }
    });
    alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);
    alphaAnimator.setDuration(1000);
    alphaAnimator.start();

All 4 comments

ObjectAnimator can be used to animate any property that provides a getter and a setter. One way to animate alpha for instance would be to create a subclass of Node with a getAlpha() and a setAlpha(). You can also use a Property instead: https://developer.android.com/reference/android/util/Property.

Thank you for the reply.
I have created one Node subclass but still not sure how to change the alpha inside setAlpha() method.

I have one more question, can we add any other animation like explosion effects while using Sceneform. I have read that Sceneform doesn't support FBX animation but is there any way we can implement it?

Provided you are using the material that comes from MaterialFactory.makeTransparentWithColor
you can set the color with a call like:
renderable.getMaterial().setFloat4(MaterialFactory.MATERIAL_COLOR,
new Color(animatedRed, animatedGreen, animatedBlue, animatedAlpha));

If using an Animator as Romain suggested above this might look like:

MaterialFactory.makeTransparentWithColor(this, new Color(android.graphics.Color.RED))
        .thenAccept(
            material -> {
              renderable =
                  ShapeFactory.makeSphere(0.1f, new Vector3(0.0f, 0.15f, 0.0f), material);
            })
        .exceptionally(
            throwable -> {
              DemoUtils.displayError(this, "Unable to build material.", throwable);
              return null;
            });
    ValueAnimator alphaAnimator = ValueAnimator.ofFloat(0.0f, 1.0f, 0.0f);
    alphaAnimator.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        float animatedAlpha = (float) animation.getAnimatedValue();
        if(sphereRenderable != null) {
          sphereRenderable.getMaterial().setFloat4(MaterialFactory.MATERIAL_COLOR,
              new Color(0.0f, 0.0f, 1.0f*animatedAlpha, animatedAlpha));
        }
      }
    });
    alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);
    alphaAnimator.setDuration(1000);
    alphaAnimator.start();

Is there a solution?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kunal-wayfair picture kunal-wayfair  路  3Comments

dementia2029 picture dementia2029  路  3Comments

rohitagarwal3011 picture rohitagarwal3011  路  4Comments

PaulTVungle picture PaulTVungle  路  3Comments

KamikX picture KamikX  路  4Comments