2017-11-06 41 views
1

私はアンカーを配置し、X座標とY座標を返すユーザーが画面をタップする場所に表示しようとしています。ARCoreスクリーン座標をワールド座標に合わせるOpenGL

tapX = tap.getX(); 
tapY = tap.getY(); 

この情報を使用して、自分のモデル用の行列を作成します。 (つまり、ユーザーがタップ私の3Dモデルを置く)

は、今私が試した:

float sceneX = (tap.getX()/mSurfaceView.getMeasuredWidth())*2.0f - 1.0f; 
float sceneY = (tap.getY()/mSurfaceView.getMeasuredHeight())*-2.0f + 1.0f; //if bottom is at -1. Otherwise same as X 
Pose temp = frame.getPose().compose(Pose.makeTranslation(sceneX, sceneY, -1.0f)).extractTranslation(); 

私はちょうど今の前に3Dオブジェクト1メートルを配置することです。私はこれで適切な場所を得ていない。

ローカル座標をワールド座標に変換する方法はありますか?

答えて

1

画面上のタップは、現実世界と1対1のマッピングを持っていません。それはカメラから伸びる光線に沿って存在する可能性のある無数の位置を(x, y, z)に与えます。たとえば、画面の中央をタップすると、それは、私から離れたメートル、2メートル離れた床、床などのオブジェクトに対応する可能性があります。

したがって、いくつかの追加の制約が必要ですこの光線に沿ってアンカーを配置する場所をARCoreに伝えます。サンプルアプリケーションでは、これはARCoreによって検出された平面とこのレイを交差させることによって行われます。平面を横切る光線は1点を表しているので、あなたは設定されています。特に、あなたのケースのために

MotionEvent tap = mQueuedSingleTaps.poll(); 
    if (tap != null && frame.getTrackingState() == TrackingState.TRACKING) { 
     for (HitResult hit : frame.hitTest(tap)) { 
      // Check if any plane was hit, and if it was hit inside the plane polygon. 
      if (hit instanceof PlaneHitResult && ((PlaneHitResult) hit).isHitInPolygon()) { 
       // Cap the number of objects created. This avoids overloading both the 
       // rendering system and ARCore. 
       if (mTouches.size() >= 16) { 
        mSession.removeAnchors(Arrays.asList(mTouches.get(0).getAnchor())); 
        mTouches.remove(0); 
       } 
       // Adding an Anchor tells ARCore that it should track this position in 
       // space. This anchor will be used in PlaneAttachment to place the 3d model 
       // in the correct position relative both to the world and to the plane. 
       mTouches.add(new PlaneAttachment(
        ((PlaneHitResult) hit).getPlane(), 
        mSession.addAnchor(hit.getHitPose()))); 

       // Hits are sorted by depth. Consider only closest hit on a plane. 
       break; 
      } 
     } 
    } 

は、私は、ARの描画アプリ用the rendering codeを見てみましょう:これは、彼らがサンプルアプリケーションでそれを行う方法です。それらの機能GetWorldCoords()は、画面からある程度の距離を置いて、画面座標から世界座標に移動します。

+0

ワールドスペースへの配置がすべて頂点シェーダで行われているため、そのコードはあまり役に立ちませんでした。しかし、それを私のプロジェクトに実装するのは難しいです。 – snowrain

関連する問題