Created
December 5, 2018 14:53
-
-
Save badlogic/38a5828b5bc494cb3c9c56e8f5f62b49 to your computer and use it in GitHub Desktop.
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 com.esotericsoftware.spine; | |
import com.badlogic.gdx.ApplicationAdapter; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.backends.lwjgl.LwjglApplication; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; | |
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; | |
import com.badlogic.gdx.utils.FloatArray; | |
import com.badlogic.gdx.utils.ShortArray; | |
import com.esotericsoftware.spine.attachments.ClippingAttachment; | |
import com.esotericsoftware.spine.utils.SkeletonClipping; | |
public class ClipNaan extends ApplicationAdapter { | |
ShapeRenderer renderer; | |
OrthographicCamera camera; | |
private ClippingAttachment clipping; | |
private float[] vertices; | |
private short[] triangles; | |
private FloatArray clippedVertices; | |
private ShortArray clippedTriangles; | |
private SkeletonClipping clipper; | |
private Skeleton skeleton; | |
@Override | |
public void create () { | |
SkeletonData skeletonData = new SkeletonData(); | |
BoneData boneData = new BoneData(0, "bone", null); | |
skeletonData.getBones().add(boneData); | |
SlotData slotData = new SlotData(0, "slot", boneData); | |
slotData.setAttachmentName("clipping"); | |
skeletonData.getSlots().add(slotData); | |
skeleton = new Skeleton(skeletonData); | |
skeleton.updateWorldTransform(); | |
camera = new OrthographicCamera(); | |
renderer = new ShapeRenderer(); | |
renderer.setAutoShapeType(true); | |
} | |
@Override | |
public void render () { | |
clipping = new ClippingAttachment("clipping"); | |
clipping.setBones(new int[] {1, 0, 1, 0, 1, 0, 1, 0}); | |
clipping.setVertices( | |
new float[] {-300.72156f, 708.0388f, 1, -300.72153f, 168.0389f, 1, 137.288f, 8.616486f, 1, 137.28796f, 548.61633f, 1}); | |
clipping.setWorldVerticesLength(8); | |
clipper = new SkeletonClipping(); | |
clipper.clipStart(skeleton.getSlots().first(), clipping); | |
vertices = new float[] {137.288f, 8.616455f, -300.72153f, 168.03888f, -300.7216f, 708.0388f, 137.28793f, 548.61633f}; | |
float[] uvs = vertices; | |
triangles = new short[] {1, 2, 3, 1, 3, 0}; | |
clipper.clipTriangles(vertices, vertices.length, triangles, triangles.length, uvs, 0, 0, false); | |
clippedVertices = clipper.getClippedVertices(); | |
clippedTriangles = clipper.getClippedTriangles(); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
camera.setToOrtho(false); | |
camera.zoom = 2f; | |
camera.position.x = -200; | |
camera.position.y = 250; | |
camera.update(); | |
renderer.setProjectionMatrix(camera.combined); | |
renderer.begin(); | |
renderer.setColor(1, 1, 1, 1); | |
drawTriangles(vertices, 2, triangles, triangles.length); | |
renderer.setColor(0, 1, 0, 1); | |
FloatArray clippingPolygon = clipper.clippingPolygons.get(0); | |
polyline(clippingPolygon.items, clippingPolygon.size, 2); | |
renderer.setColor(1, 0, 0, 1); | |
drawTriangles(clippedVertices.items, 5, clippedTriangles.items, clippedTriangles.size); | |
renderer.end(); | |
} | |
public void drawTriangles (float[] vertices, int stride, short[] triangles, int trianglesLength) { | |
for (int i = 0; i < trianglesLength; i += 3) { | |
int idx1 = triangles[i] * stride, idx2 = triangles[i + 1] * stride, idx3 = triangles[i + 2] * stride; | |
renderer.line(vertices[idx1], vertices[idx1 + 1], vertices[idx2], vertices[idx2 + 1]); | |
renderer.line(vertices[idx2], vertices[idx2 + 1], vertices[idx3], vertices[idx3 + 1]); | |
renderer.line(vertices[idx3], vertices[idx3 + 1], vertices[idx1], vertices[idx1 + 1]); | |
} | |
} | |
/** Draws a polyline in the x/y plane using {@link ShapeType#Line}. The vertices must contain at least 2 points (4 floats | |
* x,y). */ | |
public void polyline (float[] vertices, int length, int stride) { | |
for (int i = 0, n = length - stride; i < n; i += stride) { | |
float x1 = vertices[i]; | |
float y1 = vertices[i + 1]; | |
float x2; | |
float y2; | |
x2 = vertices[i + 2]; | |
y2 = vertices[i + 3]; | |
renderer.line(x1, y1, x2, y2); | |
} | |
} | |
static public void main (String[] args) throws Exception { | |
new LwjglApplication(new ClipNaan()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment