Last active
June 8, 2016 20:47
-
-
Save lukz/133da51bbcb1917f43e883214b920475 to your computer and use it in GitHub Desktop.
Group that allows to change its perspective along X axis (will be very simple to change axis) using orthographic camera and Scene2d. You have to set camera.near = -1000 and camera.far = 1000 on your OrthographicCamera.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package om.mandir.gdx.utils.actors; | |
import com.badlogic.gdx.graphics.g2d.Batch; | |
import com.badlogic.gdx.math.Affine2; | |
import com.badlogic.gdx.math.Matrix4; | |
import com.badlogic.gdx.scenes.scene2d.Group; | |
/** | |
* Allows to render Group in perspective. | |
* | |
* Requires those OrthographicCamera settings: | |
* camera.near = -1000; | |
* camera.far = 1000; | |
* @author Lukasz Zmudziak, @lukz_dev on 2016-06-06. | |
*/ | |
public class PerspectiveGroup extends Group { | |
private Matrix4 transformMatrix = new Matrix4(); | |
private Matrix4 projectionMatrix = new Matrix4(); | |
private Affine2 worldTransform = new Affine2(); | |
private float xAxisRotation = 0; | |
private float perspective = 1f / 1500f; | |
@Override | |
public void draw(Batch batch, float parentAlpha) { | |
transformMatrix.set(batch.getTransformMatrix()); | |
projectionMatrix.set(batch.getProjectionMatrix()); | |
batch.end(); | |
float originX = this.getOriginX(); | |
float originY = this.getOriginY(); | |
float rotation = this.getRotation(); | |
float scaleX = this.getScaleX(); | |
float scaleY = this.getScaleY(); | |
float scaledOriginX = originX * scaleX; | |
float scaledOriginY = originY * scaleY; | |
worldTransform.setToTrnRotScl(scaledOriginX, scaledOriginY, rotation, scaleX, scaleY); | |
if (originX != 0 || originY != 0) worldTransform.translate(-originX, -originY); | |
batch.getTransformMatrix().set(worldTransform); | |
batch.getProjectionMatrix().translate(getX() + (originX - scaledOriginX), getY() + (originY - scaledOriginY), 0); | |
batch.getProjectionMatrix().val[Matrix4.M32] = perspective; | |
batch.getProjectionMatrix().rotate(1, 0, 0, xAxisRotation); | |
batch.begin(); | |
drawChildren(batch, parentAlpha); | |
//reset | |
batch.setTransformMatrix(transformMatrix); | |
batch.setProjectionMatrix(projectionMatrix); | |
} | |
public float getPerspective() { | |
return perspective; | |
} | |
public void setPerspective(float perspective) { | |
this.perspective = perspective; | |
} | |
public float getXAxisRotation() { | |
return this.xAxisRotation; | |
} | |
public void setXAxisRotation(float val) { | |
this.xAxisRotation = val; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment