You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ARCore(Android Studio):给定坐标渲染3D图像及平面点击放置实现

Hey there! Let's break down your ARCore questions step by step—super common use cases, so I’ve got you covered.

1. 精准渲染3D模型到指定X/Y/Z坐标

First, it’s critical to remember that ARCore uses a world coordinate system where units are meters—so your X/Y/Z values should be in meters relative to the AR session’s origin (the point where the app first started tracking). Here’s how to pull this off:

  • Define your target world pose
    Use ARCore’s Pose class to create a position with optional rotation. For a fixed position with no rotation, use:

    // x, y, z are your target coordinates in meters
    Pose targetPose = new Pose(new float[]{x, y, z}, new float[]{0, 0, 0, 1});
    

    The second float array is a quaternion for rotation—{0,0,0,1} means no rotation.

  • Load your 3D model
    If you’re using the deprecated but still widely used Sceneform, load a renderable like this (handle the async load properly):

    ModelRenderable.builder()
        .setSource(context, R.raw.your_3d_model) // Replace with your model resource
        .build()
        .thenAccept(renderable -> {
            // Store the renderable for later use
            this.modelRenderable = renderable;
        })
        .exceptionally(throwable -> {
            // Handle loading errors
            Toast.makeText(context, "Model load failed", Toast.LENGTH_SHORT).show();
            return null;
        });
    

    For the newer, recommended Filament library, you’ll use ModelViewer or load glTF models directly via Filament’s asset loader.

  • Anchor the model to the world
    ARCore requires an Anchor to bind objects to the physical world (this prevents drift as the session tracks the environment). Create an anchor from your target pose, then attach your model to it:

    // Assuming you have an active ARSession
    Anchor anchor = session.createAnchor(targetPose);
    
    // For Sceneform: Use AnchorNode to tie the model to the anchor
    AnchorNode anchorNode = new AnchorNode(anchor);
    anchorNode.setParent(arFragment.getArSceneView().getScene());
    
    // Attach the model to the AnchorNode
    TransformableNode modelNode = new TransformableNode(arFragment.getTransformationSystem());
    modelNode.setParent(anchorNode);
    modelNode.setRenderable(modelRenderable);
    modelNode.select(); // Optional: Allows scaling/rotating the model
    
  • Key notes

    • Always manage the ARSession lifecycle (pause/resume it in your Activity’s onPause/onResume methods) to avoid resource leaks.
    • If your coordinates are relative to a detected plane, make sure to adjust the Y-value to align with the plane’s height.
2. Render objects on plane detection + tap

Absolutely—this is one of ARCore’s core built-in capabilities. Here’s how to implement it:

  • Enable plane detection
    First, turn on plane finding in your ARSession configuration:

    ArCoreConfig config = session.getConfig();
    // Detect both horizontal (floors/desks) and vertical (walls) planes
    config.setPlaneFindingMode(Config.PlaneFindingMode.HORIZONTAL_AND_VERTICAL);
    session.configure(config);
    
  • Set up tap-to-place logic
    Use Sceneform’s OnTapArPlaneListener to handle taps on detected planes. This listener gives you a HitResult which contains the exact pose where the tap intersects the plane:

    arFragment.getArSceneView().setOnTapArPlaneListener((hitResult, plane, motionEvent) -> {
        // Skip if the model hasn't finished loading yet
        if (modelRenderable == null) return;
        
        // Get the pose of the tap location on the plane
        Pose hitPose = hitResult.getPose();
        
        // Create an anchor at the tap position
        Anchor anchor = session.createAnchor(hitPose);
        
        // Attach the model to the anchor (same as the previous section)
        AnchorNode anchorNode = new AnchorNode(anchor);
        anchorNode.setParent(arFragment.getArSceneView().getScene());
        
        TransformableNode modelNode = new TransformableNode(arFragment.getTransformationSystem());
        modelNode.setParent(anchorNode);
        modelNode.setRenderable(modelRenderable);
        modelNode.select();
    });
    
  • Optional: Customize plane visualization
    By default, ARCore shows a grid on detected planes. You can hide it or adjust its style:

    // Hide the plane grid
    arFragment.getPlaneDiscoveryController().hide();
    // Or adjust visibility with animation
    arFragment.getPlaneDiscoveryController().show();
    
  • Key notes

    • Use plane.getType() to filter taps (e.g., only allow placing on horizontal planes).
    • For Filament, you’ll use ArFrame.hitTest() in your touch listener to get plane hits, then bind your model’s transform to the anchor’s pose updates.

内容的提问来源于stack exchange,提问作者ML learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:57:59