diff --git a/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java b/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java index 9acc580..187eb79 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java +++ b/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java @@ -31,23 +31,17 @@ import com.google.ar.core.Plane; import com.google.ar.core.Point; import com.google.ar.core.Point.OrientationMode; -import com.google.ar.core.PointCloud; import com.google.ar.core.Pose; import com.google.ar.core.Session; import com.google.ar.core.Trackable; import com.google.ar.core.TrackingState; import com.google.ar.core.examples.java.common.framework.RWT.RWTRenderer; import com.google.ar.core.examples.java.common.framework.gameMain.OvergroundActor; -import com.google.ar.core.examples.java.common.framework.model3D.BaseObject3D; import com.google.ar.core.examples.java.common.framework.model3D.CollisionResult; -import com.google.ar.core.examples.java.common.framework.model3D.ModelFactory; -import com.google.ar.core.examples.java.common.framework.model3D.ModelFileFormatException; import com.google.ar.core.examples.java.common.framework.model3D.Object3D; import com.google.ar.core.examples.java.common.framework.model3D.Position3D; import com.google.ar.core.examples.java.common.framework.model3D.Quaternion3D; import com.google.ar.core.examples.java.common.framework.model3D.Universe; -import com.google.ar.core.examples.java.common.framework.physics.AngularVelocity3D; -import com.google.ar.core.examples.java.common.framework.physics.Force3D; import com.google.ar.core.examples.java.common.framework.physics.Ground; import com.google.ar.core.examples.java.common.framework.physics.PhysicalSystem; import com.google.ar.core.examples.java.common.framework.physics.Solid3D; @@ -59,15 +53,15 @@ import com.google.ar.core.examples.java.common.helpers.TapHelper; import com.google.ar.core.examples.java.common.helpers.TrackingStateHelper; import com.google.ar.core.examples.java.common.java3d.Appearance; -import com.google.ar.core.examples.java.common.java3d.Box; import com.google.ar.core.examples.java.common.java3d.Material; +import com.google.ar.core.examples.java.common.java3d.Shape3D; import com.google.ar.core.examples.java.common.java3d.Sphere; -import com.google.ar.core.examples.java.common.java3d.Transform3D; import com.google.ar.core.examples.java.common.java3d.TransparencyAttributes; +import com.google.ar.core.examples.java.common.java3d.TriangleArray; +import com.google.ar.core.examples.java.common.java3d.TriangleFanArray; import com.google.ar.core.examples.java.common.java3d.Vector3d; import com.google.ar.core.examples.java.common.rendering.BackgroundRenderer; import com.google.ar.core.examples.java.common.rendering.ObjectRenderer; -import com.google.ar.core.examples.java.common.rendering.ObjectRenderer.BlendMode; import com.google.ar.core.examples.java.common.rendering.PlaneRenderer; import com.google.ar.core.examples.java.common.rendering.PointCloudRenderer; import com.google.ar.core.exceptions.CameraNotAvailableException; @@ -77,11 +71,9 @@ import com.google.ar.core.exceptions.UnavailableSdkTooOldException; import com.google.ar.core.exceptions.UnavailableUserDeclinedInstallationException; import java.io.IOException; +import java.nio.FloatBuffer; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; @@ -100,7 +92,7 @@ private Universe universe; private PhysicalSystem physicalSystem; private Ground ground = null; - private Object3D rectObj; + private Object3D polyObj; private boolean installRequested; @@ -393,25 +385,67 @@ placedPlane = plane; Appearance ap = new Appearance(); ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f)); - Box rect = new Box(1.0f, 1.0f, 1.0f, ap); - rectObj = new Object3D("plane", rect); + FloatBuffer vertices = plane.getPolygon(); + vertices.position(0); + TriangleFanArray polygon = new TriangleFanArray(vertices.remaining() / 2, TriangleArray.COORDINATES, new int[]{vertices.remaining() / 2}); + for (int i = 0; i < vertices.remaining() / 2; i++) { + polygon.setCoordinate(i, new float[]{vertices.get(i * 2), 0.0f, vertices.get(i * 2 + 1)}); + } + polyObj = new Object3D("plane", new Shape3D(polygon, ap)); Pose p = plane.getCenterPose(); - rectObj.scale(plane.getExtentX() / 2.0, 0.05, plane.getExtentZ() / 2.0); - rectObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); - rectObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); - System.out.println(plane + " x:" + plane.getExtentX() + ", y:" + plane.getExtentZ()); - ground = new Ground(rectObj); + polyObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); + polyObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); + ground = new Ground(polyObj); universe.place(ground); } else if (plane.equals(placedPlane)) { + Appearance ap = new Appearance(); + ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f)); + FloatBuffer vertices = plane.getPolygon(); + vertices.position(0); + TriangleFanArray polygon = new TriangleFanArray(vertices.remaining() / 2, TriangleArray.COORDINATES, new int[]{vertices.remaining() / 2}); + for (int i = 0; i < vertices.remaining() / 2; i++) { + polygon.setCoordinate(i, new float[]{vertices.get(i * 2), 0.0f, vertices.get(i * 2 + 1)}); + } + polyObj = new Object3D("plane", new Shape3D(polygon, ap)); Pose p = plane.getCenterPose(); - rectObj.scale(plane.getExtentX() / 2.0, 0.05, plane.getExtentZ() / 2.0); - rectObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); - rectObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); - System.out.println(plane + " x:" + plane.getExtentX() + ", y:" + plane.getExtentZ()); - ground.updateBody(rectObj); + polyObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); + polyObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); + universe.displace(ground); + ground = new Ground(polyObj); + universe.place(ground); } } +///////////////////////////////////////////////////////////////////////////////////// +// 以下は複数検出面に対応したコードだが、検出面が2個以上になるとちゃんと動かない? +// +// Collection updatedPlanes = frame.getUpdatedTrackables(Plane.class); +// if (updatedPlanes != null && updatedPlanes.size() > 0) { +// if (ground != null) universe.displace(ground); +// Object3D polygons[] = new Object3D[updatedPlanes.size()]; +// int i = 0; +// for (Plane plane : updatedPlanes) { +// Appearance ap = new Appearance(); +// ap.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f)); +// FloatBuffer vertices = plane.getPolygon(); +// vertices.position(0); +// TriangleFanArray polygon = new TriangleFanArray(vertices.remaining() / 2, TriangleArray.COORDINATES, new int[]{vertices.remaining() / 2}); +// for (int j = 0; j < vertices.remaining() / 2; j++) { +// polygon.setCoordinate(j, new float[]{vertices.get(j * 2), 0.0f, vertices.get(j * 2 + 1)}); +// } +// Object3D polyObj = new Object3D("plane" + i, new Shape3D(polygon, ap)); +// Pose p = plane.getCenterPose(); +// polyObj.apply(new Position3D(p.tx(), p.ty(), p.tz()), false); +// polyObj.apply(new Quaternion3D((double) p.qx(), (double) p.qy(), (double) p.qz(), (double) p.qw()), false); +// polygons[i] = polyObj; +// i++; +// } +// ground = new Ground(new Object3D("ground", polygons)); +// universe.place(ground); +// } +///////////////////////////////////////////////////////////////////////////////////// + + // planeRenderer.drawPlanes( // session.getAllTrackables(Plane.class), camera.getDisplayOrientedPose(), projmtx); //