我正在 Android Studio 中使用 Java 中的 Sceneform 和 ArCore 开发 AR 虚拟宠物应用程序,并且我有一个带有多个动画的 .glb 模型。最后,我想做一些像《Pokemon Go》这样的东西。目前,我的代码渲染了模型,但我不知道如何一个接一个地播放动画。具体来说,我想先播放“祝贺”动画,然后播放“接近”动画。
这是我的代码:
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.ar.core.Anchor;
import com.google.ar.core.HitResult;
import com.google.ar.core.Plane;
import com.google.ar.sceneform.AnchorNode;
import com.google.ar.sceneform.rendering.ModelRenderable;
import com.google.ar.sceneform.rendering.Renderable;
import com.google.ar.sceneform.ux.ArFragment;
import com.google.ar.sceneform.ux.BaseArFragment;
import com.google.ar.sceneform.ux.TransformableNode;
import java.lang.ref.WeakReference;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
private ArFragment arCam; //object of ArFragment Class
private int clickNo = 0; //helps to render the 3d model only once when we tap the screen
public static boolean checkSystemSupport(Activity activity) {
//checking whether the API version of the running Android >= 24 that means Android Nougat 7.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String openGlVersion = ((ActivityManager) Objects.requireNonNull(activity.getSystemService(Context.ACTIVITY_SERVICE))).getDeviceConfigurationInfo().getGlEsVersion();
//checking whether the OpenGL version >= 3.0
if (Double.parseDouble(openGlVersion) >= 3.0) {
return true;
} else {
Toast.makeText(activity, "App needs OpenGl Version 3.0 or later", Toast.LENGTH_SHORT).show();
activity.finish();
return false;
}
} else {
Toast.makeText(activity, "App does not support required Build Version", Toast.LENGTH_SHORT).show();
activity.finish();
return false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkSystemSupport(this)) {
arCam = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.arCameraArea);
//ArFragment is linked up with its respective id used in the activity_main.xml
arCam.setOnTapArPlaneListener((hitResult, plane, motionEvent) -> {
clickNo++;
//the 3d model comes to the scene only when clickNo is one that means once
if (clickNo == 1) {
Anchor anchor = hitResult.createAnchor();
ModelRenderable.builder()
.setSource(this, R.raw.hund)
.setIsFilamentGltf(true)
.build()
.thenAccept(modelRenderable -> addModel(anchor, modelRenderable))
.exceptionally(throwable -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Somthing is not right" + throwable.getMessage()).show();
return null;
});
}
});
} else {
return;
}
}
private void addModel(Anchor anchor, ModelRenderable modelRenderable) {
AnchorNode anchorNode = new AnchorNode(anchor);
// Creating a AnchorNode with a specific anchor
anchorNode.setParent(arCam.getArSceneView().getScene());
//attaching the anchorNode with the ArFragment
TransformableNode model = new TransformableNode(arCam.getTransformationSystem());
model.setParent(anchorNode);
//attaching the anchorNode with the TransformableNode
model.setRenderable(modelRenderable);
//attaching the 3d model with the TransformableNode that is already attached with the node
model.select();
}
如何从我的 .glb 模型访问和播放特定动画?
我将非常感谢任何可以帮助我实现此功能的代码片段或资源。预先感谢!
要在
ARCore
/Sceneform
应用程序中顺序播放多个动画,请使用以下代码:
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(
ModelAnimator.ofMultipleAnimations(character, "idle", "jog", "fall")
);
animatorSet.start();