首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >opengl es 2.0 -优化片段着色器

opengl es 2.0 -优化片段着色器
EN

Stack Overflow用户
提问于 2013-10-10 14:21:34
回答 1查看 1.8K关注 0票数 0

我正在为Android/iOS开发一个游戏,需要优化渲染。该游戏使用户能够变形地形,所以我使用一个灰度图像的地形(值1在地形意味着坚实的地面,0表示没有地面),并应用碎片阴影(也有一个背景图像)。这与60 fps常数很好,但问题是,我还需要渲染一个边界的地形边缘。为此,我在变形时模糊边缘,在碎片着色器中,我根据地形密度/透明度绘制边框(边框为1x64纹理)。

问题是,当渲染边框时,我需要做一个动态纹理读取,将帧速率降到20。有什么方法可以优化这个吗?如果我用一个统一的浮点数组来替换边框纹理,它会有帮助吗?还是和从2d纹理中读取一样?

着色器代码:

代码语言:javascript
复制
 varying mediump vec2 frag_background_texcoord;
 varying mediump vec2 frag_density_texcoord;
 varying mediump vec2 frag_terrain_texcoord;

 uniform sampler2D density_texture;
 uniform sampler2D terrain_texture;
 uniform sampler2D mix_texture;
 uniform sampler2D background_texture;

 void main()
 {
    lowp vec4 background_color = texture2D(background_texture, frag_background_texcoord);
    lowp vec4 terrain_color = texture2D(terrain_texture, frag_terrain_texcoord);

    highp float density = texture2D(density_texture, frag_density_texcoord).a;

    if(density > 0.5)
    {
         lowp vec4 mix_color = texture2D(mix_texture, vec2(density, 1.0)); <- dynamic texture read (FPS drops to 20), would replacing this with a uniform float array help (would also need to calculate the index in the array)?
         gl_FragColor = mix(terrain_color, mix_color, mix_color.a);
    } else
    {
         gl_FragColor =  background_color;
    }
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-19 11:45:05

弄明白了。我修的方法是移除所有的分支。现在运行到60 now。优化的代码:

代码语言:javascript
复制
varying mediump vec2 frag_background_texcoord;
varying mediump vec2 frag_density_texcoord;
varying mediump vec2 frag_terrain_texcoord;

uniform sampler2D density_texture;   
uniform sampler2D terrain_texture;
uniform sampler2D mix_texture;
uniform sampler2D background_texture;

void main()
{
   lowp vec4 background_color = texture2D(background_texture, frag_background_texcoord);
   lowp vec4 terrain_color = texture2D(terrain_texture, frag_terrain_texcoord);

   lowp vec4 mix_color = texture2D(mix_texture, vec2(density, 0.0));
   lowp float density = texture2D(density_texture, frag_density_texcoord).a;

   gl_FragColor = mix(mix(bg_color, terrain_color, mix_color.r), mix_color, mix_color.a);       
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19298406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档