1. yoshikawa922

    No comment

    yoshikawa922
Changes in body
Source | HTML | Preview
@@ -1,70 +1,84 @@
#3Dオブジェクトの配置、大きさ、向きを調整する。
-##配置
+##ユーザーの前0.5メートルの位置を見つけます。
カメラの位置に対してアンカーの位置を設定します。
- Session session = arFragment.getArSceneView().getSession();
- float[] pos = { 0,-0.2f,-1 };
- float[] rotation = {0,0,0,1};
- Anchor anchor = session.createAnchor(new Pose(pos, rotation));
+ SceneView sceneView = arFragment.getArSceneView();
+ Vector3 cameraPos = sceneView.getScene().getCamera().getWorldPosition();
+ Vector3 cameraForward = sceneView.getScene().getCamera().getForward();
+ Vector3 position = Vector3.add(cameraPos, cameraForward.scaled(0.5f));
+##大きさを調整
+model.getScaleController().setMinScale(0.01f);
+model.getScaleController().setMaxScale(2.0f);
+model.setLocalScale(new Vector3(0.5f,0f,0.5f));
+##向きを調整
+y軸(縦軸) を中心に180度回転
+model.setLocalRotation(Quaternion.axisAngle(new Vector3(0,1,0),-120));
```
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_PERMISSION_CODE = 0;
private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA;
private ModelRenderable modelRenderable;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArFragment arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ar_fragment);
ModelRenderable.builder()
.setSource(this, R.raw.ball)
.build()
.thenAccept(renderable -> modelRenderable = renderable)
.exceptionally(
throwable -> {
Toast toast =
Toast.makeText(this, "読み込み失敗", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return null;
});
- if(arFragment != null ) {
+ if(arFragment != null ) {
+
+
arFragment.setOnTapArPlaneListener(
(HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
+
if (modelRenderable == null) {
return;
}
- //カメラの位置に対してアンカーの位置を設定する
+ //ユーザーの前0.5メートルの位置を見つけます。
Session session = arFragment.getArSceneView().getSession();
- float[] pos = { 0,-0.2f,-1 };
- float[] rotation = {0,0,0,1};
- Anchor anchor = session.createAnchor(new Pose(pos, rotation));
-
+ SceneView sceneView = arFragment.getArSceneView();
+ Vector3 cameraPos = sceneView.getScene().getCamera().getWorldPosition();
+ Vector3 cameraForward = sceneView.getScene().getCamera().getForward();
+ Vector3 position = Vector3.add(cameraPos, cameraForward.scaled(0.5f));
+ Pose pose = Pose.makeTranslation(position.x, position.y, position.z);
+ Anchor anchor = session.createAnchor(pose);
+
+
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
TransformableNode model = new TransformableNode(arFragment.getTransformationSystem());
model.setRenderable(modelRenderable);
//大きさを指定します。
model.getScaleController().setMinScale(0.01f);
model.getScaleController().setMaxScale(2.0f);
//v:width v1:? v2:height
model.setLocalScale(new Vector3(0.5f,0f,0.5f));
//y軸 縦軸 を中心に180度回転
model.setLocalRotation(Quaternion.axisAngle(new Vector3(0,1,0),-120));
model.setParent(anchorNode);
model.select();
+
});
}
}
@Override
protected void onResume() {
super.onResume();
if (ContextCompat.checkSelfPermission(this, CAMERA_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {CAMERA_PERMISSION}, CAMERA_PERMISSION_CODE);
}
}
}
```