在libgdx中渲染重复背景的正确方法是什么?

光盘
public render(){
...
        while (y < Height){
            while(x < Width){
            batch.draw(bg,x,y,bg.getWidth()/ratio,bg.getHeight()/ratio);    
            x += bg.getWidth()/ratio;
            }
            y += bg.getHeight()/ratio;
            x = 0;
        }

...
}

我有一个图案texture(bg),它将重复进行重复的背景处理,因此我在render函数(上面的代码)中将其重复进行。刚开始时效果很好,但是当比例变大(因此精灵变小以适合背景)时,它就会变得很滞后。您能告诉我一种正确的方法,使上面的代码重复出现背景而不影响性能。

Tenfour04

我猜它会因为您要在屏幕上绘制数百个四边形而变得迟钝,并且如果有太多的四边形,它将成为CPU瓶颈并降低帧速率。但是,另一个答案可能实际上是原因:如果不使用mip映射而大幅缩小纹理,那实际上会占用GPU时间。

由于这只是重复的纹理,因此只需要为整个屏幕绘制一个四边形即可。首先,在加载纹理时,请确保它重复:

bg.setWrap(TextureWrap.Repeat, TextureWrap.Repeat); 
//call this only one time, after you instantiate the Texture

然后,您可以在屏幕上绘制一个四边形,并将顶点的UV设置为足以使其平铺的次数足够大。

// bottom left corner...wherever you want to put it.
float bottomY = 0; 
float leftX = 0;

//width and height of background. This is just an example. By the way, Java 
//convention is for all variable names to start with a lower-case letter, 
//to avoid confusion.
float width = screenWidth;
float height= screenHeight;

//I'm using RATIO like you are, which I think is the reciprocal of how much 
//you want to scale the texture up. By the way, it is convention to make  
//constant variable (static final primitives in Java) names be in all caps.

float uRight = width * RATIO / bg.getWidth();
float vTop= height * RATIO / bg.getHeight();

batch.draw(bg, leftX, bottomY, width, height, 0, 0, uRight, vTop);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Vulkan中实现“实例渲染”的正确方法是什么?

在Perl中动态重复加载代码的正确方法是什么?

在react或react-native中处理渲染函数中的多个返回的正确方法是什么

在libgdx ashley ecs框架池化引擎中创建实体的正确方法是什么?

在Gtk2hs中渲染Cairo更新的正确方法是什么?

在渲染中循环遍历此对象数组的正确方法是什么,反应

在 d3d 11 中渲染多对象场景的正确方法是什么

在React中渲染之前调用函数的正确位置是什么?

在React / Bootstrap 4中,禁用按钮以防止重复提交表单的正确方法是什么?

在Julia中重复格式字符串的正确方法是什么?

检测JSON数组中没有重复项的最快正确方法是什么?

在 php 中为 css 获取背景图像的正确方法是什么?

灰烬:在元素中渲染背景图像的正确方法

放置透明背景颜色的正确方法是什么?

使用setTimeout保持重新渲染组件的正确方法是什么,直到react js中的prop更改

在 blazor 中向数组增量添加内容而不重新渲染整个数组的正确方法是什么?

正确的方法是什么?

libgdx中是否可以进行HTML渲染(或者,创建帮助屏幕的最佳实践是什么)?

在PHP中设置正确路径的正确方法是什么?

将数据库上下文注入到Hangfire重复作业中的正确方法是什么?

在 Python 中渲染像素的最基本方法是什么?

Rails渲染方法中的partial属性的目的是什么?

在Groovy中调用方法的正确方法是什么

在Ruby on Rails中重写setter方法的正确方法是什么?

将Javascript代码和渲染的HTTP发送到客户端的正确方法是什么?

我的 useMemo useCallback 不会减少渲染次数。使用 useMemo 和 useCallback 的正确方法是什么?

避免在ManagedBeans中重复代码的推荐方法是什么?

在 Sass 中重复单个声明的最佳方法是什么?

在Rust中重复向量中元素的最佳方法是什么?