SkAR Java: drawing planes as paths

FREEZE.unindexed

Bug: skia:
Change-Id: Ifcb73345d7073f13bf027d79fd90bf3fbff73e5b
Reviewed-on: https://skia-review.googlesource.com/141229
Reviewed-by: Mike Reed <reed@google.com>
This commit is contained in:
ziadb 2018-07-13 12:40:10 -04:00 committed by Ziad Ben Hadj-Alouane
parent acb50d11c1
commit 5a31a60889

View File

@ -39,7 +39,6 @@ public class DrawManager {
private float viewportHeight;
private ColorFilter lightFilter;
private BitmapShader planeShader;
private Bitmap planeTexture;
public ArrayList<float[]> modelMatrices = new ArrayList<>();
public void updateViewport(float width, float height) {
@ -178,7 +177,6 @@ public class DrawManager {
continue;
}
// Get plane model matrix
float[] model = new float[16];
plane.getCenterPose().toMatrix(model, 0);
@ -186,58 +184,63 @@ public class DrawManager {
// Initial rotation
float[] initRot = SkARMatrix.createXYtoXZRotationMatrix();
float[] initScale = new float[16];
Matrix.setIdentityM(initScale, 0);
Matrix.scaleM(initScale, 0, 1f, 1f, 1f);
android.graphics.Matrix scale = SkARMatrix.createMatrixFrom4x4(SkARMatrix.multiplyMatrices4x4(new float[][] {initScale}));
// Matrix = mvpv
float[][] matrices = {initRot, model, viewMatrix, projectionMatrix, SkARMatrix.createViewportMatrix(viewportWidth, viewportHeight)};
android.graphics.Matrix mvpv = SkARMatrix.createMatrixFrom4x4(SkARMatrix.multiplyMatrices4x4(matrices));
canvas.save();
canvas.setMatrix(mvpv);
drawPlaneAsPath(canvas, plane);
canvas.restore();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
// Android version P and higher
drawPlane(canvas, mvpv, plane);
} else {
drawPlaneAsPath(canvas, mvpv, plane);
}
}
}
// Helper function that draws an AR plane using a path
private void drawPlaneAsPath(Canvas canvas, Plane plane) {
private void drawPlaneAsPath(Canvas canvas, android.graphics.Matrix mvpv, Plane plane) {
int vertsSize = plane.getPolygon().limit() / 2;
FloatBuffer polygon = plane.getPolygon();
polygon.rewind();
Path path = new Path();
path.moveTo(polygon.get(0), polygon.get(1));
// Build source path from polygon data
Path pathSrc = new Path();
pathSrc.moveTo(polygon.get(0), polygon.get(1));
for (int i = 1; i < vertsSize; i++) {
path.lineTo(polygon.get(i * 2), polygon.get(i * 2 + 1));
pathSrc.lineTo(polygon.get(i * 2), polygon.get(i * 2 + 1));
}
path.close();
pathSrc.close();
// Set up paint
Paint p = new Paint();
p.setShader(planeShader);
p.setColorFilter(new PorterDuffColorFilter(Color.argb(0.4f, 1, 0, 0), PorterDuff.Mode.SRC_ATOP));
p.setColorFilter(new PorterDuffColorFilter(Color.argb(0.4f, 1, 0, 0),
PorterDuff.Mode.SRC_ATOP));
p.setAlpha(120);
//canvas.drawPath(path, p); TODO: enable this when path is drawn on GPU
// Build destination path by transforming source path
Path pathDst = new Path();
pathSrc.transform(mvpv, pathDst);
RectF r = new RectF();
path.computeBounds(r, true);
canvas.drawRect(r, p);
// Shader local matrix
android.graphics.Matrix lm = new android.graphics.Matrix();
lm.setScale(0.0005f, 0.0005f);
lm.postConcat(mvpv);
planeShader.setLocalMatrix(lm);
// Draw dest path
canvas.save();
canvas.setMatrix(new android.graphics.Matrix());
canvas.drawPath(pathDst, p);
canvas.restore();
}
public void initializePlaneShader(Context context, String gridDistanceTextureName) throws IOException {
// Read the texture.
planeTexture =
Bitmap planeTexture =
BitmapFactory.decodeStream(context.getAssets().open(gridDistanceTextureName));
// Set up the shader
planeShader = new BitmapShader(planeTexture, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
android.graphics.Matrix m = new android.graphics.Matrix();
m.setScale(0.0005f, 0.0005f);
planeShader.setLocalMatrix(m);
}
private float[] getTextScaleMatrix(float size) {