如何解决如何在 Android 上的 Sceneform/ARCore 中处理模型缩放?
我正在尝试使用来自服务器 Api 的 AR Core Sceneform 加载 3d 模型。模型位置旋转也来自 API 。我能够加载 3D 模型。但问题是,两个 3D 模型都有不同的比例值。但看起来很明智,两种型号的尺寸都是一样的。这是我的问题。我无法以相同的尺寸渲染两个模型。我尝试了很多方法但没有用。不幸的是,互联网上也没有 awnser。
这是缩放3D模型的代码
modelNode.setLocalScale(new Vector3(0.0005f,0.0005f,0.0005));
ModelRenderable.builder()
.setSource(
this,Uri.fromFile(getModelPath(modelPathArray.get(finalI),jsonObjectDuplicate)))
.setIsFilamentGltf(true)
.build()
.thenAccept(
modelRenderable -> {
OfflineARActivity activity = weakActivity.get();
if (activity != null) {
activity.renderable = modelRenderable;
// Create the Anchor.
Anchor anchor = hitResult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
TransformableNode modelNode = new TransformableNode(arFragment.getTransformationSystem());
modelNode.setParent(anchorNode);
//Get Positions From Saved Data
// TinyDB tinyDB = new TinyDB(OfflineARActivity.this);
String position = tinyDB.getString(modeNameArray.get(finalI)+"Position");
String scale = tinyDB.getString(modeNameArray.get(finalI)+"Scale");
String rotate = tinyDB.getString(modeNameArray.get(finalI)+"Rotation");
String anim = tinyDB.getString(modeNameArray.get(finalI)+"Animation");
// Log.v("Plugxr","Scale : "+scale);
// Log.v("Plugxr","Model Name Inside : "+modeNameArray.get(finalI));
// Positions
try {
JSONObject jsonObject = new JSONObject(position);
String px = jsonObject.getString("x");
String py = jsonObject.getString("y");
String pz = jsonObject.getString("z");
Vector3 positions = new Vector3(Float.parseFloat(px),Float.parseFloat(py),Float.parseFloat(pz));
modelNode.setLocalPosition(positions);
Log.v("Plugxr","Positions : "+positions.toString());
Log.v("Plugxr","Positions : "+modelNode.getLocalPosition());
Log.v("Plugxr","Positions : "+modeNameArray.get(finalI));
} catch (JSONException e) {
e.printStackTrace();
}
// Scaling
try {
JSONObject jsonObject = new JSONObject(scale);
String sx = jsonObject.getString("x");
String sy = jsonObject.getString("y");
String sz = jsonObject.getString("z");
Vector3 scaling = new Vector3(Float.parseFloat(sx),Float.parseFloat(sy),Float.parseFloat(sz));
// Log.v("Plugxr","Scale : "+scaling);
// Log.v("Plugxr","Scale : "+modeNameArray.get(finalI));
// Bounding Box
/*Vector3 finalSize =
new Vector3(
(float) (Math.round((size.x * transformableNodeScale.x * 100) * 10) / 10.0),(float) (Math.round((size.y * transformableNodeScale.y * 100) * 10) / 10.0),(float) (Math.round((size.z * transformableNodeScale.z * 100) * 10) / 10.0));*/
ScaleController scaleController = modelNode.getScaleController();
scaleController.setMaxScale((scaling.x+scaling.y+scaling.z)/3);
scaleController.setMinScale(((scaling.x+scaling.y+scaling.z)/3)-0.00001f);
/*Box boundingBox = (Box) modelRenderable.getCollisionShape();
if (boundingBox != null) {
Vector3 boundingBoxSize = boundingBox.getSize();
//float maxExtent = Math.max(boundingBoxSize.x,Math.max(boundingBoxSize.y,boundingBoxSize.z));
//float targetSize = 0.1f; // Whatever size you want.
// float scaleBounfing = targetSize / maxExtent;
Vector3 finalSize =
new Vector3(
(float) (Math.round((boundingBoxSize.x * modelNode.getWorldScale().x * 100) * 10) / 10.0),(float) (Math.round((boundingBoxSize.y * modelNode.getWorldScale().y * 100) * 10) / 10.0),(float) (Math.round((boundingBoxSize.z * modelNode.getWorldScale().z * 100) * 10) / 10.0));
modelNode.setLocalScale(new Vector3(finalSize.x,finalSize.y,finalSize.z));
}*/
//Log.v("Plugxr","Scale1 : "+modelNode.getLocalScale());
modelNode.setLocalScale(new Vector3(scaling.x,scaling.y,scaling.z));
// Log.v("Plugxr","Scale2 : "+modelNode.getLocalScale() );
// modelNode.setLocalScale(scaling);
} catch (JSONException e) {
e.printStackTrace();
}
// Rotation
try {
JSONObject jsonObject = new JSONObject(rotate);
String rx = jsonObject.getString("x");
String ry = jsonObject.getString("y");
String rz = jsonObject.getString("z");
Quaternion qrx = Quaternion.eulerAngles(new Vector3(Float.parseFloat(rx) - 90,Float.parseFloat("0.0"),Float.parseFloat("0.0")));
Quaternion qry = Quaternion.eulerAngles(new Vector3(Float.parseFloat("0.0"),Float.parseFloat(ry),Float.parseFloat(rz)));
modelNode.setLocalRotation(Quaternion.multiply(qrx,qry));
} catch (JSONException e) {
e.printStackTrace();
}
modelNode.setRenderable(modelRenderable);
modelNode.select();
// Set Model Name
//modelNode.setName(modelPathArray.get(finalI).getName());
// Add model name to modelArray
//modelsArray.add(modelPathArray.get(finalI).getName());
// Later
FilamentAsset filamentAsset = modelNode.getRenderableInstance().getFilamentAsset();
if (filamentAsset.getAnimator().getAnimationCount() > 0) {
animators.add(new AnimationInstance(filamentAsset.getAnimator(),System.nanoTime()));
}
Color color = colors.get(nextColor);
nextColor++;
for (int j = 0; j < modelRenderable.getSubmeshCount(); ++j) {
Material material = modelRenderable.getMaterial(j);
material.setFloat4("baseColorFactor",color);
}
}
})
.exceptionally(
throwable -> {
/*Toast toast =
Toast.makeText(this,"Unable to load model"+modeNameArray.get(finalI),Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0);
toast.show();*/
return null;
});
解决方法
试试这个:
Anchor anchor = hitResult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
node.getScaleController().setMinScale(2.0f);
node.getScaleController().setMaxScale(3.0f);
node.setLocalScale(new Vector3(2.5f,2.5f,2.5f));
node.setParent(anchorNode);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。