Libgdx 视锥体剔除在 actor.draw() 中
我正在使用 libgdx 开发一个小游戏.这是一个 2D 自上而下的游戏,上面有一些大地图和许多对象.所以我只想在我的视锥中渲染东西.我见过 camera.frustum.***inFrustum(Paramter... p)
.但我使用的是scene2d,我在Stage
中设置了相机,将SpriteBatch
的投影矩阵设置为camera.combined
和我的Actor
在重写的 draw(SpriteBatch batch)
方法中绘制自己.如何访问 camera
或那里的视锥?有没有办法从 spritebatch 中得到它?或者还有其他方法可以实现视锥体剔除吗?
I am developing a little game using libgdx. It is a 2d top down game with some big maps and many objects on them. So i just want to render things in my view frustum. I have seen the camera.frustum.***inFrustum(Paramter... p)
. But i am using scene2d and i set up the camera in the Stage
, set the SpriteBatch
s projection matrix to camera.combined
and my Actor
s draw themself in the overridden draw(SpriteBatch batch)
method. How can i access the camera
or the view frustum there? Is there a way to get that out of the spritebatch? Or is there another way to realise fiew frustum culling?
推荐答案
你不需要自己做这个.如果您使用带有相机的舞台,它会自动绘制视图内的演员.
You do not need to do this yourself. If you are using a Stage with a camera it automatically just draw the actors that are inside of the view.
如果您想从舞台上访问相机,只需从舞台上获取它.stage.getCamera()
If you want to access the camera from the Stage simply get it from the stage. stage.getCamera()
以下是如何从舞台内的相机中获取截锥体:
Here is how you get the frustum from an camera inside of an stage:
Camera cam = this.stage.getCamera();
Frustum f = cam.frustum;
float h = cam.viewportHeight;
float w = cam.viewportWidth;
Matrix4 m = cam.combined; //combind projection matrix
Matrix4 m2= cam.projection;
...
...
您可以从相机中获取所需的每个投影矩阵...
From the camera you can get every projection matrix you need an so on...
在演员内部你可以得到舞台,在舞台中你可以得到上面显示的一切..
Inside of an actor you can get the stage and of the stage you can get everything shown above..
actor.getStage().getCamera().frustum;
相关文章