我正在玩 SKShader for iOS。
void main() {
vec4 val = texture2D(u_texture, v_tex_coord);
float len = 0.5 + 0.5 * sin(10.0 * u_time + 0.05 * (gl_FragCoord.x + gl_FragCoord.y));
vec4 c = vec4(vec3(len), 1);
gl_FragColor = c * val;
}
我想将此着色器分配给连接的 Sprite ,并且我希望 Sprite 之间的效果平滑。因此我不认为我可以在着色器中使用 v_tex_coord。我改用 gl_FragCoord。
问题是 Sprite 是可以移动的 SKNode 的子节点。由于 gl_FragCoord 在屏幕坐标中,因此移动会影响动画。
问:有什么方法可以避免 Sprite 着色器受到父节点移动的影响,而无需将包含 Sprite 的位置和 zRotation 的统一数据传递给着色器?
最佳答案
是的,与其更改制服(这将重新编译着色器),不如将大小作为 SKAttribute 传递并在 SKSceneDelegate
update
方法中更新属性。
例如:
let attributeBasedShader = SKShader(fileNamed: "UsingAttributes.fsh")
attributeBasedShader.attributes = [
SKAttribute(name: "a_sprite_position", type: .vectorFloat2)]
let sprite = SKSpriteNode()
sprite.shader = attributeBasedShader
let spritePosition = vector_float2(Float(sprite.position.x),
Float(sprite.position.y))
sprite.setValue(SKAttributeValue(vectorFloat2: spritePosition),
forAttribute: "a_sprite_position")
关于ios - 在 SKShader 中使用父坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36728482/