import com.jme3.math.FastMath; //导入方法依赖的package包/类
protected void controlRender(RenderManager rm, ViewPort vp){
BoundingVolume bv = spatial.getWorldBound();
Camera cam = vp.getCamera();
float atanNH = FastMath.atan(cam.getFrustumNear() * cam.getFrustumTop());
float ratio = (FastMath.PI / (8f * atanNH));
float newDistance = bv.distanceTo(vp.getCamera().getLocation()) / ratio;
int level;
if (Math.abs(newDistance - lastDistance) <= distTolerance)
level = lastLevel; // we haven't moved relative to the model, send the old measurement back.
else if (lastDistance > newDistance && lastLevel == 0)
level = lastLevel; // we're already at the lowest setting and we just got closer to the model, no need to keep trying.
else if (lastDistance < newDistance && lastLevel == numLevels - 1)
level = lastLevel; // we're already at the highest setting and we just got further from the model, no need to keep trying.
else{
lastDistance = newDistance;
// estimate area of polygon via bounding volume
float area = AreaUtils.calcScreenArea(bv, lastDistance, cam.getWidth());
float trisToDraw = area * trisPerPixel;
level = numLevels - 1;
for (int i = numLevels; --i >= 0;){
if (trisToDraw - numTris[i] < 0){
break;
}
level = i;
}
lastLevel = level;
}
spatial.setLodLevel(level);
}