when the chareter is visable when the camera is launch my mobile starts laging extrimaly
I am using lettest version of Sceneform android and still geting the same problem
Hi! Your model can be too highly detailed (have many polygons) for mobile devices.
I using this model in kotlin using corooting it is work smothly but when i use in java app is stuck
Do you mean that there are no lags when you use Kotlin with Sceneform?
yes
Why is this happening in Kotlin and not in Java?
Probably, that means that you block the main thread in Java while when the coroutine is suspended it frees the main thread for other tasks.
What should I d to free the main thread to java?
because in java i work on AsyncTask foe background Thread but my App is continue to stuck
You just need to use thenAccept with a callback: https://github.com/SceneView/sceneform-android/blob/993ad53f31228de3918ead4cd9509ed10347304d/samples/ar-model-viewer-java/src/main/java/com/google/ar/sceneform/samples/gltf/MainActivity.java#L94
Sceneform will load the model on a background thread.
public class MainActivity extends AppCompatActivity implements
FragmentOnAttachListener,
BaseArFragment.OnTapArPlaneListener,
BaseArFragment.OnSessionConfigurationListener,
ArFragment.OnViewCreatedListener {
private ArFragment arFragment;
private Renderable model;
private ObjectAnimator modelAnimator;
private MediaPlayer modelSound = new MediaPlayer();
private ViewRenderable viewRenderable;
public static Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().addFragmentOnAttachListener(this);
if (savedInstanceState == null) {
if (Sceneform.isSupported(this)) {
getSupportFragmentManager().beginTransaction()
.add(R.id.arFragment, ArFragment.class, null)
.commit();
}
}
MainActivity.handler.post(new Runnable() {
@Override
public void run() {
// Do Whatever
loadModels();
// handler.getLooper();
}
});
// loadModels();
}
@Override
public void onAttachFragment(@NonNull FragmentManager fragmentManager, @NonNull Fragment fragment) {
if (fragment.getId() == R.id.arFragment) {
arFragment = (ArFragment) fragment;
arFragment.setOnTapArPlaneListener(this);
arFragment.setOnViewCreatedListener(this);
arFragment.setOnSessionConfigurationListener(this);
}
}
@Override
public void onViewCreated(ArSceneView arSceneView) {
arFragment.setOnViewCreatedListener(null);
// Available modes: DEPTH_OCCLUSION_DISABLED, DEPTH_OCCLUSION_ENABLED
arSceneView.getCameraStream()
.setDepthOcclusionMode(CameraStream.DepthOcclusionMode.DEPTH_OCCLUSION_ENABLED);
}
@RequiresApi(api = Build.VERSION_CODES.N)
public void loadModels() {
WeakReference<MainActivity> weakActivity = new WeakReference<>(this);
ModelRenderable.builder()
.setSource(this, Uri.parse("astronaut.glb"))
.setIsFilamentGltf(true)
.setAsyncLoadEnabled(true)
.build()
.thenAccept(model -> {
MainActivity activity = weakActivity.get();
if (activity != null) {
activity.model = model;
}
})
.exceptionally(throwable -> {
Toast.makeText(this, "Unable to load model", Toast.LENGTH_LONG).show();
return null;
});
ViewRenderable.builder()
.setView(this, R.layout.view_model_title)
.build()
.thenAccept(viewRenderable -> {
MainActivity activity = weakActivity.get();
if (activity != null) {
activity.viewRenderable = viewRenderable;
}
})
.exceptionally(throwable -> {
Toast.makeText(this, "Unable to load model", Toast.LENGTH_LONG).show();
return null;
});
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onTapPlane(HitResult hitResult, Plane plane, MotionEvent motionEvent) {
if (model == null || viewRenderable == null) {
Toast.makeText(this, "Loading...", Toast.LENGTH_SHORT).show();
return;
}
// Create the Anchor.
Anchor anchor = hitResult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
// Create the transformable model and add it to the anchor.
TransformableNode modelNode = new TransformableNode(arFragment.getTransformationSystem());
modelNode.setParent(anchorNode);
RenderableInstance modelInstance = modelNode.setRenderable(this.model);
modelAnimator = modelInstance.animate(true);
modelAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
// playSound();
}
@Override
public void onAnimationEnd(Animator animation) {
// stopSound();
}
@Override
public void onAnimationCancel(Animator animation) {
// stopSound();
}
@Override
public void onAnimationRepeat(Animator animation) {
// stopSound();
// playSound();
}
});
modelNode.select();
modelAnimator.start();
Node titleNode = new Node();
titleNode.setParent(modelNode);
titleNode.setEnabled(false);
titleNode.setName("PANDA");
titleNode.setSelectable(true);
titleNode.setLocalPosition(new Vector3(3722.0f, 2977.0f, 23.0f));
titleNode.setRenderable(viewRenderable);
titleNode.setEnabled(true);
}
@Override
public void onSessionConfiguration(Session session, Config config) {
// Comment this in to feed the DepthTexture with Raw Depth Data.
/*if (session.isDepthModeSupported(Config.DepthMode.RAW_DEPTH_ONLY))
config.setDepthMode(Config.DepthMode.RAW_DEPTH_ONLY);*/
if (session.isDepthModeSupported(Config.DepthMode.AUTOMATIC)) {
config.setDepthMode(Config.DepthMode.AUTOMATIC);
}
config.setUpdateMode(Config.UpdateMode.LATEST_CAMERA_IMAGE);
}
}
@RequiresApi(api = Build.VERSION_CODES.N)
public void loadModels() {
WeakReference<MainActivity> weakActivity = new WeakReference<>(this);
ModelRenderable.builder()
.setSource(this, Uri.parse("astronaut.glb"))
.setIsFilamentGltf(true)
.setAsyncLoadEnabled(true)
.build()
.thenAccept(model -> {
MainActivity activity = weakActivity.get();
if (activity != null) {
activity.model = model;
}
})
.exceptionally(throwable -> {
Toast.makeText(this, "Unable to load model", Toast.LENGTH_LONG).show();
return null;
});
ViewRenderable.builder()
.setView(this, R.layout.view_model_title)
.build()
.thenAccept(viewRenderable -> {
MainActivity activity = weakActivity.get();
if (activity != null) {
activity.viewRenderable = viewRenderable;
}
})
.exceptionally(throwable -> {
Toast.makeText(this, "Unable to load model", Toast.LENGTH_LONG).show();
return null;
});
}
i have used it
i have coded same as you
Try to run the sample application from the repository. I don't see any problems in your code. However, you don't need to use a Handler.
Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.