将Pixijs v3 AbstractFilter转换为v4过滤器

时间:2017-01-21 23:04:07

标签: javascript fragment-shader pixi.js pixijs

我正在尝试将Pixi v3的自定义着色器转换为v4。

原始文章在这里: http://www.awwwards.com/a-gentle-introduction-to-shaders-with-pixi-js.html

此处的相关CodePen http://codepen.io/omarshe7ta/pen/xVeWWy

到目前为止,我已经有了这个:

function CustomFilter(fragmentSource) {
  PIXI.Filter.call(this,
      null,
      fragmentSource
  );
}
CustomFilter.prototype = Object.create(PIXI.Filter.prototype);
CustomFilter.prototype.constructor = CustomFilter;

// smoke shader
var shaderCode = document.getElementById('fragShader').innerHTML
var smokeShader = new CustomFilter(shaderCode);    
smokeShader.uniforms.resolution[0] = width;
smokeShader.uniforms.resolution[1] = height;
smokeShader.uniforms.alpha = 1.0;
smokeShader.uniforms.shift = 1.6;
smokeShader.uniforms.time = 0;
smokeShader.uniforms.speed[0] = 0.7;
smokeShader.uniforms.speed[1] = 0.4;

var bg = PIXI.Sprite.fromImage("pixi_v3_github-pad.png");
bg.width = width;
bg.height = height;
bg.filters = [smokeShader]
stage.addChild(bg);

var logo = PIXI.Sprite.fromImage("pixi_v3_github-pad.png");
logo.x = width / 2;
logo.y = height / 2;
logo.anchor.set(0.5);
logo.blendMode = PIXI.BLEND_MODES.ADD;
stage.addChild(logo)

var count = 0

animate()

function animate() {
  requestAnimationFrame(animate);

  count += 0.01
  smokeShader.uniforms.time = count;

  renderer.render(stage);
}

Edge运行得很好

Chrome会抛出错误

pixi.min.js:13 Uncaught TypeError: Cannot read property 'value' of undefined
    at e.syncUniforms (pixi.min.js:13)
    at e.applyFilter (pixi.min.js:13)
    at CustomFilter.t.apply (pixi.min.js:13)
    at e.popFilter (pixi.min.js:13)
    at e.renderAdvancedWebGL (pixi.min.js:10)
    at e.renderWebGL (pixi.min.js:10)
    at e.renderWebGL (pixi.min.js:10)
    at e.render (pixi.min.js:13)
    at animate (shader-v4.html:123)
    at shader-v4.html:113

FireFox会抛出错误,然后崩溃计算机。

t.uniforms.data[u] is undefined

任何人都有想法有什么问题以及如何修复它?

TIA

1 个答案:

答案 0 :(得分:2)

变量

uniform float alpha

未使用,因此在编译着色器时可能会被优化掉。但是,CustomFilter似乎没有注意到这一事实。

从代码中删除“alpha”可以解决问题,而不会将其注释掉。据推测,着色器源是针对制服和属性进行“愚蠢”解析的 - 不会考虑注释。

我相信Pixi现在使用glsify,所以看起来glslify可能有一个缺点。