google-ar / sceneform-android-sdk

Sceneform SDK for Android
https://developers.google.com/sceneform/develop/
Apache License 2.0
1.23k stars 604 forks source link

How to change the alpha of a node using ObjectAnimator ? #426

Open nandantal opened 5 years ago

nandantal commented 5 years ago

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? 
romainguy commented 5 years ago

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.

nandantal commented 5 years ago

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?

malik-at-work commented 5 years ago

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();
siny-yao commented 5 years ago

Is there a solution?