5

我是使用 ARCore 和 Sceneform for Android (Java) 创建 AR 应用程序的新手。我正在使用云锚构建一个多人应用程序,两个或多个人可以在一个或多个资产上进行交互。当资产改变它的位置时,如何让事件广播给其他连接的用户?

这是我的代码:

private AppAnchorState appAnchorState = AppAnchorState.NONE;

private SnackbarHelper snackbarHelper = new SnackbarHelper();

private StorageManager storageManager;

private Socket mSocket;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /******************* Here **************************/
    // connect to socket

    mSocket = new ArSync().onCreate();
    mSocket.connect();
    // listen for change event

    mSocket.on("modelChanged", new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //TODO: call the application to load reslove by number
                    //textView.setText("Current ID is :" + 
                      args[0].toString());
                    Log.d("Triggered", args[0].toString());
                    onResolveOkPressed(args[0].toString());

                }
                        });
    }
});

fragment = (CustomArFragment) getSupportFragmentManager().findFragmentById(R.id.sceneform_fragment);
//To add
fragment.getPlaneDiscoveryController().hide();
fragment.getArSceneView().getScene().setOnUpdateListener(this::onUpdateFrame);

Button clearButton = findViewById(R.id.clear_button);
clearButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        setCloudAnchor(null);
    }
});


Button resolveButton = findViewById(R.id.resolve_button);
resolveButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (cloudAnchor != null){
            snackbarHelper.showMessageWithDismiss(getParent(), "Please clear Anchor");
            return;
        }
        ResolveDialogFragment dialog = new ResolveDialogFragment();
        dialog.setOkListener(MainActivity.this::onResolveOkPressed);
        dialog.show(getSupportFragmentManager(), "Resolve");

    }
});


fragment.setOnTapArPlaneListener(
        (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {

            if (plane.getType() != Plane.Type.HORIZONTAL_UPWARD_FACING ||
                    appAnchorState != AppAnchorState.NONE){
                return;
            }

            Anchor newAnchor = fragment.getArSceneView().getSession().hostCloudAnchor(hitResult.createAnchor());

            setCloudAnchor(newAnchor);

            appAnchorState = AppAnchorState.HOSTING;
            snackbarHelper.showMessage(this, "Now hosting anchor...");


            placeObject(fragment, cloudAnchor, Uri.parse("Fox.sfb"));

        }
);

storageManager = new StorageManager(this);
}

/*.....
.......
....... 
*/

我已经编写了一个单独的Socket.io服务器NodeJS来将速记 GUID 广播到其他连接的节点,以便在启动应用程序时下载和解析资产。当我第一次渲染资产时它工作正常。但是我不确定当一个人移动该资产时如何处理。我希望它呈现其他连接的设备。我找不到任何要更改资产的事件,以便我可以将我的代码附加到它。

4

0 回答 0