一.先用代码来创建一个圆的模型
1.xml文件,用fragment是因为这里面自带的识别平面,和自带的模型放大缩小功能
<fragment
android:id="@+id/ux_fragment"
android:name="com.google.ar.sceneform.ux.ArFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2.创建一个球形模型
ShapeFactory 的三个方法
1.makeSphere 创建一个球
2.makeCube 创建一个立方体
3.makeCylinder 创建一个圆柱
/**
* 在0.0f、0.15f、0.0f位置构造立方体,并设为蓝色
*
* @param hitResult - If the hit result is a plane
* @param color 设置颜色
*/
private void makeOpaqueWithColor(HitResult hitResult, Bitmap bitmap) {
MaterialFactory.makeOpaqueWithColor(this, new
com.google.ar.sceneform.rendering.Color(android.graphics.Color.BLUE) )
.thenAccept(material -> {
addNodeToScene(fragment, hitResult.createAnchor(),
ShapeFactory.makeSphere(0.1f,
new Vector3(0.0f, 0.15f, 0.0f), material));
});
}
3. 创建一个锚点来显示模型
/**
* 将节点添加到场景和对象
*
* @param fragment - sceneform fragment
* @param anchor - created anchor at the tapped position
* @param modelObject - rendered object
*/
private void addNodeToScene(ArFragment fragment, Anchor anchor, ModelRenderable modelObject) {
AnchorNode anchorNode = new AnchorNode(anchor);
TransformableNode andy = new TransformableNode(fragment.getTransformationSystem());
andy.setParent(anchorNode);
andy.setRenderable(modelObject);
andy.select();
fragment.getArSceneView().getScene().addChild(anchorNode);
}
5. 识别平面成功后,点击显示模型
fragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);
fragment.setOnTapArPlaneListener(new BaseArFragment.OnTapArPlaneListener() {
@Override
public void onTapPlane(HitResult hitResult, Plane plane, MotionEvent motionEvent) {
makeTextureSphere(hitResult, bitmaps);
}
});
二. 模型材质动态设置
MaterialFactory.makeOpaqueWithColor(this, new com.google.ar.sceneform.rendering.Color(android.graphics.Color.BLUE))
.thenAccept(material -> {
material.setFloat3("color",
new com.google.ar.sceneform.rendering.Color(Color.GREEN));
addNodeToScene(fragment, hitResult.createAnchor(),
ShapeFactory.makeSphere(0.1f,
new Vector3(0.0f, 0.15f, 0.0f), material));
});
material材质设置
setFloat3(String, Color); //这个Color是sceneform包下的
material.setFloat3("color",new com.google.ar.sceneform.rendering.Color(Color.GREEN));