Directly put the example code, the code in the arbitrary four-sided shape for example, if you need to do freeform, note LIBGDX can not be directly filled with shaperender polygon, need to first cut into triangles.
public static void Drawclip (batch batch, Polygon Polygon, Textureregion region, float x, float y) {float[] Vertice s = polygon.getvertices (); if (shapes = = null) {shapes = new shaperenderer (); }//2. Clear our depth buffer with 1.0 GDX.GL.GLCLEARDEPTHF (1f); Gdx.gl.glClear (Gl20.gl_depth_buffer_bit); 3. Set the function to less Gdx.gl.glDepthFunc (gl20.gl_less); 4. Enable depth writing Gdx.gl.glEnable (gl20.gl_depth_test); 5. Enable depth Writing, disable RGBA color writing Gdx.gl.glDepthMask (true); Gdx.gl.glColorMask (False, False, false, false); Draw mask shape (s)//6. Render your primitive Shapes Shapes.begin (ShapeRenderer.ShapeType.Filled); Shapes.setcolor (1f, 0f, 0f, 0.5f); Shapes.identity (); Shapes.triangle (Vertices[0], vertices[1], vertices[4], vertices[5], vertices[2], vertices[3]); Shapes.triangle (Vertices[0], veRTICES[1], vertices[4], vertices[5], vertices[6], vertices[7]);//Shapes.polyline (Polygon.getvertices ()); Shapes.end (); Draw Sprite (s) to be masked if (!batch.isdrawing ()) {batch.begin (); }//8. Enable RGBA Color Writing//(Spritebatch.begin () would disable depth mask) Gdx.gl.glColorMask (true, True, True, true); 9. Make sure testing is enabled. Gdx.gl.glEnable (gl20.gl_depth_test); 10. Now depth discards pixels outside our masked shapes Gdx.gl.glDepthFunc (gl20.gl_equal); Push to the batch Batch.draw (region, X, y); End/flush your batch batch.end (); Gdx.gl.glDisable (gl20.gl_depth_test); }
Libgdx clipping polygons (clip polygon, masking polygon)