问题描述
我使用 libgdx 的渲染周期有问题,基本上我需要用方形纹理填充一个区域,而这个区域的最后一部分可能比纹理更小或形状不同,所以这意味着我需要渲染一个任意形状的四边形并在其上贴上纹理,切割我不需要的部分.
I'm having a problem with my rendering cycle using libgdx, basically I need to fill an area with a square texture, and the last part of this area may be smaller or with a different shape than the texture, so it means that i need to render a quad of arbitrary form and slap the texture on it, cutting the parts I don't need.
我对如何做到这一点有点迷茫,到目前为止,我已经看到 PolygonRegion 和 PolygonSpriteBatch 可能会为我做这件事,但我对实例化一个我只会使用的新重物有点警惕在一个物体上.
I'm a bit lost on how to do this, so far I've seen that the PolygonRegion and PolygonSpriteBatch might do it for me, but I'm a bit wary of instancing a new heavy object I'll use only on one object.
还有其他选择吗?也许是 Mesh 类,但我想确定.
Is there any alternative? Perhaps the Mesh class but i'd like to be certain.
推荐答案
我建议使用 Mesh
来准确定义您想要的区域.定义顶点并将它们映射到纹理坐标有点繁琐,但很高兴知道一些更高级别的 API(如 *Batch 位)下面发生了什么.此外,*Batch API 旨在共享跨多个对象上传单个纹理的权重,这听起来可能不适用于这种情况.(另一方面,即使 Batch 对象有点重量级",在实践中它们实际上也可能不是问题.)
I suggest using a Mesh
to define exactly what region you want. Defining the vertex points and mapping those to the texture coordinates is a bit fiddly, but its good to know what's going on underneath some of the higher level APIs (like the *Batch bits). Additionally, the *Batch APIs are designed to share the weight of uploading a single texture across multiple objects, which sounds like it might not apply in this case. (On the other hand, even if the Batch objects are a bit "heavyweight", they may not actually be a problem in practice.)
要考虑的另一种方法是将对象渲染为方形网格,但为区域外的所有像素定义透明像素的纹理.(我假设非方形是您可以离线知道的,并且不是动态的.)
Another approach to consider is to render the object as a square mesh, but to define your texture with transparent pixels for all the pixels outside the region. (I'm assuming the non-square shape is something you can know offline, and isn't dynamic.)
这篇关于使用 libgdx 绘制带纹理的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!