回答:
Cameraクラスには、スプライトがカメラのフラスタム内にある場合にtrueを返すパブリックメソッドpointInFrustum(Vector3 point)を持つFrustumオブジェクトが含まれています。他のカリングテクニックについては、ユーザーWikiをご覧ください。http://code.google.com/p/libgdx-users/wiki/Culling
タイルを使用して2Dゲームを作成している場合、独自のカリングを簡単に実装できます。これは、タイル配列内で必要なものだけを完全に消去するだけなので、はるかに安価です。
知っておくべきこと:
これで、描画するタイルの数を計算できます。
viewport.width / tileWidth
viewport.height / tileHeight
計算はすべてがどのように設定されているかによって異なりますが、非常に簡単です。たとえば、画面の中央が左上または左下のカメラ位置である場合、違いがあります。
あなたはこのようなもので終わるはずです:
int startX = cameraWorldPosX / tileWidth;
int startY = cameraWorldPosY / tileHeight;
//When you have the position of the camera in the center of the screen you do something like this:
int startX = (cameraWorldPosX - viewport.width / 2) / tileWidth;
int startY = (cameraWorldPosY - viewport.height / 2) / tileHeight;
for (int y = startY; y < startY + viewportWidth / tileWidth; y++)
{
for (int x = startX; x < startX + viewportHeight / tileHeight; x++)
{
//Draw logic
}
}
ポイントが錐台内にあるかどうかをチェックすることに対するこれの利点は、後者では、水平タイルの量と等しい設定されたタイルの量を常に反復する単純な配列を使用する代わりに、各ポイントを反復処理する必要があることです。 *実際に描画する必要がある垂直タイル。このようにして、巨大なマップを作成し、フレームレートを向上させることができます。残念ながら、これは3Dを使用する場合は難しくなり、トリッキーになりますが、ユーザーがカメラを自由に使えるようになると、指数関数的に難しくなります。キャラクターと一緒に移動する固定パースペクティブカメラが、マップを表すメッシュの配列に対して同じトリックを実行するためにハードコーディングされた変数をいくつか必要とすることを想像できます。
この関数は、アクターが表示されているかどうかをチェックします(2Dでのみ機能します)。たとえば、俳優がグループ内にいる場合など、あらゆる状況で機能します。
/**
* Returns if the actor is visible or not. Useful to implement 2D culling.
**/
public static boolean actorIsVisible(Actor actor) {
Vector2 actorStagePos = actor.localToStageCoordinates(new Vector2(0,0));
Vector2 actorStagePosTl = actor.localToStageCoordinates(new Vector2(
actor.getWidth(),
actor.getHeight()));
Vector3 actorPixelPos = new Vector3(actorStagePos.x, actorStagePos.y, 0);
Vector3 actorPixelPosTl = new Vector3(actorStagePosTl.x, actorStagePosTl.y, 0);
actorPixelPos = actor.getStage().getCamera().project(actorPixelPos);
actorPixelPosTl = actor.getStage().getCamera().project(actorPixelPosTl);
return !(actorPixelPosTl.x < 0 ||
actorPixelPos.x > Gdx.graphics.getWidth() ||
actorPixelPosTl.y < 0 ||
actorPixelPos.y > Gdx.graphics.getHeight()
);
}