SKShader表现放缓 [英] SKShader performance slowdown

查看:162
本文介绍了SKShader表现放缓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用SpriteKit实现自定义着色器节点。
在模拟器中一切正常。在设备(iPad第3代)上,着色器动画在前30秒内平滑,之后着色器的fps逐渐下降,直到它看起来像幻灯片(1 fps甚至更低)

I need to implement a custom shader node using SpriteKit. In simulator everything is ok. On a device (iPad 3rd gen) shader animation is smooth just for first ~30 seconds, after that shader's fps are gradually falling down until it looks like a slideshow (1 fps or even less)

值得注意的是,SpriteKit显示60 fps,Xcode也是如此。
CPU占用率约为75%,但着色器本身显示~1fps。

It is worth noting, that SpriteKit shows 60 fps, so does Xcode. CPU is ~75% busy, but shader itself shows ~1fps.

我只拥有第3代iPad而我目前没有机会测试它在其他设备上
着色器代码来自wwdc会话,但问题是使用我尝试过的任何动画着色器重现的。

I own only 3rd generation iPad and I currently don't have an opportunity to test it on other devices Shader code is taken from wwdc session, but the issue is reproduced with any animated shaders I've tried.

Shader本身:

void main(void)
{
    float currTime = u_time;

    vec2 uv = v_tex_coord;
    vec2 circleCenter = vec2(0.5, 0.5);
    vec3 circleColor = vec3(0.8, 0.5, 0.7);
    vec3 posColor = vec3(uv, 0.5+0.5 * sin(currTime)) * circleColor;

    float illu = pow(1. - distance(uv, circleCenter), 4.) * 1.2;
    illu *= (2. + abs(0.4 + cos(currTime * -20. + 50. * distance(uv, circleCenter))/1.5));
    gl_FragColor = vec4(posColor * illu * 2.0, illu * 2.0);
}

GameScene:

GameScene:

import SpriteKit
class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        addChild(shaderSprite(view.center))
    }

    func shaderSprite(position: CGPoint) -> SKSpriteNode {
        let sprite = SKSpriteNode(texture: SKTexture(imageNamed: "dummy"), color: nil, size: CGSizeMake(100, 100))
        sprite.shader = SKShader(fileNamed: "wwdc")
        sprite.position = position
        return sprite
    }
}

模拟器中的结果或iPad3上的前~30秒:

Result in simulator or first ~30 seconds on iPad3:

约2分钟后iPad3上的结果:

Result on iPad3 after ~2 minutes:

< img src =https://i.stack.imgur.com/A1cnv.gifalt =在此处输入图片说明>

请指出我的错误或只是在任何其他设备上构建项目,以检查它是否是特定于设备的问题。在此先感谢。

Please point my mistake or just build the project on any other device to check if it's a device-specific issue. Thanks in advance.

测试项目: https:// github.com/alexburtnik/SKShaderTest

推荐答案

这是 u_time 的问题制服,我通过添加自己的制服浮动并使用它来代替使用 u_time

This is an issue with the u_time uniform, I got around it by adding my own uniform float and using it instead of using u_time

Shader

void main(void)
     {
        float currTime = myUniform;
        ...
    }

更新

 override func update(currentTime: CFTimeInterval) {
        let shadedNode = self.childNodeWithName("shadedNode") as SKSpriteNode
        let uniform = shadedNode.shader!.uniformNamed("myUniform")
        uniform!.floatValue = Float(currentTime)
    }

这篇关于SKShader表现放缓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆