libgdx 裁剪多边形(clip polygon、masking polygon)
2016-04-25 21:28
381 查看
直接放例子代码,代码中以任意四边形为例,如果需要做任意多边形,注意libgdx不能直接用ShapeRender填充多边形,需要先切割成三角形。
public static void drawClip(Batch batch, Polygon polygon, TextureRegion region, float x, float y) { float[] vertices = 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() will 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); }
相关文章推荐
- golang的的模板引擎之pongo2
- 【GOF】单例模式singleTon的再认识
- Windows+GO+beego
- django fields lookup methods
- django站点管理1
- django 模板if判断的时候==两边需要有空格
- ContentNegotiatingViewResolver解析器
- URAL 1851|GOV-internship|最小割
- django migrate error: table 'xxx' already exists
- mongoose使用简记
- Django ValidationError中的单下划线
- category的介绍与简单实用
- Git Step By Step - Step 2: Go Back in Time
- golang中的类和接口的使用
- Golang Basic - select and channel usage
- OnePointThreeAcres Google/April -1
- Goldengate实验:源端目标端字符集不同
- goldengate关于CONVERTUCS2CLOBS参数
- Mongo DB 2.6 需要知道的一些自身限定
- Go语言为什么这么流行?