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

Simple 3d model loading into Sceneview without ArCore #431

Closed yjoo9317 closed 5 years ago

yjoo9317 commented 5 years ago

Hi, I am just trying to use sceneform to load 3d model.

The below is MainActivity. I've been trying out various things, still I could get the model displayed on the screen. Sorry for posting the abrupt question (since I am aware that it probably is not an issue but a question)

I just want to know what am I missing or doing something wrong?

`public class MainActivity extends AppCompatActivity {

private SceneView sceneView;
private Scene scene;
private ModelRenderable model;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sceneView = findViewById(R.id.sceneView);
    scene = sceneView.getScene();
    createScene();
}

private void createScene() {
    ModelRenderable.builder()
            .setSource(this,  Uri.parse("sample.obj") )
            .build()
            .thenAccept(renderable -> model = renderable)
            .exceptionally(   throwable -> {
                Toast toast =
                        Toast.makeText(this, "Unable to load model", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                return null;
            });
    Node modelNode = new Node();
    modelNode.setRenderable(model);
    scene.addChild(modelNode);
    modelNode.setLocalPosition(new Vector3(0, 0, 0));
}`

Thanks for the help.

malik-at-work commented 5 years ago

Loading the renderable is asynchronous, so you probably don't want to do modelNode.setRenderable until after you know model has finished loading. Check the model before calling setRenderable, you'll want to make sure it's been set before making that call.

yjoo9317 commented 5 years ago

Thanks.. So I bruteforcefully waited with thread.sleep until model turns into not null. But it kept waiting..so it is failing to make that renderable. I wonder why? Also if i set the source as obj file then it complains No PCB file. So then I set .sfb file as source. I am on the move right now and I can check more soon.

malik-at-work commented 5 years ago

Instead of sleeping, I would let CompletableFuture's thenAccept do the work for you. something like this:

private void createScene() {
    ModelRenderable.builder()
            .setSource(this,  Uri.parse("sample.obj") )
            .build()
            .thenAccept(renderable -> onRenderableLoaded(renderable))
            .exceptionally(   throwable -> {
                Toast toast =
                        Toast.makeText(this, "Unable to load model", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                return null;
            });
}

void onRenderableLoaded(Renderable model) {
    Node modelNode = new Node();
    modelNode.setRenderable(model);
    scene.addChild(modelNode);
    modelNode.setLocalPosition(new Vector3(0, 0, 0));
}
malik-at-work commented 5 years ago

Oh Also this likely doesn't appear because the position is 0,0,0

Typically you want to make your new node a child of an anchor.. see the intro demos https://developers.google.com/ar/develop/unity/tutorials/hello-ar-sample

yjoo9317 commented 5 years ago

thanks! malik.. I will try as you suggested.. let you know what happened..

yjoo9317 commented 5 years ago

Sorry for continuing questions.

Right now, I am trying to use sceneform as 3d model rendering purpose (not for VR/AR) Unfortunately, I got still nothing.

1. When I set sample.obj as source. it complains 2018-11-16 15:13:55.204 5547-5568/..../ModelRenderable: Unable to load Renderable registryId='sample.obj' java.util.concurrent.CompletionException: java.lang.AssertionError: No RCB file at uri: sample.obj at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:276) at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:282) at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1627) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) I don't know what RCB file is here. I cannot load obj file directly?, or do something before loading?

  1. If I set .sfb for the source and set the breakpoint, it gets something in renderable but still shows nothing on the screen. (2 sub meshes, 2 materials) Position matters in this case even without AR?
yjoo9317 commented 5 years ago

I still have black view. Here is my simple MainActivity. At least view itself should show something like its background color or so. When I checked the view's size at onResume, the size is 0. (Android OS on the device: 7.0)

`public class MainActivity extends AppCompatActivity {

SceneView mSceneView;
Scene mScene;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSceneView = findViewById(R.id.sceneView);
    createScene();
}

private void createScene() {
    mScene = new Scene(mSceneView);
    ModelRenderable.builder()
            .setSource(this, Uri.parse("file:///android_asset/model.sfb"))
            .build()
            .thenAccept(renderable->onRenderableLoaded(renderable))
            .exceptionally(throwable -> {
                Log.i("Sceneform", "failed to load model");
                return null;
            });
}

private void onRenderableLoaded(Renderable renderable) {
    Node cakeNode = new Node();
    cakeNode.setRenderable(renderable);
    cakeNode.setParent(mScene);
    cakeNode.setLocalPosition(new Vector3(0f, 0f, -1f));
    mScene.addChild(cakeNode);
}

@Override
protected void onResume() {
    super.onResume();
    try {
        mSceneView.resume();
    }
    catch (CameraNotAvailableException e){}
}

@Override
protected void onPause() {
    super.onPause();
    mSceneView.pause();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mSceneView.destroy();
}

} `

yjoo9317 commented 5 years ago

@malik-at-work Hi Malik, I am still trying to make it work.

Usually, to make typical 3d rendering work (not VR/AR context) , I had to make the scene, and load the model (obj file). And then add camera node and set the camera position and set the lookAt position as well. Then finally set the view's point of view as camera above and we have the scene on the screen!

Here, we don't have to camera stuff at all? (in particular without ARCore). I don't understand why its view size is 0 though since its width and height were set "match-parent".

malik-at-work commented 5 years ago

I see. Looking at this code I don't think mScene is actually in use. Try using mSceneView.getScene() instead.

yjoo9317 commented 5 years ago

@malik-at-work Thanks. Now I can see the model. I appreciate it !! If you look at the first version, I started from scene = sceneView.getScene(); actually though. Regarding obj file, we cannot load 'obj' file directly? Do we have to go through asset import plug in and load with sfb file? Thanks again.

malik-at-work commented 5 years ago

There is a runtime converter for some formats, see https://developers.google.com/ar/develop/java/sceneform/create-renderables#load_3d_models_at_runtime

yjoo9317 commented 5 years ago

There is a runtime converter for some formats, see https://developers.google.com/ar/develop/java/sceneform/create-renderables#load_3d_models_at_runtime

Thanks @malik-at-work for the info (Actually I saw that before). However, it said it is for glTF format. Is it OK to use with Obj format?

wasdrqm commented 5 years ago

@malik-at-work Hi, can you help me? How i can drag, rotate and scale 3d model without using ArFragment and working with only SceneView?

Jas67810 commented 5 years ago

@malik-at-work HI, I have to question。 1,use code above, I have show sfa format model. but how to control SceneView zoom, rotate and etc for 3d_models 2, how to show obj format model for sdcard. for it download form network

THX

arthurfp commented 5 years ago

I would also like to know how to control zoom, rotate and drag without ARCore. I read something about using a TransformableNode, but it seems that the TransformableNode needs the ARCore.

MuhammadAasharib commented 5 years ago

Hi, @arthurfp @malik-at-work I have same question of rotating and zooming the model in SceneView. Have you found any solution?

Chen-Alexander commented 5 years ago

There is a runtime converter for some formats, see https://developers.google.com/ar/develop/java/sceneform/create-renderables#load_3d_models_at_runtime

Thanks @malik-at-work for the info (Actually I saw that before). However, it said it is for glTF format. Is it OK to use with Obj format?

Why not let the modeling engineer convert the format?

Chen-Alexander commented 5 years ago

@malik-at-work HI, I have to question。 1,use code above, I have show sfa format model. but how to control SceneView zoom, rotate and etc for 3d_models 2, how to show obj format model for sdcard. for it download form network

THX

Rotate Zoom Drag the problem you solved? please help

Chen-Alexander commented 5 years ago

I would also like to know how to control zoom, rotate and drag without ARCore. I read something about using a TransformableNode, but it seems that the TransformableNode needs the ARCore.

Rotate Zoom Drag the problem you solved? please help

Chen-Alexander commented 5 years ago

Hi, @arthurfp @malik-at-work I have same question of rotating and zooming the model in SceneView. Have you found any solution?

Rotate Zoom Drag the problem you solved? please help

chnouman commented 4 years ago

https://github.com/chnouman/SceneView here you can find a working example.