Node与Renderable到底啥关系?
Node和Renderable没有直接关系,是通过RenderableInstance关联的。而RenderableInstance是由renderable创建的,Node一直持有RenderableInstance 的引用,故而设置renderable的时候,会根据新renderable来更新RenderableInstance。
renderable构造时先生成renderable对象,然后把LoadRenderableFromFilamentGltfTask加载后的
RenderableInternalFilamentAssetData 赋值给renderable。这样renderable就有了数据
LoadRenderableFromFilamentGltfTask(T renderable, Context context, Uri sourceUri, @Nullable Function<String, Uri> urlResolver) {
this.renderable = renderable;
IRenderableInternalData data = renderable.getRenderableData();
if (data instanceof RenderableInternalFilamentAssetData) {
this.renderableData =
(com.google.ar.sceneform.rendering.RenderableInternalFilamentAssetData) data;
} else {
throw new IllegalStateException("Expected task type " + TAG);
}
this.renderableData.resourceLoader =
new ResourceLoader(EngineInstance.getEngine().getFilamentEngine());
this.renderableData.urlResolver =
missingPath -> getUriFromMissingResource(sourceUri, missingPath, urlResolver);
this.renderableData.context = context.getApplicationContext();
this.renderable.getId().update();
}
请君听我细细说来。
Node有个方法setRenderable(@Nullable Renderable renderable)
/**
* Sets the {@link Renderable} to display for this node. If {@link
* Node#setCollisionShape(CollisionShape)} is not set, then {@link Renderable#getCollisionShape()}
* is used to detect collisions for this {@link Node}.
*
* @see ModelRenderable
* @see com.google.ar.sceneform.rendering.ViewRenderable
* @param renderable Usually a 3D model. If null, this node's current renderable will be removed.
*/
public void setRenderable(@Nullable Renderable renderable) {
AndroidPreconditions.checkUiThread();
// Renderable hasn't changed, return early.
if (renderableInstance != null && renderableInstance.getRenderable() == renderable) {
return;
}
//设置新的Renderable时,先把RenderableInstance从render里移除,然后置空,如果Renderable不为空,再创建一个
if (renderableInstance != null) {
if (active) {
// Log.d("steven","renderableInstance.detachFromRenderer() ....");
renderableInstance.detachFromRenderer();
}
renderableInstance = null;
}
if (renderable != null) {
RenderableInstance instance = renderable.createInstance(this);
if (active && (scene != null && !scene.isUnderTesting())) {
instance.attachToRenderer(getRendererOrDie());
}
renderableInstance = instance;
renderableId = renderable.getId().get();
} else {
renderableId = ChangeId.EMPTY_ID;
}
refreshCollider();
}
从上面代码我们可知,如果设置一个Renderable,会先判断这个Renderable和现在的是否同一个,是就返回。否则会先通过renderableInstance.detachFromRenderer(); 把renderableInstance从renderder里移除掉。我们顺便看看移除都做了什么
/**
* @hide
*/
public void detachFromRenderer() {
Renderer rendererToDetach = attachedRenderer;
if (rendererToDetach != null) {
detachFilamentAssetFromRenderer();
// this method was not called before
destroyAsset();
rendererToDetach.removeInstance(this);
renderable.detatchFromRenderer();
}
}
我们分别跟下上面方法的实现。
1. 第一步 RenderableInstance: detachFilamentAssetFromRenderer()
void detachFilamentAssetFromRenderer() {
FilamentAsset currentFilamentAsset = filamentAsset;
if (currentFilamentAsset != null) {
int[] entities = currentFilamentAsset.getEntities();
for (int entity : entities) {
Preconditions.checkNotNull(attachedRenderer).getFilamentScene().removeEntity(entity);
}
int root = currentFilamentAsset.getRoot();
Preconditions.checkNotNull(attachedRenderer).getFilamentScene().removeEntity(root);
}
}
从代码中看到从这里开始调用filament的类Scene去移除entity了。
- 第二步 RenderableInstance: destroyAsset() 这里调用了filament的类AssetLoader去销毁asset
public void destroyAsset() {
if (filamentAsset != null) {
loader.destroyAsset(filamentAsset);
filamentAsset = null;
}
}
- 第三步 Renderer: rendererToDetach.removeInstance()
第一行代码 removeModelInstanceInternal()其实是个空实现,啥也没处理。
第二行代码 则是调用了filament的Scene去移除entity 。
第三行代码很简单就是把instance从集合里移除掉,这个集合会在RenderableInstance.attachToRenderer()时把instance添加到集合里。
/** @hide */
void removeInstance(RenderableInstance instance) {
removeModelInstanceInternal(instance);
scene.remove(instance.getRenderedEntity());
renderableInstances.remove(instance);
}
private void removeModelInstanceInternal(RenderableInstance instance) {return ;}
- 第四步 Renderable:detatchFromRenderer()-这个是个抽象方法,具体实现我们需要看子类的实现。
Renderable有两个子类分别是ModelRenderable和ViewRenderable。很奇怪的是ModelRenderable没有实现(这里会不会内存泄漏?),只有ViewRenderable实现了,如下所示
@Override
void detatchFromRenderer() {
// Log.d("steven","detatchFromRenderer");
Preconditions.checkNotNull(viewRenderableData).getRenderView().detachView();
this.renderer = null;
}
这里仅仅把view从frameLayout移除
void detachView() {
if (viewAttachmentManager != null) {
viewAttachmentManager.removeView(this);
viewAttachmentManager = null;
}
}
void removeView(View view) {
if (view.getParent() != frameLayout) {
return;
}
frameLayout.removeView(view);
}
网友评论